Botan 3.9.0
Crypto and TLS for C&
certstor.cpp
Go to the documentation of this file.
1/*
2* Certificate Store
3* (C) 1999-2010,2013 Jack Lloyd
4* (C) 2017 Fabian Weissberg, Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/certstor.h>
10
11#include <botan/data_src.h>
12#include <botan/hash.h>
13#include <botan/pkix_types.h>
14#include <botan/internal/filesystem.h>
15
16namespace Botan {
17
19
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}
29
30std::optional<X509_Certificate> Certificate_Store::find_cert(const X509_DN& subject_dn,
31 const std::vector<uint8_t>& key_id) const {
32 const auto certs = find_all_certs(subject_dn, key_id);
33
34 if(certs.empty()) {
35 return std::nullopt;
36 }
37
38 // `count` might be greater than 1, but we'll just select the first match
39 return certs.front();
40}
41
42std::optional<X509_CRL> Certificate_Store::find_crl_for(const X509_Certificate& /*unused*/) const {
43 return std::nullopt;
44}
45
47 for(const auto& c : m_certs) {
48 if(c == cert) {
49 return;
50 }
51 }
52
53 m_certs.push_back(cert);
54}
55
56std::vector<X509_DN> Certificate_Store_In_Memory::all_subjects() const {
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}
64
65std::optional<X509_Certificate> Certificate_Store_In_Memory::find_cert(const X509_DN& subject_dn,
66 const std::vector<uint8_t>& key_id) const {
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}
84
85std::vector<X509_Certificate> Certificate_Store_In_Memory::find_all_certs(const X509_DN& subject_dn,
86 const std::vector<uint8_t>& key_id) const {
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}
105
107 const std::vector<uint8_t>& key_hash) const {
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}
123
125 const std::vector<uint8_t>& subject_hash) const {
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}
141
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}
158
159std::optional<X509_CRL> Certificate_Store_In_Memory::find_crl_for(const X509_Certificate& subject) const {
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}
179
183
184#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
186 if(dir.empty()) {
187 return;
188 }
189
190 std::vector<std::string> maybe_certs = get_files_recursive(dir);
191
192 if(maybe_certs.empty()) {
193 maybe_certs.push_back(std::string(dir));
194 }
195
196 for(auto&& cert_file : maybe_certs) {
197 try {
198 DataSource_Stream src(cert_file, true);
199 while(!src.end_of_data()) {
200 try {
201 X509_Certificate cert(src);
202 m_certs.push_back(cert);
203 } catch(std::exception&) {
204 // stop searching for other certificate at first exception
205 break;
206 }
207 }
208 } catch(std::exception&) {}
209 }
210}
211#endif
212
213} // namespace Botan
std::vector< X509_Certificate > find_all_certs(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
Definition certstor.cpp:85
std::optional< X509_Certificate > find_cert_by_pubkey_sha1(const std::vector< uint8_t > &key_hash) const override
Definition certstor.cpp:106
std::optional< X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
Definition certstor.cpp:65
void add_crl(const X509_CRL &crl)
Definition certstor.cpp:142
std::optional< X509_Certificate > find_cert_by_raw_subject_dn_sha256(const std::vector< uint8_t > &subject_hash) const override
Definition certstor.cpp:124
std::optional< X509_CRL > find_crl_for(const X509_Certificate &subject) const override
Definition certstor.cpp:159
void add_certificate(const X509_Certificate &cert)
Definition certstor.cpp:46
std::vector< X509_DN > all_subjects() const override
Definition certstor.cpp:56
virtual std::optional< X509_CRL > find_crl_for(const X509_Certificate &subject) const
Definition certstor.cpp:42
bool certificate_known(const X509_Certificate &cert) const
Definition certstor.cpp:20
virtual std::vector< X509_Certificate > find_all_certs(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const =0
virtual std::optional< X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const
Definition certstor.cpp:30
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:107
const X509_Time & this_update() const
Definition x509_crl.cpp:234
const X509_DN & issuer_dn() const
Definition x509_crl.cpp:213
const X509_DN & subject_dn() const
Definition x509cert.cpp:411
const std::vector< uint8_t > & authority_key_id() const
Definition x509cert.cpp:391
const std::vector< uint8_t > & subject_key_id() const
Definition x509cert.cpp:395
const X509_DN & issuer_dn() const
Definition x509cert.cpp:407
std::vector< std::string > get_files_recursive(std::string_view dir)