Botan 3.12.0
Crypto and TLS for C&
x509cert.h
Go to the documentation of this file.
1/*
2* X.509 Certificates
3* (C) 1999-2007,2015,2017 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_X509_CERTS_H_
9#define BOTAN_X509_CERTS_H_
10
11#include <botan/x509_obj.h>
12#include <array>
13#include <cstring>
14#include <memory>
15#include <span>
16
17namespace Botan {
18
19class AlternativeName;
20class Extensions;
21class NameConstraints;
22class Public_Key;
23class X509_DN;
24
25class X509_Certificate_Data;
26
27/**
28* This class represents an X.509 Certificate
29*
30* TODO(Botan4) mark this final once PKCS11_X509_Certificate is fixed
31*/
33 public:
34 /**
35 * Create a public key object associated with the public key bits in this
36 * certificate. If the public key bits was valid for X.509 encoding
37 * purposes but invalid algorithmically (for example, RSA with an even
38 * modulus) that will be detected at this point, and an exception will be
39 * thrown.
40 *
41 * @return subject public key of this certificate
42 */
43 std::unique_ptr<Public_Key> subject_public_key() const;
44
45 /**
46 * Create a public key object associated with the public key bits in this
47 * certificate. If the public key bits was valid for X.509 encoding
48 * purposes but invalid algorithmically (for example, RSA with an even
49 * modulus) that will be detected at this point, and an exception will be
50 * thrown.
51 *
52 * @return subject public key of this certificate
53 */
54 BOTAN_DEPRECATED("Use subject_public_key") std::unique_ptr<Public_Key> load_subject_public_key() const;
55
56 /**
57 * Get the public key associated with this certificate. This includes the
58 * outer AlgorithmIdentifier
59 * @return subject public key of this certificate
60 */
61 const std::vector<uint8_t>& subject_public_key_bits() const;
62
63 /**
64 * Get the SubjectPublicKeyInfo associated with this certificate.
65 * @return subject public key info of this certificate
66 */
67 const std::vector<uint8_t>& subject_public_key_info() const;
68
69 /**
70 * Return the algorithm identifier of the public key
71 */
73
74 /**
75 * Get the bit string of the public key associated with this certificate
76 * @return public key bits
77 */
78 const std::vector<uint8_t>& subject_public_key_bitstring() const;
79
80 /**
81 * Get the SHA-1 bit string of the public key associated with this certificate.
82 * This is used for OCSP among other protocols.
83 * This function will throw if SHA-1 is not available.
84 * @return hash of subject public key of this certificate
85 */
86 const std::vector<uint8_t>& subject_public_key_bitstring_sha1() const;
87
88 /**
89 * Get the certificate's issuer distinguished name (DN).
90 * @return issuer DN of this certificate
91 */
92 const X509_DN& issuer_dn() const;
93
94 /**
95 * Get the certificate's subject distinguished name (DN).
96 * @return subject DN of this certificate
97 */
98 const X509_DN& subject_dn() const;
99
100 /**
101 * Get a value for a specific subject_info parameter name.
102 * @param name the name of the parameter to look up. Possible names include
103 * "X509.Certificate.version", "X509.Certificate.serial",
104 * "X509.Certificate.start", "X509.Certificate.end",
105 * "X509.Certificate.v2.key_id", "X509.Certificate.public_key",
106 * "X509v3.BasicConstraints.path_constraint",
107 * "X509v3.BasicConstraints.is_ca", "X509v3.NameConstraints",
108 * "X509v3.ExtendedKeyUsage", "X509v3.CertificatePolicies",
109 * "X509v3.SubjectKeyIdentifier", "X509.Certificate.serial",
110 * "X520.CommonName", "X520.Organization", "X520.Country",
111 * "RFC822" (Email in SAN) or "PKCS9.EmailAddress" (Email in DN).
112 * @return value(s) of the specified parameter
113 */
114 std::vector<std::string> subject_info(std::string_view name) const;
115
116 /**
117 * Get a value for a specific subject_info parameter name.
118 * @param name the name of the parameter to look up. Possible names are
119 * "X509.Certificate.v2.key_id" or "X509v3.AuthorityKeyIdentifier".
120 * @return value(s) of the specified parameter
121 */
122 std::vector<std::string> issuer_info(std::string_view name) const;
123
124 /**
125 * Raw issuer DN bits
126 */
127 const std::vector<uint8_t>& raw_issuer_dn() const;
128
129 /**
130 * SHA-256 of Raw issuer DN
131 */
132 const std::vector<uint8_t>& raw_issuer_dn_sha256() const;
133
134 /**
135 * Raw subject DN
136 */
137 const std::vector<uint8_t>& raw_subject_dn() const;
138
139 /**
140 * SHA-256 of Raw subject DN
141 */
142 const std::vector<uint8_t>& raw_subject_dn_sha256() const;
143
144 /**
145 * SHA-1 of the entire certificate DER encoding
146 */
147 std::span<const uint8_t, 20> certificate_data_sha1() const;
148
149 /**
150 * SHA-256 of the entire certificate DER encoding
151 */
152 std::span<const uint8_t, 32> certificate_data_sha256() const;
153
154 /**
155 * Get the notBefore of the certificate as X509_Time
156 * @return notBefore of the certificate
157 */
158 const X509_Time& not_before() const;
159
160 /**
161 * Get the notAfter of the certificate as X509_Time
162 * @return notAfter of the certificate
163 */
164 const X509_Time& not_after() const;
165
166 /**
167 * Get the X509 version of this certificate object.
168 * @return X509 version
169 */
170 uint32_t x509_version() const;
171
172 /**
173 * Get the serial number of this certificate.
174 * @return certificates serial number
175 */
176 const std::vector<uint8_t>& serial_number() const;
177
178 /**
179 * Get the serial number's sign
180 * @return 1 iff the serial is negative.
181 */
182 bool is_serial_negative() const;
183
184 /**
185 * Get the DER encoded AuthorityKeyIdentifier of this certificate.
186 * @return DER encoded AuthorityKeyIdentifier
187 */
188 const std::vector<uint8_t>& authority_key_id() const;
189
190 /**
191 * Get the DER encoded SubjectKeyIdentifier of this certificate.
192 * @return DER encoded SubjectKeyIdentifier
193 */
194 const std::vector<uint8_t>& subject_key_id() const;
195
196 /**
197 * Check whether this certificate is self signed.
198 * If the DN issuer and subject agree,
199 * @return true if this certificate is self signed
200 */
201 bool is_self_signed() const;
202
203 /**
204 * Check whether this certificate is a CA certificate.
205 * @return true if this certificate is a CA certificate
206 */
207 bool is_CA_cert() const;
208
209 /**
210 * Returns true if the specified @param usage is set in the key usage extension
211 * or if no key usage constraints are set at all.
212 * To check if a certain key constraint is set in the certificate
213 * use @see X509_Certificate#has_constraints.
214 */
215 bool allowed_usage(Key_Constraints usage) const;
216
217 /**
218 * Returns true if the specified @param usage is set in the extended key usage extension
219 * or if no extended key usage constraints are set at all.
220 * To check if a certain extended key constraint is set in the certificate
221 * use @see X509_Certificate#has_ex_constraint.
222 */
223 bool allowed_extended_usage(std::string_view usage) const;
224
225 /**
226 * Returns true if the specified usage is set in the extended key usage extension,
227 * or if no extended key usage constraints are set at all.
228 * To check if a certain extended key constraint is set in the certificate
229 * use @see X509_Certificate#has_ex_constraint.
230 */
231 bool allowed_extended_usage(const OID& usage) const;
232
233 /**
234 * Returns true if the required key and extended key constraints are set in the certificate
235 * for the specified @param usage or if no key constraints are set in both the key usage
236 * and extended key usage extension.
237 */
238 bool allowed_usage(Usage_Type usage) const;
239
240 /**
241 * Returns true if and only if the specified @param constraints are
242 * included in the key usage extension.
243 *
244 * Typically for applications you want allowed_usage instead.
245 */
247
248 /**
249 * Returns true if and only if OID @param ex_constraint is
250 * included in the extended key extension.
251 */
252 bool has_ex_constraint(std::string_view ex_constraint) const;
253
254 /**
255 * Returns true if and only if OID @param ex_constraint is
256 * included in the extended key extension.
257 */
258 bool has_ex_constraint(const OID& ex_constraint) const;
259
260 /**
261 * Get the path length constraint as defined in the BasicConstraints extension.
262 *
263 * This returns an arbitrary value if the extension is not set (either 32 for v1
264 * self-signed certificates, or else Cert_Extension::NO_CERT_PATH_LIMIT for v3
265 * certificates without the extension)
266 *
267 * Prefer path_length_constraint
268 *
269 * @return path limit
270 */
271 BOTAN_DEPRECATED("Use X509_Certificate::path_length_constraint") uint32_t path_limit() const;
272
273 /**
274 * Get the path length constraint as defined in the BasicConstraints extension.
275 *
276 * Returns nullopt if either the extension is not set in the certificate,
277 * or if the pathLenConstraint field was absent from the extension.
278 *
279 * @return path limit
280 */
281 std::optional<size_t> path_length_constraint() const;
282
283 /**
284 * Check whenever a given X509 Extension is marked critical in this
285 * certificate.
286 */
287 bool is_critical(std::string_view ex_name) const;
288
289 /**
290 * Get the key constraints as defined in the KeyUsage extension of this
291 * certificate.
292 * @return key constraints
293 */
295
296 /**
297 * Get the key usage as defined in the ExtendedKeyUsage extension
298 * of this certificate, or else an empty vector.
299 * @return key usage
300 */
301 const std::vector<OID>& extended_key_usage() const;
302
303 /**
304 * Get the name constraints as defined in the NameConstraints
305 * extension of this certificate.
306 * @return name constraints
307 */
308 const NameConstraints& name_constraints() const;
309
310 /**
311 * Get the policies as defined in the CertificatePolicies extension
312 * of this certificate.
313 * @return certificate policies
314 */
315 const std::vector<OID>& certificate_policy_oids() const;
316
317 /**
318 * Get all extensions of this certificate.
319 * @return certificate extensions
320 */
321 const Extensions& v3_extensions() const;
322
323 /**
324 * Return the v2 issuer key ID. v2 key IDs are almost never used,
325 * instead see v3_subject_key_id.
326 */
327 const std::vector<uint8_t>& v2_issuer_key_id() const;
328
329 /**
330 * Return the v2 subject key ID. v2 key IDs are almost never used,
331 * instead see v3_subject_key_id.
332 */
333 const std::vector<uint8_t>& v2_subject_key_id() const;
334
335 /**
336 * Return the subject alternative names (DNS, IP, ...)
337 */
338 const AlternativeName& subject_alt_name() const;
339
340 /**
341 * Return the issuer alternative names (DNS, IP, ...)
342 */
343 const AlternativeName& issuer_alt_name() const;
344
345 /**
346 * Return the listed address of an OCSP responder, or empty if not set
347 */
348 BOTAN_DEPRECATED("Use ocsp_responders") std::string ocsp_responder() const;
349
350 /**
351 * Return the listed addresses of OCSP responders, or empty if not set
352 */
353 const std::vector<std::string>& ocsp_responders() const;
354
355 /**
356 * Return the listed addresses of ca issuers, or empty if not set
357 */
358 std::vector<std::string> ca_issuers() const;
359
360 /**
361 * Return the CRL distribution point, or empty if not set
362 */
363 BOTAN_DEPRECATED("Use crl_distribution_points") std::string crl_distribution_point() const;
364
365 /**
366 * Return the CRL distribution points, or empty if not set
367 */
368 std::vector<std::string> crl_distribution_points() const;
369
370 /**
371 * @return a free-form string describing the certificate
372 */
373 std::string to_string() const;
374
375 /**
376 * @return a fingerprint of the certificate
377 * @param hash_name hash function used to calculate the fingerprint
378 */
379 std::string fingerprint(std::string_view hash_name = "SHA-1") const;
380
381 /**
382 * A collision resistant binary "tag" of a certificate
383 *
384 * The actual value is deliberately not exposed; a Tag can only be hashed
385 * to a size_t, or compared with another Tag. This type is intended for use
386 * as a key in std::map and std::unordered_map, or to be saved in a
387 * std::set or std::unordered_set.
388 */
389 class Tag final {
390 public:
391 static constexpr size_t TagLen = 32;
392
393 auto operator<=>(const Tag&) const = default;
394
395 size_t hash() const noexcept {
396 size_t h = 0;
397 std::memcpy(&h, m_tag.data(), sizeof(h));
398 return h;
399 }
400
401 private:
402 friend X509_Certificate;
403
404 explicit Tag(std::array<uint8_t, TagLen> tag) : m_tag(tag) {}
405
406 std::array<std::uint8_t, TagLen> m_tag;
407 };
408
409 class TagHash final {
410 public:
411 size_t operator()(const X509_Certificate::Tag& tag) const noexcept { return tag.hash(); }
412 };
413
414 /**
415 * Return a collision resistant binary "tag" of this certificate
416 */
417 Tag tag() const;
418
419 /**
420 * Check if a certain DNS name matches up with the information in
421 * the cert
422 * @param name DNS name to match
423 *
424 * Note: this will also accept a dotted quad input, in which case
425 * the SAN for IPv4 addresses will be checked.
426 */
427 bool matches_dns_name(std::string_view name) const;
428
429 /**
430 * Check to certificates for equality.
431 * @return true both certificates are (binary) equal
432 */
433 bool operator==(const X509_Certificate& other) const;
434
435 /**
436 * Impose an arbitrary (but consistent) ordering, eg to allow sorting
437 * a container of certificate objects.
438 * @return true if this is less than other by some unspecified criteria
439 */
440 bool operator<(const X509_Certificate& other) const;
441
442 /**
443 * Create a certificate from a data source providing the DER or
444 * PEM encoded certificate.
445 * @param source the data source
446 */
447 explicit X509_Certificate(DataSource& source);
448
449#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
450 /**
451 * Create a certificate from a file containing the DER or PEM
452 * encoded certificate.
453 * @param filename the name of the certificate file
454 */
455 explicit X509_Certificate(std::string_view filename);
456#endif
457
458 /**
459 * Create a certificate from a buffer
460 * @param in the buffer containing the DER-encoded certificate
461 */
462 explicit X509_Certificate(std::span<const uint8_t> in);
463
464 /**
465 * Create a certificate from a buffer
466 * @param data the buffer containing the DER-encoded certificate
467 * @param length length of data in bytes
468 */
469 X509_Certificate(const uint8_t data[], size_t length) : X509_Certificate(std::span{data, length}) {}
470
471 /**
472 * Create an uninitialized certificate object. Any attempts to
473 * access this object will throw an exception.
474 */
475 X509_Certificate() = default;
476
477 X509_Certificate(const X509_Certificate& other) = default;
482
483 private:
484 std::string PEM_label() const override;
485
486 std::vector<std::string> alternate_PEM_labels() const override;
487
488 void force_decode() override;
489
490 const X509_Certificate_Data& data() const;
491
492 std::shared_ptr<const X509_Certificate_Data> m_data;
493};
494
495/**
496* Check two certificates for inequality
497* @param cert1 The first certificate
498* @param cert2 The second certificate
499* @return true if the arguments represent different certificates,
500* false if they are binary identical
501*/
502BOTAN_PUBLIC_API(2, 0) bool operator!=(const X509_Certificate& cert1, const X509_Certificate& cert2);
503
504} // namespace Botan
505
506#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
Name Constraints.
Definition pkix_types.h:431
size_t operator()(const X509_Certificate::Tag &tag) const noexcept
Definition x509cert.h:411
auto operator<=>(const Tag &) const =default
size_t hash() const noexcept
Definition x509cert.h:395
static constexpr size_t TagLen
Definition x509cert.h:391
const std::vector< OID > & extended_key_usage() const
Definition x509cert.cpp:465
bool is_CA_cert() const
Definition x509cert.cpp:441
Key_Constraints constraints() const
Definition x509cert.cpp:461
const NameConstraints & name_constraints() const
Definition x509cert.cpp:473
X509_Certificate(const X509_Certificate &other)=default
bool is_critical(std::string_view ex_name) const
Definition x509cert.cpp:571
const std::vector< uint8_t > & serial_number() const
Definition x509cert.cpp:406
std::string fingerprint(std::string_view hash_name="SHA-1") const
Definition x509cert.cpp:685
const X509_DN & subject_dn() const
Definition x509cert.cpp:418
X509_Certificate(const uint8_t data[], size_t length)
Definition x509cert.h:469
uint32_t path_limit() const
Definition x509cert.cpp:449
const X509_Time & not_after() const
Definition x509cert.cpp:362
const std::vector< uint8_t > & authority_key_id() const
Definition x509cert.cpp:398
std::span< const uint8_t, 32 > certificate_data_sha256() const
Definition x509cert.cpp:437
X509_Certificate & operator=(X509_Certificate &&other)=default
bool allowed_extended_usage(std::string_view usage) const
Definition x509cert.cpp:493
const AlternativeName & issuer_alt_name() const
Definition x509cert.cpp:606
const std::vector< uint8_t > & raw_subject_dn() const
Definition x509cert.cpp:426
X509_Certificate & operator=(const X509_Certificate &other)=default
const std::vector< uint8_t > & subject_key_id() const
Definition x509cert.cpp:402
~X509_Certificate() override
const std::vector< uint8_t > & subject_public_key_bits() const
Definition x509cert.cpp:378
bool has_constraints(Key_Constraints constraints) const
Definition x509cert.cpp:481
std::optional< size_t > path_length_constraint() const
Definition x509cert.cpp:457
X509_Certificate(X509_Certificate &&other)=default
const Extensions & v3_extensions() const
Definition x509cert.cpp:477
bool has_ex_constraint(std::string_view ex_constraint) const
Definition x509cert.cpp:540
std::vector< std::string > crl_distribution_points() const
Definition x509cert.cpp:590
const std::vector< uint8_t > & subject_public_key_bitstring_sha1() const
Definition x509cert.cpp:390
bool allowed_usage(Key_Constraints usage) const
Definition x509cert.cpp:486
const X509_DN & issuer_dn() const
Definition x509cert.cpp:414
const std::vector< uint8_t > & v2_issuer_key_id() const
Definition x509cert.cpp:370
std::string ocsp_responder() const
Definition x509cert.cpp:575
std::span< const uint8_t, 20 > certificate_data_sha1() const
Definition x509cert.cpp:430
std::vector< std::string > subject_info(std::string_view name) const
Definition x509cert.cpp:645
const std::vector< uint8_t > & raw_subject_dn_sha256() const
Definition x509cert.cpp:678
uint32_t x509_version() const
Definition x509cert.cpp:350
std::string crl_distribution_point() const
Definition x509cert.cpp:594
const std::vector< OID > & certificate_policy_oids() const
Definition x509cert.cpp:469
std::unique_ptr< Public_Key > load_subject_public_key() const
Definition x509cert.cpp:667
X509_Certificate(DataSource &source)
Definition x509cert.cpp:86
bool is_self_signed() const
Definition x509cert.cpp:354
const std::vector< uint8_t > & raw_issuer_dn() const
Definition x509cert.cpp:422
const std::vector< uint8_t > & raw_issuer_dn_sha256() const
Definition x509cert.cpp:671
const AlgorithmIdentifier & subject_public_key_algo() const
Definition x509cert.cpp:366
const std::vector< std::string > & ocsp_responders() const
Definition x509cert.cpp:582
const AlternativeName & subject_alt_name() const
Definition x509cert.cpp:602
std::vector< std::string > ca_issuers() const
Definition x509cert.cpp:586
const std::vector< uint8_t > & subject_public_key_info() const
Definition x509cert.cpp:382
bool is_serial_negative() const
Definition x509cert.cpp:410
const std::vector< uint8_t > & subject_public_key_bitstring() const
Definition x509cert.cpp:386
std::unique_ptr< Public_Key > subject_public_key() const
Definition x509cert.cpp:659
const std::vector< uint8_t > & v2_subject_key_id() const
Definition x509cert.cpp:374
const X509_Time & not_before() const
Definition x509cert.cpp:358
std::vector< std::string > issuer_info(std::string_view name) const
Definition x509cert.cpp:652
std::string to_string() const
Definition x509cert.cpp:764
X509_Object()=default
ASN1_Time X509_Time
Definition asn1_obj.h:23
bool operator<(const OID &a, const OID &b)
Definition asn1_oid.cpp:173
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:53