Mbed TLS v4.0.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 
24 #if !defined(MBEDTLS_X509_MAX_INTERMEDIATE_CA)
25 
33 #define MBEDTLS_X509_MAX_INTERMEDIATE_CA 8
34 #endif
35 
41 #define MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE -0x2080
42 
43 #define MBEDTLS_ERR_X509_UNKNOWN_OID -0x2100
44 
45 #define MBEDTLS_ERR_X509_INVALID_FORMAT -0x2180
46 
47 #define MBEDTLS_ERR_X509_INVALID_VERSION -0x2200
48 
49 #define MBEDTLS_ERR_X509_INVALID_SERIAL -0x2280
50 
51 #define MBEDTLS_ERR_X509_INVALID_ALG -0x2300
52 
53 #define MBEDTLS_ERR_X509_INVALID_NAME -0x2380
54 
55 #define MBEDTLS_ERR_X509_INVALID_DATE -0x2400
56 
57 #define MBEDTLS_ERR_X509_INVALID_SIGNATURE -0x2480
58 
59 #define MBEDTLS_ERR_X509_INVALID_EXTENSIONS -0x2500
60 
61 #define MBEDTLS_ERR_X509_UNKNOWN_VERSION -0x2580
62 
63 #define MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG -0x2600
64 
65 #define MBEDTLS_ERR_X509_SIG_MISMATCH -0x2680
66 
67 #define MBEDTLS_ERR_X509_CERT_VERIFY_FAILED -0x2700
68 
69 #define MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT -0x2780
70 
71 #define MBEDTLS_ERR_X509_BAD_INPUT_DATA -0x2800
72 
73 #define MBEDTLS_ERR_X509_ALLOC_FAILED PSA_ERROR_INSUFFICIENT_MEMORY
74 
75 #define MBEDTLS_ERR_X509_FILE_IO_ERROR -0x2900
76 
77 #define MBEDTLS_ERR_X509_BUFFER_TOO_SMALL PSA_ERROR_BUFFER_TOO_SMALL
78 
79 #define MBEDTLS_ERR_X509_FATAL_ERROR -0x3000
80 
86 /* Reminder: update x509_crt_verify_strings[] in library/x509_crt.c */
87 #define MBEDTLS_X509_BADCERT_EXPIRED 0x01
88 #define MBEDTLS_X509_BADCERT_REVOKED 0x02
89 #define MBEDTLS_X509_BADCERT_CN_MISMATCH 0x04
90 #define MBEDTLS_X509_BADCERT_NOT_TRUSTED 0x08
91 #define MBEDTLS_X509_BADCRL_NOT_TRUSTED 0x10
92 #define MBEDTLS_X509_BADCRL_EXPIRED 0x20
93 #define MBEDTLS_X509_BADCERT_MISSING 0x40
94 #define MBEDTLS_X509_BADCERT_SKIP_VERIFY 0x80
95 #define MBEDTLS_X509_BADCERT_OTHER 0x0100
96 #define MBEDTLS_X509_BADCERT_FUTURE 0x0200
97 #define MBEDTLS_X509_BADCRL_FUTURE 0x0400
98 #define MBEDTLS_X509_BADCERT_KEY_USAGE 0x0800
99 #define MBEDTLS_X509_BADCERT_EXT_KEY_USAGE 0x1000
100 #define MBEDTLS_X509_BADCERT_NS_CERT_TYPE 0x2000
101 #define MBEDTLS_X509_BADCERT_BAD_MD 0x4000
102 #define MBEDTLS_X509_BADCERT_BAD_PK 0x8000
103 #define MBEDTLS_X509_BADCERT_BAD_KEY 0x010000
104 #define MBEDTLS_X509_BADCRL_BAD_MD 0x020000
105 #define MBEDTLS_X509_BADCRL_BAD_PK 0x040000
106 #define MBEDTLS_X509_BADCRL_BAD_KEY 0x080000
111 /*
112  * X.509 v3 Subject Alternative Name types.
113  * otherName [0] OtherName,
114  * rfc822Name [1] IA5String,
115  * dNSName [2] IA5String,
116  * x400Address [3] ORAddress,
117  * directoryName [4] Name,
118  * ediPartyName [5] EDIPartyName,
119  * uniformResourceIdentifier [6] IA5String,
120  * iPAddress [7] OCTET STRING,
121  * registeredID [8] OBJECT IDENTIFIER
122  */
123 #define MBEDTLS_X509_SAN_OTHER_NAME 0
124 #define MBEDTLS_X509_SAN_RFC822_NAME 1
125 #define MBEDTLS_X509_SAN_DNS_NAME 2
126 #define MBEDTLS_X509_SAN_X400_ADDRESS_NAME 3
127 #define MBEDTLS_X509_SAN_DIRECTORY_NAME 4
128 #define MBEDTLS_X509_SAN_EDI_PARTY_NAME 5
129 #define MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER 6
130 #define MBEDTLS_X509_SAN_IP_ADDRESS 7
131 #define MBEDTLS_X509_SAN_REGISTERED_ID 8
132 
133 /*
134  * X.509 v3 Key Usage Extension flags
135  * Reminder: update mbedtls_x509_info_key_usage() when adding new flags.
136  */
137 #define MBEDTLS_X509_KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
138 #define MBEDTLS_X509_KU_NON_REPUDIATION (0x40) /* bit 1 */
139 #define MBEDTLS_X509_KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
140 #define MBEDTLS_X509_KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
141 #define MBEDTLS_X509_KU_KEY_AGREEMENT (0x08) /* bit 4 */
142 #define MBEDTLS_X509_KU_KEY_CERT_SIGN (0x04) /* bit 5 */
143 #define MBEDTLS_X509_KU_CRL_SIGN (0x02) /* bit 6 */
144 #define MBEDTLS_X509_KU_ENCIPHER_ONLY (0x01) /* bit 7 */
145 #define MBEDTLS_X509_KU_DECIPHER_ONLY (0x8000) /* bit 8 */
146 
147 /*
148  * Netscape certificate types
149  * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
150  */
151 
152 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
153 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
154 #define MBEDTLS_X509_NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
155 #define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
156 #define MBEDTLS_X509_NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
157 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
158 #define MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
159 #define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
160 
161 /*
162  * X.509 extension types
163  *
164  * Comments refer to the status for using certificates. Status can be
165  * different for writing certificates or reading CRLs or CSRs.
166  */
167 #define MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
168 #define MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
169 #define MBEDTLS_X509_EXT_KEY_USAGE (1 << 2)
170 #define MBEDTLS_X509_EXT_CERTIFICATE_POLICIES (1 << 3)
171 #define MBEDTLS_X509_EXT_POLICY_MAPPINGS (1 << 4)
172 #define MBEDTLS_X509_EXT_SUBJECT_ALT_NAME (1 << 5) /* Supported (DNS) */
173 #define MBEDTLS_X509_EXT_ISSUER_ALT_NAME (1 << 6)
174 #define MBEDTLS_X509_EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
175 #define MBEDTLS_X509_EXT_BASIC_CONSTRAINTS (1 << 8) /* Supported */
176 #define MBEDTLS_X509_EXT_NAME_CONSTRAINTS (1 << 9)
177 #define MBEDTLS_X509_EXT_POLICY_CONSTRAINTS (1 << 10)
178 #define MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE (1 << 11)
179 #define MBEDTLS_X509_EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
180 #define MBEDTLS_X509_EXT_INIHIBIT_ANYPOLICY (1 << 13)
181 #define MBEDTLS_X509_EXT_FRESHEST_CRL (1 << 14)
182 #define MBEDTLS_X509_EXT_NS_CERT_TYPE (1 << 16)
183 
184 /*
185  * Storage format identifiers
186  * Recognized formats: PEM and DER
187  */
188 #define MBEDTLS_X509_FORMAT_DER 1
189 #define MBEDTLS_X509_FORMAT_PEM 2
190 
191 #define MBEDTLS_X509_MAX_DN_NAME_SIZE 256
193 #ifdef __cplusplus
194 extern "C" {
195 #endif
196 
210 
215 
221 
226 
227 /*
228  * Container for the fields of the Authority Key Identifier object
229  */
230 typedef struct mbedtls_x509_authority {
231  mbedtls_x509_buf keyIdentifier;
232  mbedtls_x509_sequence authorityCertIssuer;
233  mbedtls_x509_buf authorityCertSerialNumber;
234  mbedtls_x509_buf raw;
235 }
237 
239 typedef struct mbedtls_x509_time {
240  int year, mon, day;
241  int hour, min, sec;
242 }
244 
260  mbedtls_x509_buf type_id;
261  union {
268  struct {
269  mbedtls_x509_buf oid;
270  mbedtls_x509_buf val;
271  }
273  }
274  value;
275 }
277 
286  int type;
287  union {
289  mbedtls_x509_name directory_name;
290  mbedtls_x509_buf unstructured_name;
291  }
292  san;
293 }
295 
296 typedef struct mbedtls_x509_san_list {
299 }
301 
316 int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn);
317 
334 int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name);
335 
347 static inline mbedtls_x509_name *mbedtls_x509_dn_get_next(
348  mbedtls_x509_name *dn)
349 {
350  while (dn->MBEDTLS_PRIVATE(next_merged) && dn->next != NULL) {
351  dn = dn->next;
352  }
353  return dn->next;
354 }
355 
367 int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial);
368 
380 
381 #if defined(MBEDTLS_HAVE_TIME_DATE)
382 
392 #endif /* MBEDTLS_HAVE_TIME_DATE */
393 
407 
421 
454 int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
462 
478 size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst);
479 
480 #define MBEDTLS_X509_SAFE_SNPRINTF \
481  do { \
482  if (ret < 0 || (size_t) ret >= n) \
483  return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; \
484  \
485  n -= (size_t) ret; \
486  p += (size_t) ret; \
487  } while (0)
488 
489 #ifdef __cplusplus
490 }
491 #endif
492 
493 #endif /* MBEDTLS_X509_H */
mbedtls_asn1_buf mbedtls_x509_buf
Definition: x509.h:209
mbedtls_x509_buf oid
Definition: x509.h:269
mbedtls_x509_buf raw
Definition: x509.h:234
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
union mbedtls_x509_subject_alternative_name::@2 san
mbedtls_x509_subject_alternative_name node
Definition: x509.h:297
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.
struct mbedtls_x509_san_other_name::@0::@1 hardware_module_name
mbedtls_x509_sequence authorityCertIssuer
Definition: x509.h:232
mbedtls_x509_san_other_name other_name
Definition: x509.h:288
mbedtls_asn1_sequence mbedtls_x509_sequence
Definition: x509.h:225
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:290
struct mbedtls_x509_time mbedtls_x509_time
MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t
Definition: platform_time.h:23
struct mbedtls_x509_san_list * next
Definition: x509.h:298
Build-time configuration info.
mbedtls_x509_buf val
Definition: x509.h:270
mbedtls_asn1_bitstring mbedtls_x509_bitstring
Definition: x509.h:214
struct mbedtls_x509_san_list mbedtls_x509_san_list
mbedtls_asn1_named_data mbedtls_x509_name
Definition: x509.h:220
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:231
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:347
size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
This function parses a CN string as an IP address.
struct mbedtls_asn1_named_data * next
Definition: asn1.h:185
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...
union mbedtls_x509_san_other_name::@0 value
mbedtls_x509_buf type_id
Definition: x509.h:260
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:233
mbedtls_x509_name directory_name
Definition: x509.h:289
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...