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