Botan 3.12.0
Crypto and TLS for C&
Botan::OCSP::Response Class Referencefinal

#include <ocsp.h>

Public Member Functions

const std::vector< X509_Certificate > & certificates () const
std::optional< Certificate_Status_Codedummy_status () const
std::optional< X509_Certificatefind_signing_certificate (const X509_Certificate &issuer_certificate, const Certificate_Store *trusted_ocsp_responders=nullptr) const
const X509_Timeproduced_at () const
const std::vector< uint8_t > & raw_bits () const
BOTAN_FUTURE_EXPLICIT Response (Certificate_Status_Code status)
BOTAN_FUTURE_EXPLICIT Response (const std::vector< uint8_t > &response_bits)
 Response (const uint8_t response_bits[], size_t response_bits_len)
const std::vector< uint8_t > & signer_key_hash () const
const X509_DNsigner_name () const
Response_Status_Code status () const
Certificate_Status_Code status_for (const X509_Certificate &issuer, const X509_Certificate &subject, std::chrono::system_clock::time_point ref_time=std::chrono::system_clock::now(), std::chrono::seconds max_age=std::chrono::seconds::zero()) const
Certificate_Status_Code verify_signature (const X509_Certificate &signing_certificate) const
Certificate_Status_Code verify_signature (const X509_Certificate &signing_certificate, const Path_Validation_Restrictions &restrictions) const

Detailed Description

OCSP response.

Note this class is only usable as an OCSP client

Definition at line 135 of file ocsp.h.

Constructor & Destructor Documentation

◆ Response() [1/3]

Botan::OCSP::Response::Response ( Certificate_Status_Code status)

Create a fake OCSP response from a given status code.

Parameters
statusthe status code the check functions will return

Definition at line 221 of file ocsp.cpp.

221 :
222 m_status(Response_Status_Code::Successful), m_dummy_response_status(status) {}
Response_Status_Code status() const
Definition ocsp.h:201

References status().

Referenced by Response().

◆ Response() [2/3]

BOTAN_FUTURE_EXPLICIT Botan::OCSP::Response::Response ( const std::vector< uint8_t > & response_bits)
inline

Parses an OCSP response.

Parameters
response_bitsresponse bits received

Definition at line 147 of file ocsp.h.

147 :
148 Response(response_bits.data(), response_bits.size()) {}
BOTAN_FUTURE_EXPLICIT Response(Certificate_Status_Code status)
Definition ocsp.cpp:221

References BOTAN_FUTURE_EXPLICIT, and Response().

◆ Response() [3/3]

Botan::OCSP::Response::Response ( const uint8_t response_bits[],
size_t response_bits_len )

Parses an OCSP response.

Parameters
response_bitsresponse bits received
response_bits_lenlength of response in bytes

Definition at line 224 of file ocsp.cpp.

