Botan 3.0.0
Crypto and TLS for C&
Public Member Functions | List of all members
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
 
 Response ()=default
 
 Response (Certificate_Status_Code status)
 
 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
 

Detailed Description

OCSP response.

Note this class is only usable as an OCSP client

Definition at line 133 of file ocsp.h.

Constructor & Destructor Documentation

◆ Response() [1/4]

Botan::OCSP::Response::Response ( )
default

Creates an empty OCSP response.

◆ Response() [2/4]

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 92 of file ocsp.cpp.

94 , m_dummy_response_status(status)
95 {
96 }
Response_Status_Code status() const
Definition: ocsp.h:193

◆ Response() [3/4]

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

Parses an OCSP response.

Parameters
response_bitsresponse bits received

Definition at line 151 of file ocsp.h.

151 :
152 Response(response_bits.data(), response_bits.size())
153 {}

◆ Response() [4/4]

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 98 of file ocsp.cpp.

98 :
99 m_response_bits(response_bits, response_bits + response_bits_len)
100 {
101 BER_Decoder response_outer = BER_Decoder(m_response_bits).start_sequence();
102
103 size_t resp_status = 0;
104
105 response_outer.decode(resp_status, ASN1_Type::Enumerated, ASN1_Class::Universal);
106
107 m_status = static_cast<Response_Status_Code>(resp_status);
108
110 { return; }
111
112 if(response_outer.more_items())
113 {
114 BER_Decoder response_bytes =
115 response_outer.start_context_specific(0).start_sequence();
116
117 response_bytes.decode_and_check(OID("1.3.6.1.5.5.7.48.1.1"),
118 "Unknown response type in OCSP response");
119
120 BER_Decoder basicresponse =
121 BER_Decoder(response_bytes.get_next_octet_string()).start_sequence();
122
123 basicresponse.start_sequence()
124 .raw_bytes(m_tbs_bits)
125 .end_cons()
126 .decode(m_sig_algo)
127 .decode(m_signature, ASN1_Type::BitString);
128 decode_optional_list(basicresponse, ASN1_Type(0), m_certs);
129
130 size_t responsedata_version = 0;
131 Extensions extensions;
132
133 BER_Decoder(m_tbs_bits)
134 .decode_optional(responsedata_version, ASN1_Type(0),
136
137 .decode_optional(m_signer_name, ASN1_Type(1),
139
140 .decode_optional_string(m_key_hash, ASN1_Type::OctetString, 2,
142
143 .decode(m_produced_at)
144
145 .decode_list(m_responses)
146
147 .decode_optional(extensions, ASN1_Type(1),
149 }
150
151 response_outer.end_cons();
152 }
Response_Status_Code
Definition: ocsp.h:119
ASN1_Type
Definition: asn1_obj.h:43

References Botan::BitString, Botan::Constructed, Botan::ContextSpecific, 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::end_cons(), Botan::Enumerated, Botan::BER_Decoder::get_next_octet_string(), Botan::BER_Decoder::more_items(), Botan::OctetString, Botan::BER_Decoder::start_context_specific(), Botan::BER_Decoder::start_sequence(), Botan::OCSP::Successful, and Botan::Universal.

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 236 of file ocsp.h.

236{ 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 241 of file ocsp.h.

241{ 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 184 of file ocsp.cpp.

186 {
187 using namespace std::placeholders;
188
189 // Check whether the CA issuing the certificate in question also signed this
190 if(is_issued_by(issuer_certificate))
191 {
192 return issuer_certificate;
193 }
194
195 // Then try to find a delegated responder certificate in the stapled certs
196 auto match = std::find_if(m_certs.begin(), m_certs.end(), std::bind(&Response::is_issued_by, this, _1));
197 if(match != m_certs.end())
198 {
199 return *match;
200 }
201
202 // Last resort: check the additionally provides trusted OCSP responders
203 if(trusted_ocsp_responders)
204 {
205 if(!m_key_hash.empty())
206 {
207 auto signing_cert = trusted_ocsp_responders->find_cert_by_pubkey_sha1(m_key_hash);
208 if(signing_cert)
209 return signing_cert;
210 }
211
212 if(!m_signer_name.empty())
213 {
214 auto signing_cert = trusted_ocsp_responders->find_cert(m_signer_name, {});
215 if(signing_cert)
216 return signing_cert;
217 }
218 }
219
220 return std::nullopt;
221 }
bool empty() const
Definition: pkix_types.h:67

References Botan::X509_DN::empty(), 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 198 of file ocsp.h.

198{ return m_produced_at; }

◆ raw_bits()

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

Definition at line 210 of file ocsp.h.

210{ 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 208 of file ocsp.h.

208{ 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 203 of file ocsp.h.

203{ return m_signer_name; }

◆ status()

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

Definition at line 193 of file ocsp.h.

193{ return m_status; }

◆ 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 224 of file ocsp.cpp.

228 {
229 if(m_dummy_response_status)
230 { return m_dummy_response_status.value(); }
231
232 for(const auto& response : m_responses)
233 {
234 if(response.certid().is_id_for(issuer, subject))
235 {
236 X509_Time x509_ref_time(ref_time);
237
238 if(response.cert_status() == 1)
240
241 if(response.this_update() > x509_ref_time)
243
244 if(response.next_update().time_is_set())
245 {
246 if(x509_ref_time > response.next_update())
248 }
249 else if(max_age > std::chrono::seconds::zero() && ref_time - response.this_update().to_std_timepoint() > max_age)
251
252 if(response.cert_status() == 0)
254 else
256 }
257 }
258
260 }
ASN1_Time X509_Time
Definition: asn1_obj.h:422

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, and Botan::OCSP_RESPONSE_GOOD.

◆ verify_signature()

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 154 of file ocsp.cpp.

155 {
156 if(m_dummy_response_status)
157 return m_dummy_response_status.value();
158
159 if(m_signer_name.empty() && m_key_hash.empty())
161
162 if(!is_issued_by(issuer))
164
165 try
166 {
167 auto pub_key = issuer.subject_public_key();
168
169 PK_Verifier verifier(*pub_key, m_sig_algo);
170
171 if(verifier.verify_message(ASN1::put_in_sequence(m_tbs_bits), m_signature))
173 else
175 }
176 catch(Exception&)
177 {
179 }
180 }
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
Definition: asn1_obj.cpp:192

References Botan::X509_DN::empty(), Botan::OCSP_ISSUER_NOT_FOUND, Botan::OCSP_RESPONSE_INVALID, Botan::OCSP_SIGNATURE_ERROR, Botan::OCSP_SIGNATURE_OK, Botan::ASN1::put_in_sequence(), Botan::X509_Certificate::subject_public_key(), and Botan::PK_Verifier::verify_message().


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