Botan 2.1.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 Reference

`#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::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 74 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 149 of file certstor.cpp.

References add_certificate().

150 {
151 add_certificate(cert);
152 }
Botan::Certificate_Store_In_Memory::add_certificate
void add_certificate(const X509_Certificate &cert)
Definition: certstor.cpp:19

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

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

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

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

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

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

103 {
104 std::shared_ptr<const X509_CRL> crl_s = std::make_shared<const X509_CRL>(crl);
105 return add_crl(crl_s);
106 }
Botan::Certificate_Store_In_Memory::add_crl
void add_crl(const X509_CRL &crl)
Definition: certstor.cpp:102

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

109 {
110 X509_DN crl_issuer = crl->issuer_dn();
111
112 for(auto& c : m_crls)
113 {
114 // Found an update of a previously existing one; replace it
115 if(c->issuer_dn() == crl_issuer)
116 {
117 if(c->this_update() <= crl->this_update())
118 c = crl;
119 return;
120 }
121 }
122
123 // Totally new CRL, add to the list
124 m_crls.push_back(crl);
125 }

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

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

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

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

63 {
64 return find_cert(cert.subject_dn(), cert.subject_key_id()) != nullptr;
65 }
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::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 46 of file certstor.cpp.

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

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

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

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

70 {
71 if(key_hash.size() != 20)
72 throw Invalid_Argument("Certificate_Store_In_Memory::find_cert_by_pubkey_sha1 invalid hash");
73
74 std::unique_ptr<HashFunction> hash(HashFunction::create("SHA-1"));
75
76 for(const auto& cert : m_certs){
77 hash->update(cert->subject_public_key_bitstring());
78 if(key_hash == hash->final_stdvec()) //final_stdvec also clears the hash to initial state
79 return cert;
80 }
81
82 return nullptr;
83 }
Botan::HashFunction::create
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:93
hash
MechanismType hash
Definition: p11_mechanism.cpp:63

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

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

87 {
88 if(subject_hash.size() != 32)
89 throw Invalid_Argument("Certificate_Store_In_Memory::find_cert_by_raw_subject_dn_sha256 invalid hash");
90
91 std::unique_ptr<HashFunction> hash(HashFunction::create("SHA-256"));
92
93 for(const auto& cert : m_certs){
94 hash->update(cert->raw_subject_dn());
95 if(subject_hash == hash->final_stdvec()) //final_stdvec also clears the hash to initial state
96 return cert;
97 }
98
99 return nullptr;
100 }
Botan::HashFunction::create
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:93
hash
MechanismType hash
Definition: p11_mechanism.cpp:63

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

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

128 {
129 const std::vector<uint8_t>& key_id = subject.authority_key_id();
130
131 for(const auto& c : m_crls)
132 {
133 // Only compare key ids if set in both call and in the CRL
134 if(key_id.size())
135 {
136 std::vector<uint8_t> akid = c->authority_key_id();
137
138 if(akid.size() && akid != key_id) // no match
139 continue;
140 }
141
142 if(c->issuer_dn() == subject.issuer_dn())
143 return c;
144 }
145
146 return {};
147 }

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

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