Botan 3.6.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 (std::string_view dir)
 
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 81 of file certstor.h.

Constructor & Destructor Documentation

◆ Certificate_Store_In_Memory() [1/3]

Botan::Certificate_Store_In_Memory::Certificate_Store_In_Memory ( std::string_view dir)
explicit

Attempt to parse all files in dir (including subdirectories) as certificates. Ignores errors.

◆ Certificate_Store_In_Memory() [2/3]

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

Adds given certificate to the store.

Definition at line 170 of file certstor.cpp.

170 {
171 add_certificate(cert);
172}
void add_certificate(const X509_Certificate &cert)
Definition certstor.cpp:36

References add_certificate().

◆ Certificate_Store_In_Memory() [3/3]

Botan::Certificate_Store_In_Memory::Certificate_Store_In_Memory ( )
default

Create an empty store.

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

36 {
37 for(const auto& c : m_certs) {
38 if(c == cert) {
39 return;
40 }
41 }
42
43 m_certs.push_back(cert);
44}

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

132 {
133 const X509_DN& crl_issuer = crl.issuer_dn();
134
135 for(auto& c : m_crls) {
136 // Found an update of a previously existing one; replace it
137 if(c.issuer_dn() == crl_issuer) {
138 if(c.this_update() <= crl.this_update()) {
139 c = crl;
140 }
141 return;
142 }
143 }
144
145 // Totally new CRL, add to the list
146 m_crls.push_back(crl);
147}

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

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

46 {
47 std::vector<X509_DN> subjects;
48 subjects.reserve(m_certs.size());
49 for(const auto& cert : m_certs) {
50 subjects.push_back(cert.subject_dn());
51 }
52 return subjects;
53}

◆ 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 70 of file certstor.h.

70 {
71 return find_cert(cert.subject_dn(), cert.subject_key_id()).has_value();
72 }
virtual std::optional< X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const
Definition certstor.cpp:20

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

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

76 {
77 std::vector<X509_Certificate> matches;
78
79 for(const auto& cert : m_certs) {
80 if(!key_id.empty()) {
81 const std::vector<uint8_t>& skid = cert.subject_key_id();
82
83 if(!skid.empty() && skid != key_id) { // no match
84 continue;
85 }
86 }
87
88 if(cert.subject_dn() == subject_dn) {
89 matches.push_back(cert);
90 }
91 }
92
93 return matches;
94}
bool matches(DataSource &source, std::string_view extra, size_t search_range)
Definition pem.cpp:137

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

56 {
57 for(const auto& cert : m_certs) {
58 // Only compare key ids if set in both call and in the cert
59 if(!key_id.empty()) {
60 const std::vector<uint8_t>& skid = cert.subject_key_id();
61
62 if(!skid.empty() && skid != key_id) { // no match
63 continue;
64 }
65 }
66
67 if(cert.subject_dn() == subject_dn) {
68 return cert;
69 }
70 }
71
72 return std::nullopt;
73}

Referenced by Botan::PKIX::build_certificate_path().

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

97 {
98 if(key_hash.size() != 20) {
99 throw Invalid_Argument("Certificate_Store_In_Memory::find_cert_by_pubkey_sha1 invalid hash");
100 }
101
102 auto hash = HashFunction::create("SHA-1");
103
104 for(const auto& cert : m_certs) {
105 hash->update(cert.subject_public_key_bitstring());
106 if(key_hash == hash->final_stdvec()) { //final_stdvec also clears the hash to initial state
107 return cert;
108 }
109 }
110
111 return std::nullopt;
112}
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:107

References Botan::HashFunction::create().

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

115 {
116 if(subject_hash.size() != 32) {
117 throw Invalid_Argument("Certificate_Store_In_Memory::find_cert_by_raw_subject_dn_sha256 invalid hash");
118 }
119
120 auto hash = HashFunction::create("SHA-256");
121
122 for(const auto& cert : m_certs) {
123 hash->update(cert.raw_subject_dn());
124 if(subject_hash == hash->final_stdvec()) { //final_stdvec also clears the hash to initial state
125 return cert;
126 }
127 }
128
129 return std::nullopt;
130}

References Botan::HashFunction::create().

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

149 {
150 const std::vector<uint8_t>& key_id = subject.authority_key_id();
151
152 for(const auto& c : m_crls) {
153 // Only compare key ids if set in both call and in the CRL
154 if(!key_id.empty()) {
155 const std::vector<uint8_t>& akid = c.authority_key_id();
156
157 if(!akid.empty() && akid != key_id) { // no match
158 continue;
159 }
160 }
161
162 if(c.issuer_dn() == subject.issuer_dn()) {
163 return c;
164 }
165 }
166
167 return {};
168}

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


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