Mbed TLS v4.1.2
Loading...
Searching...
No Matches
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
23
24#if !defined(MBEDTLS_X509_MAX_INTERMEDIATE_CA)
33#define MBEDTLS_X509_MAX_INTERMEDIATE_CA 8
34#endif
35
41#define MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE -0x2080
43#define MBEDTLS_ERR_X509_UNKNOWN_OID -0x2100
45#define MBEDTLS_ERR_X509_INVALID_FORMAT -0x2180
47#define MBEDTLS_ERR_X509_INVALID_VERSION -0x2200
49#define MBEDTLS_ERR_X509_INVALID_SERIAL -0x2280
51#define MBEDTLS_ERR_X509_INVALID_ALG -0x2300
53#define MBEDTLS_ERR_X509_INVALID_NAME -0x2380
55#define MBEDTLS_ERR_X509_INVALID_DATE -0x2400
57#define MBEDTLS_ERR_X509_INVALID_SIGNATURE -0x2480
59#define MBEDTLS_ERR_X509_INVALID_EXTENSIONS -0x2500
61#define MBEDTLS_ERR_X509_UNKNOWN_VERSION -0x2580
63#define MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG -0x2600
65#define MBEDTLS_ERR_X509_SIG_MISMATCH -0x2680
67#define MBEDTLS_ERR_X509_CERT_VERIFY_FAILED -0x2700
69#define MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT -0x2780
71#define MBEDTLS_ERR_X509_BAD_INPUT_DATA -0x2800
73#define MBEDTLS_ERR_X509_ALLOC_FAILED PSA_ERROR_INSUFFICIENT_MEMORY
75#define MBEDTLS_ERR_X509_FILE_IO_ERROR -0x2900
77#define MBEDTLS_ERR_X509_BUFFER_TOO_SMALL PSA_ERROR_BUFFER_TOO_SMALL
79#define MBEDTLS_ERR_X509_FATAL_ERROR -0x3000
81
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
107
110
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 * Add NONE for the benefit of x509_crt_infer_name_type().
124 */
125#define MBEDTLS_X509_SAN_NONE -1
126#define MBEDTLS_X509_SAN_OTHER_NAME 0
127#define MBEDTLS_X509_SAN_RFC822_NAME 1
128#define MBEDTLS_X509_SAN_DNS_NAME 2
129#define MBEDTLS_X509_SAN_X400_ADDRESS_NAME 3
130#define MBEDTLS_X509_SAN_DIRECTORY_NAME 4
131#define MBEDTLS_X509_SAN_EDI_PARTY_NAME 5
132#define MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER 6
133#define MBEDTLS_X509_SAN_IP_ADDRESS 7
134#define MBEDTLS_X509_SAN_REGISTERED_ID 8
135
136/*
137 * X.509 v3 Key Usage Extension flags
138 * Reminder: update mbedtls_x509_info_key_usage() when adding new flags.
139 */
140#define MBEDTLS_X509_KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
141#define MBEDTLS_X509_KU_NON_REPUDIATION (0x40) /* bit 1 */
142#define MBEDTLS_X509_KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
143#define MBEDTLS_X509_KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
144#define MBEDTLS_X509_KU_KEY_AGREEMENT (0x08) /* bit 4 */
145#define MBEDTLS_X509_KU_KEY_CERT_SIGN (0x04) /* bit 5 */
146#define MBEDTLS_X509_KU_CRL_SIGN (0x02) /* bit 6 */
147#define MBEDTLS_X509_KU_ENCIPHER_ONLY (0x01) /* bit 7 */
148#define MBEDTLS_X509_KU_DECIPHER_ONLY (0x8000) /* bit 8 */
149
150/*
151 * Netscape certificate types
152 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
153 */
154
155#define MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
156#define MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
157#define MBEDTLS_X509_NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
158#define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
159#define MBEDTLS_X509_NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
160#define MBEDTLS_X509_NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
161#define MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
162#define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
163
164/*
165 * X.509 extension types
166 *
167 * Comments refer to the status for using certificates. Status can be
168 * different for writing certificates or reading CRLs or CSRs.
169 */
170#define MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
171#define MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
172#define MBEDTLS_X509_EXT_KEY_USAGE (1 << 2)
173#define MBEDTLS_X509_EXT_CERTIFICATE_POLICIES (1 << 3)
174#define MBEDTLS_X509_EXT_POLICY_MAPPINGS (1 << 4)
175#define MBEDTLS_X509_EXT_SUBJECT_ALT_NAME (1 << 5) /* Supported (DNS) */
176#define MBEDTLS_X509_EXT_ISSUER_ALT_NAME (1 << 6)
177#define MBEDTLS_X509_EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
178#define MBEDTLS_X509_EXT_BASIC_CONSTRAINTS (1 << 8) /* Supported */
179#define MBEDTLS_X509_EXT_NAME_CONSTRAINTS (1 << 9)
180#define MBEDTLS_X509_EXT_POLICY_CONSTRAINTS (1 << 10)
181#define MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE (1 << 11)
182#define MBEDTLS_X509_EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
183#define MBEDTLS_X509_EXT_INIHIBIT_ANYPOLICY (1 << 13)
184#define MBEDTLS_X509_EXT_FRESHEST_CRL (1 << 14)
185#define MBEDTLS_X509_EXT_NS_CERT_TYPE (1 << 16)
186
187/*
188 * Storage format identifiers
189 * Recognized formats: PEM and DER
190 */
191#define MBEDTLS_X509_FORMAT_DER 1
192#define MBEDTLS_X509_FORMAT_PEM 2
193
194#define MBEDTLS_X509_MAX_DN_NAME_SIZE 256
195
196#ifdef __cplusplus
197extern "C" {
198#endif
199
203
208
213
218
224
229
230/*
231 * Container for the fields of the Authority Key Identifier object
232 */
240
242typedef struct mbedtls_x509_time {
243 int year, mon, day;
244 int hour, min, sec;
245}
247
280
298
304
307
322int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn);
323
324
333
351
365{
366 while (dn->MBEDTLS_PRIVATE(next_merged) && dn->next != NULL) {
367 dn = dn->next;
368 }
369 return dn->next;
370}
371
383int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial);
384
396
397#if defined(MBEDTLS_HAVE_TIME_DATE)
408#endif /* MBEDTLS_HAVE_TIME_DATE */
409
423
437
478
494size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst);
495
496#define MBEDTLS_X509_SAFE_SNPRINTF \
497 do { \
498 if (ret < 0 || (size_t) ret >= n) \
499 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; \
500 \
501 n -= (size_t) ret; \
502 p += (size_t) ret; \
503 } while (0)
504
505#ifdef __cplusplus
506}
507#endif
508
518
529int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
530 mbedtls_md_type_t *md_alg, mbedtls_pk_sigalg_t *pk_alg);
531
532#endif /* MBEDTLS_X509_H */
Generic ASN.1 parsing.
mbedtls_asn1_named_data mbedtls_x509_name
Definition x509.h:223
mbedtls_asn1_buf mbedtls_x509_buf
Definition x509.h:212
mbedtls_asn1_sequence mbedtls_x509_sequence
Definition x509.h:228
mbedtls_asn1_bitstring mbedtls_x509_bitstring
Definition x509.h:217
Build-time configuration info.
mbedtls_md_type_t
Supported message digests.
Definition md.h:50
Public Key abstraction layer.
mbedtls_pk_sigalg_t
Definition pk.h:49
time_t mbedtls_time_t
struct mbedtls_asn1_named_data * next
Definition asn1.h:185
Public key container.
Definition pk.h:106
mbedtls_x509_buf raw
Definition x509.h:237
mbedtls_x509_buf authorityCertSerialNumber
Definition x509.h:236
mbedtls_x509_sequence authorityCertIssuer
Definition x509.h:235
mbedtls_x509_buf keyIdentifier
Definition x509.h:234
mbedtls_x509_subject_alternative_name node
Definition x509.h:300
struct mbedtls_x509_san_list * next
Definition x509.h:301
union mbedtls_x509_san_other_name::@134003275340054302232246366074101102372024113367 value
mbedtls_x509_buf oid
Definition x509.h:272
mbedtls_x509_buf val
Definition x509.h:273
struct mbedtls_x509_san_other_name::@134003275340054302232246366074101102372024113367::@245167013252240201272374264172070373110376320321 hardware_module_name
mbedtls_x509_buf type_id
Definition x509.h:263
union mbedtls_x509_subject_alternative_name::@105322250037201342031031353211124220005156146076 san
mbedtls_x509_san_other_name other_name
Definition x509.h:291
mbedtls_x509_buf unstructured_name
Definition x509.h:293
mbedtls_x509_name directory_name
Definition x509.h:292
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.
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.
int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params, mbedtls_md_type_t *md_alg, mbedtls_pk_sigalg_t *pk_alg)
Translate a signature algorithm OID and parameters into PK sigalg.
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...
void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
Unallocate all data related to subject alternative name.
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:363
int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Store the certificate DN in printable form into buf.
int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1, const mbedtls_x509_time *t2)
Compare pair of mbedtls_x509_time.
int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now)
Fill mbedtls_x509_time with provided mbedtls_time_t.
const char * mbedtls_x509_pk_type_as_string(const mbedtls_pk_context *pk)
Return the key's type as a string.
size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
This function parses a CN string as an IP address.
int mbedtls_x509_oid_get_md_alg(const mbedtls_asn1_buf *oid, mbedtls_md_type_t *md_alg)
Translate hash algorithm OID into md_type.
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_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...