Botan 3.11.0
Crypto and TLS for C&
Botan::Certificate_Store_In_SQLite Class Referencefinal

#include <certstor_sqlite.h>

Inheritance diagram for Botan::Certificate_Store_In_SQLite:
Botan::Certificate_Store_In_SQL Botan::Certificate_Store

Public Member Functions

void affirm_cert (const X509_Certificate &cert)
 Reverses the revocation for "cert".
std::vector< X509_DNall_subjects () const override
bool certificate_known (const X509_Certificate &cert) const
 Certificate_Store_In_SQLite (std::string_view db_path, std::string_view passwd, RandomNumberGenerator &rng, std::string_view table_prefix="")
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_issuer_dn_and_serial_number (const X509_DN &issuer_dn, std::span< const uint8_t > serial_number) 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::vector< X509_Certificatefind_certs_for_key (const Private_Key &key) const
 Returns all certificates for private key "key".
std::optional< X509_CRLfind_crl_for (const X509_Certificate &issuer) const override
std::shared_ptr< const Private_Keyfind_key (const X509_Certificate &cert) const
 Returns the private key for "cert" or an empty shared_ptr if none was found.
std::vector< X509_CRLgenerate_crls () const
bool insert_cert (const X509_Certificate &cert)
bool insert_key (const X509_Certificate &cert, const Private_Key &key)
bool remove_cert (const X509_Certificate &cert)
void remove_key (const Private_Key &key)
 Removes "key" from the store.
void revoke_cert (const X509_Certificate &cert, CRL_Code reason)
 Marks "cert" as revoked with no time specified.
void revoke_cert (const X509_Certificate &cert, CRL_Code reason, const X509_Time &time)
 Marks "cert" as revoked starting from "time".

Detailed Description

Certificate and private key store backed by an sqlite (https://sqlite.org) database.

Definition at line 18 of file certstor_sqlite.h.

Constructor & Destructor Documentation

◆ Certificate_Store_In_SQLite()

Botan::Certificate_Store_In_SQLite::Certificate_Store_In_SQLite ( std::string_view db_path,
std::string_view passwd,
RandomNumberGenerator & rng,
std::string_view table_prefix = "" )

Create/open a certificate store.

Parameters
db_pathpath to the database file
passwdpassword to encrypt private keys in the database
rngused for encrypting keys
table_prefixoptional prefix for db table names

Definition at line 14 of file certstor_sqlite.cpp.

17 :
18 Certificate_Store_In_SQL(std::make_shared<Sqlite3_Database>(db_path), passwd, rng, table_prefix) {}
Certificate_Store_In_SQL(std::shared_ptr< SQL_Database > db, std::string_view passwd, RandomNumberGenerator &rng, std::string_view table_prefix="")

References Botan::Certificate_Store_In_SQL::Certificate_Store_In_SQL().

Member Function Documentation

◆ affirm_cert()

void Botan::Certificate_Store_In_SQL::affirm_cert ( const X509_Certificate & cert)
inherited

Reverses the revocation for "cert".

Definition at line 287 of file certstor_sql.cpp.

287 {
288 auto stmt = m_database->new_statement("DELETE FROM " + m_prefix + "revoked WHERE fingerprint == ?1");
289
290 stmt->bind(1, cert.fingerprint("SHA-256"));
291 stmt->spin();
292}

References Botan::X509_Certificate::fingerprint().

◆ all_subjects()

std::vector< X509_DN > Botan::Certificate_Store_In_SQL::all_subjects ( ) const
overridevirtualinherited

Returns all subject DNs known to the store instance.

Implements Botan::Certificate_Store.

Definition at line 128 of file certstor_sql.cpp.

128 {
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(blob.first, blob.second);
135 X509_DN dn;
136
137 dn.decode_from(dec);
138
139 ret.push_back(dn);
140 }
141
142 return ret;
143}

References Botan::X509_DN::decode_from().

◆ certificate_known()

bool Botan::Certificate_Store::certificate_known ( const X509_Certificate & cert) const
inlineinherited
Returns
whether the certificate is known
Parameters
certcertificate to be searched

