9#include <botan/certstor_sql.h>
11#include <botan/ber_dec.h>
12#include <botan/data_src.h>
13#include <botan/pk_keys.h>
14#include <botan/pkcs8.h>
15#include <botan/pkix_types.h>
20 std::string_view passwd,
22 std::string_view table_prefix) :
23 m_rng(rng), m_database(std::move(db)), m_prefix(table_prefix), m_password(passwd) {
24 m_database->create_table(
"CREATE TABLE IF NOT EXISTS " + m_prefix +
26 fingerprint BLOB PRIMARY KEY, \
29 priv_fingerprint BLOB, \
30 certificate BLOB UNIQUE NOT NULL\
32 m_database->create_table(
"CREATE TABLE IF NOT EXISTS " + m_prefix +
34 fingerprint BLOB PRIMARY KEY, \
35 key BLOB UNIQUE NOT NULL \
37 m_database->create_table(
"CREATE TABLE IF NOT EXISTS " + m_prefix +
39 fingerprint BLOB PRIMARY KEY, \
40 reason BLOB NOT NULL, \
47 const std::vector<uint8_t>& key_id)
const {
48 std::shared_ptr<SQL_Database::Statement> stmt;
50 const std::vector<uint8_t> dn_encoding = subject_dn.
BER_encode();
53 stmt = m_database->new_statement(
"SELECT certificate FROM " + m_prefix +
54 "certificates WHERE subject_dn == ?1 LIMIT 1");
55 stmt->bind(1, dn_encoding);
57 stmt = m_database->new_statement(
"SELECT certificate FROM " + m_prefix +
59 subject_dn == ?1 AND (key_id == NULL OR key_id == ?2) LIMIT 1");
60 stmt->bind(1, dn_encoding);
61 stmt->bind(2, key_id);
65 auto blob = stmt->get_blob(0);
69 return std::optional<X509_Certificate>();
73 const std::vector<uint8_t>& key_id)
const {
74 std::vector<X509_Certificate> certs;
76 std::shared_ptr<SQL_Database::Statement> stmt;
78 const std::vector<uint8_t> dn_encoding = subject_dn.
BER_encode();
81 stmt = m_database->new_statement(
"SELECT certificate FROM " + m_prefix +
"certificates WHERE subject_dn == ?1");
82 stmt->bind(1, dn_encoding);
84 stmt = m_database->new_statement(
"SELECT certificate FROM " + m_prefix +
86 subject_dn == ?1 AND (key_id == NULL OR key_id == ?2)");
87 stmt->bind(1, dn_encoding);
88 stmt->bind(2, key_id);
91 std::optional<X509_Certificate> cert;
93 auto blob = stmt->get_blob(0);
101 const std::vector<uint8_t>& )
const {
102 throw Not_Implemented(
"Certificate_Store_In_SQL::find_cert_by_pubkey_sha1");
106 const std::vector<uint8_t>& )
const {
107 throw Not_Implemented(
"Certificate_Store_In_SQL::find_cert_by_raw_subject_dn_sha256");
113 for(
auto crl : all_crls) {
114 if(!crl.get_revoked().empty() && crl.issuer_dn() == subject.
issuer_dn()) {
119 return std::optional<X509_CRL>();
123 std::vector<X509_DN> ret;
124 auto stmt = m_database->new_statement(
"SELECT subject_dn FROM " + m_prefix +
"certificates");
126 while(stmt->step()) {
127 auto blob = stmt->get_blob(0);
141 const std::vector<uint8_t> cert_encoding = cert.
BER_encode();
143 auto stmt = m_database->new_statement(
"INSERT OR REPLACE INTO " + m_prefix +
150 ) VALUES ( ?1, ?2, ?3, ?4, ?5 )");
153 stmt->bind(2, dn_encoding);
155 stmt->bind(4, std::vector<uint8_t>());
156 stmt->bind(5, cert_encoding);
167 auto stmt = m_database->new_statement(
"DELETE FROM " + m_prefix +
"certificates WHERE fingerprint == ?1");
177 auto stmt = m_database->new_statement(
"SELECT key FROM " + m_prefix +
180 m_prefix +
"certificates ON " + m_prefix +
"keys.fingerprint == " + m_prefix +
181 "certificates.priv_fingerprint "
183 m_prefix +
"certificates.fingerprint == ?1");
186 std::shared_ptr<const Private_Key> key;
187 while(stmt->step()) {
188 auto blob = stmt->get_blob(0);
199 m_database->new_statement(
"SELECT certificate FROM " + m_prefix +
"certificates WHERE priv_fingerprint == ?1");
203 std::vector<X509_Certificate> certs;
204 while(stmt->step()) {
205 auto blob = stmt->get_blob(0);
223 m_database->new_statement(
"INSERT OR REPLACE INTO " + m_prefix +
"keys ( fingerprint, key ) VALUES ( ?1, ?2 )");
226 stmt1->bind(2, pkcs8.data(), pkcs8.size());
229 auto stmt2 = m_database->new_statement(
"UPDATE " + m_prefix +
230 "certificates SET priv_fingerprint = ?1 WHERE fingerprint == ?2");
241 auto stmt = m_database->new_statement(
"DELETE FROM " + m_prefix +
"keys WHERE fingerprint == ?1");
251 auto stmt1 = m_database->new_statement(
"INSERT OR REPLACE INTO " + m_prefix +
252 "revoked ( fingerprint, reason, time ) VALUES ( ?1, ?2, ?3 )");
255 stmt1->bind(2,
static_cast<uint32_t
>(code));
260 stmt1->bind(3,
static_cast<size_t>(-1));
267 auto stmt = m_database->new_statement(
"DELETE FROM " + m_prefix +
"revoked WHERE fingerprint == ?1");
274 auto stmt = m_database->new_statement(
"SELECT certificate,reason,time FROM " + m_prefix +
277 m_prefix +
"certificates ON " + m_prefix +
278 "certificates.fingerprint == " + m_prefix +
"revoked.fingerprint");
280 std::map<X509_DN, std::vector<CRL_Entry>> crls;
281 while(stmt->step()) {
282 auto blob = stmt->get_blob(0);
283 auto cert =
X509_Certificate(std::vector<uint8_t>(blob.first, blob.first + blob.second));
284 auto code =
static_cast<CRL_Code>(stmt->get_size_t(1));
287 auto i = crls.find(cert.issuer_dn());
288 if(i == crls.end()) {
289 crls.insert(std::make_pair(cert.issuer_dn(), std::vector<CRL_Entry>({ent})));
291 i->second.push_back(ent);
295 X509_Time t(std::chrono::system_clock::now());
297 std::vector<X509_CRL> ret;
298 ret.reserve(crls.size());
300 for(
const auto& p : crls) {
301 ret.push_back(
X509_CRL(p.first, t, t, p.second));
std::vector< uint8_t > BER_encode() const
bool time_is_set() const
Return if the time has been set somehow.
void affirm_cert(const X509_Certificate &)
Reverses the revokation for "cert".
std::vector< X509_DN > all_subjects() const override
std::shared_ptr< const Private_Key > find_key(const X509_Certificate &) const
Returns the private key for "cert" or an empty shared_ptr if none was found.
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
void revoke_cert(const X509_Certificate &, CRL_Code, const X509_Time &time=X509_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".
bool remove_cert(const X509_Certificate &cert)
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
std::string fingerprint(std::string_view hash_name="SHA-1") const
const X509_DN & subject_dn() const
const std::vector< uint8_t > & subject_key_id() const
const X509_DN & issuer_dn() const
void decode_from(BER_Decoder &) override
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)
std::unique_ptr< Private_Key > load_key(DataSource &source, const std::function< std::string()> &get_pass)