9#include <botan/x509path.h>
11#include <botan/ocsp.h>
12#include <botan/pk_keys.h>
13#include <botan/x509_ext.h>
14#include <botan/internal/stl_util.h>
22#if defined(BOTAN_HAS_ONLINE_REVOCATION_CHECKS)
23 #include <botan/internal/http_util.h>
27#if defined(BOTAN_HAS_ECC_KEY)
28 #include <botan/ecc_key.h>
37 std::chrono::system_clock::time_point ref_time,
38 std::string_view hostname,
41 if(cert_path.empty()) {
45 const bool self_signed_ee_cert = (cert_path.size() == 1);
52 for(
size_t i = 0; i != cert_path.size(); ++i) {
53 std::set<Certificate_Status_Code>& status = cert_status.at(i);
55 const bool at_self_signed_root = (i == cert_path.size() - 1);
58 const X509_Certificate& issuer = cert_path[at_self_signed_root ? (i) : (i + 1)];
64 std::unique_ptr<Public_Key> issuer_key;
79 status.insert(sig_status.first);
82 const std::string hash_used_for_signature = sig_status.second;
87 if(!trusted_hashes.empty() && !at_self_signed_root) {
88 if(!trusted_hashes.contains(hash_used_for_signature)) {
103 for(
size_t i = 0; i != cert_path.size(); ++i) {
104 for(
auto status : cert_status.at(i)) {
107 if(
static_cast<uint32_t
>(status) >= 5000) {
113 if(!hostname.empty() && !cert_path[0].matches_dns_name(hostname)) {
117 if(!cert_path[0].allowed_usage(usage)) {
136 for(
size_t i = 0; i != cert_path.size(); ++i) {
137 std::set<Certificate_Status_Code>& status = cert_status.at(i);
139 const bool at_self_signed_root = (i == cert_path.size() - 1);
142 const X509_Certificate& issuer = cert_path[at_self_signed_root ? (i) : (i + 1)];
163 if(dn_ub > 0 && dn_pair.second.size() > dn_ub) {
169 const bool is_trusted_root_and_time_ignored =
173 if(is_trusted_root_and_time_ignored) {
180 if(validation_time > subject.
not_after()) {
181 if(is_trusted_root_and_time_ignored) {
189 if(!issuer.
is_CA_cert() && !self_signed_ee_cert) {
202 const auto& extensions_vec = extensions.
extensions();
203 if(subject.
x509_version() < 3 && !extensions_vec.empty()) {
206 for(
auto& extension : extensions_vec) {
207 extension.first->validate(subject, issuer, cert_path, cert_status, i);
215 size_t max_path_length = cert_path.size();
216 for(
size_t i = cert_path.size() - 1; i > 0; --i) {
217 std::set<Certificate_Status_Code>& status = cert_status.at(i);
225 if(max_path_length > 0) {
236 if(subject.
path_limit() != Cert_Extension::NO_CERT_PATH_LIMIT && subject.
path_limit() < max_path_length) {
248 const std::vector<X509_Certificate>& extra_certs,
249 const std::vector<Certificate_Store*>& certstores,
250 std::chrono::system_clock::time_point ref_time,
267 if(signing_cert == ca) {
287 const auto ocsp_timeout = std::chrono::milliseconds::zero();
288 const auto relaxed_restrictions =
289 Path_Validation_Restrictions(
false ,
295 relaxed_restrictions,
302 return validation_result.
result();
308 const std::vector<std::optional<OCSP::Response>>& ocsp_responses,
309 const std::vector<Certificate_Store*>& certstores,
310 std::chrono::system_clock::time_point ref_time,
312 if(cert_path.empty()) {
318 for(
size_t i = 0; i != cert_path.size() - 1; ++i) {
319 std::set<Certificate_Status_Code>& status = cert_status.at(i);
324 if(i < ocsp_responses.size() && (ocsp_responses.at(i) != std::nullopt) &&
327 const auto& ocsp_response = ocsp_responses.at(i);
329 if(
auto dummy_status = ocsp_response->dummy_status()) {
331 status.insert(dummy_status.value());
332 }
else if(
auto signing_cert =
336 }
else if(
auto ocsp_signing_cert_status =
337 verify_ocsp_signing_cert(signing_cert.value(),
339 concat(ocsp_response->certificates(), cert_path),
344 status.insert(ocsp_signing_cert_status);
347 status.insert(ocsp_response->status_for(ca, subject, ref_time, restrictions.
max_ocsp_age()));
355 while(!cert_status.empty() && cert_status.back().empty()) {
356 cert_status.pop_back();
363 const std::vector<std::optional<X509_CRL>>& crls,
364 std::chrono::system_clock::time_point ref_time) {
365 if(cert_path.empty()) {
370 const X509_Time validation_time(ref_time);
372 for(
size_t i = 0; i != cert_path.size() - 1; ++i) {
373 std::set<Certificate_Status_Code>& status = cert_status.at(i);
375 if(i < crls.size() && crls[i].has_value()) {
383 if(validation_time < crls[i]->this_update()) {
387 if(validation_time > crls[i]->next_update()) {
392 if(crls[i]->check_signature(*ca_key) ==
false) {
398 if(crls[i]->is_revoked(subject)) {
404 const auto crl_idp = crls[i]->crl_issuing_distribution_point();
406 if(std::find(dp.begin(), dp.end(), crl_idp) == dp.end()) {
411 for(
const auto& extension : crls[i]->extensions().extensions()) {
416 if(extension.second && !extension.first->oid_of().registered_oid()) {
426 while(!cert_status.empty() && cert_status.back().empty()) {
427 cert_status.pop_back();
434 const std::vector<Certificate_Store*>& certstores,
435 std::chrono::system_clock::time_point ref_time) {
436 if(cert_path.empty()) {
440 if(certstores.empty()) {
444 std::vector<std::optional<X509_CRL>> crls(cert_path.size());
446 for(
size_t i = 0; i != cert_path.size(); ++i) {
447 for(
auto certstore : certstores) {
448 crls[i] = certstore->find_crl_for(cert_path[i]);
458#if defined(BOTAN_HAS_ONLINE_REVOCATION_CHECKS)
461 const std::vector<Certificate_Store*>& trusted_certstores,
462 std::chrono::system_clock::time_point ref_time,
463 std::chrono::milliseconds timeout,
465 if(cert_path.empty()) {
469 std::vector<std::future<std::optional<OCSP::Response>>> ocsp_response_futures;
474 to_ocsp = cert_path.size() - 1;
476 if(cert_path.size() == 1) {
480 for(
size_t i = 0; i < to_ocsp; ++i) {
481 const X509_Certificate& subject = cert_path.at(i);
482 const X509_Certificate& issuer = cert_path.at(i + 1);
484 if(subject.ocsp_responder().empty()) {
485 ocsp_response_futures.emplace_back(std::async(std::launch::deferred, [&]() -> std::optional<OCSP::Response> {
489 ocsp_response_futures.emplace_back(std::async(std::launch::async, [&]() -> std::optional<OCSP::Response> {
495 "application/ocsp-request",
499 }
catch(std::exception&) {
502 if(http.status_code() != 200) {
507 return OCSP::Response(http.body());
512 std::vector<std::optional<OCSP::Response>> ocsp_responses;
513 ocsp_responses.reserve(ocsp_response_futures.size());
515 for(
auto& ocsp_response_future : ocsp_response_futures) {
516 ocsp_responses.push_back(ocsp_response_future.get());
519 return PKIX::check_ocsp(cert_path, ocsp_responses, trusted_certstores, ref_time, restrictions);
523 const std::vector<Certificate_Store*>& certstores,
524 Certificate_Store_In_Memory* crl_store,
525 std::chrono::system_clock::time_point ref_time,
526 std::chrono::milliseconds timeout) {
527 if(cert_path.empty()) {
528 throw Invalid_Argument(
"PKIX::check_crl_online cert_path empty");
530 if(certstores.empty()) {
531 throw Invalid_Argument(
"PKIX::check_crl_online certstores empty");
534 std::vector<std::future<std::optional<X509_CRL>>> future_crls;
535 std::vector<std::optional<X509_CRL>> crls(cert_path.size());
537 for(
size_t i = 0; i != cert_path.size(); ++i) {
538 const std::optional<X509_Certificate>& cert = cert_path.at(i);
539 for(
auto certstore : certstores) {
540 crls[i] = certstore->find_crl_for(*cert);
541 if(crls[i].has_value()) {
554 future_crls.emplace_back(std::future<std::optional<X509_CRL>>());
555 }
else if(cert->crl_distribution_point().empty()) {
557 future_crls.emplace_back(std::async(std::launch::deferred, [&]() -> std::optional<X509_CRL> {
558 throw Not_Implemented(
"No CRL distribution point for this certificate");
561 future_crls.emplace_back(std::async(std::launch::async, [&]() -> std::optional<X509_CRL> {
566 http.throw_unless_ok();
568 return X509_CRL(http.body());
573 for(
size_t i = 0; i != future_crls.size(); ++i) {
574 if(future_crls[i].valid()) {
576 crls[i] = future_crls[i].get();
577 }
catch(std::exception&) {
587 for(
size_t i = 0; i != crl_status.size(); ++i) {
591 crl_store->add_crl(*crls[i]);
602 const std::vector<Certificate_Store*>& trusted_certstores,
604 const std::vector<X509_Certificate>& end_entity_extra) {
615 std::set<std::string> certs_seen;
617 cert_path.push_back(end_entity);
618 certs_seen.insert(end_entity.
fingerprint(
"SHA-256"));
621 for(
const auto& cert : end_entity_extra) {
631 std::optional<X509_Certificate> issuer;
632 bool trusted_issuer =
false;
635 issuer = store->find_cert(issuer_dn, auth_key_id);
637 trusted_issuer =
true;
644 issuer = ee_extras.
find_cert(issuer_dn, auth_key_id);
651 const std::string fprint = issuer->fingerprint(
"SHA-256");
653 if(certs_seen.contains(fprint)) {
658 certs_seen.insert(fprint);
659 cert_path.push_back(*issuer);
661 if(issuer->is_self_signed()) {
676using cert_maybe_trusted = std::pair<std::optional<X509_Certificate>,
bool>;
697 const std::vector<Certificate_Store*>& trusted_certstores,
698 const std::optional<X509_Certificate>& end_entity,
699 const std::vector<X509_Certificate>& end_entity_extra) {
700 if(!cert_paths_out.empty()) {
701 throw Invalid_Argument(
"PKIX::build_all_certificate_paths: cert_paths_out must be empty");
704 if(end_entity->is_self_signed()) {
711 std::vector<Certificate_Status_Code> stats;
713 Certificate_Store_In_Memory ee_extras;
714 for(
const auto& cert : end_entity_extra) {
715 ee_extras.add_certificate(cert);
724 std::set<std::string> certs_seen;
728 std::vector<X509_Certificate> path_so_far;
731 std::vector<cert_maybe_trusted> stack = {{end_entity,
false}};
733 while(!stack.empty()) {
734 std::optional<X509_Certificate> last = stack.back().first;
736 if(last == std::nullopt) {
738 std::string fprint = path_so_far.back().fingerprint(
"SHA-256");
739 certs_seen.erase(fprint);
740 path_so_far.pop_back();
744 const bool trusted = stack.back().second;
748 const std::string fprint = last->fingerprint(
"SHA-256");
749 if(certs_seen.count(fprint) == 1) {
756 if(last->is_self_signed()) {
759 cert_paths_out.push_back(path_so_far);
760 cert_paths_out.back().push_back(*last);
771 const X509_DN issuer_dn = last->issuer_dn();
772 const std::vector<uint8_t> auth_key_id = last->authority_key_id();
775 std::vector<X509_Certificate> trusted_issuers;
776 for(Certificate_Store* store : trusted_certstores) {
777 auto new_issuers = store->find_all_certs(issuer_dn, auth_key_id);
778 trusted_issuers.insert(trusted_issuers.end(), new_issuers.begin(), new_issuers.end());
782 std::vector<X509_Certificate> misc_issuers = ee_extras.find_all_certs(issuer_dn, auth_key_id);
785 if(trusted_issuers.empty() && misc_issuers.empty()) {
791 path_so_far.push_back(*last);
792 certs_seen.emplace(fprint);
795 stack.push_back({std::optional<X509_Certificate>(),
false});
797 for(
const auto& trusted_cert : trusted_issuers) {
798 stack.push_back({trusted_cert,
true});
801 for(
const auto& misc : misc_issuers) {
802 stack.push_back({misc,
false});
808 if(cert_paths_out.empty()) {
810 throw Internal_Error(
"X509 path building failed for unknown reasons");
824 if(chain_status.empty()) {
825 throw Invalid_Argument(
"PKIX::merge_revocation_status chain_status was empty");
828 for(
size_t i = 0; i != chain_status.size() - 1; ++i) {
829 bool had_crl =
false, had_ocsp =
false;
831 if(i < crl.size() && !crl[i].empty()) {
832 for(
auto&& code : crl[i]) {
836 chain_status[i].insert(code);
840 if(i < ocsp.size() && !ocsp[i].empty()) {
841 for(
auto&& code : ocsp[i]) {
849 chain_status[i].insert(code);
853 if(had_crl ==
false && had_ocsp ==
false) {
863 if(cert_status.empty()) {
870 for(
const std::set<Certificate_Status_Code>& s : cert_status) {
872 auto worst = *s.rbegin();
884 const std::vector<Certificate_Store*>& trusted_roots,
885 std::string_view hostname,
887 std::chrono::system_clock::time_point ref_time,
888 std::chrono::milliseconds ocsp_timeout,
889 const std::vector<std::optional<OCSP::Response>>& ocsp_resp) {
890 if(end_certs.empty()) {
895 std::vector<X509_Certificate> end_entity_extra;
896 for(
size_t i = 1; i < end_certs.size(); ++i) {
897 end_entity_extra.push_back(end_certs[i]);
900 std::vector<std::vector<X509_Certificate>> cert_paths;
909 std::vector<Path_Validation_Result> error_results;
911 for(
auto cert_path : cert_paths) {
918 if(!ocsp_resp.empty()) {
919 ocsp_status =
PKIX::check_ocsp(cert_path, ocsp_resp, trusted_roots, ref_time, restrictions);
922 if(ocsp_status.empty() && ocsp_timeout != std::chrono::milliseconds(0)) {
923#if defined(BOTAN_TARGET_OS_HAS_THREADS) && defined(BOTAN_HAS_HTTP_UTIL)
924 ocsp_status = PKIX::check_ocsp_online(cert_path, trusted_roots, ref_time, ocsp_timeout, restrictions);
926 ocsp_status.resize(1);
937 error_results.push_back(std::move(pvd));
940 return error_results[0];
945 const std::vector<Certificate_Store*>& trusted_roots,
946 std::string_view hostname,
948 std::chrono::system_clock::time_point when,
949 std::chrono::milliseconds ocsp_timeout,
950 const std::vector<std::optional<OCSP::Response>>& ocsp_resp) {
951 std::vector<X509_Certificate> certs;
952 certs.push_back(end_cert);
953 return x509_path_validate(certs, restrictions, trusted_roots, hostname, usage, when, ocsp_timeout, ocsp_resp);
959 std::string_view hostname,
961 std::chrono::system_clock::time_point when,
962 std::chrono::milliseconds ocsp_timeout,
963 const std::vector<std::optional<OCSP::Response>>& ocsp_resp) {
964 std::vector<Certificate_Store*> trusted_roots;
967 return x509_path_validate(end_certs, restrictions, trusted_roots, hostname, usage, when, ocsp_timeout, ocsp_resp);
973 std::string_view hostname,
975 std::chrono::system_clock::time_point when,
976 std::chrono::milliseconds ocsp_timeout,
977 const std::vector<std::optional<OCSP::Response>>& ocsp_resp) {
978 std::vector<X509_Certificate> certs;
979 certs.push_back(end_cert);
981 std::vector<Certificate_Store*> trusted_roots;
984 return x509_path_validate(certs, restrictions, trusted_roots, hostname, usage, when, ocsp_timeout, ocsp_resp);
989 bool ocsp_intermediates,
990 std::chrono::seconds max_ocsp_age,
991 std::unique_ptr<Certificate_Store> trusted_ocsp_responders,
992 bool ignore_trusted_root_time_range) :
993 m_require_revocation_information(require_rev),
994 m_ocsp_all_intermediates(ocsp_intermediates),
995 m_minimum_key_strength(key_strength),
996 m_max_ocsp_age(max_ocsp_age),
997 m_trusted_ocsp_responders(std::move(trusted_ocsp_responders)),
998 m_ignore_trusted_root_time_range(ignore_trusted_root_time_range) {
999 if(key_strength <= 80) {
1000 m_trusted_hashes.insert(
"SHA-1");
1003 m_trusted_hashes.insert(
"SHA-224");
1004 m_trusted_hashes.insert(
"SHA-256");
1005 m_trusted_hashes.insert(
"SHA-384");
1006 m_trusted_hashes.insert(
"SHA-512");
1007 m_trusted_hashes.insert(
"SHAKE-256(512)");
1008 m_trusted_hashes.insert(
"SHAKE-256(912)");
1014 for(
const auto& status_set_i : all_statuses) {
1015 std::set<Certificate_Status_Code> warning_set_i;
1016 for(
const auto& code : status_set_i) {
1019 warning_set_i.insert(code);
1022 warnings.push_back(warning_set_i);
1029 std::vector<X509_Certificate>&& cert_chain) :
1030 m_all_status(std::move(status)),
1031 m_warnings(find_warnings(m_all_status)),
1032 m_cert_path(std::move(cert_chain)),
1033 m_overall(PKIX::overall_status(m_all_status)) {}
1036 if(m_cert_path.empty()) {
1037 throw Invalid_State(
"Path_Validation_Result::trust_root no path set");
1040 throw Invalid_State(
"Path_Validation_Result::trust_root meaningless with invalid status");
1043 return m_cert_path[m_cert_path.size() - 1];
1052 for(
const auto& status_set_i : m_warnings) {
1053 if(!status_set_i.empty()) {
1073 return "Unknown error";
1077 const std::string sep(
", ");
1078 std::ostringstream oss;
1079 for(
size_t i = 0; i < m_warnings.size(); i++) {
1080 for(
auto code : m_warnings[i]) {
1081 oss <<
"[" << std::to_string(i) <<
"] " <<
status_string(code) << sep;
1085 std::string res = oss.str();
1087 if(res.size() >= sep.size()) {
1088 res = res.substr(0, res.size() - sep.size());
#define BOTAN_ASSERT_NOMSG(expr)
static BigInt from_bytes(std::span< const uint8_t > bytes)
std::optional< X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
void add_certificate(const X509_Certificate &cert)
bool certificate_known(const X509_Certificate &cert) const
const std::vector< OID > & get_extension_oids() const
std::vector< std::pair< std::unique_ptr< Certificate_Extension >, bool > > extensions() const
bool registered_oid() const
bool require_revocation_information() const
bool ocsp_all_intermediates() const
const std::set< std::string > & trusted_hashes() const
Path_Validation_Restrictions(bool require_rev=false, size_t minimum_key_strength=110, bool ocsp_all_intermediates=false, std::chrono::seconds max_ocsp_age=std::chrono::seconds::zero(), std::unique_ptr< Certificate_Store > trusted_ocsp_responders=std::make_unique< Certificate_Store_In_Memory >(), bool ignore_trusted_root_time_range=false)
std::chrono::seconds max_ocsp_age() const
bool ignore_trusted_root_time_range() const
size_t minimum_key_strength() const
const Certificate_Store * trusted_ocsp_responders() const
Certificate_Status_Code result() const
Path_Validation_Result(CertificatePathStatusCodes status, std::vector< X509_Certificate > &&cert_chain)
bool successful_validation() const
static const char * status_string(Certificate_Status_Code code)
const X509_Certificate & trust_root() const
std::string result_string() const
std::string warnings_string() const
CertificatePathStatusCodes warnings() const
std::string fingerprint(std::string_view hash_name="SHA-1") const
const X509_DN & subject_dn() const
uint32_t path_limit() const
const X509_Time & not_after() const
const std::vector< uint8_t > & authority_key_id() const
const Extensions & v3_extensions() const
std::vector< std::string > crl_distribution_points() const
bool allowed_usage(Key_Constraints usage) const
const X509_DN & issuer_dn() const
const std::vector< uint8_t > & v2_issuer_key_id() const
uint32_t x509_version() const
bool is_self_signed() const
bool is_serial_negative() const
std::unique_ptr< Public_Key > subject_public_key() const
const std::vector< uint8_t > & v2_subject_key_id() const
const X509_Time & not_before() const
static size_t lookup_ub(const OID &oid)
const std::vector< std::pair< OID, ASN1_String > > & dn_info() const
const AlgorithmIdentifier & signature_algorithm() const
std::pair< Certificate_Status_Code, std::string > verify_signature(const Public_Key &key) const
Response POST_sync(std::string_view url, std::string_view content_type, const std::vector< uint8_t > &body, size_t allowable_redirects, std::chrono::milliseconds timeout)
Response GET_sync(std::string_view url, size_t allowable_redirects, std::chrono::milliseconds timeout)
void merge_revocation_status(CertificatePathStatusCodes &chain_status, const CertificatePathStatusCodes &crl_status, const CertificatePathStatusCodes &ocsp_status, const Path_Validation_Restrictions &restrictions)
Certificate_Status_Code build_certificate_path(std::vector< X509_Certificate > &cert_path_out, const std::vector< Certificate_Store * > &trusted_certstores, const X509_Certificate &end_entity, const std::vector< X509_Certificate > &end_entity_extra)
Certificate_Status_Code overall_status(const CertificatePathStatusCodes &cert_status)
CertificatePathStatusCodes check_ocsp(const std::vector< X509_Certificate > &cert_path, const std::vector< std::optional< OCSP::Response > > &ocsp_responses, const std::vector< Certificate_Store * > &certstores, std::chrono::system_clock::time_point ref_time, const Path_Validation_Restrictions &restrictions)
Certificate_Status_Code build_all_certificate_paths(std::vector< std::vector< X509_Certificate > > &cert_paths, const std::vector< Certificate_Store * > &trusted_certstores, const std::optional< X509_Certificate > &end_entity, const std::vector< X509_Certificate > &end_entity_extra)
CertificatePathStatusCodes check_chain(const std::vector< X509_Certificate > &cert_path, std::chrono::system_clock::time_point ref_time, std::string_view hostname, Usage_Type usage, const Path_Validation_Restrictions &restrictions)
CertificatePathStatusCodes check_crl(const std::vector< X509_Certificate > &cert_path, const std::vector< std::optional< X509_CRL > > &crls, std::chrono::system_clock::time_point ref_time)
std::vector< std::set< Certificate_Status_Code > > CertificatePathStatusCodes
Path_Validation_Result x509_path_validate(const std::vector< X509_Certificate > &end_certs, const Path_Validation_Restrictions &restrictions, const std::vector< Certificate_Store * > &trusted_roots, std::string_view hostname, Usage_Type usage, std::chrono::system_clock::time_point ref_time, std::chrono::milliseconds ocsp_timeout, const std::vector< std::optional< OCSP::Response > > &ocsp_resp)
constexpr auto concat(Rs &&... ranges)
std::string to_string(ErrorType type)
Convert an ErrorType to string.
@ TRUSTED_CERT_NOT_YET_VALID
@ DUPLICATE_CERT_EXTENSION
@ OCSP_RESPONSE_MISSING_KEYUSAGE
@ TRUSTED_CERT_HAS_EXPIRED
@ OCSP_ISSUER_NOT_TRUSTED
@ CA_CERT_NOT_FOR_CRL_ISSUER
@ CA_CERT_NOT_FOR_CERT_ISSUER
@ OCSP_SERVER_NOT_AVAILABLE
@ V2_IDENTIFIERS_IN_V1_CERT
@ SIGNATURE_METHOD_TOO_WEAK