Definition at line 80 of file certstor.h.

80 {
81 return find_cert(cert.subject_dn(), cert.subject_key_id()).has_value();
82 }
virtual std::optional< X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const
Definition certstor.cpp:22

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

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

◆ find_all_certs()

std::vector< X509_Certificate > Botan::Certificate_Store_In_SQL::find_all_certs ( const X509_DN & subject_dn,
const std::vector< uint8_t > & key_id ) const
overridevirtualinherited

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 74 of file certstor_sql.cpp.

75 {
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 == 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}

References Botan::ASN1_Object::BER_encode().

◆ find_cert()

std::optional< X509_Certificate > Botan::Certificate_Store_In_SQL::find_cert ( const X509_DN & subject_dn,
const std::vector< uint8_t > & key_id ) const
overridevirtualinherited

Returns the first certificate with matching subject DN and optional key ID.

Reimplemented from Botan::Certificate_Store.

Definition at line 48 of file certstor_sql.cpp.

49 {
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 == 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}

References Botan::ASN1_Object::BER_encode().

Referenced by remove_cert().

◆ find_cert_by_issuer_dn_and_serial_number()

std::optional< X509_Certificate > Botan::Certificate_Store_In_SQL::find_cert_by_issuer_dn_and_serial_number ( const X509_DN & issuer_dn,
std::span< const uint8_t > serial_number ) const
overridevirtualinherited

Find a certificate by searching for one with a matching issuer DN and serial number. Used for CMS or PKCS#7.

Parameters
issuer_dnthe distinguished name of the issuer
serial_numberthe certificate's serial number
Returns
a matching certificate or nullopt otherwise

Implements Botan::Certificate_Store.

Definition at line 111 of file certstor_sql.cpp.

112 {
113 throw Not_Implemented("Certificate_Store_In_SQL::find_cert_by_issuer_dn_and_serial_number");
114}

◆ find_cert_by_pubkey_sha1()

std::optional< X509_Certificate > Botan::Certificate_Store_In_SQL::find_cert_by_pubkey_sha1 ( const std::vector< uint8_t > & key_hash) const
overridevirtualinherited

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 101 of file certstor_sql.cpp.

102 {
103 throw Not_Implemented("Certificate_Store_In_SQL::find_cert_by_pubkey_sha1");
104}

◆ find_cert_by_raw_subject_dn_sha256()

std::optional< X509_Certificate > Botan::Certificate_Store_In_SQL::find_cert_by_raw_subject_dn_sha256 ( const std::vector< uint8_t > & subject_hash) const
overridevirtualinherited

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 106 of file certstor_sql.cpp.

107 {
108 throw Not_Implemented("Certificate_Store_In_SQL::find_cert_by_raw_subject_dn_sha256");
109}

◆ find_certs_for_key()

std::vector< X509_Certificate > Botan::Certificate_Store_In_SQL::find_certs_for_key ( const Private_Key & key) const
inherited

Returns all certificates for private key "key".

Definition at line 202 of file certstor_sql.cpp.

202 {
203 auto fprint = key.fingerprint_private("SHA-256");
204 auto stmt =
205 m_database->new_statement("SELECT certificate FROM " + m_prefix + "certificates WHERE priv_fingerprint == ?1");
206
207 stmt->bind(1, fprint);
208
209 std::vector<X509_Certificate> certs;
210 while(stmt->step()) {
211 auto blob = stmt->get_blob(0);
212 certs.push_back(X509_Certificate(blob.first, blob.second));
213 }
214
215 return certs;
216}

References Botan::Private_Key::fingerprint_private().

◆ find_crl_for()

std::optional< X509_CRL > Botan::Certificate_Store_In_SQL::find_crl_for ( const X509_Certificate & issuer) const
overridevirtualinherited

Generates a CRL for all certificates issued by the given issuer.

Reimplemented from Botan::Certificate_Store.

Definition at line 116 of file certstor_sql.cpp.

116 {
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}
std::vector< X509_CRL > generate_crls() const

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

◆ find_key()

