Botan 3.11.1
Crypto and TLS for C&
certstor.h
Go to the documentation of this file.
1/*
2* Certificate Store
3* (C) 1999-2010,2013 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_CERT_STORE_H_
9#define BOTAN_CERT_STORE_H_
10
11#include <botan/x509_crl.h>
12#include <botan/x509cert.h>
13#include <optional>
14
15namespace Botan {
16
17/**
18* Certificate Store Interface
19*/
20class BOTAN_PUBLIC_API(2, 0) Certificate_Store /* NOLINT(*-special-member-functions) */ {
21 public:
23
24 /**
25 * Find a certificate by Subject DN and (optionally) key identifier
26 * @param subject_dn the subject's distinguished name
27 * @param key_id an optional key id
28 * @return a matching certificate or nullopt otherwise
29 * If more than one certificate in the certificate store matches, then
30 * a single value is selected arbitrarily.
31 */
32 virtual std::optional<X509_Certificate> find_cert(const X509_DN& subject_dn,
33 const std::vector<uint8_t>& key_id) const;
34
35 /**
36 * Find all certificates with a given Subject DN.
37 * Subject DN and even the key identifier might not be unique.
38 */
39 virtual std::vector<X509_Certificate> find_all_certs(const X509_DN& subject_dn,
40 const std::vector<uint8_t>& key_id) const = 0;
41
42 /**
43 * Find a certificate by searching for one with a matching SHA-1 hash of
44 * public key. Used for OCSP.
45 * @param key_hash SHA-1 hash of the subject's public key
46 * @return a matching certificate or nullopt otherwise
47 */
48 virtual std::optional<X509_Certificate> find_cert_by_pubkey_sha1(const std::vector<uint8_t>& key_hash) const = 0;
49
50 /**
51 * Find a certificate by searching for one with a matching SHA-256 hash of
52 * raw subject name. Used for OCSP.
53 * @param subject_hash SHA-256 hash of the subject's raw name
54 * @return a matching certificate or nullopt otherwise
55 */
56 virtual std::optional<X509_Certificate> find_cert_by_raw_subject_dn_sha256(
57 const std::vector<uint8_t>& subject_hash) const = 0;
58
59 /**
60 * Find a certificate by searching for one with a matching issuer DN and
61 * serial number. Used for CMS or PKCS#7.
62 * @param issuer_dn the distinguished name of the issuer
63 * @param serial_number the certificate's serial number
64 * @return a matching certificate or nullopt otherwise
65 */
66 virtual std::optional<X509_Certificate> find_cert_by_issuer_dn_and_serial_number(
67 const X509_DN& issuer_dn, std::span<const uint8_t> serial_number) const = 0;
68
69 /**
70 * Finds a CRL for the given certificate
71 * @param subject the subject certificate
72 * @return the CRL for subject or nullopt otherwise
73 */
74 virtual std::optional<X509_CRL> find_crl_for(const X509_Certificate& subject) const;
75
76 /**
77 * @return whether this certificate is contained within the store
78 * @param cert certificate to be searched
79 */
80 bool certificate_known(const X509_Certificate& cert) const;
81
82 // remove this (used by TLS::Server)
83 virtual std::vector<X509_DN> all_subjects() const = 0;
84};
85
86/**
87* In Memory Certificate Store
88*/
90 public:
91#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
92 /**
93 * Attempt to parse all files in dir (including subdirectories)
94 * as certificates. Ignores errors.
95 */
96 explicit Certificate_Store_In_Memory(std::string_view dir);
97#endif
98
99 /**
100 * Adds given certificate to the store.
101 */
102 explicit Certificate_Store_In_Memory(const X509_Certificate& cert);
103
104 /**
105 * Adds given certificate and CRL to the store.
106 */
108
109 /**
110 * Create an empty store.
111 */
113
114 /**
115 * Add a certificate to the store.
116 * @param cert certificate to be added
117 */
118 void add_certificate(const X509_Certificate& cert);
119
120 /**
121 * Add a certificate revocation list (CRL) to the store.
122 * @param crl CRL to be added
123 */
124 void add_crl(const X509_CRL& crl);
125
126 /**
127 * @return DNs for all certificates managed by the store
128 */
129 std::vector<X509_DN> all_subjects() const override;
130
131 /*
132 * Find a certificate by Subject DN and (optionally) key identifier
133 * @return the first certificate that matches
134 */
135 std::optional<X509_Certificate> find_cert(const X509_DN& subject_dn,
136 const std::vector<uint8_t>& key_id) const override;
137
138 /*
139 * Find all certificates with a given Subject DN.
140 * Subject DN and even the key identifier might not be unique.
141 */
142 std::vector<X509_Certificate> find_all_certs(const X509_DN& subject_dn,
143 const std::vector<uint8_t>& key_id) const override;
144
145 std::optional<X509_Certificate> find_cert_by_pubkey_sha1(const std::vector<uint8_t>& key_hash) const override;
146
147 std::optional<X509_Certificate> find_cert_by_raw_subject_dn_sha256(
148 const std::vector<uint8_t>& subject_hash) const override;
149
150 std::optional<X509_Certificate> find_cert_by_issuer_dn_and_serial_number(
151 const X509_DN& issuer_dn, std::span<const uint8_t> serial_number) const override;
152
153 /**
154 * Finds a CRL for the given certificate
155 */
156 std::optional<X509_CRL> find_crl_for(const X509_Certificate& subject) const override;
157
158 private:
159 // TODO: Add indexing on the DN and key id to avoid linear search
160 std::vector<X509_Certificate> m_certs;
161 std::vector<X509_CRL> m_crls;
162};
163
164} // namespace Botan
165
166#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
Certificate_Store_In_Memory(const X509_Certificate &cert)
Definition certstor.cpp:193
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
Definition certstor.cpp:144
std::vector< X509_Certificate > find_all_certs(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
Definition certstor.cpp:87
std::optional< X509_Certificate > find_cert_by_pubkey_sha1(const std::vector< uint8_t > &key_hash) const override
Definition certstor.cpp:108
std::optional< X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
Definition certstor.cpp:67
void add_crl(const X509_CRL &crl)
Definition certstor.cpp:155
std::optional< X509_Certificate > find_cert_by_raw_subject_dn_sha256(const std::vector< uint8_t > &subject_hash) const override
Definition certstor.cpp:126
std::optional< X509_CRL > find_crl_for(const X509_Certificate &subject) const override
Definition certstor.cpp:172
void add_certificate(const X509_Certificate &cert)
Definition certstor.cpp:48
std::vector< X509_DN > all_subjects() const override
Definition certstor.cpp:58
virtual std::optional< X509_CRL > find_crl_for(const X509_Certificate &subject) const
Definition certstor.cpp:44
bool certificate_known(const X509_Certificate &cert) const
Definition certstor.cpp:22
virtual std::vector< X509_DN > all_subjects() const =0
virtual std::optional< X509_Certificate > find_cert_by_raw_subject_dn_sha256(const std::vector< uint8_t > &subject_hash) const =0
virtual 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 =0
virtual std::optional< X509_Certificate > find_cert_by_pubkey_sha1(const std::vector< uint8_t > &key_hash) const =0
virtual std::vector< X509_Certificate > find_all_certs(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const =0
virtual std::optional< X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const
Definition certstor.cpp:32