blob: 8b6a1daee50d26f7b93b7fec9c234e2337d603ff [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file x509.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004 * \brief X.509 generic defines and structures
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_X509_H
11#define MBEDTLS_X509_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020012#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000013
Bence Szépkútic662b362021-05-27 11:25:03 +020014#include "mbedtls/build_info.h"
Paul Bakkered27a042013-04-18 22:46:23 +020015
Jaeden Amero6609aef2019-07-04 20:01:14 +010016#include "mbedtls/asn1.h"
17#include "mbedtls/pk.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000018
Paul Bakker7c6b2c32013-09-16 13:49:26 +020019/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000020 * \addtogroup x509_module
Paul Bakker7c6b2c32013-09-16 13:49:26 +020021 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000022 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if !defined(MBEDTLS_X509_MAX_INTERMEDIATE_CA)
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +010025/**
26 * Maximum number of intermediate CAs in a verification chain.
27 * That is, maximum length of the chain, excluding the end-entity certificate
28 * and the trusted root certificate.
29 *
30 * Set this to a low value to prevent an adversary from making you waste
31 * resources verifying an overlong certificate chain.
32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define MBEDTLS_X509_MAX_INTERMEDIATE_CA 8
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +010034#endif
35
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000037 * \name X509 Error codes
38 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000039 */
Gilles Peskined2971572021-07-26 18:48:10 +020040/** Unavailable feature, e.g. RSA hashing/encryption combination. */
41#define MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE -0x2080
42/** Requested OID is unknown. */
43#define MBEDTLS_ERR_X509_UNKNOWN_OID -0x2100
44/** The CRT/CRL/CSR format is invalid, e.g. different type expected. */
45#define MBEDTLS_ERR_X509_INVALID_FORMAT -0x2180
46/** The CRT/CRL/CSR version element is invalid. */
47#define MBEDTLS_ERR_X509_INVALID_VERSION -0x2200
48/** The serial tag or value is invalid. */
49#define MBEDTLS_ERR_X509_INVALID_SERIAL -0x2280
50/** The algorithm tag or value is invalid. */
51#define MBEDTLS_ERR_X509_INVALID_ALG -0x2300
52/** The name tag or value is invalid. */
53#define MBEDTLS_ERR_X509_INVALID_NAME -0x2380
54/** The date tag or value is invalid. */
55#define MBEDTLS_ERR_X509_INVALID_DATE -0x2400
56/** The signature tag or value invalid. */
57#define MBEDTLS_ERR_X509_INVALID_SIGNATURE -0x2480
58/** The extension tag or value is invalid. */
59#define MBEDTLS_ERR_X509_INVALID_EXTENSIONS -0x2500
60/** CRT/CRL/CSR has an unsupported version number. */
61#define MBEDTLS_ERR_X509_UNKNOWN_VERSION -0x2580
62/** Signature algorithm (oid) is unsupported. */
63#define MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG -0x2600
64/** Signature algorithms do not match. (see \c ::mbedtls_x509_crt sig_oid) */
65#define MBEDTLS_ERR_X509_SIG_MISMATCH -0x2680
66/** Certificate verification failed, e.g. CRL, CA or signature check failed. */
67#define MBEDTLS_ERR_X509_CERT_VERIFY_FAILED -0x2700
68/** Format not recognized as DER or PEM. */
69#define MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT -0x2780
70/** Input invalid. */
71#define MBEDTLS_ERR_X509_BAD_INPUT_DATA -0x2800
72/** Allocation of memory failed. */
Felix Conway37ede2c2025-08-18 14:46:39 +010073#define MBEDTLS_ERR_X509_ALLOC_FAILED PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskined2971572021-07-26 18:48:10 +020074/** Read/write of file failed. */
75#define MBEDTLS_ERR_X509_FILE_IO_ERROR -0x2900
76/** Destination buffer is too small. */
Felix Conway37ede2c2025-08-18 14:46:39 +010077#define MBEDTLS_ERR_X509_BUFFER_TOO_SMALL PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskined2971572021-07-26 18:48:10 +020078/** A fatal error occurred, eg the chain is too long or the vrfy callback failed. */
79#define MBEDTLS_ERR_X509_FATAL_ERROR -0x3000
Andrzej Kurek38d4fdd2021-12-28 16:22:52 +010080/** \} name X509 Error codes */
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Paul Bakker37ca75d2011-01-06 12:28:03 +000082/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000083 * \name X509 Verify codes
84 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +000085 */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +010086/* Reminder: update x509_crt_verify_strings[] in library/x509_crt.c */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010087#define MBEDTLS_X509_BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#define MBEDTLS_X509_BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */
89#define MBEDTLS_X509_BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */
90#define MBEDTLS_X509_BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +010091#define MBEDTLS_X509_BADCRL_NOT_TRUSTED 0x10 /**< The CRL is not correctly signed by the trusted CA. */
92#define MBEDTLS_X509_BADCRL_EXPIRED 0x20 /**< The CRL is expired. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010093#define MBEDTLS_X509_BADCERT_MISSING 0x40 /**< Certificate was missing. */
94#define MBEDTLS_X509_BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */
95#define MBEDTLS_X509_BADCERT_OTHER 0x0100 /**< Other reason (can be used by verify callback) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#define MBEDTLS_X509_BADCERT_FUTURE 0x0200 /**< The certificate validity starts in the future. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010097#define MBEDTLS_X509_BADCRL_FUTURE 0x0400 /**< The CRL is from the future */
98#define MBEDTLS_X509_BADCERT_KEY_USAGE 0x0800 /**< Usage does not match the keyUsage extension. */
99#define MBEDTLS_X509_BADCERT_EXT_KEY_USAGE 0x1000 /**< Usage does not match the extendedKeyUsage extension. */
100#define MBEDTLS_X509_BADCERT_NS_CERT_TYPE 0x2000 /**< Usage does not match the nsCertType extension. */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200101#define MBEDTLS_X509_BADCERT_BAD_MD 0x4000 /**< The certificate is signed with an unacceptable hash. */
102#define MBEDTLS_X509_BADCERT_BAD_PK 0x8000 /**< The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA). */
103#define MBEDTLS_X509_BADCERT_BAD_KEY 0x010000 /**< The certificate is signed with an unacceptable key (eg bad curve, RSA too short). */
104#define MBEDTLS_X509_BADCRL_BAD_MD 0x020000 /**< The CRL is signed with an unacceptable hash. */
105#define MBEDTLS_X509_BADCRL_BAD_PK 0x040000 /**< The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA). */
106#define MBEDTLS_X509_BADCRL_BAD_KEY 0x080000 /**< The CRL is signed with an unacceptable key (eg bad curve, RSA too short). */
107
Andrzej Kurek38d4fdd2021-12-28 16:22:52 +0100108/** \} name X509 Verify codes */
109/** \} addtogroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
111/*
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200112 * 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/*
Paul Bakker74111d32011-01-15 16:57:55 +0000134 * X.509 v3 Key Usage Extension flags
Przemek Stekiel21c37282023-01-16 08:47:49 +0100135 * Reminder: update mbedtls_x509_info_key_usage() when adding new flags.
Paul Bakker74111d32011-01-15 16:57:55 +0000136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#define MBEDTLS_X509_KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
138#define MBEDTLS_X509_KU_NON_REPUDIATION (0x40) /* bit 1 */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100139#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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#define MBEDTLS_X509_KU_KEY_CERT_SIGN (0x04) /* bit 5 */
143#define MBEDTLS_X509_KU_CRL_SIGN (0x02) /* bit 6 */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200144#define MBEDTLS_X509_KU_ENCIPHER_ONLY (0x01) /* bit 7 */
145#define MBEDTLS_X509_KU_DECIPHER_ONLY (0x8000) /* bit 8 */
Paul Bakker74111d32011-01-15 16:57:55 +0000146
147/*
Paul Bakker74111d32011-01-15 16:57:55 +0000148 * Netscape certificate types
149 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
150 */
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#define MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100153#define MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#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 */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100157#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 */
Paul Bakker74111d32011-01-15 16:57:55 +0000160
Paul Bakkerc70b9822013-04-07 22:00:46 +0200161/*
162 * X.509 extension types
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +0200163 *
164 * Comments refer to the status for using certificates. Status can be
165 * different for writing certificates or reading CRLs or CSRs.
Paul Bakkerc70b9822013-04-07 22:00:46 +0200166 */
Gilles Peskine32a11122025-04-09 21:51:46 +0200167#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)
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000184/*
185 * Storage format identifiers
186 * Recognized formats: PEM and DER
187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#define MBEDTLS_X509_FORMAT_DER 1
189#define MBEDTLS_X509_FORMAT_PEM 2
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#define MBEDTLS_X509_MAX_DN_NAME_SIZE 256 /**< Maximum value size of a DN entry */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200192
Paul Bakker407a0da2013-06-27 14:29:21 +0200193#ifdef __cplusplus
194extern "C" {
195#endif
196
Paul Bakkere5eae762013-08-26 12:05:14 +0200197/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000198 * \addtogroup x509_module
199 * \{ */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000200
201/**
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200202 * \name Structures for parsing X.509 certificates, CRLs and CSRs
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000203 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000204 */
Paul Bakkere5eae762013-08-26 12:05:14 +0200205
206/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000207 * Type-length-value structure that allows for ASN1 using DER.
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209typedef mbedtls_asn1_buf mbedtls_x509_buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Paul Bakker37ca75d2011-01-06 12:28:03 +0000211/**
Paul Bakker74111d32011-01-15 16:57:55 +0000212 * Container for ASN1 bit strings.
213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214typedef mbedtls_asn1_bitstring mbedtls_x509_bitstring;
Paul Bakker74111d32011-01-15 16:57:55 +0000215
216/**
Paul Bakkere5eae762013-08-26 12:05:14 +0200217 * Container for ASN1 named information objects.
Manuel Pégourié-Gonnard83b04de2015-03-09 17:33:07 +0000218 * It allows for Relative Distinguished Names (e.g. cn=localhost,ou=code,etc.).
Paul Bakker37ca75d2011-01-06 12:28:03 +0000219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220typedef mbedtls_asn1_named_data mbedtls_x509_name;
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Paul Bakker74111d32011-01-15 16:57:55 +0000222/**
223 * Container for a sequence of ASN.1 items
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225typedef mbedtls_asn1_sequence mbedtls_x509_sequence;
Paul Bakker74111d32011-01-15 16:57:55 +0000226
toth92ga41954d2021-02-12 16:11:17 +0100227/*
228 * Container for the fields of the Authority Key Identifier object
229 */
230typedef struct mbedtls_x509_authority {
231 mbedtls_x509_buf keyIdentifier;
toth92g8d435a02021-05-10 15:16:33 +0200232 mbedtls_x509_sequence authorityCertIssuer;
toth92ga41954d2021-02-12 16:11:17 +0100233 mbedtls_x509_buf authorityCertSerialNumber;
234 mbedtls_x509_buf raw;
235}
236mbedtls_x509_authority;
237
Paul Bakker37ca75d2011-01-06 12:28:03 +0000238/** Container for date and time (precision in seconds). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239typedef struct mbedtls_x509_time {
Gilles Peskine842edf42021-08-04 21:56:10 +0200240 int year, mon, day; /**< Date. */
241 int hour, min, sec; /**< Time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000242}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243mbedtls_x509_time;
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +0100245/**
246 * From RFC 5280 section 4.2.1.6:
247 * OtherName ::= SEQUENCE {
248 * type-id OBJECT IDENTIFIER,
249 * value [0] EXPLICIT ANY DEFINED BY type-id }
250 *
251 * Future versions of the library may add new fields to this structure or
252 * to its embedded union and structure.
253 */
254typedef struct mbedtls_x509_san_other_name {
255 /**
256 * The type_id is an OID as defined in RFC 5280.
257 * To check the value of the type id, you should use
258 * \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf.
259 */
260 mbedtls_x509_buf type_id; /**< The type id. */
261 union {
262 /**
263 * From RFC 4108 section 5:
264 * HardwareModuleName ::= SEQUENCE {
265 * hwType OBJECT IDENTIFIER,
266 * hwSerialNum OCTET STRING }
267 */
268 struct {
269 mbedtls_x509_buf oid; /**< The object identifier. */
270 mbedtls_x509_buf val; /**< The named value. */
271 }
272 hardware_module_name;
273 }
274 value;
275}
276mbedtls_x509_san_other_name;
277
278/**
279 * A structure for holding the parsed Subject Alternative Name,
280 * according to type.
281 *
282 * Future versions of the library may add new fields to this structure or
283 * to its embedded union and structure.
284 */
285typedef struct mbedtls_x509_subject_alternative_name {
286 int type; /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */
287 union {
Andrzej Kurek17473042023-04-30 14:11:23 -0400288 mbedtls_x509_san_other_name other_name;
Andrzej Kureke12b01d2023-01-10 06:47:38 -0500289 mbedtls_x509_name directory_name;
290 mbedtls_x509_buf unstructured_name; /**< The buffer for the unstructured types. rfc822Name, dnsName and uniformResourceIdentifier are currently supported. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +0100291 }
292 san; /**< A union of the supported SAN types */
293}
294mbedtls_x509_subject_alternative_name;
295
Andrzej Kurek1a752692023-04-01 09:44:11 -0400296typedef struct mbedtls_x509_san_list {
297 mbedtls_x509_subject_alternative_name node;
298 struct mbedtls_x509_san_list *next;
299}
300mbedtls_x509_san_list;
301
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200302/** \} name Structures for parsing X.509 certificates, CRLs and CSRs */
Valerio Setti25b282e2024-01-17 10:55:32 +0100303/** \} addtogroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000304
Paul Bakker37ca75d2011-01-06 12:28:03 +0000305/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000307 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000308 *
309 * \param buf Buffer to write to
310 * \param size Maximum size of buffer
311 * \param dn The X509 name to represent
312 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200313 * \return The length of the string written (not including the
314 * terminated nul byte), or a negative error code.
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100316int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn);
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400318/**
David Horstmannef950cc2024-02-26 13:59:43 +0000319 * \brief Convert the certificate DN string \p name into
320 * a linked list of mbedtls_x509_name (equivalent to
321 * mbedtls_asn1_named_data).
322 *
323 * \note This function allocates a linked list, and places the head
324 * pointer in \p head. This list must later be freed by a
325 * call to mbedtls_asn1_free_named_data_list().
326 *
327 * \param[out] head Address in which to store the pointer to the head of the
Manuel Pégourié-Gonnard43a1e732025-05-05 16:41:52 +0200328 * allocated list of mbedtls_x509_name. Must point to NULL on
329 * entry.
David Horstmannef950cc2024-02-26 13:59:43 +0000330 * \param[in] name The string representation of a DN to convert
331 *
332 * \return 0 on success, or a negative error code.
333 */
334int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name);
335
336/**
David Horstmann762f9f92024-02-19 11:07:50 +0000337 * \brief Return the next relative DN in an X509 name.
338 *
339 * \note Intended use is to compare function result to dn->next
340 * in order to detect boundaries of multi-valued RDNs.
341 *
342 * \param dn Current node in the X509 name
343 *
344 * \return Pointer to the first attribute-value pair of the
345 * next RDN in sequence, or NULL if end is reached.
346 */
347static 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
356/**
357 * \brief Store the certificate serial in printable form into buf;
358 * no more than size characters will be written.
359 *
360 * \param buf Buffer to write to
361 * \param size Maximum size of buffer
362 * \param serial The X509 serial to represent
363 *
364 * \return The length of the string written (not including the
365 * terminated nul byte), or a negative error code.
366 */
367int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial);
368
369/**
370 * \brief Compare pair of mbedtls_x509_time.
371 *
372 * \param t1 mbedtls_x509_time to compare
373 * \param t2 mbedtls_x509_time to compare
374 *
375 * \return < 0 if t1 is before t2
376 * 0 if t1 equals t2
377 * > 0 if t1 is after t2
378 */
379int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1, const mbedtls_x509_time *t2);
380
381#if defined(MBEDTLS_HAVE_TIME_DATE)
382/**
383 * \brief Fill mbedtls_x509_time with provided mbedtls_time_t.
384 *
385 * \param tt mbedtls_time_t to convert
386 * \param now mbedtls_x509_time to fill with converted mbedtls_time_t
387 *
388 * \return \c 0 on success
389 * \return A non-zero return value on failure.
390 */
391int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now);
392#endif /* MBEDTLS_HAVE_TIME_DATE */
393
394/**
395 * \brief Check a given mbedtls_x509_time against the system time
396 * and tell if it's in the past.
397 *
398 * \note Intended usage is "if( is_past( valid_to ) ) ERROR".
399 * Hence the return value of 1 if on internal errors.
400 *
401 * \param to mbedtls_x509_time to check
402 *
403 * \return 1 if the given time is in the past or an error occurred,
404 * 0 otherwise.
405 */
406int mbedtls_x509_time_is_past(const mbedtls_x509_time *to);
407
408/**
409 * \brief Check a given mbedtls_x509_time against the system time
410 * and tell if it's in the future.
411 *
412 * \note Intended usage is "if( is_future( valid_from ) ) ERROR".
413 * Hence the return value of 1 if on internal errors.
414 *
415 * \param from mbedtls_x509_time to check
416 *
417 * \return 1 if the given time is in the future or an error occurred,
418 * 0 otherwise.
419 */
420int mbedtls_x509_time_is_future(const mbedtls_x509_time *from);
421
422/**
423 * \brief This function parses an item in the SubjectAlternativeNames
424 * extension. Please note that this function might allocate
425 * additional memory for a subject alternative name, thus
426 * mbedtls_x509_free_subject_alt_name has to be called
427 * to dispose of this additional memory afterwards.
428 *
429 * \param san_buf The buffer holding the raw data item of the subject
430 * alternative name.
431 * \param san The target structure to populate with the parsed presentation
432 * of the subject alternative name encoded in \p san_buf.
433 *
434 * \note Supported GeneralName types, as defined in RFC 5280:
435 * "rfc822Name", "dnsName", "directoryName",
436 * "uniformResourceIdentifier" and "hardware_module_name"
437 * of type "otherName", as defined in RFC 4108.
438 *
439 * \note This function should be called on a single raw data of
440 * subject alternative name. For example, after successful
441 * certificate parsing, one must iterate on every item in the
442 * \c crt->subject_alt_names sequence, and pass it to
443 * this function.
444 *
445 * \warning The target structure contains pointers to the raw data of the
446 * parsed certificate, and its lifetime is restricted by the
447 * lifetime of the certificate.
448 *
449 * \return \c 0 on success
450 * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported
451 * SAN type.
452 * \return Another negative value for any other failure.
453 */
454int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
455 mbedtls_x509_subject_alternative_name *san);
456/**
457 * \brief Unallocate all data related to subject alternative name
458 *
459 * \param san SAN structure - extra memory owned by this structure will be freed
460 */
461void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san);
462
463/**
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400464 * \brief This function parses a CN string as an IP address.
465 *
Andrzej Kurekc6beb3a2023-06-15 09:54:37 -0400466 * \param cn The CN string to parse. CN string MUST be null-terminated.
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400467 * \param dst The target buffer to populate with the binary IP address.
468 * The buffer MUST be 16 bytes to save IPv6, and should be
469 * 4-byte aligned if the result will be used as struct in_addr.
470 * e.g. uint32_t dst[4]
471 *
Andrzej Kurek5d9aeba2023-05-22 15:16:05 -0400472 * \note \p cn is parsed as an IPv6 address if string contains ':',
473 * else \p cn is parsed as an IPv4 address.
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400474 *
475 * \return Length of binary IP address; num bytes written to target.
476 * \return \c 0 on failure to parse CN string as an IP address.
477 */
478size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst);
479
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200480#define MBEDTLS_X509_SAFE_SNPRINTF \
481 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if (ret < 0 || (size_t) ret >= n) \
483 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; \
484 \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200485 n -= (size_t) ret; \
486 p += (size_t) ret; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 } while (0)
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200488
Paul Bakker30b95fa2013-10-01 10:09:06 +0200489#ifdef __cplusplus
490}
491#endif
492
Valerio Setti25b282e2024-01-17 10:55:32 +0100493#endif /* MBEDTLS_X509_H */