std::shared_ptr< const Private_Key > Botan::Certificate_Store_In_SQL::find_key ( const X509_Certificate & cert) const
inherited

Returns the private key for "cert" or an empty shared_ptr if none was found.

Definition at line 182 of file certstor_sql.cpp.

182 {
183 auto stmt = m_database->new_statement("SELECT key FROM " + m_prefix +
184 "keys "
185 "JOIN " +
186 m_prefix + "certificates ON " + m_prefix + "keys.fingerprint == " + m_prefix +
187 "certificates.priv_fingerprint "
188 "WHERE " +
189 m_prefix + "certificates.fingerprint == ?1");
190 stmt->bind(1, cert.fingerprint("SHA-256"));
191
192 std::shared_ptr<const Private_Key> key;
193 while(stmt->step()) {
194 auto blob = stmt->get_blob(0);
195 DataSource_Memory src(blob.first, blob.second);
196 key = PKCS8::load_key(src, m_password);
197 }
198
199 return key;
200}
std::unique_ptr< Private_Key > load_key(DataSource &source, const std::function< std::string()> &get_pass)
Definition pkcs8.cpp:314

References Botan::X509_Certificate::fingerprint(), and Botan::PKCS8::load_key().

Referenced by insert_key().

◆ generate_crls()

std::vector< X509_CRL > Botan::Certificate_Store_In_SQL::generate_crls ( ) const
inherited

Generates Certificate Revocation Lists for all certificates marked as revoked. A CRL is returned for each unique issuer DN.

Definition at line 294 of file certstor_sql.cpp.

294 {
295 auto stmt = m_database->new_statement("SELECT certificate,reason,time FROM " + m_prefix +
296 "revoked "
297 "JOIN " +
298 m_prefix + "certificates ON " + m_prefix +
299 "certificates.fingerprint == " + m_prefix + "revoked.fingerprint");
300
301 std::map<X509_DN, std::vector<CRL_Entry>> crls;
302 while(stmt->step()) {
303 auto blob = stmt->get_blob(0);
304 auto cert = X509_Certificate(std::vector<uint8_t>(blob.first, blob.first + blob.second));
305 auto code = static_cast<CRL_Code>(stmt->get_size_t(1));
306 auto ent = CRL_Entry(cert, code);
307
308 auto i = crls.find(cert.issuer_dn());
309 if(i == crls.end()) {
310 crls.insert(std::make_pair(cert.issuer_dn(), std::vector<CRL_Entry>({ent})));
311 } else {
312 i->second.push_back(ent);
313 }
314 }
315
316 const X509_Time t(std::chrono::system_clock::now());
317
318 std::vector<X509_CRL> ret;
319 ret.reserve(crls.size());
320
321 for(const auto& p : crls) {
322 ret.push_back(X509_CRL(p.first, t, t, p.second));
323 }
324
325 return ret;
326}
ASN1_Time X509_Time
Definition asn1_obj.h:23

Referenced by find_crl_for().

◆ insert_cert()

bool Botan::Certificate_Store_In_SQL::insert_cert ( const X509_Certificate & cert)
inherited

Inserts "cert" into the store, returns false if the certificate is already known and true if insertion was successful.

Definition at line 145 of file certstor_sql.cpp.

145 {
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}

References Botan::ASN1_Object::BER_encode(), Botan::X509_Certificate::fingerprint(), Botan::X509_Certificate::subject_dn(), and Botan::X509_Certificate::subject_key_id().

Referenced by insert_key(), revoke_cert(), and revoke_cert().

◆ insert_key()

bool Botan::Certificate_Store_In_SQL::insert_key ( const X509_Certificate & cert,
const Private_Key & key )
inherited

Inserts "key" for "cert" into the store, returns false if the key is already known and true if insertion was successful.

Definition at line 218 of file certstor_sql.cpp.

