Botan 3.9.0
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)
 
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_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 79 of file certstor.h.

Constructor & Destructor Documentation

◆ Certificate_Store_In_Memory() [1/2]

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

Adds given certificate to the store.

Definition at line 180 of file certstor.cpp.

180 {
181 add_certificate(cert);
182}
void add_certificate(const X509_Certificate &cert)
Definition certstor.cpp:46

References add_certificate().

◆ Certificate_Store_In_Memory() [2/2]

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 46 of file certstor.cpp.

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

Referenced by Botan::PKIX::build_all_certificate_paths(), Botan::PKIX::build_certificate_path(), 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 142 of file certstor.cpp.

142 {
143 const X509_DN& crl_issuer = crl.issuer_dn();
144
145 for(auto& c : m_crls) {
146 // Found an update of a previously existing one; replace it
147 if(c.issuer_dn() == crl_issuer) {
148 if(c.this_update() <= crl.this_update()) {
149 c = crl;
150 }
151 return;
152 }
153 }
154
155 // Totally new CRL, add to the list
156 m_crls.push_back(crl);
157}

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

Referenced by 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 56 of file certstor.cpp.

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

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 20 of file certstor.cpp.

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

◆ 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 85 of file certstor.cpp.

86 {
87 std::vector<X509_Certificate> matches;
88
89 for(const auto& cert : m_certs) {
90 if(!key_id.empty()) {
91 const std::vector<uint8_t>& skid = cert.subject_key_id();
92
93 if(!skid.empty() && skid != key_id) { // no match
94 continue;
95 }
96 }
97
98 if(cert.subject_dn() == subject_dn) {
99 matches.push_back(cert);
100 }
101 }
102
103 return matches;
104}
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 65 of file certstor.cpp.

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

Referenced by Botan::PKIX::build_certificate_path(), and 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 106 of file certstor.cpp.

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

References Botan::HashFunction::create().

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 124 of file certstor.cpp.

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

References Botan::HashFunction::create().

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 159 of file certstor.cpp.

159 {
160 const std::vector<uint8_t>& key_id = subject.authority_key_id();
161
162 for(const auto& c : m_crls) {
163 // Only compare key ids if set in both call and in the CRL
164 if(!key_id.empty()) {
165 const std::vector<uint8_t>& akid = c.authority_key_id();
166
167 if(!akid.empty() && akid != key_id) { // no match
168 continue;
169 }
170 }
171
172 if(c.issuer_dn() == subject.issuer_dn()) {
173 return c;
174 }
175 }
176
177 return {};
178}

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: