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