Botan 3.13.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,2026 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;
25
26class DNSName;
27class EmailAddress;
28class IPv4Address;
29class IPv6Address;
30class URI;
31
32class X509_Certificate_Data;
33
34/**
35* This class represents an X.509 Certificate
36*
37* TODO(Botan4) mark this final once PKCS11_X509_Certificate is fixed
38*/
40 public:
41 /**
42 * Create a public key object associated with the public key bits in this
43 * certificate. If the public key bits was valid for X.509 encoding
44 * purposes but invalid algorithmically (for example, RSA with an even
45 * modulus) that will be detected at this point, and an exception will be
46 * thrown.
47 *
48 * @return subject public key of this certificate
49 */
50 std::unique_ptr<Public_Key> subject_public_key() const;
51
52 /**
53 * Create a public key object associated with the public key bits in this
54 * certificate. If the public key bits was valid for X.509 encoding
55 * purposes but invalid algorithmically (for example, RSA with an even
56 * modulus) that will be detected at this point, and an exception will be
57 * thrown.
58 *
59 * @return subject public key of this certificate
60 */
61 BOTAN_DEPRECATED("Use subject_public_key") std::unique_ptr<Public_Key> load_subject_public_key() const;
62
63 /**
64 * Get the public key associated with this certificate. This includes the
65 * outer AlgorithmIdentifier
66 * @return subject public key of this certificate
67 */
68 const std::vector<uint8_t>& subject_public_key_bits() const;
69
70 /**
71 * Get the SubjectPublicKeyInfo associated with this certificate.
72 * @return subject public key info of this certificate
73 */
74 const std::vector<uint8_t>& subject_public_key_info() const;
75
76 /**
77 * Return the algorithm identifier of the public key
78 */
80
81 /**
82 * Get the bit string of the public key associated with this certificate
83 * @return public key bits
84 */
85 const std::vector<uint8_t>& subject_public_key_bitstring() const;
86
87 /**
88 * Get the SHA-1 bit string of the public key associated with this certificate.
89 * This is used for OCSP among other protocols.
90 * This function will throw if SHA-1 is not available.
91 * @return hash of subject public key of this certificate
92 */
93 const std::vector<uint8_t>& subject_public_key_bitstring_sha1() const;
94
95 /**
96 * Get the SHA-256 bit string of the public key associated with this certificate.
97 * This is used for OCSP among other protocols.
98 * @return hash of subject public key of this certificate
99 */
100 std::span<const uint8_t, 32> subject_public_key_bitstring_sha256() const;
101
102 /**
103 * Get the certificate's issuer distinguished name (DN).
104 * @return issuer DN of this certificate
105 */
106 const X509_DN& issuer_dn() const;
107
108 /**
109 * Get the certificate's subject distinguished name (DN).
110 * @return subject DN of this certificate
111 */
112 const X509_DN& subject_dn() const;
113
114 /**
115 * Get a value for a specific subject_info parameter name.
116 * @param name the name of the parameter to look up.
117 * @return value(s) of the specified parameter or empty if not found
118 */
119 BOTAN_DEPRECATED("Use subject_dn and subject_alt_name to access subject names")
120 std::vector<std::string> subject_info(std::string_view name) const;
121
122 /**
123 * Get a value for a specific subject_info parameter name.
124 * @param name the name of the parameter to look up.
125 * @return value(s) of the specified parameter or empty if not found
126 */
127 BOTAN_DEPRECATED("Use issuer_dn and issuer_alt_name to access issuer names")
128 std::vector<std::string> issuer_info(std::string_view name) const;
129
130 /**
131 * Raw issuer DN bits
132 */
133 const std::vector<uint8_t>& raw_issuer_dn() const;
134
135 /**
136 * SHA-1 of Raw issuer DN
137 */
138 std::span<const uint8_t, 20> raw_issuer_dn_sha1() const;
139
140 /**
141 * SHA-256 of Raw issuer DN
142 */
143 const std::vector<uint8_t>& raw_issuer_dn_sha256() const;
144
145 /**
146 * Raw subject DN
147 */
148 const std::vector<uint8_t>& raw_subject_dn() const;
149
150 /**
151 * SHA-1 of Raw subject DN
152 */
153 std::span<const uint8_t, 20> raw_subject_dn_sha1() const;
154
155 /**
156 * SHA-256 of Raw subject DN
157 */
158 const std::vector<uint8_t>& raw_subject_dn_sha256() const;
159
160 /**
161 * SHA-1 of the entire certificate DER encoding
162 */
163 std::span<const uint8_t, 20> certificate_data_sha1() const;
164
165 /**
166 * SHA-256 of the entire certificate DER encoding
167 */
168 std::span<const uint8_t, 32> certificate_data_sha256() const;
169
170 /**
171 * Get the notBefore of the certificate as X509_Time
172 * @return notBefore of the certificate
173 */
174 const X509_Time& not_before() const;
175
176 /**
177 * Get the notAfter of the certificate as X509_Time
178 * @return notAfter of the certificate
179 */
180 const X509_Time& not_after() const;
181
182 /**
183 * Get the X509 version of this certificate object.
184 * @return X509 version
185 */
186 uint32_t x509_version() const;
187
188 /**
189 * Get the serial number of this certificate.
190 *
191 * Note this is the absolute value; the (rare, non-conforming) negative
192 * serial numbers are indistinguishable from their positive counterpart.
193 * Prefer serial() which preserves the sign.
194 *
195 * @return certificates serial number
196 */
197 const std::vector<uint8_t>& serial_number() const;
198
199 /**
200 * Get the serial number of this certificate
201 */
202 const X509_Serial_Number& serial() const;
203
204 /**
205 * Get the serial number's sign
206 * @return 1 iff the serial is negative.
207 */
208 BOTAN_DEPRECATED("Use serial().is_negative()") bool is_serial_negative() const;
209
210 /**
211 * Return true if revocation status checking of this certificate should be
212 * skipped, as indicated by the presence of either the noRevAvail extension
213 * (RFC 9608) or the ocsp-nocheck extension (RFC 6960).
214 */
215 bool skip_revocation_check() const;
216
217 /**
218 * Get the DER encoded AuthorityKeyIdentifier of this certificate.
219 * @return DER encoded AuthorityKeyIdentifier
220 */
221 const std::vector<uint8_t>& authority_key_id() const;
222
223 /**
224 * Get the DER encoded SubjectKeyIdentifier of this certificate.
225 * @return DER encoded SubjectKeyIdentifier
226 */
227 const std::vector<uint8_t>& subject_key_id() const;
228
229 /**
230 * Check whether this certificate is self signed.
231 * If the DN issuer and subject agree,
232 * @return true if this certificate is self signed
233 */
234 bool is_self_signed() const;
235
236 /**
237 * Check whether this certificate is a CA certificate.
238 * @return true if this certificate is a CA certificate
239 */
240 bool is_CA_cert() const;
241
242 /**
243 * Returns true if the specified @param usage is set in the key usage extension
244 * or if no key usage constraints are set at all.
245 * To check if a certain key constraint is set in the certificate
246 * use @see X509_Certificate#has_constraints.
247 */
248 bool allowed_usage(Key_Constraints usage) const;
249
250 /**
251 * Returns true if the specified @param usage is set in the extended key usage extension
252 * or if no extended key usage constraints are set at all.
253 * To check if a certain extended key constraint is set in the certificate
254 * use @see X509_Certificate#has_ex_constraint.
255 */
256 bool allowed_extended_usage(std::string_view usage) const;
257
258 /**
259 * Returns true if the specified usage is set in the extended key usage extension,
260 * or if no extended key usage constraints are set at all.
261 * To check if a certain extended key constraint is set in the certificate
262 * use @see X509_Certificate#has_ex_constraint.
263 */
264 bool allowed_extended_usage(const OID& usage) const;
265
266 /**
267 * Returns true if the required key and extended key constraints are set in the certificate
268 * for the specified @param usage or if no key constraints are set in both the key usage
269 * and extended key usage extension.
270 */
271 bool allowed_usage(Usage_Type usage) const;
272
273 /**
274 * Returns true if and only if the specified @param constraints are
275 * included in the key usage extension.
276 *
277 * Typically for applications you want allowed_usage instead.
278 */
280
281 /**
282 * Returns true if and only if OID @param ex_constraint is
283 * included in the extended key extension.
284 */
285 bool has_ex_constraint(std::string_view ex_constraint) const;
286
287 /**
288 * Returns true if and only if OID @param ex_constraint is
289 * included in the extended key extension.
290 */
291 bool has_ex_constraint(const OID& ex_constraint) const;
292
293 /**
294 * Get the path length constraint as defined in the BasicConstraints extension.
295 *
296 * This returns an arbitrary value if the extension is not set (either 32 for v1
297 * self-signed certificates, or else Cert_Extension::NO_CERT_PATH_LIMIT for v3
298 * certificates without the extension)
299 *
300 * Prefer path_length_constraint
301 *
302 * @return path limit
303 */
304 BOTAN_DEPRECATED("Use X509_Certificate::path_length_constraint") uint32_t path_limit() const;
305
306 /**
307 * Get the path length constraint as defined in the BasicConstraints extension.
308 *
309 * Returns nullopt if either the extension is not set in the certificate,
310 * or if the pathLenConstraint field was absent from the extension.
311 *
312 * @return path limit
313 */
314 std::optional<size_t> path_length_constraint() const;
315
316 /**
317 * Check whenever a given X509 Extension is marked critical in this
318 * certificate.
319 */
320 bool is_critical(std::string_view ex_name) const;
321
322 /**
323 * Get the key constraints as defined in the KeyUsage extension of this
324 * certificate.
325 * @return key constraints
326 */
328
329 /**
330 * Get the key usage as defined in the ExtendedKeyUsage extension
331 * of this certificate, or else an empty vector.
332 * @return key usage
333 */
334 const std::vector<OID>& extended_key_usage() const;
335
336 /**
337 * Get the name constraints as defined in the NameConstraints
338 * extension of this certificate.
339 * @return name constraints
340 */
341 const NameConstraints& name_constraints() const;
342
343 /**
344 * Get the policies as defined in the CertificatePolicies extension
345 * of this certificate.
346 * @return certificate policies
347 */
348 const std::vector<OID>& certificate_policy_oids() const;
349
350 /**
351 * Get all extensions of this certificate.
352 * @return certificate extensions
353 */
354 const Extensions& v3_extensions() const;
355
356 /**
357 * Return the v2 issuer key ID. v2 key IDs are almost never used,
358 * instead see v3_subject_key_id.
359 */
360 const std::vector<uint8_t>& v2_issuer_key_id() const;
361
362 /**
363 * Return the v2 subject key ID. v2 key IDs are almost never used,
364 * instead see v3_subject_key_id.
365 */
366 const std::vector<uint8_t>& v2_subject_key_id() const;
367
368 /**
369 * Return the subject alternative names (DNS, IP, ...)
370 */
371 const AlternativeName& subject_alt_name() const;
372
373 /**
374 * Return the issuer alternative names (DNS, IP, ...)
375 */
376 const AlternativeName& issuer_alt_name() const;
377
378 /**
379 * Return the listed address of an OCSP responder, or empty if not set
380 */
381 BOTAN_DEPRECATED("Use ocsp_responder_uris") std::string ocsp_responder() const;
382
383 /**
384 * Return the listed addresses of OCSP responders, or empty if not set
385 */
386 BOTAN_DEPRECATED("Use ocsp_responder_uris") std::vector<std::string> ocsp_responders() const;
387
388 /**
389 * Return the listed addresses of OCSP responders, or empty if not set
390 */
391 const std::vector<URI>& ocsp_responder_uris() const;
392
393 /**
394 * Return the listed addresses of ca issuers, or empty if not set
395 */
396 BOTAN_DEPRECATED("Use ca_issuer_uris") std::vector<std::string> ca_issuers() const;
397
398 /**
399 * Return the listed addresses of ca issuers, or empty if not set
400 */
401 const std::vector<URI>& ca_issuer_uris() const;
402
403 /**
404 * Return the CRL distribution point, or empty if not set
405 */
406 BOTAN_DEPRECATED("Use crl_distribution_point_uris") std::string crl_distribution_point() const;
407
408 /**
409 * Return the CRL distribution points, or empty if not set
410 */
411 BOTAN_DEPRECATED("Use crl_distribution_point_uris") std::vector<std::string> crl_distribution_points() const;
412
413 /**
414 * Return the CRL distribution points, or empty if not set
415 */
416 const std::vector<URI>& crl_distribution_point_uris() const;
417
418 /**
419 * Return all email addresses associated with the subject of this
420 * certificate, in parsed form.
421 *
422 * This combines RFC 822 names from the subjectAltName extension with
423 * email addresses carried in the subject DN's emailAddress attribute
424 * (the latter is the legacy location for subject email, see RFC 5280
425 * 4.2.1.10). DN attribute values that fail to parse as a mailbox are
426 * silently skipped.
427 */
428 std::vector<EmailAddress> subject_email_addresses() const;
429
430 /**
431 * @return a free-form string describing the certificate
432 */
433 std::string to_string() const;
434
435 /**
436 * @return a fingerprint of the certificate
437 * @param hash_name hash function used to calculate the fingerprint
438 */
439 std::string fingerprint(std::string_view hash_name = "SHA-1") const;
440
441 /**
442 * A collision resistant binary "tag" of a certificate
443 *
444 * The actual value is deliberately not exposed; a Tag can only be hashed
445 * to a size_t, or compared with another Tag. This type is intended for use
446 * as a key in std::map and std::unordered_map, or to be saved in a
447 * std::set or std::unordered_set.
448 */
449 class Tag final {
450 public:
451 static constexpr size_t TagLen = 32;
452
453 auto operator<=>(const Tag&) const = default;
454
455 size_t hash() const noexcept {
456 size_t h = 0;
457 std::memcpy(&h, m_tag.data(), sizeof(h));
458 return h;
459 }
460
461 private:
462 friend X509_Certificate;
463
464 explicit Tag(std::array<uint8_t, TagLen> tag) : m_tag(tag) {}
465
466 std::array<std::uint8_t, TagLen> m_tag;
467 };
468
469 class TagHash final {
470 public:
471 size_t operator()(const X509_Certificate::Tag& tag) const noexcept { return tag.hash(); }
472 };
473
474 /**
475 * Return a collision resistant binary "tag" of this certificate
476 */
477 Tag tag() const;
478
479 /**
480 * Check if a certain DNS name matches up with the information in
481 * the cert
482 *
483 * The string variant additionally accepts a dotted-quad IPv4 input,
484 * in which case the SAN for IPv4 addresses will be checked. Prefer
485 * the typed overloads for IP and DNS matching.
486 *
487 * @param name DNS name to match
488 */
489 BOTAN_DEPRECATED("Use the DNSName / IPv4Address / IPv6Address overload")
490 bool matches_dns_name(std::string_view name) const;
491
492 /**
493 * Check whether @p name matches the subject DNS names in this certificate.
494 *
495 * Compares against the dnsName entries in the subjectAltName, with the
496 * RFC 6125 wildcard rules. If the certificate has no SAN at all, falls
497 * back to a wildcard comparison against the subject CN.
498 */
499 bool matches_dns_name(const DNSName& name) const;
500
501 /**
502 * Check whether @p address appears as an iPAddress entry in the subjectAltName.
503 */
504 bool matches_ip(const IPv4Address& address) const;
505
506 /**
507 * Check whether @p address appears as an iPAddress entry in the subjectAltName.
508 */
509 bool matches_ip(const IPv6Address& address) const;
510
511 /**
512 * Check to certificates for equality.
513 * @return true both certificates are (binary) equal
514 */
515 bool operator==(const X509_Certificate& other) const;
516
517 /**
518 * Impose an arbitrary (but consistent) ordering, eg to allow sorting
519 * a container of certificate objects.
520 * @return true if this is less than other by some unspecified criteria
521 */
522 bool operator<(const X509_Certificate& other) const;
523
524 /**
525 * Create a certificate from a data source providing the DER or
526 * PEM encoded certificate.
527 * @param source the data source
528 */
529 explicit X509_Certificate(DataSource& source);
530
531#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
532 /**
533 * Create a certificate from a file containing the DER or PEM
534 * encoded certificate.
535 * @param filename the name of the certificate file
536 */
537 explicit X509_Certificate(std::string_view filename);
538#endif
539
540 /**
541 * Create a certificate from a buffer
542 * @param in the buffer containing the DER-encoded certificate
543 */
544 explicit X509_Certificate(std::span<const uint8_t> in);
545
546 /**
547 * Create a certificate from a buffer
548 * @param data the buffer containing the DER-encoded certificate
549 * @param length length of data in bytes
550 */
551 X509_Certificate(const uint8_t data[], size_t length) : X509_Certificate(std::span{data, length}) {}
552
553 /**
554 * Create an uninitialized certificate object. Any attempts to
555 * access this object will throw an exception.
556 */
557 X509_Certificate() = default;
558
559 X509_Certificate(const X509_Certificate& other) = default;
564
565 private:
566 std::string PEM_label() const override;
567
568 std::vector<std::string> alternate_PEM_labels() const override;
569
570 void force_decode() override;
571
572 const X509_Certificate_Data& data() const;
573
574 std::shared_ptr<const X509_Certificate_Data> m_data;
575};
576
577/**
578* Check two certificates for inequality
579* @param cert1 The first certificate
580* @param cert2 The second certificate
581* @return true if the arguments represent different certificates,
582* false if they are binary identical
583*/
584BOTAN_PUBLIC_API(2, 0) bool operator!=(const X509_Certificate& cert1, const X509_Certificate& cert2);
585
586} // namespace Botan
587
588#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:750
size_t operator()(const X509_Certificate::Tag &tag) const noexcept
Definition x509cert.h:471
auto operator<=>(const Tag &) const =default
size_t hash() const noexcept
Definition x509cert.h:455
static constexpr size_t TagLen
Definition x509cert.h:451
const std::vector< OID > & extended_key_usage() const
Definition x509cert.cpp:507
bool is_CA_cert() const
Definition x509cert.cpp:483
Key_Constraints constraints() const
Definition x509cert.cpp:503
const NameConstraints & name_constraints() const
Definition x509cert.cpp:515
X509_Certificate(const X509_Certificate &other)=default
std::vector< std::string > ocsp_responders() const
Definition x509cert.cpp:637
bool is_critical(std::string_view ex_name) const
Definition x509cert.cpp:613
const std::vector< uint8_t > & serial_number() const
Definition x509cert.cpp:440
std::string fingerprint(std::string_view hash_name="SHA-1") const
Definition x509cert.cpp:792
const std::vector< URI > & ocsp_responder_uris() const
Definition x509cert.cpp:641
const X509_DN & subject_dn() const
Definition x509cert.cpp:460
bool skip_revocation_check() const
Definition x509cert.cpp:452
X509_Certificate(const uint8_t data[], size_t length)
Definition x509cert.h:551
uint32_t path_limit() const
Definition x509cert.cpp:491
const X509_Serial_Number & serial() const
Definition x509cert.cpp:444
const X509_Time & not_after() const
Definition x509cert.cpp:392
const std::vector< uint8_t > & authority_key_id() const
Definition x509cert.cpp:432
std::span< const uint8_t, 32 > certificate_data_sha256() const
Definition x509cert.cpp:479
X509_Certificate & operator=(X509_Certificate &&other)=default
bool allowed_extended_usage(std::string_view usage) const
Definition x509cert.cpp:535
const AlternativeName & issuer_alt_name() const
Definition x509cert.cpp:692
const std::vector< uint8_t > & raw_subject_dn() const
Definition x509cert.cpp:468
X509_Certificate & operator=(const X509_Certificate &other)=default
const std::vector< uint8_t > & subject_key_id() const
Definition x509cert.cpp:436
~X509_Certificate() override
std::span< const uint8_t, 32 > subject_public_key_bitstring_sha256() const
Definition x509cert.cpp:428
const std::vector< uint8_t > & subject_public_key_bits() const
Definition x509cert.cpp:408
bool has_constraints(Key_Constraints constraints) const
Definition x509cert.cpp:523
std::optional< size_t > path_length_constraint() const
Definition x509cert.cpp:499
X509_Certificate(X509_Certificate &&other)=default
const Extensions & v3_extensions() const
Definition x509cert.cpp:519
bool has_ex_constraint(std::string_view ex_constraint) const
Definition x509cert.cpp:582
std::vector< std::string > crl_distribution_points() const
Definition x509cert.cpp:653
const std::vector< uint8_t > & subject_public_key_bitstring_sha1() const
Definition x509cert.cpp:420
bool allowed_usage(Key_Constraints usage) const
Definition x509cert.cpp:528
const X509_DN & issuer_dn() const
Definition x509cert.cpp:456
const std::vector< uint8_t > & v2_issuer_key_id() const
Definition x509cert.cpp:400
std::string ocsp_responder() const
Definition x509cert.cpp:630
std::span< const uint8_t, 20 > certificate_data_sha1() const
Definition x509cert.cpp:472
std::vector< std::string > subject_info(std::string_view name) const
Definition x509cert.cpp:744
const std::vector< uint8_t > & raw_subject_dn_sha256() const
Definition x509cert.cpp:777
const std::vector< URI > & crl_distribution_point_uris() const
Definition x509cert.cpp:657
uint32_t x509_version() const
Definition x509cert.cpp:380
const std::vector< URI > & ca_issuer_uris() const
Definition x509cert.cpp:649
std::string crl_distribution_point() const
Definition x509cert.cpp:661
const std::vector< OID > & certificate_policy_oids() const
Definition x509cert.cpp:511
std::unique_ptr< Public_Key > load_subject_public_key() const
Definition x509cert.cpp:766
X509_Certificate(DataSource &source)
Definition x509cert.cpp:96
bool is_self_signed() const
Definition x509cert.cpp:384
const std::vector< uint8_t > & raw_issuer_dn() const
Definition x509cert.cpp:464
std::span< const uint8_t, 20 > raw_subject_dn_sha1() const
Definition x509cert.cpp:788
const std::vector< uint8_t > & raw_issuer_dn_sha256() const
Definition x509cert.cpp:770
const AlgorithmIdentifier & subject_public_key_algo() const
Definition x509cert.cpp:396
const AlternativeName & subject_alt_name() const
Definition x509cert.cpp:688
std::vector< std::string > ca_issuers() const
Definition x509cert.cpp:645
const std::vector< uint8_t > & subject_public_key_info() const
Definition x509cert.cpp:412
std::vector< EmailAddress > subject_email_addresses() const
Definition x509cert.cpp:669
bool is_serial_negative() const
Definition x509cert.cpp:448
const std::vector< uint8_t > & subject_public_key_bitstring() const
Definition x509cert.cpp:416
std::unique_ptr< Public_Key > subject_public_key() const
Definition x509cert.cpp:758
const std::vector< uint8_t > & v2_subject_key_id() const
Definition x509cert.cpp:404
const X509_Time & not_before() const
Definition x509cert.cpp:388
std::vector< std::string > issuer_info(std::string_view name) const
Definition x509cert.cpp:751
std::span< const uint8_t, 20 > raw_issuer_dn_sha1() const
Definition x509cert.cpp:784
std::string to_string() const
Definition x509cert.cpp:944
X509_Object()=default
ASN1_Time X509_Time
Definition asn1_obj.h:27