9#include <botan/x509path.h>
10#include <botan/x509_ext.h>
11#include <botan/pk_keys.h>
12#include <botan/ocsp.h>
13#include <botan/oids.h>
21#if defined(BOTAN_HAS_ONLINE_REVOCATION_CHECKS)
23 #include <botan/http_util.h>
32PKIX::check_chain(
const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
33 std::chrono::system_clock::time_point ref_time,
34 const std::string& hostname,
36 size_t min_signature_algo_strength,
37 const std::set<std::string>& trusted_hashes)
40 throw Invalid_Argument(
"PKIX::check_chain cert_path empty");
42 const bool self_signed_ee_cert = (cert_path.size() == 1);
48 if(!hostname.empty() && !cert_path[0]->matches_dns_name(hostname))
51 if(!cert_path[0]->allowed_usage(usage))
54 if(cert_path[0]->is_CA_cert() ==
false &&
68 for(
size_t i = 0; i != cert_path.size(); ++i)
70 std::set<Certificate_Status_Code>& status = cert_status.at(i);
72 const bool at_self_signed_root = (i == cert_path.size() - 1);
74 const std::shared_ptr<const X509_Certificate>& subject = cert_path[i];
76 const std::shared_ptr<const X509_Certificate>& issuer = cert_path[at_self_signed_root ? (i) : (i + 1)];
78 if(at_self_signed_root && (issuer->is_self_signed() ==
false))
83 if(subject->issuer_dn() != issuer->subject_dn())
89 if(subject->is_serial_negative())
96 for(
const auto& dn_pair : subject->subject_dn().dn_info())
100 if(dn_ub > 0 && dn_pair.second.size() > dn_ub)
107 if(validation_time < subject->not_before())
110 if(validation_time > subject->not_after())
114 if(!issuer->is_CA_cert() && !self_signed_ee_cert)
117 std::unique_ptr<Public_Key> issuer_key(issuer->subject_public_key());
136 status.insert(sig_status);
138 if(issuer_key->estimated_strength() < min_signature_algo_strength)
143 if(trusted_hashes.size() > 0 && !at_self_signed_root)
145 if(trusted_hashes.count(subject->hash_used_for_signature()) == 0)
152 if(subject->x509_version() == 1)
154 if(subject->v2_issuer_key_id().empty() ==
false ||
155 subject->v2_subject_key_id().empty() ==
false)
161 Extensions extensions = subject->v3_extensions();
162 const auto& extensions_vec = extensions.extensions();
163 if(subject->x509_version() < 3 && !extensions_vec.empty())
167 for(
auto& extension : extensions_vec)
169 extension.first->validate(*subject, *issuer, cert_path, cert_status, i);
171 if(extensions.extensions().size() != extensions.get_extension_oids().size())
178 size_t max_path_length = cert_path.size();
179 for(
size_t i = cert_path.size() - 1; i > 0 ; --i)
181 std::set<Certificate_Status_Code>& status = cert_status.at(i);
182 const std::shared_ptr<const X509_Certificate>& subject = cert_path[i];
188 if(subject->subject_dn() != subject->issuer_dn())
190 if(max_path_length > 0)
204 if(subject->path_limit() != Cert_Extension::NO_CERT_PATH_LIMIT && subject->path_limit() < max_path_length)
206 max_path_length = subject->path_limit();
214PKIX::check_ocsp(
const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
215 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_responses,
216 const std::vector<Certificate_Store*>& trusted_certstores,
217 std::chrono::system_clock::time_point ref_time,
218 std::chrono::seconds max_ocsp_age)
220 if(cert_path.empty())
221 throw Invalid_Argument(
"PKIX::check_ocsp cert_path empty");
225 for(
size_t i = 0; i != cert_path.size() - 1; ++i)
227 std::set<Certificate_Status_Code>& status = cert_status.at(i);
229 std::shared_ptr<const X509_Certificate> subject = cert_path.at(i);
230 std::shared_ptr<const X509_Certificate> ca = cert_path.at(i+1);
232 if(i < ocsp_responses.size() && (ocsp_responses.at(i) !=
nullptr)
240 std::vector<std::shared_ptr<const X509_Certificate>> ocsp_cert_path(cert_path.begin() + i, cert_path.end());
241 Certificate_Status_Code ocsp_signature_status = ocsp_responses.at(i)->check_signature(trusted_certstores, ocsp_cert_path);
247 status.insert(ocsp_status);
252 status.insert(ocsp_signature_status);
262 while(cert_status.size() > 0 && cert_status.back().empty())
263 cert_status.pop_back();
269PKIX::check_crl(
const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
270 const std::vector<std::shared_ptr<const X509_CRL>>& crls,
271 std::chrono::system_clock::time_point ref_time)
273 if(cert_path.empty())
274 throw Invalid_Argument(
"PKIX::check_crl cert_path empty");
277 const X509_Time validation_time(ref_time);
279 for(
size_t i = 0; i != cert_path.size() - 1; ++i)
281 std::set<Certificate_Status_Code>& status = cert_status.at(i);
283 if(i < crls.size() && crls.at(i))
285 std::shared_ptr<const X509_Certificate> subject = cert_path.at(i);
286 std::shared_ptr<const X509_Certificate> ca = cert_path.at(i+1);
291 if(validation_time < crls[i]->this_update())
294 if(validation_time > crls[i]->next_update())
297 if(crls[i]->check_signature(ca->subject_public_key()) ==
false)
302 if(crls[i]->is_revoked(*subject))
305 std::string dp = subject->crl_distribution_point();
308 if(dp != crls[i]->crl_issuing_distribution_point())
314 for(
const auto& extension : crls[i]->extensions().extensions())
332 while(cert_status.size() > 0 && cert_status.back().empty())
333 cert_status.pop_back();
339PKIX::check_crl(
const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
340 const std::vector<Certificate_Store*>& certstores,
341 std::chrono::system_clock::time_point ref_time)
343 if(cert_path.empty())
344 throw Invalid_Argument(
"PKIX::check_crl cert_path empty");
346 if(certstores.empty())
347 throw Invalid_Argument(
"PKIX::check_crl certstores empty");
349 std::vector<std::shared_ptr<const X509_CRL>> crls(cert_path.size());
351 for(
size_t i = 0; i != cert_path.size(); ++i)
354 for(
size_t c = 0; c != certstores.size(); ++c)
356 crls[i] = certstores[c]->find_crl_for(*cert_path[i]);
362 return PKIX::check_crl(cert_path, crls, ref_time);
365#if defined(BOTAN_HAS_ONLINE_REVOCATION_CHECKS)
368PKIX::check_ocsp_online(
const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
369 const std::vector<Certificate_Store*>& trusted_certstores,
370 std::chrono::system_clock::time_point ref_time,
371 std::chrono::milliseconds timeout,
372 bool ocsp_check_intermediate_CAs,
373 std::chrono::seconds max_ocsp_age)
375 if(cert_path.empty())
376 throw Invalid_Argument(
"PKIX::check_ocsp_online cert_path empty");
378 std::vector<std::future<std::shared_ptr<const OCSP::Response>>> ocsp_response_futures;
382 if(ocsp_check_intermediate_CAs)
383 to_ocsp = cert_path.size() - 1;
384 if(cert_path.size() == 1)
387 for(
size_t i = 0; i < to_ocsp; ++i)
389 const std::shared_ptr<const X509_Certificate>& subject = cert_path.at(i);
390 const std::shared_ptr<const X509_Certificate>& issuer = cert_path.at(i+1);
392 if(subject->ocsp_responder() ==
"")
394 ocsp_response_futures.emplace_back(std::async(std::launch::deferred, [&]() -> std::shared_ptr<const OCSP::Response> {
400 ocsp_response_futures.emplace_back(std::async(std::launch::async, [&]() -> std::shared_ptr<const OCSP::Response> {
401 OCSP::Request req(*issuer,
BigInt::decode(subject->serial_number()));
407 "application/ocsp-request",
412 catch(std::exception&)
416 if (http.status_code() != 200)
420 return std::make_shared<const OCSP::Response>(http.body());
425 std::vector<std::shared_ptr<const OCSP::Response>> ocsp_responses;
427 for(
size_t i = 0; i < ocsp_response_futures.size(); ++i)
429 ocsp_responses.push_back(ocsp_response_futures[i].get());
432 return PKIX::check_ocsp(cert_path, ocsp_responses, trusted_certstores, ref_time, max_ocsp_age);
436PKIX::check_crl_online(
const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
437 const std::vector<Certificate_Store*>& certstores,
438 Certificate_Store_In_Memory* crl_store,
439 std::chrono::system_clock::time_point ref_time,
440 std::chrono::milliseconds timeout)
442 if(cert_path.empty())
443 throw Invalid_Argument(
"PKIX::check_crl_online cert_path empty");
444 if(certstores.empty())
445 throw Invalid_Argument(
"PKIX::check_crl_online certstores empty");
447 std::vector<std::future<std::shared_ptr<const X509_CRL>>> future_crls;
448 std::vector<std::shared_ptr<const X509_CRL>> crls(cert_path.size());
450 for(
size_t i = 0; i != cert_path.size(); ++i)
452 const std::shared_ptr<const X509_Certificate>& cert = cert_path.at(i);
453 for(
size_t c = 0; c != certstores.size(); ++c)
455 crls[i] = certstores[c]->find_crl_for(*cert);
469 future_crls.emplace_back(std::future<std::shared_ptr<const X509_CRL>>());
471 else if(cert->crl_distribution_point() ==
"")
474 future_crls.emplace_back(std::async(std::launch::deferred, [&]() -> std::shared_ptr<const X509_CRL> {
475 throw Not_Implemented(
"No CRL distribution point for this certificate");
480 future_crls.emplace_back(std::async(std::launch::async, [&]() -> std::shared_ptr<const X509_CRL> {
484 http.throw_unless_ok();
486 return std::make_shared<const X509_CRL>(http.body());
491 for(
size_t i = 0; i != future_crls.size(); ++i)
493 if(future_crls[i].valid())
497 crls[i] = future_crls[i].get();
499 catch(std::exception&)
511 for(
size_t i = 0; i != crl_status.size(); ++i)
517 crl_store->add_crl(crls[i]);
528PKIX::build_certificate_path(std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
529 const std::vector<Certificate_Store*>& trusted_certstores,
530 const std::shared_ptr<const X509_Certificate>& end_entity,
531 const std::vector<std::shared_ptr<const X509_Certificate>>& end_entity_extra)
533 if(end_entity->is_self_signed())
544 std::set<std::string> certs_seen;
546 cert_path.push_back(end_entity);
547 certs_seen.insert(end_entity->fingerprint(
"SHA-256"));
549 Certificate_Store_In_Memory ee_extras;
550 for(
size_t i = 0; i != end_entity_extra.size(); ++i)
551 ee_extras.add_certificate(end_entity_extra[i]);
556 const X509_Certificate& last = *cert_path.back();
557 const X509_DN issuer_dn = last.issuer_dn();
558 const std::vector<uint8_t> auth_key_id = last.authority_key_id();
560 std::shared_ptr<const X509_Certificate> issuer;
561 bool trusted_issuer =
false;
563 for(Certificate_Store* store : trusted_certstores)
565 issuer = store->find_cert(issuer_dn, auth_key_id);
568 trusted_issuer =
true;
576 issuer = ee_extras.find_cert(issuer_dn, auth_key_id);
582 const std::string fprint = issuer->fingerprint(
"SHA-256");
584 if(certs_seen.count(fprint) > 0)
589 certs_seen.insert(fprint);
590 cert_path.push_back(issuer);
592 if(issuer->is_self_signed())
612using cert_maybe_trusted = std::pair<std::shared_ptr<const X509_Certificate>,
bool>;
633PKIX::build_all_certificate_paths(std::vector<std::vector<std::shared_ptr<const X509_Certificate>>>& cert_paths_out,
634 const std::vector<Certificate_Store*>& trusted_certstores,
635 const std::shared_ptr<const X509_Certificate>& end_entity,
636 const std::vector<std::shared_ptr<const X509_Certificate>>& end_entity_extra)
638 if(!cert_paths_out.empty())
640 throw Invalid_Argument(
"PKIX::build_all_certificate_paths: cert_paths_out must be empty");
643 if(end_entity->is_self_signed())
651 std::vector<Certificate_Status_Code> stats;
653 Certificate_Store_In_Memory ee_extras;
654 for(
size_t i = 0; i != end_entity_extra.size(); ++i)
656 ee_extras.add_certificate(end_entity_extra[i]);
665 std::set<std::string> certs_seen;
669 std::vector<std::shared_ptr<const X509_Certificate>> path_so_far;
672 std::vector<cert_maybe_trusted> stack = { {end_entity,
false} };
674 while(!stack.empty())
677 if(stack.back().first ==
nullptr)
680 std::string fprint = path_so_far.back()->fingerprint(
"SHA-256");
681 certs_seen.erase(fprint);
682 path_so_far.pop_back();
687 std::shared_ptr<const X509_Certificate> last = stack.back().first;
688 bool trusted = stack.back().second;
692 const std::string fprint = last->fingerprint(
"SHA-256");
693 if(certs_seen.count(fprint) == 1)
701 if(last->is_self_signed())
706 cert_paths_out.push_back(path_so_far);
707 cert_paths_out.back().push_back(last);
719 const X509_DN issuer_dn = last->issuer_dn();
720 const std::vector<uint8_t> auth_key_id = last->authority_key_id();
723 std::vector<std::shared_ptr<const X509_Certificate>> trusted_issuers;
724 for(Certificate_Store* store : trusted_certstores)
726 auto new_issuers = store->find_all_certs(issuer_dn, auth_key_id);
727 trusted_issuers.insert(trusted_issuers.end(), new_issuers.begin(), new_issuers.end());
731 std::vector<std::shared_ptr<const X509_Certificate>> misc_issuers =
732 ee_extras.find_all_certs(issuer_dn, auth_key_id);
735 if(trusted_issuers.size() + misc_issuers.size() == 0)
742 path_so_far.push_back(last);
743 certs_seen.emplace(fprint);
746 stack.push_back({std::shared_ptr<const X509_Certificate>(
nullptr),
false});
748 for(
const auto& trusted_cert : trusted_issuers)
750 stack.push_back({trusted_cert,
true});
753 for(
const auto& misc : misc_issuers)
755 stack.push_back({misc,
false});
761 if(cert_paths_out.empty())
764 throw Internal_Error(
"X509 path building failed for unknown reasons");
779 bool require_rev_on_end_entity,
780 bool require_rev_on_intermediates)
782 if(chain_status.empty())
783 throw Invalid_Argument(
"PKIX::merge_revocation_status chain_status was empty");
785 for(
size_t i = 0; i != chain_status.size() - 1; ++i)
787 bool had_crl =
false, had_ocsp =
false;
789 if(i < crl.size() && crl[i].size() > 0)
791 for(
auto&& code : crl[i])
797 chain_status[i].insert(code);
801 if(i < ocsp.size() && ocsp[i].size() > 0)
803 for(
auto&& code : ocsp[i])
812 chain_status[i].insert(code);
816 if(had_crl ==
false && had_ocsp ==
false)
818 if((require_rev_on_end_entity && i == 0) ||
819 (require_rev_on_intermediates && i > 0))
829 if(cert_status.empty())
830 throw Invalid_Argument(
"PKIX::overall_status empty cert status");
835 for(
const std::set<Certificate_Status_Code>& s : cert_status)
839 auto worst = *s.rbegin();
843 overall_status = worst;
847 return overall_status;
851 const std::vector<X509_Certificate>& end_certs,
853 const std::vector<Certificate_Store*>& trusted_roots,
854 const std::string& hostname,
856 std::chrono::system_clock::time_point ref_time,
857 std::chrono::milliseconds ocsp_timeout,
858 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp)
860 if(end_certs.empty())
865 std::shared_ptr<const X509_Certificate> end_entity(std::make_shared<const X509_Certificate>(end_certs[0]));
866 std::vector<std::shared_ptr<const X509_Certificate>> end_entity_extra;
867 for(
size_t i = 1; i < end_certs.size(); ++i)
869 end_entity_extra.push_back(std::make_shared<const X509_Certificate>(end_certs[i]));
872 std::vector<std::vector<std::shared_ptr<const X509_Certificate>>> cert_paths;
873 Certificate_Status_Code path_building_result = PKIX::build_all_certificate_paths(cert_paths, trusted_roots, end_entity, end_entity_extra);
881 std::vector<Path_Validation_Result> error_results;
883 for(
auto cert_path : cert_paths)
886 PKIX::check_chain(cert_path, ref_time,
892 PKIX::check_crl(cert_path, trusted_roots, ref_time);
896 if(ocsp_resp.size() > 0)
898 ocsp_status = PKIX::check_ocsp(cert_path, ocsp_resp, trusted_roots, ref_time, restrictions.
max_ocsp_age());
901 if(ocsp_status.empty() && ocsp_timeout != std::chrono::milliseconds(0))
903#if defined(BOTAN_TARGET_OS_HAS_THREADS) && defined(BOTAN_HAS_HTTP_UTIL)
904 ocsp_status = PKIX::check_ocsp_online(cert_path, trusted_roots, ref_time,
907 ocsp_status.resize(1);
912 PKIX::merge_revocation_status(status, crl_status, ocsp_status,
923 error_results.push_back(std::move(pvd));
926 return error_results[0];
932 const std::vector<Certificate_Store*>& trusted_roots,
933 const std::string& hostname,
935 std::chrono::system_clock::time_point when,
936 std::chrono::milliseconds ocsp_timeout,
937 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp)
939 std::vector<X509_Certificate> certs;
940 certs.push_back(end_cert);
941 return x509_path_validate(certs, restrictions, trusted_roots, hostname, usage, when, ocsp_timeout, ocsp_resp);
945 const std::vector<X509_Certificate>& end_certs,
948 const std::string& hostname,
950 std::chrono::system_clock::time_point when,
951 std::chrono::milliseconds ocsp_timeout,
952 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp)
954 std::vector<Certificate_Store*> trusted_roots;
957 return x509_path_validate(end_certs, restrictions, trusted_roots, hostname, usage, when, ocsp_timeout, ocsp_resp);
964 const std::string& hostname,
966 std::chrono::system_clock::time_point when,
967 std::chrono::milliseconds ocsp_timeout,
968 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp)
970 std::vector<X509_Certificate> certs;
971 certs.push_back(end_cert);
973 std::vector<Certificate_Store*> trusted_roots;
976 return x509_path_validate(certs, restrictions, trusted_roots, hostname, usage, when, ocsp_timeout, ocsp_resp);
981 bool ocsp_intermediates,
982 std::chrono::seconds max_ocsp_age) :
983 m_require_revocation_information(require_rev),
984 m_ocsp_all_intermediates(ocsp_intermediates),
985 m_minimum_key_strength(key_strength),
986 m_max_ocsp_age(max_ocsp_age)
988 if(key_strength <= 80)
989 { m_trusted_hashes.insert(
"SHA-160"); }
991 m_trusted_hashes.insert(
"SHA-224");
992 m_trusted_hashes.insert(
"SHA-256");
993 m_trusted_hashes.insert(
"SHA-384");
994 m_trusted_hashes.insert(
"SHA-512");
1001 for(
const auto& status_set_i : all_statuses)
1003 std::set<Certificate_Status_Code> warning_set_i;
1004 for(
const auto& code : status_set_i)
1009 warning_set_i.insert(code);
1012 warnings.push_back(warning_set_i);
1019 std::vector<std::shared_ptr<const X509_Certificate>>&& cert_chain) :
1020 m_all_status(status),
1021 m_warnings(find_warnings(m_all_status)),
1022 m_cert_path(cert_chain),
1023 m_overall(PKIX::overall_status(m_all_status))
1029 if(m_cert_path.empty())
1030 throw Invalid_State(
"Path_Validation_Result::trust_root no path set");
1032 throw Invalid_State(
"Path_Validation_Result::trust_root meaningless with invalid status");
1034 return *m_cert_path[m_cert_path.size()-1];
1039 std::set<std::string> hashes;
1040 for(
size_t i = 0; i != m_cert_path.size(); ++i)
1041 hashes.insert(m_cert_path[i]->hash_used_for_signature());
1054 for(
auto status_set_i : m_warnings)
1055 if(!status_set_i.empty())
1075 return "Unknown error";
1080 const std::string sep(
", ");
1082 for(
size_t i = 0; i < m_warnings.size(); i++)
1084 for(
auto code : m_warnings[i])
1085 res +=
"[" + std::to_string(i) +
"] " +
status_string(code) + sep;
1088 if(res.size() >= sep.size())
1089 res = res.substr(0, res.size() - sep.size());
#define BOTAN_ASSERT_NONNULL(ptr)
static BigInt decode(const uint8_t buf[], size_t length)
bool require_revocation_information() const
bool ocsp_all_intermediates() const
const std::set< std::string > & trusted_hashes() const
std::chrono::seconds max_ocsp_age() 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())
size_t minimum_key_strength() const
Certificate_Status_Code result() const
Path_Validation_Result(CertificatePathStatusCodes status, std::vector< std::shared_ptr< const 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::set< std::string > trusted_hashes() const
std::string warnings_string() const
CertificatePathStatusCodes warnings() const
static size_t lookup_ub(const OID &oid)
Response GET_sync(const std::string &url, size_t allowable_redirects, std::chrono::milliseconds timeout)
Response POST_sync(const std::string &url, const std::string &content_type, const std::vector< uint8_t > &body, size_t allowable_redirects, std::chrono::milliseconds timeout)
BOTAN_UNSTABLE_API std::string oid2str_or_empty(const OID &oid)
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, const std::string &hostname, Usage_Type usage, std::chrono::system_clock::time_point ref_time, std::chrono::milliseconds ocsp_timeout, const std::vector< std::shared_ptr< const OCSP::Response > > &ocsp_resp)
std::string to_string(ErrorType type)
Convert an ErrorType to string.
@ DUPLICATE_CERT_EXTENSION
@ 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