Botan 3.12.0
Crypto and TLS for C&
certstor_sql.cpp
Go to the documentation of this file.
1/*
2* Certificate Store in SQL
3* (C) 2016 Kai Michaelis, Rohde & Schwarz Cybersecurity
4* (C) 2018 Jack Lloyd
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/certstor_sql.h>
10
11#include <botan/asn1_obj.h>
12#include <botan/asn1_time.h>
13#include <botan/ber_dec.h>
14#include <botan/data_src.h>
15#include <botan/pk_keys.h>
16#include <botan/pkcs8.h>
17#include <botan/pkix_types.h>
18
19namespace Botan {
20
22 std::string_view passwd,
24 std::string_view table_prefix) :
25 m_rng(rng), m_database(std::move(db)), m_prefix(table_prefix), m_password(passwd) {
26 m_database->create_table("CREATE TABLE IF NOT EXISTS " + m_prefix +
27 "certificates ( \
28 fingerprint BLOB PRIMARY KEY, \
29 subject_dn BLOB, \
30 key_id BLOB, \
31 priv_fingerprint BLOB, \
32 certificate BLOB UNIQUE NOT NULL\
33 )");
34 m_database->create_table("CREATE TABLE IF NOT EXISTS " + m_prefix +
35 "keys (\
36 fingerprint BLOB PRIMARY KEY, \
37 key BLOB UNIQUE NOT NULL \
38 )");
39 m_database->create_table("CREATE TABLE IF NOT EXISTS " + m_prefix +
40 "revoked (\
41 fingerprint BLOB PRIMARY KEY, \
42 reason BLOB NOT NULL, \
43 time BLOB NOT NULL \
44 )");
45}
46
47// Certificate handling
48std::optional<X509_Certificate> Certificate_Store_In_SQL::find_cert(const X509_DN& subject_dn,
49 const std::vector<uint8_t>& key_id) const {
50 std::shared_ptr<SQL_Database::Statement> stmt;
51
52 const std::vector<uint8_t> dn_encoding = subject_dn.BER_encode();
53
54 if(key_id.empty()) {
55 stmt = m_database->new_statement("SELECT certificate FROM " + m_prefix +
56 "certificates WHERE subject_dn == ?1 LIMIT 1");
57 stmt->bind(1, dn_encoding);
58 } else {
59 stmt = m_database->new_statement("SELECT certificate FROM " + m_prefix +
60 "certificates WHERE\
61 subject_dn == ?1 AND (key_id IS NULL OR key_id == ?2) LIMIT 1");
62 stmt->bind(1, dn_encoding);
63 stmt->bind(2, key_id);
64 }
65
66 while(stmt->step()) {
67 auto blob = stmt->get_blob(0);
68 return X509_Certificate(blob.first, blob.second);
69 }
70
71 return std::optional<X509_Certificate>();
72}
73
74std::vector<X509_Certificate> Certificate_Store_In_SQL::find_all_certs(const X509_DN& subject_dn,
75 const std::vector<uint8_t>& key_id) const {
76 std::vector<X509_Certificate> certs;
77
78 std::shared_ptr<SQL_Database::Statement> stmt;
79
80 const std::vector<uint8_t> dn_encoding = subject_dn.BER_encode();
81
82 if(key_id.empty()) {
83 stmt = m_database->new_statement("SELECT certificate FROM " + m_prefix + "certificates WHERE subject_dn == ?1");
84 stmt->bind(1, dn_encoding);
85 } else {
86 stmt = m_database->new_statement("SELECT certificate FROM " + m_prefix +
87 "certificates WHERE\
88 subject_dn == ?1 AND (key_id IS NULL OR key_id == ?2)");
89 stmt->bind(1, dn_encoding);
90 stmt->bind(2, key_id);
91 }
92
93 while(stmt->step()) {
94 auto blob = stmt->get_blob(0);
95 certs.push_back(X509_Certificate(blob.first, blob.second));
96 }
97
98 return certs;
99}
100
102 const std::vector<uint8_t>& /*key_hash*/) const {
103 throw Not_Implemented("Certificate_Store_In_SQL::find_cert_by_pubkey_sha1");
104}
105
107 const std::vector<uint8_t>& /*subject_hash*/) const {
108 throw Not_Implemented("Certificate_Store_In_SQL::find_cert_by_raw_subject_dn_sha256");
109}
110
112 const X509_DN& /*issuer_dn*/, std::span<const uint8_t> /*serial_number*/) const {
113 throw Not_Implemented("Certificate_Store_In_SQL::find_cert_by_issuer_dn_and_serial_number");
114}
115
116std::optional<X509_CRL> Certificate_Store_In_SQL::find_crl_for(const X509_Certificate& subject) const {
117 const auto all_crls = generate_crls();
118
119 for(const auto& crl : all_crls) {
120 if(!crl.get_revoked().empty() && crl.issuer_dn() == subject.issuer_dn()) {
121 return crl;
122 }
123 }
124
125 return std::optional<X509_CRL>();
126}
127
128std::vector<X509_DN> Certificate_Store_In_SQL::all_subjects() const {
129 std::vector<X509_DN> ret;
130 auto stmt = m_database->new_statement("SELECT subject_dn FROM " + m_prefix + "certificates");
131
132 while(stmt->step()) {
133 auto blob = stmt->get_blob(0);
134 BER_Decoder dec(std::span<const uint8_t>{blob.first, blob.second}, BER_Decoder::Limits::DER());
135 X509_DN dn;
136
137 dn.decode_from(dec);
138
139 ret.push_back(dn);
140 }
141
142 return ret;
143}
144
146 const std::vector<uint8_t> dn_encoding = cert.subject_dn().BER_encode();
147 const std::vector<uint8_t> cert_encoding = cert.BER_encode();
148
149 auto stmt = m_database->new_statement("INSERT OR REPLACE INTO " + m_prefix +
150 "certificates (\
151 fingerprint, \
152 subject_dn, \
153 key_id, \
154 priv_fingerprint, \
155 certificate \
156 ) VALUES ( ?1, ?2, ?3, ?4, ?5 )");
157
158 stmt->bind(1, cert.fingerprint("SHA-256"));
159 stmt->bind(2, dn_encoding);
160 stmt->bind(3, cert.subject_key_id());
161 stmt->bind(4, std::vector<uint8_t>());
162 stmt->bind(5, cert_encoding);
163 stmt->spin();
164
165 return true;
166}
167
169 auto stmt = m_database->new_statement("SELECT 1 FROM " + m_prefix + "certificates WHERE fingerprint == ?1");
170 stmt->bind(1, cert.fingerprint("SHA-256"));
171 return stmt->step();
172}
173
175 if(!find_cert(cert.subject_dn(), cert.subject_key_id())) {
176 return false;
177 }
178
179 auto stmt = m_database->new_statement("DELETE FROM " + m_prefix + "certificates WHERE fingerprint == ?1");
180
181 stmt->bind(1, cert.fingerprint("SHA-256"));
182 stmt->spin();
183
184 return true;
185}
186
187// Private key handling
188std::shared_ptr<const Private_Key> Certificate_Store_In_SQL::find_key(const X509_Certificate& cert) const {
189 auto stmt = m_database->new_statement("SELECT key FROM " + m_prefix +
190 "keys "
191 "JOIN " +
192 m_prefix + "certificates ON " + m_prefix + "keys.fingerprint == " + m_prefix +
193 "certificates.priv_fingerprint "
194 "WHERE " +
195 m_prefix + "certificates.fingerprint == ?1");
196 stmt->bind(1, cert.fingerprint("SHA-256"));
197
198 std::shared_ptr<const Private_Key> key;
199 while(stmt->step()) {
200 auto blob = stmt->get_blob(0);
201 DataSource_Memory src(blob.first, blob.second);
202 key = PKCS8::load_key(src, m_password);
203 }
204
205 return key;
206}
207
208std::vector<X509_Certificate> Certificate_Store_In_SQL::find_certs_for_key(const Private_Key& key) const {
209 auto fprint = key.fingerprint_private("SHA-256");
210 auto stmt =
211 m_database->new_statement("SELECT certificate FROM " + m_prefix + "certificates WHERE priv_fingerprint == ?1");
212
213 stmt->bind(1, fprint);
214
215 std::vector<X509_Certificate> certs;
216 while(stmt->step()) {
217 auto blob = stmt->get_blob(0);
218 certs.push_back(X509_Certificate(blob.first, blob.second));
219 }
220
221 return certs;
222}
223
225 insert_cert(cert);
226
227 if(find_key(cert)) {
228 return false;
229 }
230
231 auto pkcs8 = PKCS8::BER_encode(key, m_rng, m_password);
232 auto fprint = key.fingerprint_private("SHA-256");
233
234 auto stmt1 =
235 m_database->new_statement("INSERT OR REPLACE INTO " + m_prefix + "keys ( fingerprint, key ) VALUES ( ?1, ?2 )");
236
237 stmt1->bind(1, fprint);
238 stmt1->bind(2, pkcs8.data(), pkcs8.size());
239 stmt1->spin();
240
241 auto stmt2 = m_database->new_statement("UPDATE " + m_prefix +
242 "certificates SET priv_fingerprint = ?1 WHERE fingerprint == ?2");
243
244 stmt2->bind(1, fprint);
245 stmt2->bind(2, cert.fingerprint("SHA-256"));
246 stmt2->spin();
247
248 return true;
249}
250
252 auto fprint = key.fingerprint_private("SHA-256");
253 auto stmt = m_database->new_statement("DELETE FROM " + m_prefix + "keys WHERE fingerprint == ?1");
254
255 stmt->bind(1, fprint);
256 stmt->spin();
257}
258
259// Revocation
261 // TODO(Botan4) require that time be valid
262 insert_cert(cert);
263
264 auto stmt1 = m_database->new_statement("INSERT OR REPLACE INTO " + m_prefix +
265 "revoked ( fingerprint, reason, time ) VALUES ( ?1, ?2, ?3 )");
266
267 stmt1->bind(1, cert.fingerprint("SHA-256"));
268 stmt1->bind(2, static_cast<uint32_t>(code));
269
270 if(time.time_is_set()) {
271 stmt1->bind(3, time.BER_encode());
272 } else {
273 stmt1->bind(3, static_cast<size_t>(-1));
274 }
275
276 stmt1->spin();
277}
278
279// Revocation
281 insert_cert(cert);
282
283 auto stmt1 = m_database->new_statement("INSERT OR REPLACE INTO " + m_prefix +
284 "revoked ( fingerprint, reason, time ) VALUES ( ?1, ?2, ?3 )");
285
286 stmt1->bind(1, cert.fingerprint("SHA-256"));
287 stmt1->bind(2, static_cast<uint32_t>(code));
288 stmt1->bind(3, static_cast<size_t>(-1));
289
290 stmt1->spin();
291}
292
294 auto stmt = m_database->new_statement("DELETE FROM " + m_prefix + "revoked WHERE fingerprint == ?1");
295
296 stmt->bind(1, cert.fingerprint("SHA-256"));
297 stmt->spin();
298}
299
300std::vector<X509_CRL> Certificate_Store_In_SQL::generate_crls() const {
301 auto stmt = m_database->new_statement("SELECT certificate,reason,time FROM " + m_prefix +
302 "revoked "
303 "JOIN " +
304 m_prefix + "certificates ON " + m_prefix +
305 "certificates.fingerprint == " + m_prefix + "revoked.fingerprint");
306
307 std::map<X509_DN, std::vector<CRL_Entry>> crls;
308 while(stmt->step()) {
309 auto blob = stmt->get_blob(0);
310 auto cert = X509_Certificate(std::vector<uint8_t>(blob.first, blob.first + blob.second));
311 auto code = static_cast<CRL_Code>(stmt->get_size_t(1));
312 auto ent = CRL_Entry(cert, code);
313
314 auto i = crls.find(cert.issuer_dn());
315 if(i == crls.end()) {
316 crls.insert(std::make_pair(cert.issuer_dn(), std::vector<CRL_Entry>({ent})));
317 } else {
318 i->second.push_back(ent);
319 }
320 }
321
322 const X509_Time t(std::chrono::system_clock::now());
323
324 std::vector<X509_CRL> ret;
325 ret.reserve(crls.size());
326
327 for(const auto& p : crls) {
328 ret.push_back(X509_CRL(p.first, t, t, p.second));
329 }
330
331 return ret;
332}
333
334} // namespace Botan
std::vector< uint8_t > BER_encode() const
Definition asn1_obj.cpp:20
bool time_is_set() const
Return if the time has been set somehow.
static Limits DER()
Definition ber_dec.h:35
Definition x509_crl.h:29
std::optional< X509_Certificate > find_cert_by_issuer_dn_and_serial_number(const X509_DN &issuer_dn, std::span< const uint8_t > serial_number) const override
std::vector< X509_DN > all_subjects() const override
bool insert_cert(const X509_Certificate &cert)
std::vector< X509_Certificate > find_all_certs(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
std::optional< X509_Certificate > find_cert_by_raw_subject_dn_sha256(const std::vector< uint8_t > &subject_hash) const override
bool contains(const X509_Certificate &cert) const override
void revoke_cert(const X509_Certificate &cert, CRL_Code reason, const X509_Time &time)
Marks "cert" as revoked starting from "time".
std::optional< X509_CRL > find_crl_for(const X509_Certificate &issuer) const override
Certificate_Store_In_SQL(std::shared_ptr< SQL_Database > db, std::string_view passwd, RandomNumberGenerator &rng, std::string_view table_prefix="")
std::vector< X509_Certificate > find_certs_for_key(const Private_Key &key) const
Returns all certificates for private key "key".
void affirm_cert(const X509_Certificate &cert)
Reverses the revocation for "cert".
bool remove_cert(const X509_Certificate &cert)
std::shared_ptr< const Private_Key > find_key(const X509_Certificate &cert) const
Returns the private key for "cert" or an empty shared_ptr if none was found.
std::optional< X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
void remove_key(const Private_Key &key)
Removes "key" from the store.
bool insert_key(const X509_Certificate &cert, const Private_Key &key)
std::optional< X509_Certificate > find_cert_by_pubkey_sha1(const std::vector< uint8_t > &key_hash) const override
std::vector< X509_CRL > generate_crls() const
std::string fingerprint_private(std::string_view alg) const
Definition pk_keys.cpp:101
std::string fingerprint(std::string_view hash_name="SHA-1") const
Definition x509cert.cpp:685
const X509_DN & subject_dn() const
Definition x509cert.cpp:418
const std::vector< uint8_t > & subject_key_id() const
Definition x509cert.cpp:402
const X509_DN & issuer_dn() const
Definition x509cert.cpp:414
void decode_from(BER_Decoder &from) override
Definition x509_dn.cpp:338
std::vector< uint8_t > BER_encode(const Private_Key &key, RandomNumberGenerator &rng, std::string_view pass, std::chrono::milliseconds msec, std::string_view pbe_algo)
Definition pkcs8.cpp:166
std::unique_ptr< Private_Key > load_key(DataSource &source, const std::function< std::string()> &get_pass)
Definition pkcs8.cpp:319
ASN1_Time X509_Time
Definition asn1_obj.h:23