10#include <botan/base64.h>
11#include <botan/ber_dec.h>
12#include <botan/certstor.h>
13#include <botan/der_enc.h>
14#include <botan/hash.h>
15#include <botan/pubkey.h>
17#include <botan/x509_ext.h>
18#include <botan/x509path.h>
19#include <botan/internal/fmt.h>
21#if defined(BOTAN_HAS_HTTP_UTIL)
22 #include <botan/internal/http_util.h>
35void check_generalized_time(
const ASN1_Time& time,
const char* field) {
37 throw Decoding_Error(
fmt(
"OCSP response {} was not encoded as GeneralizedTime", field));
47 m_subject_serial(subject_serial) {
56 m_issuer_dn_hash = hash->process<std::vector<uint8_t>>(issuer.
raw_subject_dn());
61 if(subject.
serial() != m_subject_serial) {
65 const auto hash_algo = m_hash_id.oid().registered_name();
78 if(hash_algo ==
"SHA-1") {
85 }
else if(hash_algo ==
"SHA-256") {
129 if(!m_hash_id.parameters_are_null_or_empty()) {
130 throw Decoding_Error(
"OCSP CertID hashAlgorithm has unexpected parameters");
149 std::optional<CRL_Code> reason,
158 std::optional<X509_Time> revocation_time,
159 std::optional<CRL_Code> revocation_reason,
162 m_certid(std::move(certid)),
163 m_cert_status(cert_status),
164 m_thisupdate(std::move(this_update)),
165 m_nextupdate(std::move(next_update)),
166 m_revocation_time(std::move(revocation_time)),
167 m_revocation_reason(revocation_reason) {
168 const auto require_generalized_time = [](
const X509_Time& t,
const char* field) {
174 require_generalized_time(m_thisupdate,
"thisUpdate");
176 require_generalized_time(m_nextupdate,
"nextUpdate");
178 if(m_cert_status == 1) {
179 if(!m_revocation_time.has_value()) {
180 throw Invalid_Argument(
"Revoked OCSP SingleResponse lacks a revocation time");
182 require_generalized_time(*m_revocation_time,
"revocationTime");
191 if(m_cert_status == 1) {
202 const std::span<const uint8_t> empty;
207 if(m_nextupdate.time_is_set()) {
242 check_generalized_time(m_thisupdate,
"thisUpdate");
243 if(m_nextupdate.time_is_set()) {
244 check_generalized_time(m_nextupdate,
"nextUpdate");
247 if(seq.more_items()) {
248 const BER_Object next = seq.get_next_object();
259 const auto cert_status_class =
cert_status.get_class();
262 throw Decoding_Error(
"OCSP::SingleResponse: certStatus has unexpected class tag");
265 m_cert_status =
static_cast<uint32_t
>(
cert_status.type());
266 if(m_cert_status > 2) {
270 m_revocation_time.reset();
271 m_revocation_reason.reset();
273 if(m_cert_status == 1) {
283 if(reason == 7 || reason > 10) {
286 m_revocation_reason =
static_cast<CRL_Code>(reason);
301void decode_optional_list(
BER_Decoder& ber,
ASN1_Type tag, std::vector<X509_Certificate>& output) {
310 auto seq = list.start_sequence();
311 while(seq.more_items()) {
312 output.push_back([&] {
325 m_issuer(issuer_cert), m_certid(m_issuer, subject_cert.serial()) {
327 throw Invalid_Argument(
"Invalid cert pair to OCSP::Request (mismatched issuer,subject args?)");
332 m_issuer(issuer_cert), m_certid(m_issuer, subject_serial) {}
349 std::vector<uint8_t> output;
354 .
encode(
static_cast<size_t>(0))
375 m_response_bits(response_bits, response_bits + response_bits_len) {
392 size_t resp_status = 0;
409 if(resp_status == 4 || resp_status >= 7) {
420 const bool has_response_bytes = response_outer.
more_items();
422 if(successful && !has_response_bytes) {
423 throw Decoding_Error(
"OCSP response with successful status is missing responseBytes");
425 if(!successful && has_response_bytes) {
426 throw Decoding_Error(
"OCSP response with non-successful status includes responseBytes");
434 "Unknown response type in OCSP response");
453 decode_optional_list(basicresponse,
ASN1_Type(0), m_certs);
472 size_t responsedata_version = 0;
488 check_generalized_time(m_produced_at,
"producedAt");
502 const bool has_signer = !m_signer_name.empty();
503 const bool has_key_hash = !m_key_hash.empty();
505 if(has_signer && has_key_hash) {
506 throw Decoding_Error(
"OCSP response includes both byName and byKey in responderID field");
508 if(!has_signer && !has_key_hash) {
509 throw Decoding_Error(
"OCSP response contains neither byName nor byKey in responderID field");
511 if(has_key_hash && m_key_hash.size() != 20) {
513 throw Decoding_Error(
"OCSP response contains a byKey with invalid length");
526 if(m_has_unknown_critical_ext ==
false) {
528 for(
const auto& sr : m_responses) {
529 if(sr.has_unknown_critical_extension()) {
530 m_has_unknown_critical_ext =
true;
538 if(!m_signer_name.
empty()) {
539 return (candidate.
subject_dn() == m_signer_name);
542 if(!m_key_hash.empty()) {
557 if(m_dummy_response_status) {
558 return m_dummy_response_status.value();
561 if(m_signer_name.empty() && m_key_hash.empty()) {
565 if(!is_issued_by(issuer)) {
574 verifier.
update(m_tbs_bits);
577 if(valid_signature ==
false) {
581 if(m_has_unknown_critical_ext) {
586 if(!trusted_hashes.empty() && !trusted_hashes.contains(verifier.
hash_function())) {
602 using namespace std::placeholders;
605 if(is_issued_by(issuer_certificate)) {
606 return issuer_certificate;
610 for(
const auto& cert : m_certs) {
611 if(this->is_issued_by(cert)) {
617 if(trusted_ocsp_responders !=
nullptr) {
618 if(!m_key_hash.empty()) {
625 if(!m_signer_name.empty()) {
626 auto signing_cert = trusted_ocsp_responders->
find_cert(m_signer_name, {});
638 std::chrono::system_clock::time_point ref_time,
639 std::chrono::seconds max_age)
const {
640 if(m_dummy_response_status) {
641 return m_dummy_response_status.value();
644 for(
const auto& response : m_responses) {
645 if(response.certid().is_id_for(issuer, subject)) {
655 if(response.cert_status() == 1) {
660 if(response.this_update() > x509_ref_time) {
664 if(response.next_update().time_is_set()) {
665 if(x509_ref_time > response.next_update()) {
668 }
else if(max_age > std::chrono::seconds::zero() &&
669 ref_time - response.this_update().to_std_timepoint() > max_age) {
677 if(response.cert_status() == 0) {
688#if defined(BOTAN_HAS_HTTP_UTIL)
691 const BigInt& subject_serial,
692 std::string_view ocsp_responder,
693 std::chrono::milliseconds timeout) {
694 if(ocsp_responder.empty()) {
699 return online_check(issuer, subject_serial, *uri, timeout);
701 throw Invalid_Argument(
"Unparsable URI for OCSP responder");
705Response online_check(
const X509_Certificate& issuer,
706 const BigInt& subject_serial,
707 const URI& ocsp_responder,
708 std::chrono::milliseconds timeout) {
709 const OCSP::Request req(issuer, subject_serial);
712 "application/ocsp-request",
714 HTTP::RequestLimits().set_timeout(timeout).set_max_body_size(64 * 1024));
716 http.throw_unless_ok();
720 return OCSP::Response(http.body());
723Response online_check(
const X509_Certificate& issuer,
724 const X509_Certificate& subject,
725 std::chrono::milliseconds timeout) {
726 if(subject.issuer_dn() != issuer.subject_dn()) {
727 throw Invalid_Argument(
"Invalid cert pair to OCSP::online_check (mismatched issuer,subject args?)");
732 if(responders.empty()) {
733 throw Invalid_Argument(
"No HTTP OCSP responder URLs available for this certificate");
736 const auto subject_serial = subject.serial().to_bigint();
739 for(
size_t i = 0; i + 1 < responders.size(); ++i) {
741 return online_check(issuer, subject_serial, responders[i], timeout);
746 return online_check(issuer, subject_serial, responders.back(), timeout);
ASN1_Type tagging() const
Return the tag (UtcTime or GeneralizedTime) this time was encoded with.
bool time_is_set() const
Return if the time has been set somehow.
BER_Decoder & raw_bytes(T &out_wrapped)
BER_Decoder & decode_optional_string(T &out_wrapped, ASN1_Type real_type, uint32_t expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
const BER_Object & peek_next_object()
void push_back(const BER_Object &obj)
BER_Object get_next_object()
BER_Decoder & decode(bool &out)
std::vector< uint8_t > get_next_octet_string()
BER_Decoder & verify_end()
BER_Decoder start_sequence()
BER_Decoder start_context_specific(uint32_t tag)
BER_Decoder & decode_octet_aligned_bitstring(T &out_wrapped, ASN1_Type type_tag=ASN1_Type::BitString, ASN1_Class class_tag=ASN1_Class::Universal)
BER_Decoder & decode_optional(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
BER_Decoder & decode_and_check(const T &expected, std::string_view error_msg)
BER_Decoder & get_next(BER_Object &ber)
BER_Decoder & decode_list(std::vector< T > &vec, ASN1_Type type_tag=ASN1_Type::Sequence, ASN1_Class class_tag=ASN1_Class::Universal)
bool is_a(ASN1_Type type_tag, ASN1_Class class_tag) const
virtual std::optional< X509_Certificate > find_cert_by_pubkey_sha1(const std::vector< uint8_t > &key_hash) const =0
virtual std::optional< X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const
DER_Encoder & add_object(ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length)
DER_Encoder & end_explicit()
DER_Encoder & start_explicit(uint16_t type_tag)
DER_Encoder & start_sequence()
DER_Encoder & start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
DER_Encoder & encode(bool b)
std::vector< OID > critical_extensions() const
void decode_from(BER_Decoder &from) override
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
void decode_from(BER_Decoder &from) override
void encode_into(DER_Encoder &to) const override
bool is_id_for(const X509_Certificate &issuer, const X509_Certificate &subject) const
std::string base64_encode() const
Request(const X509_Certificate &issuer_cert, const X509_Certificate &subject_cert)
std::vector< uint8_t > BER_encode() const
BOTAN_FUTURE_EXPLICIT Response(Certificate_Status_Code status)
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
Response_Status_Code status() const
std::optional< X509_Certificate > find_signing_certificate(const X509_Certificate &issuer_certificate, const Certificate_Store *trusted_ocsp_responders=nullptr) const
Certificate_Status_Code verify_signature(const X509_Certificate &signing_certificate) const
const std::optional< X509_Time > & revocation_time() const
The revocationTime; set only when cert_status() is 1 (revoked).
const CertID & certid() const
static SingleResponse unknown(CertID certid, X509_Time this_update, X509_Time next_update)
As good(), but asserting an unknown status.
const X509_Time & next_update() const
static SingleResponse revoked(CertID certid, X509_Time revocation_time, std::optional< CRL_Code > reason, X509_Time this_update, X509_Time next_update)
As good(), but asserting a revoked status with the given RevokedInfo.
static SingleResponse good(CertID certid, X509_Time this_update, X509_Time next_update)
void decode_from(BER_Decoder &from) override
const X509_Time & this_update() const
size_t cert_status() const
void encode_into(DER_Encoder &to) const override
static OID from_string(std::string_view str)
std::string hash_function() const
bool check_signature(const uint8_t sig[], size_t length)
const std::set< std::string > & trusted_hashes() const
size_t minimum_key_strength() const
static std::optional< URI > from_string(std::string_view raw)
static std::vector< URI > filter_scheme(std::string_view scheme, std::span< const URI > uris)
const X509_DN & subject_dn() const
const X509_Serial_Number & serial() const
const std::vector< uint8_t > & raw_subject_dn() const
std::span< const uint8_t, 32 > subject_public_key_bitstring_sha256() const
const std::vector< uint8_t > & subject_public_key_bitstring_sha1() const
const X509_DN & issuer_dn() const
const std::vector< uint8_t > & raw_issuer_dn_sha256() const
const std::vector< uint8_t > & subject_public_key_bitstring() const
std::unique_ptr< Public_Key > subject_public_key() const
std::span< const uint8_t, 20 > raw_issuer_dn_sha1() const
void decode_from(BER_Decoder &from) override
std::vector< uint8_t > der_sequence_header(size_t contents_len)
Response POST_sync(const URI &uri, std::string_view content_type, const std::vector< uint8_t > &body, const RequestLimits &limits)
std::string fmt(std::string_view format, const T &... args)
@ UNKNOWN_CRITICAL_EXTENSION
@ SIGNATURE_METHOD_TOO_WEAK
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)