218 {
219 insert_cert(cert);
220
221 if(find_key(cert)) {
222 return false;
223 }
224
225 auto pkcs8 = PKCS8::BER_encode(key, m_rng, m_password);
226 auto fprint = key.fingerprint_private("SHA-256");
227
228 auto stmt1 =
229 m_database->new_statement("INSERT OR REPLACE INTO " + m_prefix + "keys ( fingerprint, key ) VALUES ( ?1, ?2 )");
230
231 stmt1->bind(1, fprint);
232 stmt1->bind(2, pkcs8.data(), pkcs8.size());
233 stmt1->spin();
234
235 auto stmt2 = m_database->new_statement("UPDATE " + m_prefix +
236 "certificates SET priv_fingerprint = ?1 WHERE fingerprint == ?2");
237
238 stmt2->bind(1, fprint);
239 stmt2->bind(2, cert.fingerprint("SHA-256"));
240 stmt2->spin();
241
242 return true;
243}
bool insert_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::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:161

References Botan::PKCS8::BER_encode(), find_key(), Botan::X509_Certificate::fingerprint(), Botan::Private_Key::fingerprint_private(), and insert_cert().

◆ remove_cert()

bool Botan::Certificate_Store_In_SQL::remove_cert ( const X509_Certificate & cert)
inherited

Removes "cert" from the store. Returns false if the certificate could not be found and true if removal was successful.

Definition at line 168 of file certstor_sql.cpp.

168 {
169 if(!find_cert(cert.subject_dn(), cert.subject_key_id())) {
170 return false;
171 }
172
173 auto stmt = m_database->new_statement("DELETE FROM " + m_prefix + "certificates WHERE fingerprint == ?1");
174
175 stmt->bind(1, cert.fingerprint("SHA-256"));
176 stmt->spin();
177
178 return true;
179}
std::optional< X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override

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

◆ remove_key()

void Botan::Certificate_Store_In_SQL::remove_key ( const Private_Key & key)
inherited

Removes "key" from the store.

Definition at line 245 of file certstor_sql.cpp.

245 {
246 auto fprint = key.fingerprint_private("SHA-256");
247 auto stmt = m_database->new_statement("DELETE FROM " + m_prefix + "keys WHERE fingerprint == ?1");
248
249 stmt->bind(1, fprint);
250 stmt->spin();
251}

References Botan::Private_Key::fingerprint_private().

◆ revoke_cert() [1/2]

void Botan::Certificate_Store_In_SQL::revoke_cert ( const X509_Certificate & cert,
CRL_Code reason )
inherited

Marks "cert" as revoked with no time specified.

Definition at line 274 of file certstor_sql.cpp.

274 {
275 insert_cert(cert);
276
277 auto stmt1 = m_database->new_statement("INSERT OR REPLACE INTO " + m_prefix +
278 "revoked ( fingerprint, reason, time ) VALUES ( ?1, ?2, ?3 )");
279
280 stmt1->bind(1, cert.fingerprint("SHA-256"));
281 stmt1->bind(2, static_cast<uint32_t>(code));
282 stmt1->bind(3, static_cast<size_t>(-1));
283
284 stmt1->spin();
285}

References Botan::X509_Certificate::fingerprint(), and insert_cert().

◆ revoke_cert() [2/2]

void Botan::Certificate_Store_In_SQL::revoke_cert ( const X509_Certificate & cert,
CRL_Code reason,
const X509_Time & time )
inherited

Marks "cert" as revoked starting from "time".

Definition at line 254 of file certstor_sql.cpp.

254 {
255 // TODO(Botan4) require that time be valid
256 insert_cert(cert);
257
258 auto stmt1 = m_database->new_statement("INSERT OR REPLACE INTO " + m_prefix +
259 "revoked ( fingerprint, reason, time ) VALUES ( ?1, ?2, ?3 )");
260
261 stmt1->bind(1, cert.fingerprint("SHA-256"));
262 stmt1->bind(2, static_cast<uint32_t>(code));
263
264 if(time.time_is_set()) {
265 stmt1->bind(3, time.BER_encode());
266 } else {
267 stmt1->bind(3, static_cast<size_t>(-1));
268 }
269
270 stmt1->spin();
271}

References Botan::ASN1_Object::BER_encode(), Botan::X509_Certificate::fingerprint(), insert_cert(), and Botan::ASN1_Time::time_is_set().


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