Botan 3.12.0
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/pkix_types.h>
12#include <botan/x509_crl.h>
13#include <botan/x509cert.h>
14#include <memory>
15#include <optional>
16#include <vector>
17
18namespace Botan {
19
20/**
21* Certificate Store Interface
22*/
23class BOTAN_PUBLIC_API(2, 0) Certificate_Store /* NOLINT(*-special-member-functions) */ {
24 public:
26
27 /**
28 * Find a certificate by Subject DN and (optionally) key identifier
29 * @param subject_dn the subject's distinguished name
30 * @param key_id an optional key id
31 * @return a matching certificate or nullopt otherwise
32 * If more than one certificate in the certificate store matches, then
33 * a single value is selected arbitrarily.
34 */
35 virtual std::optional<X509_Certificate> find_cert(const X509_DN& subject_dn,
36 const std::vector<uint8_t>& key_id) const;
37
38 /**
39 * Find all certificates with a given Subject DN.
40 * Subject DN and even the key identifier might not be unique.
41 */
42 virtual std::vector<X509_Certificate> find_all_certs(const X509_DN& subject_dn,
43 const std::vector<uint8_t>& key_id) const = 0;
44
45 /**
46 * Find a certificate by searching for one with a matching SHA-1 hash of
47 * public key. Used for OCSP.
48 * @param key_hash SHA-1 hash of the subject's public key
49 * @return a matching certificate or nullopt otherwise
50 */
51 virtual std::optional<X509_Certificate> find_cert_by_pubkey_sha1(const std::vector<uint8_t>& key_hash) const = 0;
52
53 /**
54 * Find a certificate by searching for one with a matching SHA-256 hash of
55 * raw subject name. Used for OCSP.
56 * @param subject_hash SHA-256 hash of the subject's raw name
57 * @return a matching certificate or nullopt otherwise
58 */
59 virtual std::optional<X509_Certificate> find_cert_by_raw_subject_dn_sha256(
60 const std::vector<uint8_t>& subject_hash) const = 0;
61
62 /**
63 * Find a certificate by searching for one with a matching issuer DN and
64 * serial number. Used for CMS or PKCS#7.
65 * @param issuer_dn the distinguished name of the issuer
66 * @param serial_number the certificate's serial number
67 * @return a matching certificate or nullopt otherwise
68 */
69 virtual std::optional<X509_Certificate> find_cert_by_issuer_dn_and_serial_number(
70 const X509_DN& issuer_dn, std::span<const uint8_t> serial_number) const = 0;
71
72 /**
73 * Finds a CRL for the given certificate
74 * @param subject the subject certificate
75 * @return the CRL for subject or nullopt otherwise
76 */
77 virtual std::optional<X509_CRL> find_crl_for(const X509_Certificate& subject) const;
78
79 /**
80 * @return whether this certificate is contained within the store
81 * @param cert certificate to be searched
82 *
83 * Default implementation uses find_all_certs
84 */
85 virtual bool contains(const X509_Certificate& cert) const;
86
87 /**
88 * Old version of contains
89 */
90 bool certificate_known(const X509_Certificate& cert) const;
91
92 // remove this (used by TLS::Server)
93 virtual std::vector<X509_DN> all_subjects() const = 0;
94};
95
96/**
97* In Memory Certificate Store
98*/
100 public:
101#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
102 /**
103 * Attempt to parse all files in dir (including subdirectories)
104 * as certificates. Ignores errors.
105 */
106 explicit Certificate_Store_In_Memory(std::string_view dir);
107#endif
108
109 /**
110 * Adds given certificate to the store.
111 */
112 explicit Certificate_Store_In_Memory(const X509_Certificate& cert);
113
114 /**
115 * Adds given certificate and CRL to the store.
116 */
118
119 /**
120 * Create an empty store.
121 */
123
126
129
131
132 /**
133 * Add a certificate to the store.
134 * @param cert certificate to be added
135 */
136 void add_certificate(const X509_Certificate& cert);
137
138 /**
139 * Add a certificate revocation list (CRL) to the store.
140 * @param crl CRL to be added
141 */
142 void add_crl(const X509_CRL& crl);
143
144 /**
145 * @return DNs for all certificates managed by the store
146 */
147 std::vector<X509_DN> all_subjects() const override;
148
149 /*
150 * Find a certificate by Subject DN and (optionally) key identifier
151 * @return the first certificate that matches
152 */
153 std::optional<X509_Certificate> find_cert(const X509_DN& subject_dn,
154 const std::vector<uint8_t>& key_id) const override;
155
156 /*
157 * Find all certificates with a given Subject DN.
158 * Subject DN and even the key identifier might not be unique.
159 */
160 std::vector<X509_Certificate> find_all_certs(const X509_DN& subject_dn,
161 const std::vector<uint8_t>& key_id) const override;
162
163 std::optional<X509_Certificate> find_cert_by_pubkey_sha1(const std::vector<uint8_t>& key_hash) const override;
164
165 std::optional<X509_Certificate> find_cert_by_raw_subject_dn_sha256(
166 const std::vector<uint8_t>& subject_hash) const override;
167
168 std::optional<X509_Certificate> find_cert_by_issuer_dn_and_serial_number(
169 const X509_DN& issuer_dn, std::span<const uint8_t> serial_number) const override;
170
171 /**
172 * Finds a CRL for the given certificate
173 */
174 std::optional<X509_CRL> find_crl_for(const X509_Certificate& subject) const override;
175
176 bool contains(const X509_Certificate& cert) const override;
177
178 private:
179 class Impl;
180
181 Impl& impl();
182 const Impl& impl() const;
183
184 std::unique_ptr<Impl> m_impl;
185};
186
187} // namespace Botan
188
189#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
Certificate_Store_In_Memory(const X509_Certificate &cert)
Definition certstor.cpp:246
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:188
std::vector< X509_Certificate > find_all_certs(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
Definition certstor.cpp:131
std::optional< X509_Certificate > find_cert_by_pubkey_sha1(const std::vector< uint8_t > &key_hash) const override
Definition certstor.cpp:158
Certificate_Store_In_Memory(Certificate_Store_In_Memory &&other) noexcept
std::optional< X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
Definition certstor.cpp:106
Certificate_Store_In_Memory & operator=(Certificate_Store_In_Memory &&other) noexcept
Certificate_Store_In_Memory & operator=(const Certificate_Store_In_Memory &other)=delete
void add_crl(const X509_CRL &crl)
Definition certstor.cpp:199
std::optional< X509_Certificate > find_cert_by_raw_subject_dn_sha256(const std::vector< uint8_t > &subject_hash) const override
Definition certstor.cpp:173
std::optional< X509_CRL > find_crl_for(const X509_Certificate &subject) const override
Definition certstor.cpp:219
bool contains(const X509_Certificate &cert) const override
Definition certstor.cpp:242
void add_certificate(const X509_Certificate &cert)
Definition certstor.cpp:85
std::vector< X509_DN > all_subjects() const override
Definition certstor.cpp:96
virtual std::optional< X509_CRL > find_crl_for(const X509_Certificate &subject) const
Definition certstor.cpp:50
bool certificate_known(const X509_Certificate &cert) const
Definition certstor.cpp:24
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 bool contains(const X509_Certificate &cert) const
Definition certstor.cpp:28
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:38