Mbed TLS v3.6.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
x509.h
Go to the documentation of this file.
1 
6 /*
7  * Copyright The Mbed TLS Contributors
8  * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  */
10 #ifndef MBEDTLS_X509_H
11 #define MBEDTLS_X509_H
12 #include "mbedtls/private_access.h"
13 
14 #include "mbedtls/build_info.h"
15 
16 #include "mbedtls/asn1.h"
17 #include "mbedtls/pk.h"
18 
19 #if defined(MBEDTLS_RSA_C)
20 #include "mbedtls/rsa.h"
21 #endif
22 
28 #if !defined(MBEDTLS_X509_MAX_INTERMEDIATE_CA)
29 
37 #define MBEDTLS_X509_MAX_INTERMEDIATE_CA 8
38 #endif
39 
45 #define MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE -0x2080
46 
47 #define MBEDTLS_ERR_X509_UNKNOWN_OID -0x2100
48 
49 #define MBEDTLS_ERR_X509_INVALID_FORMAT -0x2180
50 
51 #define MBEDTLS_ERR_X509_INVALID_VERSION -0x2200
52 
53 #define MBEDTLS_ERR_X509_INVALID_SERIAL -0x2280
54 
55 #define MBEDTLS_ERR_X509_INVALID_ALG -0x2300
56 
57 #define MBEDTLS_ERR_X509_INVALID_NAME -0x2380
58 
59 #define MBEDTLS_ERR_X509_INVALID_DATE -0x2400
60 
61 #define MBEDTLS_ERR_X509_INVALID_SIGNATURE -0x2480
62 
63 #define MBEDTLS_ERR_X509_INVALID_EXTENSIONS -0x2500
64 
65 #define MBEDTLS_ERR_X509_UNKNOWN_VERSION -0x2580
66 
67 #define MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG -0x2600
68 
69 #define MBEDTLS_ERR_X509_SIG_MISMATCH -0x2680
70 
71 #define MBEDTLS_ERR_X509_CERT_VERIFY_FAILED -0x2700
72 
73 #define MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT -0x2780
74 
75 #define MBEDTLS_ERR_X509_BAD_INPUT_DATA -0x2800
76 
77 #define MBEDTLS_ERR_X509_ALLOC_FAILED -0x2880
78 
79 #define MBEDTLS_ERR_X509_FILE_IO_ERROR -0x2900
80 
81 #define MBEDTLS_ERR_X509_BUFFER_TOO_SMALL -0x2980
82 
83 #define MBEDTLS_ERR_X509_FATAL_ERROR -0x3000
84 
90 /* Reminder: update x509_crt_verify_strings[] in library/x509_crt.c */
91 #define MBEDTLS_X509_BADCERT_EXPIRED 0x01
92 #define MBEDTLS_X509_BADCERT_REVOKED 0x02
93 #define MBEDTLS_X509_BADCERT_CN_MISMATCH 0x04
94 #define MBEDTLS_X509_BADCERT_NOT_TRUSTED 0x08
95 #define MBEDTLS_X509_BADCRL_NOT_TRUSTED 0x10
96 #define MBEDTLS_X509_BADCRL_EXPIRED 0x20
97 #define MBEDTLS_X509_BADCERT_MISSING 0x40
98 #define MBEDTLS_X509_BADCERT_SKIP_VERIFY 0x80
99 #define MBEDTLS_X509_BADCERT_OTHER 0x0100
100 #define MBEDTLS_X509_BADCERT_FUTURE 0x0200
101 #define MBEDTLS_X509_BADCRL_FUTURE 0x0400
102 #define MBEDTLS_X509_BADCERT_KEY_USAGE 0x0800
103 #define MBEDTLS_X509_BADCERT_EXT_KEY_USAGE 0x1000
104 #define MBEDTLS_X509_BADCERT_NS_CERT_TYPE 0x2000
105 #define MBEDTLS_X509_BADCERT_BAD_MD 0x4000
106 #define MBEDTLS_X509_BADCERT_BAD_PK 0x8000
107 #define MBEDTLS_X509_BADCERT_BAD_KEY 0x010000
108 #define MBEDTLS_X509_BADCRL_BAD_MD 0x020000
109 #define MBEDTLS_X509_BADCRL_BAD_PK 0x040000
110 #define MBEDTLS_X509_BADCRL_BAD_KEY 0x080000
115 /*
116  * X.509 v3 Subject Alternative Name types.
117  * otherName [0] OtherName,
118  * rfc822Name [1] IA5String,
119  * dNSName [2] IA5String,
120  * x400Address [3] ORAddress,
121  * directoryName [4] Name,
122  * ediPartyName [5] EDIPartyName,
123  * uniformResourceIdentifier [6] IA5String,
124  * iPAddress [7] OCTET STRING,
125  * registeredID [8] OBJECT IDENTIFIER
126  */
127 #define MBEDTLS_X509_SAN_OTHER_NAME 0
128 #define MBEDTLS_X509_SAN_RFC822_NAME 1
129 #define MBEDTLS_X509_SAN_DNS_NAME 2
130 #define MBEDTLS_X509_SAN_X400_ADDRESS_NAME 3
131 #define MBEDTLS_X509_SAN_DIRECTORY_NAME 4
132 #define MBEDTLS_X509_SAN_EDI_PARTY_NAME 5
133 #define MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER 6
134 #define MBEDTLS_X509_SAN_IP_ADDRESS 7
135 #define MBEDTLS_X509_SAN_REGISTERED_ID 8
136 
137 /*
138  * X.509 v3 Key Usage Extension flags
139  * Reminder: update mbedtls_x509_info_key_usage() when adding new flags.
140  */
141 #define MBEDTLS_X509_KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
142 #define MBEDTLS_X509_KU_NON_REPUDIATION (0x40) /* bit 1 */
143 #define MBEDTLS_X509_KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
144 #define MBEDTLS_X509_KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
145 #define MBEDTLS_X509_KU_KEY_AGREEMENT (0x08) /* bit 4 */
146 #define MBEDTLS_X509_KU_KEY_CERT_SIGN (0x04) /* bit 5 */
147 #define MBEDTLS_X509_KU_CRL_SIGN (0x02) /* bit 6 */
148 #define MBEDTLS_X509_KU_ENCIPHER_ONLY (0x01) /* bit 7 */
149 #define MBEDTLS_X509_KU_DECIPHER_ONLY (0x8000) /* bit 8 */
150 
151 /*
152  * Netscape certificate types
153  * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
154  */
155 
156 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
157 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
158 #define MBEDTLS_X509_NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
159 #define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
160 #define MBEDTLS_X509_NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
161 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
162 #define MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
163 #define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
164 
165 /*
166  * X.509 extension types
167  *
168  * Comments refer to the status for using certificates. Status can be
169  * different for writing certificates or reading CRLs or CSRs.
170  *
171  * Those are defined in oid.h as oid.c needs them in a data structure. Since
172  * these were previously defined here, let's have aliases for compatibility.
173  */
174 #define MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER MBEDTLS_OID_X509_EXT_AUTHORITY_KEY_IDENTIFIER
175 #define MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER MBEDTLS_OID_X509_EXT_SUBJECT_KEY_IDENTIFIER
176 #define MBEDTLS_X509_EXT_KEY_USAGE MBEDTLS_OID_X509_EXT_KEY_USAGE
177 #define MBEDTLS_X509_EXT_CERTIFICATE_POLICIES MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES
178 #define MBEDTLS_X509_EXT_POLICY_MAPPINGS MBEDTLS_OID_X509_EXT_POLICY_MAPPINGS
179 #define MBEDTLS_X509_EXT_SUBJECT_ALT_NAME MBEDTLS_OID_X509_EXT_SUBJECT_ALT_NAME /* Supported (DNS) */
180 #define MBEDTLS_X509_EXT_ISSUER_ALT_NAME MBEDTLS_OID_X509_EXT_ISSUER_ALT_NAME
181 #define MBEDTLS_X509_EXT_SUBJECT_DIRECTORY_ATTRS MBEDTLS_OID_X509_EXT_SUBJECT_DIRECTORY_ATTRS
182 #define MBEDTLS_X509_EXT_BASIC_CONSTRAINTS MBEDTLS_OID_X509_EXT_BASIC_CONSTRAINTS /* Supported */
183 #define MBEDTLS_X509_EXT_NAME_CONSTRAINTS MBEDTLS_OID_X509_EXT_NAME_CONSTRAINTS
184 #define MBEDTLS_X509_EXT_POLICY_CONSTRAINTS MBEDTLS_OID_X509_EXT_POLICY_CONSTRAINTS
185 #define MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE MBEDTLS_OID_X509_EXT_EXTENDED_KEY_USAGE
186 #define MBEDTLS_X509_EXT_CRL_DISTRIBUTION_POINTS MBEDTLS_OID_X509_EXT_CRL_DISTRIBUTION_POINTS
187 #define MBEDTLS_X509_EXT_INIHIBIT_ANYPOLICY MBEDTLS_OID_X509_EXT_INIHIBIT_ANYPOLICY
188 #define MBEDTLS_X509_EXT_FRESHEST_CRL MBEDTLS_OID_X509_EXT_FRESHEST_CRL
189 #define MBEDTLS_X509_EXT_NS_CERT_TYPE MBEDTLS_OID_X509_EXT_NS_CERT_TYPE
190 
191 /*
192  * Storage format identifiers
193  * Recognized formats: PEM and DER
194  */
195 #define MBEDTLS_X509_FORMAT_DER 1
196 #define MBEDTLS_X509_FORMAT_PEM 2
197 
198 #define MBEDTLS_X509_MAX_DN_NAME_SIZE 256
200 #ifdef __cplusplus
201 extern "C" {
202 #endif
203 
217 
222 
228 
233 
234 /*
235  * Container for the fields of the Authority Key Identifier object
236  */
237 typedef struct mbedtls_x509_authority {
238  mbedtls_x509_buf keyIdentifier;
239  mbedtls_x509_sequence authorityCertIssuer;
240  mbedtls_x509_buf authorityCertSerialNumber;
241  mbedtls_x509_buf raw;
242 }
244 
246 typedef struct mbedtls_x509_time {
247  int year, mon, day;
248  int hour, min, sec;
249 }
251 
267  mbedtls_x509_buf type_id;
268  union {
275  struct {
276  mbedtls_x509_buf oid;
277  mbedtls_x509_buf val;
278  }
280  }
281  value;
282 }
284 
293  int type;
294  union {
296  mbedtls_x509_name directory_name;
297  mbedtls_x509_buf unstructured_name;
298  }
299  san;
300 }
302 
303 typedef struct mbedtls_x509_san_list {
306 }
308 
323 int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn);
324 
340 int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name);
341 
353 static inline mbedtls_x509_name *mbedtls_x509_dn_get_next(
354  mbedtls_x509_name *dn)
355 {
356  while (dn->MBEDTLS_PRIVATE(next_merged) && dn->next != NULL) {
357  dn = dn->next;
358  }
359  return dn->next;
360 }
361 
373 int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial);
374 
386 
387 #if defined(MBEDTLS_HAVE_TIME_DATE)
388 
398 #endif /* MBEDTLS_HAVE_TIME_DATE */
399 
413 
427 
460 int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
468 
484 size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst);
485 
486 #define MBEDTLS_X509_SAFE_SNPRINTF \
487  do { \
488  if (ret < 0 || (size_t) ret >= n) \
489  return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; \
490  \
491  n -= (size_t) ret; \
492  p += (size_t) ret; \
493  } while (0)
494 
495 #ifdef __cplusplus
496 }
497 #endif
498 
499 #endif /* MBEDTLS_X509_H */
mbedtls_asn1_buf mbedtls_x509_buf
Definition: x509.h:216
mbedtls_x509_buf oid
Definition: x509.h:276
mbedtls_x509_buf raw
Definition: x509.h:241
int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Check a given mbedtls_x509_time against the system time and tell if it's in the future.
struct mbedtls_x509_san_other_name mbedtls_x509_san_other_name
mbedtls_x509_subject_alternative_name node
Definition: x509.h:304
int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
Convert the certificate DN string name into a linked list of mbedtls_x509_name (equivalent to mbedtls...
int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1, const mbedtls_x509_time *t2)
Compare pair of mbedtls_x509_time.
Public Key abstraction layer.
time_t mbedtls_time_t
Definition: platform_time.h:27
mbedtls_x509_sequence authorityCertIssuer
Definition: x509.h:239
mbedtls_x509_san_other_name other_name
Definition: x509.h:295
mbedtls_asn1_sequence mbedtls_x509_sequence
Definition: x509.h:232
int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now)
Fill mbedtls_x509_time with provided mbedtls_time_t.
int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Store the certificate serial in printable form into buf; no more than size characters will be written...
struct mbedtls_x509_authority mbedtls_x509_authority
Generic ASN.1 parsing.
mbedtls_x509_buf unstructured_name
Definition: x509.h:297
struct mbedtls_x509_time mbedtls_x509_time
struct mbedtls_x509_san_list * next
Definition: x509.h:305
Macro wrapper for struct's members.
mbedtls_x509_buf val
Definition: x509.h:277
mbedtls_asn1_bitstring mbedtls_x509_bitstring
Definition: x509.h:221
Build-time configuration info.
struct mbedtls_x509_san_list mbedtls_x509_san_list
mbedtls_asn1_named_data mbedtls_x509_name
Definition: x509.h:227
struct mbedtls_x509_san_other_name::@10::@11 hardware_module_name
union mbedtls_x509_subject_alternative_name::@12 san
This file provides an API for the RSA public-key cryptosystem.
int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Check a given mbedtls_x509_time against the system time and tell if it's in the past.
mbedtls_x509_buf keyIdentifier
Definition: x509.h:238
static mbedtls_x509_name * mbedtls_x509_dn_get_next(mbedtls_x509_name *dn)
Return the next relative DN in an X509 name.
Definition: x509.h:353
size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
This function parses a CN string as an IP address.
union mbedtls_x509_san_other_name::@10 value
struct mbedtls_asn1_named_data * next
Definition: asn1.h:189
int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Store the certificate DN in printable form into buf; no more than size characters will be written...
mbedtls_x509_buf type_id
Definition: x509.h:267
struct mbedtls_x509_subject_alternative_name mbedtls_x509_subject_alternative_name
void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
Unallocate all data related to subject alternative name.
mbedtls_x509_buf authorityCertSerialNumber
Definition: x509.h:240
mbedtls_x509_name directory_name
Definition: x509.h:296
int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf, mbedtls_x509_subject_alternative_name *san)
This function parses an item in the SubjectAlternativeNames extension. Please note that this function...