224 :
225 m_response_bits(response_bits, response_bits + response_bits_len) {
226 /*
227 * RFC 6960 Section 4.2.1
228 *
229 * OCSPResponse ::= SEQUENCE {
230 * responseStatus OCSPResponseStatus,
231 * responseBytes [0] EXPLICIT ResponseBytes OPTIONAL }
232 *
233 * OCSPResponseStatus ::= ENUMERATED { ... }
234 *
235 * ResponseBytes ::= SEQUENCE {
236 * responseType OBJECT IDENTIFIER,
237 * response OCTET STRING }
238 */
239 BER_Decoder outer_decoder(m_response_bits, BER_Decoder::Limits::DER());
240 BER_Decoder response_outer = outer_decoder.start_sequence();
241
242 size_t resp_status = 0;
243
244 response_outer.decode(resp_status, ASN1_Type::Enumerated, ASN1_Class::Universal);
245
246 /*
247 RFC 6960 4.2.1
248
249 OCSPResponseStatus ::= ENUMERATED {
250 successful (0), -- Response has valid confirmations
251 malformedRequest (1), -- Illegal confirmation request
252 internalError (2), -- Internal error in issuer
253 tryLater (3), -- Try again later
254 -- (4) is not used
255 sigRequired (5), -- Must sign the request
256 unauthorized (6) -- Request unauthorized
257 }
258 */
259 if(resp_status >= 7) {
260 throw Decoding_Error("Unknown OCSPResponseStatus code");
261 }
262
263 m_status = static_cast<Response_Status_Code>(resp_status);
264
265 if(m_status != Response_Status_Code::Successful) {
266 return;
267 }
268
269 if(response_outer.more_items()) {
270 BER_Decoder response_bytes_ctx = response_outer.start_context_specific(0);
271 BER_Decoder response_bytes = response_bytes_ctx.start_sequence();
272
273 response_bytes.decode_and_check(OID({1, 3, 6, 1, 5, 5, 7, 48, 1, 1}), "Unknown response type in OCSP response");
274
275 /*
276 * RFC 6960 Section 4.2.1
277 *
278 * BasicOCSPResponse ::= SEQUENCE {
279 * tbsResponseData ResponseData,
280 * signatureAlgorithm AlgorithmIdentifier,
281 * signature BIT STRING,
282 * certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL }
283 */
284 BER_Decoder basic_response_decoder(response_bytes.get_next_octet_string(), BER_Decoder::Limits::DER());
285 BER_Decoder basicresponse = basic_response_decoder.start_sequence();
286
287 basicresponse.start_sequence()
288 .raw_bytes(m_tbs_bits)
289 .end_cons()
290 .decode(m_sig_algo)
291 .decode(m_signature, ASN1_Type::BitString);
292 decode_optional_list(basicresponse, ASN1_Type(0), m_certs);
293
294 basicresponse.verify_end();
295 basic_response_decoder.verify_end();
296
297 /*
298 * RFC 6960 Section 4.2.1
299 *
300 * ResponseData ::= SEQUENCE {
301 * version [0] EXPLICIT Version DEFAULT v1,
302 * responderID ResponderID,
303 * producedAt GeneralizedTime,
304 * responses SEQUENCE OF SingleResponse,
305 * responseExtensions [1] EXPLICIT Extensions OPTIONAL }
306 *
307 * ResponderID ::= CHOICE {
308 * byName [1] Name,
309 * byKey [2] KeyHash }
310 */
311 size_t responsedata_version = 0;
312 Extensions extensions;
313
314 BER_Decoder(m_tbs_bits, BER_Decoder::Limits::DER())
315 .decode_optional(responsedata_version, ASN1_Type(0), ASN1_Class::ContextSpecific | ASN1_Class::Constructed)
316
317 .decode_optional(m_signer_name, ASN1_Type(1), ASN1_Class::ContextSpecific | ASN1_Class::Constructed)
318
319 .decode_optional_string(
321
322 .decode(m_produced_at)
323
324 .decode_list(m_responses)
325
326 .decode_optional(extensions, ASN1_Type(1), ASN1_Class::ContextSpecific | ASN1_Class::Constructed)
327
328 .verify_end();
329
330 const bool has_signer = !m_signer_name.empty();
331 const bool has_key_hash = !m_key_hash.empty();
332
333 if(has_signer && has_key_hash) {
334 throw Decoding_Error("OCSP response includes both byName and byKey in responderID field");
335 }
336 if(!has_signer && !has_key_hash) {
337 throw Decoding_Error("OCSP response contains neither byName nor byKey in responderID field");
338 }
339 if(has_key_hash && m_key_hash.size() != 20) {
340 // KeyHash ::= OCTET STRING -- SHA-1 hash of responder's public key
341 throw Decoding_Error("OCSP response contains a byKey with invalid length");
342 }
343
344 response_bytes.verify_end();
345 response_bytes_ctx.verify_end();
346
347 // We don't currently recognize any extensions here so if any are critical we should reject
348 m_has_unknown_critical_ext = !extensions.critical_extensions().empty();
349 }
350
351 response_outer.verify_end();
352 outer_decoder.verify_end();
353
354 if(m_has_unknown_critical_ext == false) {
355 // Check all of the SingleResponse extensions
356 for(const auto& sr : m_responses) {
357 if(sr.has_unknown_critical_extension()) {
358 m_has_unknown_critical_ext = true;
359 break;
360 }
361 }
362 }
363}
static Limits DER()
Definition ber_dec.h:35
Response_Status_Code
Definition ocsp.h:121
ASN1_Type
Definition asn1_obj.h:43

References Botan::BitString, Botan::Constructed, Botan::ContextSpecific, Botan::Extensions::critical_extensions(), Botan::BER_Decoder::decode(), Botan::BER_Decoder::decode_and_check(), Botan::BER_Decoder::decode_list(), Botan::BER_Decoder::decode_optional(), Botan::BER_Decoder::decode_optional_string(), Botan::BER_Decoder::Limits::DER(), Botan::BER_Decoder::end_cons(), Botan::Enumerated, Botan::BER_Decoder::get_next_octet_string(), Botan::BER_Decoder::more_items(), Botan::OctetString, Botan::BER_Decoder::raw_bytes(), Botan::BER_Decoder::start_context_specific(), Botan::BER_Decoder::start_sequence(), Botan::OCSP::Successful, Botan::Universal, and Botan::BER_Decoder::verify_end().

Member Function Documentation

◆ certificates()

const std::vector< X509_Certificate > & Botan::OCSP::Response::certificates ( ) const
inline
Returns
the certificate chain, if provided in response

Definition at line 245 of file ocsp.h.

245{ return m_certs; }

◆ dummy_status()

std::optional< Certificate_Status_Code > Botan::OCSP::Response::dummy_status ( ) const
inline
Returns
the dummy response if this is a 'fake' OCSP response otherwise std::nullopt

Definition at line 250 of file ocsp.h.

250{ return m_dummy_response_status; }

◆ find_signing_certificate()

std::optional< X509_Certificate > Botan::OCSP::Response::find_signing_certificate ( const X509_Certificate & issuer_certificate,
const Certificate_Store * trusted_ocsp_responders = nullptr ) const

Find the certificate that signed this OCSP response from all possible candidates and taking the attached certificates into account.

Parameters
issuer_certificateis the issuer of the certificate in question
trusted_ocsp_respondersoptionally, a certificate store containing additionally trusted responder certificates
Returns
the certificate that signed this response or std::nullopt if not found

Definition at line 427 of file ocsp.cpp.

428 {
429 using namespace std::placeholders;
430
431 // Check whether the CA issuing the certificate in question also signed this
432 if(is_issued_by(issuer_certificate)) {
433 return issuer_certificate;
434 }
435
436 // Then try to find a delegated responder certificate in the stapled certs
437 for(const auto& cert : m_certs) {
438 if(this->is_issued_by(cert)) {
439 return cert;
440 }
441 }
442
443 // Last resort: check the additionally provides trusted OCSP responders
444 if(trusted_ocsp_responders != nullptr) {
445 if(!m_key_hash.empty()) {
446 auto signing_cert = trusted_ocsp_responders->find_cert_by_pubkey_sha1(m_key_hash);
447 if(signing_cert) {
448 return signing_cert;
449 }
450 }
451
452 if(!m_signer_name.empty()) {
453 auto signing_cert = trusted_ocsp_responders->find_cert(m_signer_name, {});
454 if(signing_cert) {
455 return signing_cert;
456 }
457 }
458 }
459
460 return std::nullopt;
461}

References Botan::Certificate_Store::find_cert(), and Botan::Certificate_Store::find_cert_by_pubkey_sha1().

◆ produced_at()

const X509_Time & Botan::OCSP::Response::produced_at ( ) const
inline
Returns
the time this OCSP response was supposedly produced at

Definition at line 206 of file ocsp.h.

206{ return m_produced_at; }

◆ raw_bits()

const std::vector< uint8_t > & Botan::OCSP::Response::raw_bits ( ) const
inline

Definition at line 218 of file ocsp.h.

218{ return m_response_bits; }

◆ signer_key_hash()

const std::vector< uint8_t > & Botan::OCSP::Response::signer_key_hash ( ) const
inline
Returns
key hash, if provided in response (may be empty)

Definition at line 216 of file ocsp.h.

216{ return m_key_hash; }

◆ signer_name()

const X509_DN & Botan::OCSP::Response::signer_name ( ) const
inline
Returns
DN of signer, if provided in response (may be empty)

Definition at line 211 of file ocsp.h.

211{ return m_signer_name; }

◆ status()

Response_Status_Code Botan::OCSP::Response::status ( ) const
inline
Returns
the status of the response

Definition at line 201 of file ocsp.h.

201{ return m_status; }

Referenced by Response().

◆ status_for()

Certificate_Status_Code Botan::OCSP::Response::status_for ( const X509_Certificate & issuer,
const X509_Certificate & subject,
std::chrono::system_clock::time_point ref_time = std::chrono::system_clock::now(),
std::chrono::seconds max_age = std::chrono::seconds::zero() ) const

Searches the OCSP response for issuer and subject certificate.

Parameters
issuerissuer certificate
subjectsubject certificate
ref_timethe reference time
max_agethe maximum age the response should be considered valid if next_update is not set
Returns
OCSP status code, possible values: CERT_IS_REVOKED, OCSP_NOT_YET_VALID, OCSP_HAS_EXPIRED, OCSP_IS_TOO_OLD, OCSP_RESPONSE_GOOD, OCSP_BAD_STATUS, OCSP_CERT_NOT_LISTED

Definition at line 463 of file ocsp.cpp.

466 {
467 if(m_dummy_response_status) {
468 return m_dummy_response_status.value();
469 }
470
471 for(const auto& response : m_responses) {
472 if(response.certid().is_id_for(issuer, subject)) {
473 const X509_Time x509_ref_time(ref_time);
474
475 /*
476 * We check certificate status prior to checking expiration, since otherwise it's
477 * possible to take an OCSP response indicating revocation, wait for it to expire,
478 * and then staple it. If such a response was reported as "expired" rather than
479 * "revoked" it's easy to dismiss as a clock issue or other misconfiguration.
480 */
481
482 if(response.cert_status() == 1) {
484 }
485
486 try {
487 if(response.this_update() > x509_ref_time) {
489 }
490
491 if(response.next_update().time_is_set()) {
492 if(x509_ref_time > response.next_update()) {
494 }
495 } else if(max_age > std::chrono::seconds::zero() &&
496 ref_time - response.this_update().to_std_timepoint() > max_age) {
498 }
499 } catch(Exception&) {
500 // This can occur if eg the OCSP time is not representable by the system clock
502 }
503
504 if(response.cert_status() == 0) {
506 } else {
508 }
509 }
510 }
511
513}
ASN1_Time X509_Time
Definition asn1_obj.h:23

References Botan::CERT_IS_REVOKED, Botan::OCSP_BAD_STATUS, Botan::OCSP_CERT_NOT_LISTED, Botan::OCSP_HAS_EXPIRED, Botan::OCSP_IS_TOO_OLD, Botan::OCSP_NOT_YET_VALID, Botan::OCSP_RESPONSE_GOOD, and Botan::OCSP_RESPONSE_INVALID.

◆ verify_signature() [1/2]

Certificate_Status_Code Botan::OCSP::Response::verify_signature ( const X509_Certificate & signing_certificate) const

Check signature of the OCSP response.

Note: It is the responsibility of the caller to verify that signing certificate is trustworthy and authorized to do so.

Parameters
signing_certificatethe certificate that signed this response (
See also
Response::find_signing_certificate).
Returns
status code indicating the validity of the signature

Definition at line 377 of file ocsp.cpp.

377 {
378 const Path_Validation_Restrictions restrictions;
379
380 return this->verify_signature(issuer, restrictions);
381}
Certificate_Status_Code verify_signature(const X509_Certificate &signing_certificate) const
Definition ocsp.cpp:377

References verify_signature().

Referenced by verify_signature().

◆ verify_signature() [2/2]

Certificate_Status_Code Botan::OCSP::Response::verify_signature ( const X509_Certificate & signing_certificate,
const Path_Validation_Restrictions & restrictions ) const

Check signature of the OCSP response.

Note: It is the responsibility of the caller to verify that signing certificate is trustworthy and authorized to do so.

Parameters
signing_certificatethe certificate that signed this response (
See also
Response::find_signing_certificate)
Parameters
restrictionson the signature validation
Returns
status code indicating the validity of the signature

Definition at line 383 of file ocsp.cpp.

384 {
385 if(m_dummy_response_status) {
386 return m_dummy_response_status.value();
387 }
388
389 if(m_signer_name.empty() && m_key_hash.empty()) {
391 }
392
393 if(!is_issued_by(issuer)) {
395 }
396
397 try {
398 auto pub_key = issuer.subject_public_key();
399
400 PK_Verifier verifier(*pub_key, m_sig_algo);
401
402 const bool valid_signature = verifier.verify_message(ASN1::put_in_sequence(m_tbs_bits), m_signature);
403
404 if(valid_signature == false) {
406 }
407
408 if(m_has_unknown_critical_ext) {
410 }
411
412 const auto& trusted_hashes = restrictions.trusted_hashes();
413 if(!trusted_hashes.empty() && !trusted_hashes.contains(verifier.hash_function())) {
415 }
416
417 if(pub_key->estimated_strength() < restrictions.minimum_key_strength()) {
419 }
420
422 } catch(Exception&) {
424 }
425}
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
Definition asn1_obj.cpp:177

References Botan::PK_Verifier::hash_function(), Botan::Path_Validation_Restrictions::minimum_key_strength(), Botan::OCSP_ISSUER_NOT_FOUND, Botan::OCSP_RESPONSE_INVALID, Botan::OCSP_SIGNATURE_ERROR, Botan::OCSP_SIGNATURE_OK, Botan::ASN1::put_in_sequence(), Botan::SIGNATURE_METHOD_TOO_WEAK, Botan::X509_Certificate::subject_public_key(), Botan::Path_Validation_Restrictions::trusted_hashes(), Botan::UNKNOWN_CRITICAL_EXTENSION, Botan::UNTRUSTED_HASH, and Botan::PK_Verifier::verify_message().


The documentation for this class was generated from the following files: