Botan 3.6.1
Crypto and TLS for C&
certstor_flatfile.cpp
Go to the documentation of this file.
1/*
2* Certificate Store
3* (C) 1999-2019 Jack Lloyd
4* (C) 2019 Patrick Schmidt
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/certstor_flatfile.h>
10
11#include <botan/data_src.h>
12#include <botan/pem.h>
13#include <botan/pkix_types.h>
14#include <stdexcept>
15
16namespace Botan {
17namespace {
18std::vector<std::vector<uint8_t>> decode_all_certificates(DataSource& source) {
19 std::vector<std::vector<uint8_t>> pems;
20
21 while(!source.end_of_data()) {
22 std::string label;
23 std::vector<uint8_t> cert;
24 try {
25 cert = unlock(PEM_Code::decode(source, label));
26
27 if(label == "CERTIFICATE" || label == "X509 CERTIFICATE" || label == "TRUSTED CERTIFICATE") {
28 pems.push_back(cert);
29 }
30 } catch(const Decoding_Error&) {}
31 }
32
33 return pems;
34}
35} // namespace
36
37Flatfile_Certificate_Store::Flatfile_Certificate_Store(std::string_view file, bool ignore_non_ca) {
38 if(file.empty()) {
39 throw Invalid_Argument("Flatfile_Certificate_Store::Flatfile_Certificate_Store invalid file path");
40 }
41
42 DataSource_Stream file_stream(file);
43
44 for(const std::vector<uint8_t>& der : decode_all_certificates(file_stream)) {
45 X509_Certificate cert(der);
46
47 /*
48 * Various weird or misconfigured system roots include intermediate certificates,
49 * or even stranger certificates which are not valid for cert issuance at all.
50 * Previously this code would error on such cases as an obvious misconfiguration,
51 * but we cannot fix the trust store. So instead just ignore any such certificate.
52 */
53 if(cert.is_self_signed() && cert.is_CA_cert()) {
54 m_all_subjects.push_back(cert.subject_dn());
55 m_dn_to_cert[cert.subject_dn()].push_back(cert);
56 m_pubkey_sha1_to_cert.emplace(cert.subject_public_key_bitstring_sha1(), cert);
57 m_subject_dn_sha256_to_cert.emplace(cert.raw_subject_dn_sha256(), cert);
58 } else if(!ignore_non_ca) {
59 throw Invalid_Argument("Flatfile_Certificate_Store received non CA cert " + cert.subject_dn().to_string());
60 }
61 }
62
63 if(m_all_subjects.empty()) {
64 throw Invalid_Argument("Flatfile_Certificate_Store::Flatfile_Certificate_Store cert file is empty");
65 }
66}
67
68std::vector<X509_DN> Flatfile_Certificate_Store::all_subjects() const {
69 return m_all_subjects;
70}
71
72std::vector<X509_Certificate> Flatfile_Certificate_Store::find_all_certs(const X509_DN& subject_dn,
73 const std::vector<uint8_t>& key_id) const {
74 std::vector<X509_Certificate> found_certs;
75 try {
76 const auto certs = m_dn_to_cert.at(subject_dn);
77
78 for(const auto& cert : certs) {
79 if(key_id.empty() || key_id == cert.subject_key_id()) {
80 found_certs.push_back(cert);
81 }
82 }
83 } catch(const std::out_of_range&) {
84 return {};
85 }
86
87 return found_certs;
88}
89
91 const std::vector<uint8_t>& key_hash) const {
92 if(key_hash.size() != 20) {
93 throw Invalid_Argument("Flatfile_Certificate_Store::find_cert_by_pubkey_sha1 invalid hash");
94 }
95
96 auto found_cert = m_pubkey_sha1_to_cert.find(key_hash);
97
98 if(found_cert != m_pubkey_sha1_to_cert.end()) {
99 return found_cert->second;
100 }
101
102 return std::nullopt;
103}
104
106 const std::vector<uint8_t>& subject_hash) const {
107 if(subject_hash.size() != 32) {
108 throw Invalid_Argument("Flatfile_Certificate_Store::find_cert_by_raw_subject_dn_sha256 invalid hash");
109 }
110
111 auto found_cert = m_subject_dn_sha256_to_cert.find(subject_hash);
112
113 if(found_cert != m_subject_dn_sha256_to_cert.end()) {
114 return found_cert->second;
115 }
116
117 return std::nullopt;
118}
119
120std::optional<X509_CRL> Flatfile_Certificate_Store::find_crl_for(const X509_Certificate& subject) const {
121 BOTAN_UNUSED(subject);
122 return {};
123}
124} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:118
std::optional< X509_CRL > find_crl_for(const X509_Certificate &subject) const override
std::vector< X509_DN > all_subjects() const override
std::optional< X509_Certificate > find_cert_by_raw_subject_dn_sha256(const std::vector< uint8_t > &subject_hash) const override
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_pubkey_sha1(const std::vector< uint8_t > &key_hash) const override
Flatfile_Certificate_Store(std::string_view file, bool ignore_non_ca=false)
bool is_CA_cert() const
Definition x509cert.cpp:407
const X509_DN & subject_dn() const
Definition x509cert.cpp:395
std::vector< uint8_t > raw_subject_dn_sha256() const
Definition x509cert.cpp:608
const std::vector< uint8_t > & subject_public_key_bitstring_sha1() const
Definition x509cert.cpp:367
bool is_self_signed() const
Definition x509cert.cpp:331
std::string to_string() const
Definition x509_dn.cpp:402
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
Definition pem.cpp:62
std::vector< T > unlock(const secure_vector< T > &in)
Definition secmem.h:75