Botan 3.10.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/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 the certificate is known
78 * @param cert certififcate to be searched
79 */
80 bool certificate_known(const X509_Certificate& cert) const {
81 return find_cert(cert.subject_dn(), cert.subject_key_id()).has_value();
82 }
83
84 // remove this (used by TLS::Server)
85 virtual std::vector<X509_DN> all_subjects() const = 0;
86};
87
88/**
89* In Memory Certificate Store
90*/
92 public:
93#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
94 /**
95 * Attempt to parse all files in dir (including subdirectories)
96 * as certificates. Ignores errors.
97 */
98 explicit Certificate_Store_In_Memory(std::string_view dir);
99#endif
100
101 /**
102 * Adds given certificate to the store.
103 */
104 explicit Certificate_Store_In_Memory(const X509_Certificate& cert);
105
106 /**
107 * Adds given certificate and CRL to the store.
108 */
110
111 /**
112 * Create an empty store.
113 */
115
116 /**
117 * Add a certificate to the store.
118 * @param cert certificate to be added
119 */
120 void add_certificate(const X509_Certificate& cert);
121
122 /**
123 * Add a certificate revocation list (CRL) to the store.
124 * @param crl CRL to be added
125 */
126 void add_crl(const X509_CRL& crl);
127
128 /**
129 * @return DNs for all certificates managed by the store
130 */
131 std::vector<X509_DN> all_subjects() const override;
132
133 /*
134 * Find a certificate by Subject DN and (optionally) key identifier
135 * @return the first certificate that matches
136 */
137 std::optional<X509_Certificate> find_cert(const X509_DN& subject_dn,
138 const std::vector<uint8_t>& key_id) const override;
139
140 /*
141 * Find all certificates with a given Subject DN.
142 * Subject DN and even the key identifier might not be unique.
143 */
144 std::vector<X509_Certificate> find_all_certs(const X509_DN& subject_dn,
145 const std::vector<uint8_t>& key_id) const override;
146
147 std::optional<X509_Certificate> find_cert_by_pubkey_sha1(const std::vector<uint8_t>& key_hash) const override;
148
149 std::optional<X509_Certificate> find_cert_by_raw_subject_dn_sha256(
150 const std::vector<uint8_t>& subject_hash) const override;
151
152 std::optional<X509_Certificate> find_cert_by_issuer_dn_and_serial_number(
153 const X509_DN& issuer_dn, std::span<const uint8_t> serial_number) const override;
154
155 /**
156 * Finds a CRL for the given certificate
157 */
158 std::optional<X509_CRL> find_crl_for(const X509_Certificate& subject) const override;
159
160 private:
161 // TODO: Add indexing on the DN and key id to avoid linear search
162 std::vector<X509_Certificate> m_certs;
163 std::vector<X509_CRL> m_crls;
164};
165
166} // namespace Botan
167
168#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
Certificate_Store_In_Memory(const X509_Certificate &cert)
Definition certstor.cpp:182
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:133
std::vector< X509_Certificate > find_all_certs(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
Definition certstor.cpp:76
std::optional< X509_Certificate > find_cert_by_pubkey_sha1(const std::vector< uint8_t > &key_hash) const override
Definition certstor.cpp:97
std::optional< X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
Definition certstor.cpp:56
void add_crl(const X509_CRL &crl)
Definition certstor.cpp:144
std::optional< X509_Certificate > find_cert_by_raw_subject_dn_sha256(const std::vector< uint8_t > &subject_hash) const override
Definition certstor.cpp:115
std::optional< X509_CRL > find_crl_for(const X509_Certificate &subject) const override
Definition certstor.cpp:161
void add_certificate(const X509_Certificate &cert)
Definition certstor.cpp:37
std::vector< X509_DN > all_subjects() const override
Definition certstor.cpp:47
virtual std::optional< X509_CRL > find_crl_for(const X509_Certificate &subject) const
Definition certstor.cpp:33
bool certificate_known(const X509_Certificate &cert) const
Definition certstor.h:80
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:21
const X509_DN & subject_dn() const
Definition x509cert.cpp:411
const std::vector< uint8_t > & subject_key_id() const
Definition x509cert.cpp:395