9#include <botan/certstor_flatfile.h>
11#include <botan/data_src.h>
13#include <botan/pkix_types.h>
18std::vector<std::vector<uint8_t>> decode_all_certificates(DataSource& source) {
19 std::vector<std::vector<uint8_t>> pems;
21 while(!source.end_of_data()) {
23 std::vector<uint8_t> cert;
27 if(label ==
"CERTIFICATE" || label ==
"X509 CERTIFICATE" || label ==
"TRUSTED CERTIFICATE") {
30 }
catch(
const Decoding_Error&) {}
39 throw Invalid_Argument(
"Flatfile_Certificate_Store::Flatfile_Certificate_Store invalid file path");
44 for(
const std::vector<uint8_t>& der : decode_all_certificates(file_stream)) {
55 m_dn_to_cert[cert.
subject_dn()].push_back(cert);
58 }
else if(!ignore_non_ca) {
63 if(m_all_subjects.empty()) {
64 throw Invalid_Argument(
"Flatfile_Certificate_Store::Flatfile_Certificate_Store cert file is empty");
69 return m_all_subjects;
73 const std::vector<uint8_t>& key_id)
const {
74 std::vector<X509_Certificate> found_certs;
76 const auto certs = m_dn_to_cert.at(subject_dn);
78 for(
const auto& cert : certs) {
79 if(key_id.empty() || key_id == cert.subject_key_id()) {
80 found_certs.push_back(cert);
83 }
catch(
const std::out_of_range&) {
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");
96 auto found_cert = m_pubkey_sha1_to_cert.find(key_hash);
98 if(found_cert != m_pubkey_sha1_to_cert.end()) {
99 return found_cert->second;
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");
111 auto found_cert = m_subject_dn_sha256_to_cert.find(subject_hash);
113 if(found_cert != m_subject_dn_sha256_to_cert.end()) {
114 return found_cert->second;
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)
const X509_DN & subject_dn() const
std::vector< uint8_t > raw_subject_dn_sha256() const
const std::vector< uint8_t > & subject_public_key_bitstring_sha1() const
bool is_self_signed() const
std::string to_string() const
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
std::vector< T > unlock(const secure_vector< T > &in)