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>
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)
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);
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();
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())
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)
237 Certificate_Status_Code ocsp_signature_status = ocsp_responses.at(i)->check_signature(trusted_certstores, cert_path);
243 status.insert(ocsp_status);
248 status.insert(ocsp_signature_status);
258 while(cert_status.size() > 0 && cert_status.back().empty())
259 cert_status.pop_back();
265 PKIX::check_crl(
const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
266 const std::vector<std::shared_ptr<const X509_CRL>>& crls,
267 std::chrono::system_clock::time_point ref_time)
269 if(cert_path.empty())
273 const X509_Time validation_time(ref_time);
275 for(
size_t i = 0; i != cert_path.size() - 1; ++i)
277 std::set<Certificate_Status_Code>& status = cert_status.at(i);
279 if(i < crls.size() && crls.at(i))
281 std::shared_ptr<const X509_Certificate> subject = cert_path.at(i);
282 std::shared_ptr<const X509_Certificate> ca = cert_path.at(i+1);
287 if(validation_time < crls[i]->this_update())
290 if(validation_time > crls[i]->next_update())
293 if(crls[i]->check_signature(ca->subject_public_key()) ==
false)
298 if(crls[i]->is_revoked(*subject))
301 std::string dp = subject->crl_distribution_point();
304 if(dp != crls[i]->crl_issuing_distribution_point())
310 for(
const auto& extension : crls[i]->extensions().extensions())
328 while(cert_status.size() > 0 && cert_status.back().empty())
329 cert_status.pop_back();
335 PKIX::check_crl(
const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
336 const std::vector<Certificate_Store*>& certstores,
337 std::chrono::system_clock::time_point ref_time)
339 if(cert_path.empty())
342 if(certstores.empty())
345 std::vector<std::shared_ptr<const X509_CRL>> crls(cert_path.size());
347 for(
size_t i = 0; i != cert_path.size(); ++i)
350 for(
size_t c = 0; c != certstores.size(); ++c)
352 crls[i] = certstores[c]->find_crl_for(*cert_path[i]);
361 #if defined(BOTAN_HAS_ONLINE_REVOCATION_CHECKS)
364 PKIX::check_ocsp_online(
const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
365 const std::vector<Certificate_Store*>& trusted_certstores,
366 std::chrono::system_clock::time_point ref_time,
367 std::chrono::milliseconds timeout,
368 bool ocsp_check_intermediate_CAs,
369 std::chrono::seconds max_ocsp_age)
371 if(cert_path.empty())
374 std::vector<std::future<std::shared_ptr<const OCSP::Response>>> ocsp_response_futures;
378 if(ocsp_check_intermediate_CAs)
379 to_ocsp = cert_path.size() - 1;
380 if(cert_path.size() == 1)
383 for(
size_t i = 0; i < to_ocsp; ++i)
385 const std::shared_ptr<const X509_Certificate>& subject = cert_path.at(i);
386 const std::shared_ptr<const X509_Certificate>& issuer = cert_path.at(i+1);
388 if(subject->ocsp_responder() ==
"")
390 ocsp_response_futures.emplace_back(std::async(std::launch::deferred, [&]() -> std::shared_ptr<const OCSP::Response> {
396 ocsp_response_futures.emplace_back(std::async(std::launch::async, [&]() -> std::shared_ptr<const OCSP::Response> {
397 OCSP::Request req(*issuer,
BigInt::decode(subject->serial_number()));
403 "application/ocsp-request",
408 catch(std::exception&)
412 if (http.status_code() != 200)
416 return std::make_shared<const OCSP::Response>(http.body());
421 std::vector<std::shared_ptr<const OCSP::Response>> ocsp_responses;
423 for(
size_t i = 0; i < ocsp_response_futures.size(); ++i)
425 ocsp_responses.push_back(ocsp_response_futures[i].
get());
428 return PKIX::check_ocsp(cert_path, ocsp_responses, trusted_certstores, ref_time, max_ocsp_age);
432 PKIX::check_crl_online(
const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
433 const std::vector<Certificate_Store*>& certstores,
434 Certificate_Store_In_Memory* crl_store,
435 std::chrono::system_clock::time_point ref_time,
436 std::chrono::milliseconds timeout)
438 if(cert_path.empty())
439 throw Invalid_Argument(
"PKIX::check_crl_online cert_path empty");
440 if(certstores.empty())
441 throw Invalid_Argument(
"PKIX::check_crl_online certstores empty");
443 std::vector<std::future<std::shared_ptr<const X509_CRL>>> future_crls;
444 std::vector<std::shared_ptr<const X509_CRL>> crls(cert_path.size());
446 for(
size_t i = 0; i != cert_path.size(); ++i)
448 const std::shared_ptr<const X509_Certificate>& cert = cert_path.at(i);
449 for(
size_t c = 0; c != certstores.size(); ++c)
451 crls[i] = certstores[c]->find_crl_for(*cert);
465 future_crls.emplace_back(std::future<std::shared_ptr<const X509_CRL>>());
467 else if(cert->crl_distribution_point() ==
"")
470 future_crls.emplace_back(std::async(std::launch::deferred, [&]() -> std::shared_ptr<const X509_CRL> {
471 throw Not_Implemented(
"No CRL distribution point for this certificate");
476 future_crls.emplace_back(std::async(std::launch::async, [&]() -> std::shared_ptr<const X509_CRL> {
482 return std::make_shared<const X509_CRL>(http.body());
487 for(
size_t i = 0; i != future_crls.size(); ++i)
489 if(future_crls[i].valid())
493 crls[i] = future_crls[i].get();
495 catch(std::exception&)
507 for(
size_t i = 0; i != crl_status.size(); ++i)
513 crl_store->add_crl(crls[i]);
525 const std::vector<Certificate_Store*>& trusted_certstores,
526 const std::shared_ptr<const X509_Certificate>& end_entity,
527 const std::vector<std::shared_ptr<const X509_Certificate>>& end_entity_extra)
529 if(end_entity->is_self_signed())
540 std::set<std::string> certs_seen;
542 cert_path.push_back(end_entity);
543 certs_seen.insert(end_entity->fingerprint(
"SHA-256"));
546 for(
size_t i = 0; i != end_entity_extra.size(); ++i)
556 std::shared_ptr<const X509_Certificate> issuer;
557 bool trusted_issuer =
false;
561 issuer = store->find_cert(issuer_dn, auth_key_id);
564 trusted_issuer =
true;
572 issuer = ee_extras.
find_cert(issuer_dn, auth_key_id);
578 const std::string fprint = issuer->fingerprint(
"SHA-256");
580 if(certs_seen.count(fprint) > 0)
585 certs_seen.insert(fprint);
586 cert_path.push_back(issuer);
588 if(issuer->is_self_signed())
608 using cert_maybe_trusted = std::pair<std::shared_ptr<const X509_Certificate>,
bool>;
630 const std::vector<Certificate_Store*>& trusted_certstores,
631 const std::shared_ptr<const X509_Certificate>& end_entity,
632 const std::vector<std::shared_ptr<const X509_Certificate>>& end_entity_extra)
634 if(!cert_paths_out.empty())
636 throw Invalid_Argument(
"PKIX::build_all_certificate_paths: cert_paths_out must be empty");
639 if(end_entity->is_self_signed())
647 std::vector<Certificate_Status_Code> stats;
650 for(
size_t i = 0; i != end_entity_extra.size(); ++i)
661 std::set<std::string> certs_seen;
665 std::vector<std::shared_ptr<const X509_Certificate>> path_so_far;
668 std::vector<cert_maybe_trusted> stack = { {end_entity,
false} };
670 while(!stack.empty())
673 if(stack.back().first ==
nullptr)
676 std::string fprint = path_so_far.back()->fingerprint(
"SHA-256");
677 certs_seen.erase(fprint);
678 path_so_far.pop_back();
683 std::shared_ptr<const X509_Certificate> last = stack.back().first;
684 bool trusted = stack.back().second;
688 const std::string fprint = last->fingerprint(
"SHA-256");
689 if(certs_seen.count(fprint) == 1)
697 if(last->is_self_signed())
702 cert_paths_out.push_back(path_so_far);
703 cert_paths_out.back().push_back(last);
715 const X509_DN issuer_dn = last->issuer_dn();
716 const std::vector<uint8_t> auth_key_id = last->authority_key_id();
719 std::vector<std::shared_ptr<const X509_Certificate>> trusted_issuers;
722 auto new_issuers = store->find_all_certs(issuer_dn, auth_key_id);
723 trusted_issuers.insert(trusted_issuers.end(), new_issuers.begin(), new_issuers.end());
727 std::vector<std::shared_ptr<const X509_Certificate>> misc_issuers =
731 if(trusted_issuers.size() + misc_issuers.size() == 0)
738 path_so_far.push_back(last);
739 certs_seen.emplace(fprint);
742 stack.push_back({std::shared_ptr<const X509_Certificate>(
nullptr),
false});
744 for(
const auto& trusted_cert : trusted_issuers)
746 stack.push_back({trusted_cert,
true});
749 for(
const auto& misc : misc_issuers)
751 stack.push_back({misc,
false});
757 if(cert_paths_out.empty())
760 throw Internal_Error(
"X509 path building failed for unknown reasons");
775 bool require_rev_on_end_entity,
776 bool require_rev_on_intermediates)
778 if(chain_status.empty())
779 throw Invalid_Argument(
"PKIX::merge_revocation_status chain_status was empty");
781 for(
size_t i = 0; i != chain_status.size() - 1; ++i)
783 bool had_crl =
false, had_ocsp =
false;
785 if(i < crl.size() && crl[i].size() > 0)
787 for(
auto&& code : crl[i])
793 chain_status[i].insert(code);
797 if(i < ocsp.size() && ocsp[i].size() > 0)
799 for(
auto&& code : ocsp[i])
808 chain_status[i].insert(code);
812 if(had_crl ==
false && had_ocsp ==
false)
814 if((require_rev_on_end_entity && i == 0) ||
815 (require_rev_on_intermediates && i > 0))
825 if(cert_status.empty())
831 for(
const std::set<Certificate_Status_Code>& s : cert_status)
835 auto worst = *s.rbegin();
839 overall_status = worst;
847 const std::vector<X509_Certificate>& end_certs,
849 const std::vector<Certificate_Store*>& trusted_roots,
850 const std::string& hostname,
852 std::chrono::system_clock::time_point ref_time,
853 std::chrono::milliseconds ocsp_timeout,
854 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp)
856 if(end_certs.empty())
861 std::shared_ptr<const X509_Certificate> end_entity(std::make_shared<const X509_Certificate>(end_certs[0]));
862 std::vector<std::shared_ptr<const X509_Certificate>> end_entity_extra;
863 for(
size_t i = 1; i < end_certs.size(); ++i)
865 end_entity_extra.push_back(std::make_shared<const X509_Certificate>(end_certs[i]));
868 std::vector<std::vector<std::shared_ptr<const X509_Certificate>>> cert_paths;
877 std::vector<Path_Validation_Result> error_results;
879 for(
auto cert_path : cert_paths)
892 if(ocsp_resp.size() > 0)
897 if(ocsp_status.empty() && ocsp_timeout != std::chrono::milliseconds(0))
899 #if defined(BOTAN_TARGET_OS_HAS_THREADS) && defined(BOTAN_HAS_HTTP_UTIL)
900 ocsp_status = PKIX::check_ocsp_online(cert_path, trusted_roots, ref_time,
903 ocsp_status.resize(1);
913 if(pvd.successful_validation())
919 error_results.push_back(std::move(pvd));
922 return error_results[0];
928 const std::vector<Certificate_Store*>& trusted_roots,
929 const std::string& hostname,
931 std::chrono::system_clock::time_point when,
932 std::chrono::milliseconds ocsp_timeout,
933 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp)
935 std::vector<X509_Certificate> certs;
936 certs.push_back(end_cert);
937 return x509_path_validate(certs, restrictions, trusted_roots, hostname, usage, when, ocsp_timeout, ocsp_resp);
941 const std::vector<X509_Certificate>& end_certs,
944 const std::string& hostname,
946 std::chrono::system_clock::time_point when,
947 std::chrono::milliseconds ocsp_timeout,
948 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp)
950 std::vector<Certificate_Store*> trusted_roots;
951 trusted_roots.push_back(const_cast<Certificate_Store*>(&store));
953 return x509_path_validate(end_certs, restrictions, trusted_roots, hostname, usage, when, ocsp_timeout, ocsp_resp);
960 const std::string& hostname,
962 std::chrono::system_clock::time_point when,
963 std::chrono::milliseconds ocsp_timeout,
964 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp)
966 std::vector<X509_Certificate> certs;
967 certs.push_back(end_cert);
969 std::vector<Certificate_Store*> trusted_roots;
970 trusted_roots.push_back(const_cast<Certificate_Store*>(&store));
972 return x509_path_validate(certs, restrictions, trusted_roots, hostname, usage, when, ocsp_timeout, ocsp_resp);
977 bool ocsp_intermediates,
978 std::chrono::seconds max_ocsp_age) :
979 m_require_revocation_information(require_rev),
980 m_ocsp_all_intermediates(ocsp_intermediates),
981 m_minimum_key_strength(key_strength),
982 m_max_ocsp_age(max_ocsp_age)
984 if(key_strength <= 80)
985 { m_trusted_hashes.insert(
"SHA-160"); }
987 m_trusted_hashes.insert(
"SHA-224");
988 m_trusted_hashes.insert(
"SHA-256");
989 m_trusted_hashes.insert(
"SHA-384");
990 m_trusted_hashes.insert(
"SHA-512");
997 for(
const auto& status_set_i : all_statuses)
999 std::set<Certificate_Status_Code> warning_set_i;
1000 for(
const auto& code : status_set_i)
1005 warning_set_i.insert(code);
1008 warnings.push_back(warning_set_i);
1015 std::vector<std::shared_ptr<const X509_Certificate>>&& cert_chain) :
1016 m_all_status(status),
1017 m_warnings(find_warnings(m_all_status)),
1018 m_cert_path(cert_chain),
1025 if(m_cert_path.empty())
1026 throw Invalid_State(
"Path_Validation_Result::trust_root no path set");
1028 throw Invalid_State(
"Path_Validation_Result::trust_root meaningless with invalid status");
1030 return *m_cert_path[m_cert_path.size()-1];
1035 std::set<std::string> hashes;
1036 for(
size_t i = 0; i != m_cert_path.size(); ++i)
1037 hashes.insert(m_cert_path[i]->hash_used_for_signature());
1050 for(
auto status_set_i : m_warnings)
1051 if(!status_set_i.empty())
1071 return "Unknown error";
1076 const std::string sep(
", ");
1078 for(
size_t i = 0; i < m_warnings.size(); i++)
1080 for(
auto code : m_warnings[i])
1084 if(res.size() >= sep.size())
1085 res = res.substr(0, res.size() - sep.size());
CertificatePathStatusCodes check_chain(const std::vector< std::shared_ptr< const X509_Certificate >> &cert_path, std::chrono::system_clock::time_point ref_time, const std::string &hostname, Usage_Type usage, size_t min_signature_algo_strength, const std::set< std::string > &trusted_hashes)
Path_Validation_Result(CertificatePathStatusCodes status, std::vector< std::shared_ptr< const X509_Certificate >> &&cert_chain)
std::vector< std::shared_ptr< const X509_Certificate > > find_all_certs(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
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)
const std::vector< uint8_t > & authority_key_id() const
Certificate_Status_Code overall_status(const CertificatePathStatusCodes &cert_status)
std::shared_ptr< const X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
std::string to_string(const BER_Object &obj)
std::vector< std::pair< std::unique_ptr< Certificate_Extension >, bool > > extensions() const
void merge_revocation_status(CertificatePathStatusCodes &chain_status, const CertificatePathStatusCodes &crl_status, const CertificatePathStatusCodes &ocsp_status, bool require_rev_on_end_entity, bool require_rev_on_intermediates)
std::string to_string(ErrorType type)
Convert an ErrorType to string.
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)
bool require_revocation_information() const
#define BOTAN_ASSERT_NONNULL(ptr)
const X509_DN & issuer_dn() const
Certificate_Status_Code result() const
const std::vector< OID > & get_extension_oids() 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())
CertificatePathStatusCodes check_ocsp(const std::vector< std::shared_ptr< const X509_Certificate >> &cert_path, const std::vector< std::shared_ptr< const OCSP::Response >> &ocsp_responses, const std::vector< Certificate_Store * > &certstores, std::chrono::system_clock::time_point ref_time, std::chrono::seconds max_ocsp_age=std::chrono::seconds::zero())
CertificatePathStatusCodes check_crl(const std::vector< std::shared_ptr< const X509_Certificate >> &cert_path, const std::vector< std::shared_ptr< const X509_CRL >> &crls, std::chrono::system_clock::time_point ref_time)
bool ocsp_all_intermediates() const
void add_certificate(const X509_Certificate &cert)
Certificate_Status_Code build_all_certificate_paths(std::vector< std::vector< std::shared_ptr< const X509_Certificate >>> &cert_paths, const std::vector< Certificate_Store * > &trusted_certstores, const std::shared_ptr< const X509_Certificate > &end_entity, const std::vector< std::shared_ptr< const X509_Certificate >> &end_entity_extra)
std::set< std::string > trusted_hashes() const
std::string result_string() const
const std::set< std::string > & trusted_hashes() const
Certificate_Status_Code build_certificate_path(std::vector< std::shared_ptr< const X509_Certificate >> &cert_path_out, const std::vector< Certificate_Store * > &trusted_certstores, const std::shared_ptr< const X509_Certificate > &end_entity, const std::vector< std::shared_ptr< const X509_Certificate >> &end_entity_extra)
CertificatePathStatusCodes warnings() const
std::chrono::seconds max_ocsp_age() const
static BigInt decode(const uint8_t buf[], size_t length)
static const char * status_string(Certificate_Status_Code code)
std::vector< std::set< Certificate_Status_Code > > CertificatePathStatusCodes
static size_t lookup_ub(const OID &oid)
bool successful_validation() const
size_t minimum_key_strength() const
const X509_Certificate & trust_root() const
std::string warnings_string() const