Botan 3.11.1
Crypto and TLS for C&
Botan::Certificate_Store_In_Memory Class Referencefinal

#include <certstor.h>

Inheritance diagram for Botan::Certificate_Store_In_Memory:
Botan::Certificate_Store

Public Member Functions

void add_certificate (const X509_Certificate &cert)
void add_crl (const X509_CRL &crl)
std::vector< X509_DNall_subjects () const override
bool certificate_known (const X509_Certificate &cert) const
 Certificate_Store_In_Memory ()=default
 Certificate_Store_In_Memory (const X509_Certificate &cert)
 Certificate_Store_In_Memory (const X509_Certificate &cert, const X509_CRL &crl)
std::vector< X509_Certificatefind_all_certs (const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
std::optional< X509_Certificatefind_cert (const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
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

Detailed Description

In Memory Certificate Store

Definition at line 89 of file certstor.h.

Constructor & Destructor Documentation

◆ Certificate_Store_In_Memory() [1/3]

Botan::Certificate_Store_In_Memory::Certificate_Store_In_Memory ( const X509_Certificate & cert)
explicit

Adds given certificate to the store.

Definition at line 193 of file certstor.cpp.

193 {
194 add_certificate(cert);
195}
void add_certificate(const X509_Certificate &cert)
Definition certstor.cpp:48

References add_certificate().

◆ Certificate_Store_In_Memory() [2/3]

Botan::Certificate_Store_In_Memory::Certificate_Store_In_Memory ( const X509_Certificate & cert,
const X509_CRL & crl )

Adds given certificate and CRL to the store.

Definition at line 197 of file certstor.cpp.

197 {
198 add_certificate(cert);
199 add_crl(crl);
200}
void add_crl(const X509_CRL &crl)
Definition certstor.cpp:155

References add_certificate(), and add_crl().

◆ Certificate_Store_In_Memory() [3/3]

Botan::Certificate_Store_In_Memory::Certificate_Store_In_Memory ( )
default

Member Function Documentation

◆ add_certificate()

void Botan::Certificate_Store_In_Memory::add_certificate ( const X509_Certificate & cert)

Add a certificate to the store.

Parameters
certcertificate to be added

Definition at line 48 of file certstor.cpp.

48 {
49 for(const auto& c : m_certs) {
50 if(c == cert) {
51 return;
52 }
53 }
54
55 m_certs.push_back(cert);
56}

Referenced by Botan::PKIX::build_all_certificate_paths(), Certificate_Store_In_Memory(), Certificate_Store_In_Memory(), and Certificate_Store_In_Memory().

◆ add_crl()

void Botan::Certificate_Store_In_Memory::add_crl ( const X509_CRL & crl)

Add a certificate revocation list (CRL) to the store.

Parameters
crlCRL to be added

Definition at line 155 of file certstor.cpp.

155 {
156 const X509_DN& crl_issuer = crl.issuer_dn();
157
158 for(auto& c : m_crls) {
159 // Found an update of a previously existing one; replace it
160 if(c.issuer_dn() == crl_issuer) {
161 if(c.this_update() <= crl.this_update()) {
162 c = crl;
163 }
164 return;
165 }
166 }
167
168 // Totally new CRL, add to the list
169 m_crls.push_back(crl);
170}

References Botan::X509_CRL::issuer_dn(), and Botan::X509_CRL::this_update().

Referenced by Certificate_Store_In_Memory(), and Certificate_Store_In_Memory().

◆ all_subjects()

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

Implements Botan::Certificate_Store.

Definition at line 58 of file certstor.cpp.

58 {
59 std::vector<X509_DN> subjects;
60 subjects.reserve(m_certs.size());
61 for(const auto& cert : m_certs) {
62 subjects.push_back(cert.subject_dn());
63 }
64 return subjects;
65}

Referenced by Certificate_Store_In_Memory().

◆ certificate_known()

bool Botan::Certificate_Store::certificate_known ( const X509_Certificate & cert) const
inherited
Returns
whether this certificate is contained within the store
Parameters
certcertificate to be searched

Definition at line 22 of file certstor.cpp.

22 {
23 for(const auto& cert : find_all_certs(searching.subject_dn(), searching.subject_key_id())) {
24 if(cert == searching) {
25 return true;
26 }
27 }
28
29 return false;
30}
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(), Botan::X509_Certificate::subject_dn(), and Botan::X509_Certificate::subject_key_id().

Referenced by Botan::PKIX::build_all_certificate_paths(), and find_cert_by_issuer_dn_and_serial_number().

◆ find_all_certs()

std::vector< X509_Certificate > Botan::Certificate_Store_In_Memory::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 87 of file certstor.cpp.

88 {
89 std::vector<X509_Certificate> matches;
90
91 for(const auto& cert : m_certs) {
92 if(!key_id.empty()) {
93 const std::vector<uint8_t>& skid = cert.subject_key_id();
94
95 if(!skid.empty() && skid != key_id) { // no match
96 continue;
97 }
98 }
99
100 if(cert.subject_dn() == subject_dn) {
101 matches.push_back(cert);
102 }
103 }
104
105 return matches;
106}
bool matches(DataSource &source, std::string_view extra, size_t search_range)
Definition pem.cpp:140

Referenced by Botan::PKIX::build_all_certificate_paths(), and Certificate_Store_In_Memory().

◆ find_cert()

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

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 from Botan::Certificate_Store.

Definition at line 67 of file certstor.cpp.

68 {
69 for(const auto& cert : m_certs) {
70 // Only compare key ids if set in both call and in the cert
71 if(!key_id.empty()) {
72 const std::vector<uint8_t>& skid = cert.subject_key_id();
73
74 if(!skid.empty() && skid != key_id) { // no match
75 continue;
76 }
77 }
78
79 if(cert.subject_dn() == subject_dn) {
80 return cert;
81 }
82 }
83
84 return std::nullopt;
85}

Referenced by Certificate_Store_In_Memory().

◆ find_cert_by_issuer_dn_and_serial_number()

std::optional< X509_Certificate > Botan::Certificate_Store_In_Memory::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 144 of file certstor.cpp.

145 {
146 for(const auto& cert : m_certs) {
147 if(cert.issuer_dn() == issuer_dn && std::ranges::equal(cert.serial_number(), serial_number)) {
148 return cert;
149 }
150 }
151
152 return std::nullopt;
153}

Referenced by Certificate_Store_In_Memory().

◆ find_cert_by_pubkey_sha1()

std::optional< X509_Certificate > Botan::Certificate_Store_In_Memory::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. Used for OCSP.

Parameters
key_hashSHA-1 hash of the subject's public key
Returns
a matching certificate or nullopt otherwise

Implements Botan::Certificate_Store.

Definition at line 108 of file certstor.cpp.

109 {
110 if(key_hash.size() != 20) {
111 throw Invalid_Argument("Certificate_Store_In_Memory::find_cert_by_pubkey_sha1 invalid hash");
112 }
113
114 auto hash = HashFunction::create_or_throw("SHA-1");
115
116 for(const auto& cert : m_certs) {
117 hash->update(cert.subject_public_key_bitstring());
118 if(key_hash == hash->final_stdvec()) { //final_stdvec also clears the hash to initial state
119 return cert;
120 }
121 }
122
123 return std::nullopt;
124}
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:308

References Botan::HashFunction::create_or_throw().

Referenced by Certificate_Store_In_Memory().

◆ find_cert_by_raw_subject_dn_sha256()

std::optional< X509_Certificate > Botan::Certificate_Store_In_Memory::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 126 of file certstor.cpp.

127 {
128 if(subject_hash.size() != 32) {
129 throw Invalid_Argument("Certificate_Store_In_Memory::find_cert_by_raw_subject_dn_sha256 invalid hash");
130 }
131
132 auto hash = HashFunction::create_or_throw("SHA-256");
133
134 for(const auto& cert : m_certs) {
135 hash->update(cert.raw_subject_dn());
136 if(subject_hash == hash->final_stdvec()) { //final_stdvec also clears the hash to initial state
137 return cert;
138 }
139 }
140
141 return std::nullopt;
142}

References Botan::HashFunction::create_or_throw().

Referenced by Certificate_Store_In_Memory().

◆ find_crl_for()

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

Finds a CRL for the given certificate

Reimplemented from Botan::Certificate_Store.

Definition at line 172 of file certstor.cpp.

172 {
173 const std::vector<uint8_t>& key_id = subject.authority_key_id();
174
175 for(const auto& c : m_crls) {
176 // Only compare key ids if set in both call and in the CRL
177 if(!key_id.empty()) {
178 const std::vector<uint8_t>& akid = c.authority_key_id();
179
180 if(!akid.empty() && akid != key_id) { // no match
181 continue;
182 }
183 }
184
185 if(c.issuer_dn() == subject.issuer_dn()) {
186 return c;
187 }
188 }
189
190 return {};
191}

References Botan::X509_Certificate::authority_key_id(), and Botan::X509_Certificate::issuer_dn().

Referenced by Certificate_Store_In_Memory().


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