Botan 3.10.0
Crypto and TLS for C&
Botan::Flatfile_Certificate_Store Class Referencefinal

#include <certstor_flatfile.h>

Inheritance diagram for Botan::Flatfile_Certificate_Store:
Botan::Certificate_Store

Public Member Functions

std::vector< X509_DNall_subjects () const override
bool certificate_known (const X509_Certificate &cert) const
std::vector< X509_Certificatefind_all_certs (const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
virtual std::optional< X509_Certificatefind_cert (const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const
std::optional< X509_Certificatefind_cert_by_issuer_dn_and_serial_number (const X509_DN &issuer_dn, std::span< const uint8_t > serial_number) const override
std::optional< X509_Certificatefind_cert_by_pubkey_sha1 (const std::vector< uint8_t > &key_hash) const override
std::optional< X509_Certificatefind_cert_by_raw_subject_dn_sha256 (const std::vector< uint8_t > &subject_hash) const override
std::optional< X509_CRLfind_crl_for (const X509_Certificate &subject) const override
 Flatfile_Certificate_Store (const Flatfile_Certificate_Store &)=default
 Flatfile_Certificate_Store (Flatfile_Certificate_Store &&)=default
BOTAN_FUTURE_EXPLICIT Flatfile_Certificate_Store (std::string_view file, bool ignore_non_ca=false)
Flatfile_Certificate_Storeoperator= (const Flatfile_Certificate_Store &)=default
Flatfile_Certificate_Storeoperator= (Flatfile_Certificate_Store &&)=default
 ~Flatfile_Certificate_Store () override=default

Detailed Description

Certificate Store that is backed by a file of PEMs of trusted CAs.

Definition at line 23 of file certstor_flatfile.h.

Constructor & Destructor Documentation

◆ Flatfile_Certificate_Store() [1/3]

Botan::Flatfile_Certificate_Store::Flatfile_Certificate_Store ( std::string_view file,
bool ignore_non_ca = false )

Construct a new Certificate_Store given a file path to a file including PEMs of trusted self-signed CAs.

Parameters
filethe name of the file to read certificates from
ignore_non_caif true, certs that are not self-signed CA certs will be ignored. Otherwise (if false), an exception will be thrown instead.

Definition at line 37 of file certstor_flatfile.cpp.

37 {
38 if(file.empty()) {
39 throw Invalid_Argument("Flatfile_Certificate_Store::Flatfile_Certificate_Store invalid file path");
40 }
41
42 DataSource_Stream file_stream(file);
43
44 for(const std::vector<uint8_t>& der : decode_all_certificates(file_stream)) {
45 X509_Certificate cert(der);
46
47 /*
48 * Various weird or misconfigured system roots include intermediate certificates,
49 * or even stranger certificates which are not valid for cert issuance at all.
50 * Previously this code would error on such cases as an obvious misconfiguration,
51 * but we cannot fix the trust store. So instead just ignore any such certificate.
52 */
53 if(cert.is_self_signed() && cert.is_CA_cert()) {
54 m_all_subjects.push_back(cert.subject_dn());
55 m_dn_to_cert[cert.subject_dn()].push_back(cert);
56 m_pubkey_sha1_to_cert.emplace(cert.subject_public_key_bitstring_sha1(), cert);
57 m_subject_dn_sha256_to_cert.emplace(cert.raw_subject_dn_sha256(), cert);
58 m_issuer_dn_to_cert[cert.issuer_dn()].push_back(cert);
59 } else if(!ignore_non_ca) {
60 throw Invalid_Argument("Flatfile_Certificate_Store received non CA cert " + cert.subject_dn().to_string());
61 }
62 }
63
64 if(m_all_subjects.empty()) {
65 throw Invalid_Argument("Flatfile_Certificate_Store::Flatfile_Certificate_Store cert file is empty");
66 }
67}

References Botan::X509_Certificate::is_CA_cert(), Botan::X509_Certificate::is_self_signed(), Botan::X509_Certificate::issuer_dn(), Botan::X509_Certificate::raw_subject_dn_sha256(), Botan::X509_Certificate::subject_dn(), Botan::X509_Certificate::subject_public_key_bitstring_sha1(), and Botan::X509_DN::to_string().

Referenced by Flatfile_Certificate_Store(), Flatfile_Certificate_Store(), operator=(), and operator=().

◆ Flatfile_Certificate_Store() [2/3]

Botan::Flatfile_Certificate_Store::Flatfile_Certificate_Store ( const Flatfile_Certificate_Store & )
default

◆ Flatfile_Certificate_Store() [3/3]

Botan::Flatfile_Certificate_Store::Flatfile_Certificate_Store ( Flatfile_Certificate_Store && )
default

◆ ~Flatfile_Certificate_Store()

Botan::Flatfile_Certificate_Store::~Flatfile_Certificate_Store ( )
overridedefault

Member Function Documentation

◆ all_subjects()

std::vector< X509_DN > Botan::Flatfile_Certificate_Store::all_subjects ( ) const
overridevirtual
Returns
DNs for all certificates managed by the store

Implements Botan::Certificate_Store.

Definition at line 69 of file certstor_flatfile.cpp.

69 {
70 return m_all_subjects;
71}

Referenced by ~Flatfile_Certificate_Store().

◆ certificate_known()

bool Botan::Certificate_Store::certificate_known ( const X509_Certificate & cert) const
inlineinherited
Returns
whether the certificate is known
Parameters
certcertififcate to be searched

Definition at line 80 of file certstor.h.

80 {
81 return find_cert(cert.subject_dn(), cert.subject_key_id()).has_value();
82 }
virtual std::optional< X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const
Definition certstor.cpp:21

References find_cert(), Botan::X509_Certificate::subject_dn(), and Botan::X509_Certificate::subject_key_id().

◆ find_all_certs()

std::vector< X509_Certificate > Botan::Flatfile_Certificate_Store::find_all_certs ( const X509_DN & subject_dn,
const std::vector< uint8_t > & key_id ) const
overridevirtual

Find all certificates with a given Subject DN. Subject DN and even the key identifier might not be unique.

Implements Botan::Certificate_Store.

Definition at line 73 of file certstor_flatfile.cpp.

74 {
75 if(!m_dn_to_cert.contains(subject_dn)) {
76 return {};
77 }
78
79 const auto& certs = m_dn_to_cert.at(subject_dn);
80
81 std::vector<X509_Certificate> found_certs;
82 for(const auto& cert : certs) {
83 if(key_id.empty() || key_id == cert.subject_key_id()) {
84 found_certs.push_back(cert);
85 }
86 }
87
88 return found_certs;
89}

Referenced by ~Flatfile_Certificate_Store().

◆ find_cert()

std::optional< X509_Certificate > Botan::Certificate_Store::find_cert ( const X509_DN & subject_dn,
const std::vector< uint8_t > & key_id ) const
virtualinherited

Find a certificate by Subject DN and (optionally) key identifier

Parameters
subject_dnthe subject's distinguished name
key_idan optional key id
Returns
a matching certificate or nullopt otherwise If more than one certificate in the certificate store matches, then a single value is selected arbitrarily.

Reimplemented in Botan::Certificate_Store_In_Memory, Botan::Certificate_Store_In_SQL, Botan::Certificate_Store_MacOS, Botan::Certificate_Store_Windows, and Botan::System_Certificate_Store.

Definition at line 21 of file certstor.cpp.

22 {
23 const auto certs = find_all_certs(subject_dn, key_id);
24
25 if(certs.empty()) {
26 return std::nullopt;
27 }
28
29 // `count` might be greater than 1, but we'll just select the first match
30 return certs.front();
31}
virtual std::vector< X509_Certificate > find_all_certs(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const =0

References find_all_certs().

Referenced by certificate_known(), Botan::OCSP::Response::find_signing_certificate(), and ~Certificate_Store().

◆ find_cert_by_issuer_dn_and_serial_number()

std::optional< X509_Certificate > Botan::Flatfile_Certificate_Store::find_cert_by_issuer_dn_and_serial_number ( const X509_DN & issuer_dn,
std::span< const uint8_t > serial_number ) const
overridevirtual

Find a certificate by searching for one with a matching issuer DN and serial number. Used for CMS or PKCS#7.

Parameters
issuer_dnthe distinguished name of the issuer
serial_numberthe certificate's serial number
Returns
a matching certificate or nullopt otherwise

Implements Botan::Certificate_Store.

Definition at line 121 of file certstor_flatfile.cpp.

122 {
123 if(!m_issuer_dn_to_cert.contains(issuer_dn)) {
124 return std::nullopt;
125 }
126
127 const auto& certs = m_issuer_dn_to_cert.at(issuer_dn);
128
129 for(const auto& cert : certs) {
130 if(std::ranges::equal(cert.serial_number(), serial_number)) {
131 return cert;
132 }
133 }
134
135 return std::nullopt;
136}

Referenced by ~Flatfile_Certificate_Store().

◆ find_cert_by_pubkey_sha1()

std::optional< X509_Certificate > Botan::Flatfile_Certificate_Store::find_cert_by_pubkey_sha1 ( const std::vector< uint8_t > & key_hash) const
overridevirtual

Find a certificate by searching for one with a matching SHA-1 hash of public key.

Returns
a matching certificate or nullptr otherwise

Implements Botan::Certificate_Store.

Definition at line 91 of file certstor_flatfile.cpp.

92 {
93 if(key_hash.size() != 20) {
94 throw Invalid_Argument("Flatfile_Certificate_Store::find_cert_by_pubkey_sha1 invalid hash");
95 }
96
97 auto found_cert = m_pubkey_sha1_to_cert.find(key_hash);
98
99 if(found_cert != m_pubkey_sha1_to_cert.end()) {
100 return found_cert->second;
101 }
102
103 return std::nullopt;
104}

Referenced by ~Flatfile_Certificate_Store().

◆ find_cert_by_raw_subject_dn_sha256()

std::optional< X509_Certificate > Botan::Flatfile_Certificate_Store::find_cert_by_raw_subject_dn_sha256 ( const std::vector< uint8_t > & subject_hash) const
overridevirtual

Find a certificate by searching for one with a matching SHA-256 hash of raw subject name. Used for OCSP.

Parameters
subject_hashSHA-256 hash of the subject's raw name
Returns
a matching certificate or nullopt otherwise

Implements Botan::Certificate_Store.

Definition at line 106 of file certstor_flatfile.cpp.

107 {
108 if(subject_hash.size() != 32) {
109 throw Invalid_Argument("Flatfile_Certificate_Store::find_cert_by_raw_subject_dn_sha256 invalid hash");
110 }
111
112 auto found_cert = m_subject_dn_sha256_to_cert.find(subject_hash);
113
114 if(found_cert != m_subject_dn_sha256_to_cert.end()) {
115 return found_cert->second;
116 }
117
118 return std::nullopt;
119}

Referenced by ~Flatfile_Certificate_Store().

◆ find_crl_for()

std::optional< X509_CRL > Botan::Flatfile_Certificate_Store::find_crl_for ( const X509_Certificate & subject) const
overridevirtual

Fetching CRLs is not supported by this certificate store. This will always return an empty list.

Reimplemented from Botan::Certificate_Store.

Definition at line 138 of file certstor_flatfile.cpp.

138 {
139 BOTAN_UNUSED(subject);
140 return {};
141}
#define BOTAN_UNUSED
Definition assert.h:144

References BOTAN_UNUSED.

Referenced by ~Flatfile_Certificate_Store().

◆ operator=() [1/2]

Flatfile_Certificate_Store & Botan::Flatfile_Certificate_Store::operator= ( const Flatfile_Certificate_Store & )
default

◆ operator=() [2/2]

Flatfile_Certificate_Store & Botan::Flatfile_Certificate_Store::operator= ( Flatfile_Certificate_Store && )
default

The documentation for this class was generated from the following files: