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)
151 Extensions extensions = subject->v3_extensions();
152 const auto& extensions_vec = extensions.
extensions();
153 if(subject->x509_version() < 3 && !extensions_vec.empty())
157 for(
auto& extension : extensions_vec)
159 extension.first->validate(*subject, *issuer, cert_path, cert_status, i);
168 size_t max_path_length = cert_path.size();
169 for(
size_t i = cert_path.size() - 1; i > 0 ; --i)
171 std::set<Certificate_Status_Code>& status = cert_status.at(i);
172 const std::shared_ptr<const X509_Certificate>& subject = cert_path[i];
178 if(subject->subject_dn() != subject->issuer_dn())
180 if(max_path_length > 0)
194 if(subject->path_limit() != Cert_Extension::NO_CERT_PATH_LIMIT && subject->path_limit() < max_path_length)
196 max_path_length = subject->path_limit();
205 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_responses,
206 const std::vector<Certificate_Store*>& trusted_certstores,
207 std::chrono::system_clock::time_point ref_time,
208 std::chrono::seconds max_ocsp_age)
210 if(cert_path.empty())
215 for(
size_t i = 0; i != cert_path.size() - 1; ++i)
217 std::set<Certificate_Status_Code>& status = cert_status.at(i);
219 std::shared_ptr<const X509_Certificate> subject = cert_path.at(i);
220 std::shared_ptr<const X509_Certificate> ca = cert_path.at(i+1);
222 if(i < ocsp_responses.size() && (ocsp_responses.at(i) !=
nullptr)
227 Certificate_Status_Code ocsp_signature_status = ocsp_responses.at(i)->check_signature(trusted_certstores, cert_path);
233 status.insert(ocsp_status);
238 status.insert(ocsp_signature_status);
248 while(cert_status.size() > 0 && cert_status.back().empty())
249 cert_status.pop_back();
255 PKIX::check_crl(
const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
256 const std::vector<std::shared_ptr<const X509_CRL>>& crls,
257 std::chrono::system_clock::time_point ref_time)
259 if(cert_path.empty())
263 const X509_Time validation_time(ref_time);
265 for(
size_t i = 0; i != cert_path.size() - 1; ++i)
267 std::set<Certificate_Status_Code>& status = cert_status.at(i);
269 if(i < crls.size() && crls.at(i))
271 std::shared_ptr<const X509_Certificate> subject = cert_path.at(i);
272 std::shared_ptr<const X509_Certificate> ca = cert_path.at(i+1);
277 if(validation_time < crls[i]->this_update())
280 if(validation_time > crls[i]->next_update())
283 if(crls[i]->check_signature(ca->subject_public_key()) ==
false)
288 if(crls[i]->is_revoked(*subject))
291 std::string dp = subject->crl_distribution_point();
294 if(dp != crls[i]->crl_issuing_distribution_point())
300 for(
const auto& extension : crls[i]->extensions().extensions())
318 while(cert_status.size() > 0 && cert_status.back().empty())
319 cert_status.pop_back();
325 PKIX::check_crl(
const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
326 const std::vector<Certificate_Store*>& certstores,
327 std::chrono::system_clock::time_point ref_time)
329 if(cert_path.empty())
332 if(certstores.empty())
335 std::vector<std::shared_ptr<const X509_CRL>> crls(cert_path.size());
337 for(
size_t i = 0; i != cert_path.size(); ++i)
340 for(
size_t c = 0; c != certstores.size(); ++c)
342 crls[i] = certstores[c]->find_crl_for(*cert_path[i]);
351 #if defined(BOTAN_HAS_ONLINE_REVOCATION_CHECKS)
354 PKIX::check_ocsp_online(
const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
355 const std::vector<Certificate_Store*>& trusted_certstores,
356 std::chrono::system_clock::time_point ref_time,
357 std::chrono::milliseconds timeout,
358 bool ocsp_check_intermediate_CAs,
359 std::chrono::seconds max_ocsp_age)
361 if(cert_path.empty())
364 std::vector<std::future<std::shared_ptr<const OCSP::Response>>> ocsp_response_futures;
368 if(ocsp_check_intermediate_CAs)
369 to_ocsp = cert_path.size() - 1;
370 if(cert_path.size() == 1)
373 for(
size_t i = 0; i < to_ocsp; ++i)
375 const std::shared_ptr<const X509_Certificate>& subject = cert_path.at(i);
376 const std::shared_ptr<const X509_Certificate>& issuer = cert_path.at(i+1);
378 if(subject->ocsp_responder() ==
"")
380 ocsp_response_futures.emplace_back(std::async(std::launch::deferred, [&]() -> std::shared_ptr<const OCSP::Response> {
386 ocsp_response_futures.emplace_back(std::async(std::launch::async, [&]() -> std::shared_ptr<const OCSP::Response> {
387 OCSP::Request req(*issuer,
BigInt::decode(subject->serial_number()));
393 "application/ocsp-request",
398 catch(std::exception&)
402 if (http.status_code() != 200)
406 return std::make_shared<const OCSP::Response>(http.body());
411 std::vector<std::shared_ptr<const OCSP::Response>> ocsp_responses;
413 for(
size_t i = 0; i < ocsp_response_futures.size(); ++i)
415 ocsp_responses.push_back(ocsp_response_futures[i].
get());
418 return PKIX::check_ocsp(cert_path, ocsp_responses, trusted_certstores, ref_time, max_ocsp_age);
422 PKIX::check_crl_online(
const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
423 const std::vector<Certificate_Store*>& certstores,
424 Certificate_Store_In_Memory* crl_store,
425 std::chrono::system_clock::time_point ref_time,
426 std::chrono::milliseconds timeout)
428 if(cert_path.empty())
429 throw Invalid_Argument(
"PKIX::check_crl_online cert_path empty");
430 if(certstores.empty())
431 throw Invalid_Argument(
"PKIX::check_crl_online certstores empty");
433 std::vector<std::future<std::shared_ptr<const X509_CRL>>> future_crls;
434 std::vector<std::shared_ptr<const X509_CRL>> crls(cert_path.size());
436 for(
size_t i = 0; i != cert_path.size(); ++i)
438 const std::shared_ptr<const X509_Certificate>& cert = cert_path.at(i);
439 for(
size_t c = 0; c != certstores.size(); ++c)
441 crls[i] = certstores[c]->find_crl_for(*cert);
455 future_crls.emplace_back(std::future<std::shared_ptr<const X509_CRL>>());
457 else if(cert->crl_distribution_point() ==
"")
460 future_crls.emplace_back(std::async(std::launch::deferred, [&]() -> std::shared_ptr<const X509_CRL> {
461 throw Not_Implemented(
"No CRL distribution point for this certificate");
466 future_crls.emplace_back(std::async(std::launch::async, [&]() -> std::shared_ptr<const X509_CRL> {
472 return std::make_shared<const X509_CRL>(http.body());
477 for(
size_t i = 0; i != future_crls.size(); ++i)
479 if(future_crls[i].valid())
483 crls[i] = future_crls[i].get();
485 catch(std::exception&)
497 for(
size_t i = 0; i != crl_status.size(); ++i)
503 crl_store->add_crl(crls[i]);
515 const std::vector<Certificate_Store*>& trusted_certstores,
516 const std::shared_ptr<const X509_Certificate>& end_entity,
517 const std::vector<std::shared_ptr<const X509_Certificate>>& end_entity_extra)
519 if(end_entity->is_self_signed())
530 std::set<std::string> certs_seen;
532 cert_path.push_back(end_entity);
533 certs_seen.insert(end_entity->fingerprint(
"SHA-256"));
536 for(
size_t i = 0; i != end_entity_extra.size(); ++i)
546 std::shared_ptr<const X509_Certificate> issuer;
547 bool trusted_issuer =
false;
551 issuer = store->find_cert(issuer_dn, auth_key_id);
554 trusted_issuer =
true;
562 issuer = ee_extras.
find_cert(issuer_dn, auth_key_id);
568 const std::string fprint = issuer->fingerprint(
"SHA-256");
570 if(certs_seen.count(fprint) > 0)
575 certs_seen.insert(fprint);
576 cert_path.push_back(issuer);
578 if(issuer->is_self_signed())
598 using cert_maybe_trusted = std::pair<std::shared_ptr<const X509_Certificate>,
bool>;
620 const std::vector<Certificate_Store*>& trusted_certstores,
621 const std::shared_ptr<const X509_Certificate>& end_entity,
622 const std::vector<std::shared_ptr<const X509_Certificate>>& end_entity_extra)
624 if(!cert_paths_out.empty())
626 throw Invalid_Argument(
"PKIX::build_all_certificate_paths: cert_paths_out must be empty");
629 if(end_entity->is_self_signed())
637 std::vector<Certificate_Status_Code> stats;
640 for(
size_t i = 0; i != end_entity_extra.size(); ++i)
651 std::set<std::string> certs_seen;
655 std::vector<std::shared_ptr<const X509_Certificate>> path_so_far;
658 std::vector<cert_maybe_trusted> stack = { {end_entity,
false} };
660 while(!stack.empty())
663 if(stack.back().first ==
nullptr)
666 std::string fprint = path_so_far.back()->fingerprint(
"SHA-256");
667 certs_seen.erase(fprint);
668 path_so_far.pop_back();
673 std::shared_ptr<const X509_Certificate> last = stack.back().first;
674 bool trusted = stack.back().second;
678 const std::string fprint = last->fingerprint(
"SHA-256");
679 if(certs_seen.count(fprint) == 1)
687 if(last->is_self_signed())
692 cert_paths_out.push_back(path_so_far);
693 cert_paths_out.back().push_back(last);
705 const X509_DN issuer_dn = last->issuer_dn();
706 const std::vector<uint8_t> auth_key_id = last->authority_key_id();
709 std::vector<std::shared_ptr<const X509_Certificate>> trusted_issuers;
712 auto new_issuers = store->find_all_certs(issuer_dn, auth_key_id);
713 trusted_issuers.insert(trusted_issuers.end(), new_issuers.begin(), new_issuers.end());
717 std::vector<std::shared_ptr<const X509_Certificate>> misc_issuers =
721 if(trusted_issuers.size() + misc_issuers.size() == 0)
728 path_so_far.push_back(last);
729 certs_seen.emplace(fprint);
732 stack.push_back({std::shared_ptr<const X509_Certificate>(
nullptr),
false});
734 for(
const auto trusted_cert : trusted_issuers)
736 stack.push_back({trusted_cert,
true});
739 for(
const auto misc : misc_issuers)
741 stack.push_back({misc,
false});
747 if(cert_paths_out.empty())
750 throw Internal_Error(
"X509 path building failed for unknown reasons");
765 bool require_rev_on_end_entity,
766 bool require_rev_on_intermediates)
768 if(chain_status.empty())
769 throw Invalid_Argument(
"PKIX::merge_revocation_status chain_status was empty");
771 for(
size_t i = 0; i != chain_status.size() - 1; ++i)
773 bool had_crl =
false, had_ocsp =
false;
775 if(i < crl.size() && crl[i].size() > 0)
777 for(
auto&& code : crl[i])
783 chain_status[i].insert(code);
787 if(i < ocsp.size() && ocsp[i].size() > 0)
789 for(
auto&& code : ocsp[i])
798 chain_status[i].insert(code);
802 if(had_crl ==
false && had_ocsp ==
false)
804 if((require_rev_on_end_entity && i == 0) ||
805 (require_rev_on_intermediates && i > 0))
815 if(cert_status.empty())
821 for(
const std::set<Certificate_Status_Code>& s : cert_status)
825 auto worst = *s.rbegin();
829 overall_status = worst;
837 const std::vector<X509_Certificate>& end_certs,
839 const std::vector<Certificate_Store*>& trusted_roots,
840 const std::string& hostname,
842 std::chrono::system_clock::time_point ref_time,
843 std::chrono::milliseconds ocsp_timeout,
844 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp)
846 if(end_certs.empty())
851 std::shared_ptr<const X509_Certificate> end_entity(std::make_shared<const X509_Certificate>(end_certs[0]));
852 std::vector<std::shared_ptr<const X509_Certificate>> end_entity_extra;
853 for(
size_t i = 1; i < end_certs.size(); ++i)
855 end_entity_extra.push_back(std::make_shared<const X509_Certificate>(end_certs[i]));
858 std::vector<std::vector<std::shared_ptr<const X509_Certificate>>> cert_paths;
867 std::vector<Path_Validation_Result> error_results;
869 for(
auto cert_path : cert_paths)
882 if(ocsp_resp.size() > 0)
887 if(ocsp_status.empty() && ocsp_timeout != std::chrono::milliseconds(0))
889 #if defined(BOTAN_TARGET_OS_HAS_THREADS) && defined(BOTAN_HAS_HTTP_UTIL)
890 ocsp_status = PKIX::check_ocsp_online(cert_path, trusted_roots, ref_time,
893 ocsp_status.resize(1);
903 if(pvd.successful_validation())
909 error_results.push_back(std::move(pvd));
912 return error_results[0];
918 const std::vector<Certificate_Store*>& trusted_roots,
919 const std::string& hostname,
921 std::chrono::system_clock::time_point when,
922 std::chrono::milliseconds ocsp_timeout,
923 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp)
925 std::vector<X509_Certificate> certs;
926 certs.push_back(end_cert);
927 return x509_path_validate(certs, restrictions, trusted_roots, hostname, usage, when, ocsp_timeout, ocsp_resp);
931 const std::vector<X509_Certificate>& end_certs,
934 const std::string& hostname,
936 std::chrono::system_clock::time_point when,
937 std::chrono::milliseconds ocsp_timeout,
938 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp)
940 std::vector<Certificate_Store*> trusted_roots;
941 trusted_roots.push_back(const_cast<Certificate_Store*>(&store));
943 return x509_path_validate(end_certs, restrictions, trusted_roots, hostname, usage, when, ocsp_timeout, ocsp_resp);
950 const std::string& hostname,
952 std::chrono::system_clock::time_point when,
953 std::chrono::milliseconds ocsp_timeout,
954 const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp)
956 std::vector<X509_Certificate> certs;
957 certs.push_back(end_cert);
959 std::vector<Certificate_Store*> trusted_roots;
960 trusted_roots.push_back(const_cast<Certificate_Store*>(&store));
962 return x509_path_validate(certs, restrictions, trusted_roots, hostname, usage, when, ocsp_timeout, ocsp_resp);
967 bool ocsp_intermediates,
968 std::chrono::seconds max_ocsp_age) :
969 m_require_revocation_information(require_rev),
970 m_ocsp_all_intermediates(ocsp_intermediates),
971 m_minimum_key_strength(key_strength),
972 m_max_ocsp_age(max_ocsp_age)
974 if(key_strength <= 80)
975 { m_trusted_hashes.insert(
"SHA-160"); }
977 m_trusted_hashes.insert(
"SHA-224");
978 m_trusted_hashes.insert(
"SHA-256");
979 m_trusted_hashes.insert(
"SHA-384");
980 m_trusted_hashes.insert(
"SHA-512");
987 for(
const auto& status_set_i : all_statuses)
989 std::set<Certificate_Status_Code> warning_set_i;
990 for(
const auto& code : status_set_i)
995 warning_set_i.insert(code);
998 warnings.push_back(warning_set_i);
1005 std::vector<std::shared_ptr<const X509_Certificate>>&& cert_chain) :
1006 m_all_status(status),
1007 m_warnings(find_warnings(m_all_status)),
1008 m_cert_path(cert_chain),
1015 if(m_cert_path.empty())
1016 throw Invalid_State(
"Path_Validation_Result::trust_root no path set");
1018 throw Invalid_State(
"Path_Validation_Result::trust_root meaningless with invalid status");
1020 return *m_cert_path[m_cert_path.size()-1];
1025 std::set<std::string> hashes;
1026 for(
size_t i = 0; i != m_cert_path.size(); ++i)
1027 hashes.insert(m_cert_path[i]->hash_used_for_signature());
1040 for(
auto status_set_i : m_warnings)
1041 if(!status_set_i.empty())
1061 return "Unknown error";
1066 const std::string sep(
", ");
1068 for(
size_t i = 0; i < m_warnings.size(); i++)
1070 for(
auto code : m_warnings[i])
1074 if(res.size() >= sep.size())
1075 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