Botan 3.13.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 * TODO(Botan4) change this to use X509_Serial_Number
70 */
71 virtual std::optional<X509_Certificate> find_cert_by_issuer_dn_and_serial_number(
72 const X509_DN& issuer_dn, std::span<const uint8_t> serial_number) const = 0;
73
74 /**
75 * Finds a CRL for the given certificate
76 * @param subject the subject certificate
77 * @return the CRL for subject or nullopt otherwise
78 */
79 virtual std::optional<X509_CRL> find_crl_for(const X509_Certificate& subject) const;
80
81 /**
82 * @return whether this certificate is contained within the store
83 * @param cert certificate to be searched
84 *
85 * Default implementation uses find_all_certs
86 */
87 virtual bool contains(const X509_Certificate& cert) const;
88
89 /**
90 * Old version of contains
91 */
92 bool certificate_known(const X509_Certificate& cert) const;
93
94 // remove this (used by TLS::Server)
95 virtual std::vector<X509_DN> all_subjects() const = 0;
96};
97
98/**
99* In Memory Certificate Store
100*/
102 public:
103#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
104 /**
105 * Attempt to parse all files in dir (including subdirectories)
106 * as certificates. Ignores errors.
107 */
108 explicit Certificate_Store_In_Memory(std::string_view dir);
109#endif
110
111 /**
112 * Adds given certificate to the store.
113 */
114 explicit Certificate_Store_In_Memory(const X509_Certificate& cert);
115
116 /**
117 * Adds given certificate and CRL to the store.
118 */
120
121 /**
122 * Create an empty store.
123 */
125
128
131
133
134 /**
135 * Add a certificate to the store.
136 * @param cert certificate to be added
137 */
138 void add_certificate(const X509_Certificate& cert);
139
140 /**
141 * Add a certificate revocation list (CRL) to the store.
142 * @param crl CRL to be added
143 */
144 void add_crl(const X509_CRL& crl);
145
146 /**
147 * @return DNs for all certificates managed by the store
148 */
149 std::vector<X509_DN> all_subjects() const override;
150
151 /*
152 * Find a certificate by Subject DN and (optionally) key identifier
153 * @return the first certificate that matches
154 */
155 std::optional<X509_Certificate> find_cert(const X509_DN& subject_dn,
156 const std::vector<uint8_t>& key_id) const override;
157
158 /*
159 * Find all certificates with a given Subject DN.
160 * Subject DN and even the key identifier might not be unique.
161 */
162 std::vector<X509_Certificate> find_all_certs(const X509_DN& subject_dn,
163 const std::vector<uint8_t>& key_id) const override;
164
165 std::optional<X509_Certificate> find_cert_by_pubkey_sha1(const std::vector<uint8_t>& key_hash) const override;
166
167 std::optional<X509_Certificate> find_cert_by_raw_subject_dn_sha256(
168 const std::vector<uint8_t>& subject_hash) const override;
169
170 std::optional<X509_Certificate> find_cert_by_issuer_dn_and_serial_number(
171 const X509_DN& issuer_dn, std::span<const uint8_t> serial_number) const override;
172
173 /**
174 * Finds a CRL for the given certificate
175 */
176 std::optional<X509_CRL> find_crl_for(const X509_Certificate& subject) const override;
177
178 bool contains(const X509_Certificate& cert) const override;
179
180 private:
181 class Impl;
182
183 Impl& impl();
184 const Impl& impl() const;
185
186 std::unique_ptr<Impl> m_impl;
187};
188
189} // namespace Botan
190
191#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