Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | List of all members
Botan::Certificate_Store_In_Memory Class Referencefinal

`#include <certstor.h>`

Inheritance diagram for Botan::Certificate_Store_In_Memory:

## Public Member Functions
void | add_certificate (const X509_Certificate &cert)
void | add_certificate (std::shared_ptr< const X509_Certificate > cert)
void | add_crl (const X509_CRL &crl)
void | add_crl (std::shared_ptr< const X509_CRL > crl)
std::vector< X509_DN > | all_subjects () const override
bool | certificate_known (const X509_Certificate &cert) const
| Certificate_Store_In_Memory (const std::string &dir)
| Certificate_Store_In_Memory (const X509_Certificate &cert)
| Certificate_Store_In_Memory ()=default
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
std::shared_ptr< const X509_Certificate > | find_cert (const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
std::shared_ptr< const X509_Certificate > | find_cert_by_pubkey_sha1 (const std::vector< uint8_t > &key_hash) const override
std::shared_ptr< const X509_Certificate > | find_cert_by_raw_subject_dn_sha256 (const std::vector< uint8_t > &subject_hash) const override
std::shared_ptr< const X509_CRL > | find_crl_for (const X509_Certificate &subject) const override

## Detailed Description

In Memory Certificate Store

Definition at line 82 of file certstor.h.

## Constructor & Destructor Documentation

Botan::Certificate_Store_In_Memory::Certificate_Store_In_Memory | ( | const std::string & | dir | ) |
| explicit

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

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

Adds given certificate to the store.

Definition at line 173 of file certstor.cpp.

References add_certificate().

174 {
175 add_certificate(cert);
176 }
Botan::Certificate_Store_In_Memory::add_certificate
void add_certificate(const X509_Certificate &cert)
Definition: certstor.cpp:21

Botan::Certificate_Store_In_Memory::Certificate_Store_In_Memory | ( |  | ) |
| default

Create an empty store.

## Member Function Documentation

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

Add a certificate to the store.

Parameters

  cert | certificate to be added

Definition at line 21 of file certstor.cpp.

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

22 {
23 for(const auto& c : m_certs)
24 if(*c == cert)
25 return;
26
27 m_certs.push_back(std::make_shared<const X509_Certificate>(cert));
28 }

void Botan::Certificate_Store_In_Memory::add_certificate | ( | std::shared_ptr< const X509_Certificate > | cert | ) |

Add a certificate already in a shared_ptr to the store.

Parameters

  cert | certificate to be added

Definition at line 30 of file certstor.cpp.

31 {
32 for(const auto& c : m_certs)
33 if(*c == *cert)
34 return;
35
36 m_certs.push_back(cert);
37 }

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

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

Parameters

  crl | CRL to be added

Definition at line 126 of file certstor.cpp.

127 {
128 std::shared_ptr<const X509_CRL> crl_s = std::make_shared<const X509_CRL>(crl);
129 return add_crl(crl_s);
130 }
Botan::Certificate_Store_In_Memory::add_crl
void add_crl(const X509_CRL &crl)
Definition: certstor.cpp:126

void Botan::Certificate_Store_In_Memory::add_crl | ( | std::shared_ptr< const X509_CRL > | crl | ) |

Add a certificate revocation list (CRL) to the store as a shared_ptr

Parameters

  crl | CRL to be added

Definition at line 132 of file certstor.cpp.

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

std::vector< X509_DN > Botan::Certificate_Store_In_Memory::all_subjects | ( |  | ) | const
| override virtual

Returns
  DNs for all certificates managed by the store

Implements Botan::Certificate_Store.

Definition at line 39 of file certstor.cpp.

40 {
41 std::vector<X509_DN> subjects;
42 for(const auto& cert : m_certs)
43 subjects.push_back(cert->subject_dn());
44 return subjects;
45 }

bool Botan::Certificate_Store::certificate_known | ( | const X509_Certificate & | cert | ) | const
| inline inherited

Returns
  whether the certificate is known

Parameters

  cert | certififcate to be searched

Definition at line 70 of file certstor.h.

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

71 {
72 return find_cert(cert.subject_dn(), cert.subject_key_id()) != nullptr;
73 }
Botan::Certificate_Store::find_cert
virtual std::shared_ptr< const X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const =0

std::vector< std::shared_ptr< const X509_Certificate > > Botan::Certificate_Store_In_Memory::find_all_certs | ( | const X509_DN & | subject_dn,
|  | const std::vector< uint8_t > & | key_id
| ) |  | const
| override virtual

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

References Botan::PEM_Code::matches().

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

72 {
73 std::vector<std::shared_ptr<const X509_Certificate>> matches;
74
75 for(const auto& cert : m_certs)
76 {
77 if(key_id.size())
78 {
79 std::vector<uint8_t> skid = cert->subject_key_id();
80
81 if(skid.size() && skid != key_id) // no match
82 continue;
83 }
84
85 if(cert->subject_dn() == subject_dn)
86 matches.push_back(cert);
87 }
88
89 return matches;
90 }
Botan::PEM_Code::matches
bool matches(DataSource &source, const std::string &extra, size_t search_range)
Definition: pem.cpp:142

std::shared_ptr< const X509_Certificate > Botan::Certificate_Store_In_Memory::find_cert | ( | const X509_DN & | subject_dn,
|  | const std::vector< uint8_t > & | key_id
| ) |  | const
| override virtual

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

Parameters

  subject_dn | the subject's distinguished name
  key_id | an optional key id

Returns
  a matching certificate or nullptr otherwise

Implements Botan::Certificate_Store.

Definition at line 48 of file certstor.cpp.

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

50 {
51 for(const auto& cert : m_certs)
52 {
53 // Only compare key ids if set in both call and in the cert
54 if(key_id.size())
55 {
56 std::vector<uint8_t> skid = cert->subject_key_id();
57
58 if(skid.size() && skid != key_id) // no match
59 continue;
60 }
61
62 if(cert->subject_dn() == subject_dn)
63 return cert;
64 }
65
66 return nullptr;
67 }

std::shared_ptr< const X509_Certificate > Botan::Certificate_Store_In_Memory::find_cert_by_pubkey_sha1 | ( | const std::vector< uint8_t > & | key_hash | ) | const
| override virtual

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

Parameters

  key_hash | SHA-1 hash of the subject's public key

Returns
  a matching certificate or nullptr otherwise

Implements Botan::Certificate_Store.

Definition at line 93 of file certstor.cpp.

References Botan::HashFunction::create(), and hash.

94 {
95 if(key_hash.size() != 20)
96 throw Invalid_Argument("Certificate_Store_In_Memory::find_cert_by_pubkey_sha1 invalid hash");
97
98 std::unique_ptr<HashFunction> hash(HashFunction::create("SHA-1"));
99
100 for(const auto& cert : m_certs){
101 hash->update(cert->subject_public_key_bitstring());
102 if(key_hash == hash->final_stdvec()) //final_stdvec also clears the hash to initial state
103 return cert;
104 }
105
106 return nullptr;
107 }
Botan::HashFunction::create
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:106
hash
MechanismType hash
Definition: p11_mechanism.cpp:64

std::shared_ptr< const X509_Certificate > Botan::Certificate_Store_In_Memory::find_cert_by_raw_subject_dn_sha256 | ( | const std::vector< uint8_t > & | subject_hash | ) | const
| override virtual

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

Parameters

  subject_hash | SHA-256 hash of the subject's raw name

Returns
  a matching certificate or nullptr otherwise

Implements Botan::Certificate_Store.

Definition at line 110 of file certstor.cpp.

References Botan::HashFunction::create(), and hash.

111 {
112 if(subject_hash.size() != 32)
113 throw Invalid_Argument("Certificate_Store_In_Memory::find_cert_by_raw_subject_dn_sha256 invalid hash");
114
115 std::unique_ptr<HashFunction> hash(HashFunction::create("SHA-256"));
116
117 for(const auto& cert : m_certs){
118 hash->update(cert->raw_subject_dn());
119 if(subject_hash == hash->final_stdvec()) //final_stdvec also clears the hash to initial state
120 return cert;
121 }
122
123 return nullptr;
124 }
Botan::HashFunction::create
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:106
hash
MechanismType hash
Definition: p11_mechanism.cpp:64

std::shared_ptr< const X509_CRL > Botan::Certificate_Store_In_Memory::find_crl_for | ( | const X509_Certificate & | subject | ) | const
| override virtual

Finds a CRL for the given certificate

Reimplemented from Botan::Certificate_Store.

Definition at line 151 of file certstor.cpp.

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

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

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

- src/lib/x509/certstor.h
- src/lib/x509/certstor.cpp