blob: e6d6a2cc10802af077e7f7ac8e0c11dde5ff9b51 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file x509_crt.h
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
4 * \brief X.509 certificate parsing and writing
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 Rodgman7ff79652023-11-03 12:04:52 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker7c6b2c32013-09-16 13:49:26 +02009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_X509_CRT_H
11#define MBEDTLS_X509_CRT_H
Paul Bakker7c6b2c32013-09-16 13:49:26 +020012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amero6609aef2019-07-04 20:01:14 +010014#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020015#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020017#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018
Jaeden Amero6609aef2019-07-04 20:01:14 +010019#include "mbedtls/x509.h"
20#include "mbedtls/x509_crl.h"
21#include "mbedtls/bignum.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020022
23/**
24 * \addtogroup x509_module
25 * \{
26 */
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/**
33 * \name Structures and functions for parsing and writing X.509 certificates
34 * \{
35 */
36
37/**
38 * Container for an X.509 certificate. The certificate may be chained.
39 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010040typedef struct mbedtls_x509_crt {
Hanno Becker1a65dcd2019-01-31 08:57:44 +000041 int own_buffer; /**< Indicates if \c raw is owned
42 * by the structure or not. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
44 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020046 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
48 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
51 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
54 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
57 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058
Hanno Becker494dd7a2019-02-06 16:13:41 +000059 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
63 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
64 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
Ron Eldor78c30402019-05-13 15:49:53 +030065 mbedtls_x509_sequence subject_alt_names; /**< Optional list of raw entries of Subject Alternative Names extension (currently only dNSName and OtherName are listed). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Ron Eldorc8b5f3f2019-05-15 15:15:55 +030067 mbedtls_x509_sequence certificate_policies; /**< Optional list of certificate policies (Only anyPolicy is printed and enforced, however the rest of the policies are still listed). */
Ron Eldor74d9acc2019-03-21 14:00:03 +020068
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069 int ext_types; /**< Bit string containing detected and parsed extensions */
70 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
71 int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. Path length is 1 higher than RFC 5280 'meaning', so 1+ */
72
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +010073 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020077 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
80 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
81 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
82 void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087
Janos Follath5091bec2019-05-08 15:23:08 +010088/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +020089 * From RFC 5280 section 4.2.1.6:
90 * OtherName ::= SEQUENCE {
91 * type-id OBJECT IDENTIFIER,
92 * value [0] EXPLICIT ANY DEFINED BY type-id }
93 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094typedef struct mbedtls_x509_san_other_name {
Ron Eldor890819a2019-05-13 19:03:04 +030095 /**
Tom Cosgrove2b150752022-05-26 11:55:43 +010096 * The type_id is an OID as defined in RFC 5280.
Ron Eldor890819a2019-05-13 19:03:04 +030097 * To check the value of the type id, you should use
98 * \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf.
99 */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200100 mbedtls_x509_buf type_id; /**< The type id. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 union {
Janos Follath5091bec2019-05-08 15:23:08 +0100102 /**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200103 * From RFC 4108 section 5:
104 * HardwareModuleName ::= SEQUENCE {
105 * hwType OBJECT IDENTIFIER,
106 * hwSerialNum OCTET STRING }
107 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 struct {
Ron Eldorf05f5942019-05-13 19:11:31 +0300109 mbedtls_x509_buf oid; /**< The object identifier. */
110 mbedtls_x509_buf val; /**< The named value. */
111 }
112 hardware_module_name;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200113 }
114 value;
115}
116mbedtls_x509_san_other_name;
117
Janos Follath5091bec2019-05-08 15:23:08 +0100118/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200119 * A structure for holding the parsed Subject Alternative Name, according to type
120 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121typedef struct mbedtls_x509_subject_alternative_name {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200122 int type; /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */
123 union {
124 mbedtls_x509_san_other_name other_name; /**< The otherName supported type. */
125 mbedtls_x509_buf unstructured_name; /**< The buffer for the un constructed types. Only dnsName currently supported */
126 }
127 san; /**< A union of the supported SAN types */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200128}
129mbedtls_x509_subject_alternative_name;
130
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200131/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200132 * Build flag from an algorithm/curve identifier (pk, md, ecp)
133 * Since 0 is always XXX_NONE, ignore it.
134 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135#define MBEDTLS_X509_ID_FLAG(id) (1 << ((id) - 1))
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200136
137/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200138 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200139 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200140 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200141 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142typedef struct mbedtls_x509_crt_profile {
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200143 uint32_t allowed_mds; /**< MDs for signatures */
Hanno Beckerf501cb52018-10-11 11:36:29 +0100144 uint32_t allowed_pks; /**< PK algs for public keys;
Manuel Pégourié-Gonnard2b28e4e2022-04-25 10:12:01 +0200145 * this applies to all certificates
Hanno Beckerf501cb52018-10-11 11:36:29 +0100146 * in the provided chain. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200147 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
148 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200149}
150mbedtls_x509_crt_profile;
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#define MBEDTLS_X509_CRT_VERSION_1 0
153#define MBEDTLS_X509_CRT_VERSION_2 1
154#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
157#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159#if !defined(MBEDTLS_X509_MAX_FILE_PATH_LEN)
Andres AGf9113192016-09-02 14:06:04 +0100160#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
161#endif
162
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163/**
164 * Container for writing a certificate (CRT)
165 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166typedef struct mbedtls_x509write_cert {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200167 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_mpi serial;
169 mbedtls_pk_context *subject_key;
170 mbedtls_pk_context *issuer_key;
171 mbedtls_asn1_named_data *subject;
172 mbedtls_asn1_named_data *issuer;
173 mbedtls_md_type_t md_alg;
174 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
175 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
176 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200177}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200179
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200180/**
181 * Item in a verification chain: cert and flags for it
182 */
183typedef struct {
184 mbedtls_x509_crt *crt;
185 uint32_t flags;
186} mbedtls_x509_crt_verify_chain_item;
187
188/**
189 * Max size of verification chain: end-entity + intermediates + trusted root
190 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200192
193/**
194 * Verification chain as built by \c mbedtls_crt_verify_chain()
195 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196typedef struct {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200197 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200198 unsigned len;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000199
200#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
201 /* This stores the list of potential trusted signers obtained from
202 * the CA callback used for the CRT verification, if configured.
203 * We must track it somewhere because the callback passes its
204 * ownership to the caller. */
205 mbedtls_x509_crt *trust_ca_cb_result;
206#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200207} mbedtls_x509_crt_verify_chain;
208
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200209#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
210
211/**
212 * \brief Context for resuming X.509 verify operations
213 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214typedef struct {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200215 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200216 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200217
218 /* for find_parent_in() */
219 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
220 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200221 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200222
223 /* for find_parent() */
224 int parent_is_trusted; /* -1 if find_parent is not in progress */
225
226 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200227 enum {
228 x509_crt_rs_none,
229 x509_crt_rs_find_parent,
230 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200231 int self_cnt;
232 mbedtls_x509_crt_verify_chain ver_chain;
233
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200234} mbedtls_x509_crt_restart_ctx;
235
236#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
237
238/* Now we can declare functions that take a pointer to that */
239typedef void mbedtls_x509_crt_restart_ctx;
240
241#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200244/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200245 * Default security profile. Should provide a good balance between security
246 * and compatibility with current deployments.
Gilles Peskine0ecd7192021-06-07 21:24:26 +0200247 *
248 * This profile permits:
249 * - SHA2 hashes.
250 * - All supported elliptic curves.
251 * - RSA with 2048 bits and above.
252 *
253 * New minor versions of Mbed TLS may extend this profile, for example if
254 * new curves are added to the library. New minor versions of Mbed TLS will
255 * not reduce this profile unless serious security concerns require it.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200256 */
257extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
258
259/**
260 * Expected next default profile. Recommended for new deployments.
Gilles Peskine0ecd7192021-06-07 21:24:26 +0200261 * Currently targets a 128-bit security level, except for allowing RSA-2048.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200262 */
263extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
264
265/**
266 * NSA Suite B profile.
267 */
268extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
269
270/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271 * \brief Parse a single DER formatted certificate and add it
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000272 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200273 *
Gilles Peskine07ae2082023-03-07 20:22:51 +0100274 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
275 * subsystem must have been initialized by calling
276 * psa_crypto_init() before calling this function.
277 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000278 * \param chain The pointer to the start of the CRT chain to attach to.
279 * When parsing the first CRT in a chain, this should point
280 * to an instance of ::mbedtls_x509_crt initialized through
281 * mbedtls_x509_crt_init().
282 * \param buf The buffer holding the DER encoded certificate.
283 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000285 * \note This function makes an internal copy of the CRT buffer
286 * \p buf. In particular, \p buf may be destroyed or reused
287 * after this call returns. To avoid duplicating the CRT
288 * buffer (at the cost of stricter lifetime constraints),
289 * use mbedtls_x509_crt_parse_der_nocopy() instead.
290 *
291 * \return \c 0 if successful.
292 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
295 const unsigned char *buf,
296 size_t buflen);
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000297
298/**
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200299 * \brief The type of certificate extension callbacks.
300 *
301 * Callbacks of this type are passed to and used by the
Nicola Di Lietofde98f72020-05-28 08:34:33 +0200302 * mbedtls_x509_crt_parse_der_with_ext_cb() routine when
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200303 * it encounters either an unsupported extension or a
304 * "certificate policies" extension containing any
305 * unsupported certificate policies.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200306 * Future versions of the library may invoke the callback
307 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200308 *
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200309 * \param p_ctx An opaque context passed to the callback.
ndilieto6e249802020-05-28 06:17:38 +0000310 * \param crt The certificate being parsed.
311 * \param oid The OID of the extension.
312 * \param critical Whether the extension is critical.
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200313 * \param p Pointer to the start of the extension value
ndilieto6e249802020-05-28 06:17:38 +0000314 * (the content of the OCTET STRING).
ndilieto6e249802020-05-28 06:17:38 +0000315 * \param end End of extension value.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200316 *
317 * \note The callback must fail and return a negative error code
318 * if it can not parse or does not support the extension.
319 * When the callback fails to parse a critical extension
320 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
321 * When the callback fails to parse a non critical extension
322 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
323 * the extension and continues parsing.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200324 *
325 * \return \c 0 on success.
326 * \return A negative error code on failure.
327 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328typedef int (*mbedtls_x509_crt_ext_cb_t)(void *p_ctx,
329 mbedtls_x509_crt const *crt,
330 mbedtls_x509_buf const *oid,
331 int critical,
332 const unsigned char *p,
333 const unsigned char *end);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200334
335/**
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200336 * \brief Parse a single DER formatted certificate and add it
337 * to the end of the provided chained list.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200338 *
Gilles Peskine07ae2082023-03-07 20:22:51 +0100339 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
340 * subsystem must have been initialized by calling
341 * psa_crypto_init() before calling this function.
342 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200343 * \param chain The pointer to the start of the CRT chain to attach to.
344 * When parsing the first CRT in a chain, this should point
345 * to an instance of ::mbedtls_x509_crt initialized through
346 * mbedtls_x509_crt_init().
347 * \param buf The buffer holding the DER encoded certificate.
348 * \param buflen The size in Bytes of \p buf.
349 * \param make_copy When not zero this function makes an internal copy of the
350 * CRT buffer \p buf. In particular, \p buf may be destroyed
351 * or reused after this call returns.
352 * When zero this function avoids duplicating the CRT buffer
353 * by taking temporary ownership thereof until the CRT
354 * is destroyed (like mbedtls_x509_crt_parse_der_nocopy())
355 * \param cb A callback invoked for every unsupported certificate
356 * extension.
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200357 * \param p_ctx An opaque context passed to the callback.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200358 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200359 * \note This call is functionally equivalent to
360 * mbedtls_x509_crt_parse_der(), and/or
361 * mbedtls_x509_crt_parse_der_nocopy()
362 * but it calls the callback with every unsupported
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200363 * certificate extension and additionally the
364 * "certificate policies" extension if it contains any
365 * unsupported certificate policies.
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200366 * The callback must return a negative error code if it
367 * does not know how to handle such an extension.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200368 * When the callback fails to parse a critical extension
369 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
370 * When the callback fails to parse a non critical extension
371 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
372 * the extension and continues parsing.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200373 * Future versions of the library may invoke the callback
374 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200375 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200376 * \return \c 0 if successful.
377 * \return A negative error code on failure.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200378 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
380 const unsigned char *buf,
381 size_t buflen,
382 int make_copy,
383 mbedtls_x509_crt_ext_cb_t cb,
384 void *p_ctx);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200385
386/**
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000387 * \brief Parse a single DER formatted certificate and add it
388 * to the end of the provided chained list. This is a
389 * variant of mbedtls_x509_crt_parse_der() which takes
390 * temporary ownership of the CRT buffer until the CRT
391 * is destroyed.
392 *
Gilles Peskine07ae2082023-03-07 20:22:51 +0100393 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
394 * subsystem must have been initialized by calling
395 * psa_crypto_init() before calling this function.
396 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000397 * \param chain The pointer to the start of the CRT chain to attach to.
398 * When parsing the first CRT in a chain, this should point
399 * to an instance of ::mbedtls_x509_crt initialized through
400 * mbedtls_x509_crt_init().
401 * \param buf The address of the readable buffer holding the DER encoded
402 * certificate to use. On success, this buffer must be
403 * retained and not be changed for the liftetime of the
404 * CRT chain \p chain, that is, until \p chain is destroyed
405 * through a call to mbedtls_x509_crt_free().
406 * \param buflen The size in Bytes of \p buf.
407 *
408 * \note This call is functionally equivalent to
409 * mbedtls_x509_crt_parse_der(), but it avoids creating a
410 * copy of the input buffer at the cost of stronger lifetime
411 * constraints. This is useful in constrained environments
412 * where duplication of the CRT cannot be tolerated.
413 *
414 * \return \c 0 if successful.
415 * \return A negative error code on failure.
416 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
418 const unsigned char *buf,
419 size_t buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420
421/**
Hanno Becker89a91122018-08-23 16:14:00 +0100422 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100423 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100425 * For CRTs in PEM encoding, the function parses permissively:
426 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100427 * returns the number of certificates for which parsing failed
428 * (hence \c 0 if all certificates were parsed successfully).
429 * If no certificate could be parsed, the function returns
430 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100432 * PEM encoded certificates may be interleaved by other data
433 * such as human readable descriptions of their content, as
434 * long as the certificates are enclosed in the PEM specific
435 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
436 *
Gilles Peskine07ae2082023-03-07 20:22:51 +0100437 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
438 * subsystem must have been initialized by calling
439 * psa_crypto_init() before calling this function.
440 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100441 * \param chain The chain to which to add the parsed certificates.
442 * \param buf The buffer holding the certificate data in PEM or DER format.
443 * For certificates in PEM encoding, this may be a concatenation
444 * of multiple certificates; for DER encoding, the buffer must
445 * comprise exactly one certificate.
446 * \param buflen The size of \p buf, including the terminating \c NULL byte
447 * in case of PEM encoded data.
448 *
449 * \return \c 0 if all certificates were parsed successfully.
450 * \return The (positive) number of certificates that couldn't
451 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100452 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100453 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458/**
459 * \brief Load one or more certificates and add them
460 * to the chained list. Parses permissively. If some
461 * certificates can be parsed, the result is the number
462 * of failed certificates it encountered. If none complete
463 * correctly, the first error is returned.
464 *
Gilles Peskine07ae2082023-03-07 20:22:51 +0100465 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
466 * subsystem must have been initialized by calling
467 * psa_crypto_init() before calling this function.
468 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469 * \param chain points to the start of the chain
470 * \param path filename to read the certificates from
471 *
472 * \return 0 if all certificates parsed successfully, a positive number
473 * if partly successful or a specific X509 or PEM error code
474 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100475int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476
477/**
478 * \brief Load one or more certificate files from a path and add them
479 * to the chained list. Parses permissively. If some
480 * certificates can be parsed, the result is the number
481 * of failed certificates it encountered. If none complete
482 * correctly, the first error is returned.
483 *
484 * \param chain points to the start of the chain
485 * \param path directory / folder to read the certificate files from
486 *
487 * \return 0 if all certificates parsed successfully, a positive number
488 * if partly successful or a specific X509 or PEM error code
489 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path);
Janos Follath11b41eb2019-05-08 15:26:49 +0100491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#endif /* MBEDTLS_FS_IO */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200493/**
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300494 * \brief This function parses an item in the SubjectAlternativeNames
495 * extension.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200496 *
Ron Eldor890819a2019-05-13 19:03:04 +0300497 * \param san_buf The buffer holding the raw data item of the subject
498 * alternative name.
499 * \param san The target structure to populate with the parsed presentation
Andrzej Kurekdb54dbd2023-07-14 05:35:38 -0400500 * of the subject alternative name encoded in \p san_buf.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200501 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300502 * \note Only "dnsName" and "otherName" of type hardware_module_name
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200503 * as defined in RFC 4180 is supported.
504 *
Ron Eldor890819a2019-05-13 19:03:04 +0300505 * \note This function should be called on a single raw data of
Ron Eldorcc45cd12019-05-15 10:20:09 +0300506 * subject alternative name. For example, after successful
Ron Eldor890819a2019-05-13 19:03:04 +0300507 * certificate parsing, one must iterate on every item in the
Andrzej Kurekdb54dbd2023-07-14 05:35:38 -0400508 * \c crt->subject_alt_names sequence, and pass it to
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300509 * this function.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200510 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300511 * \warning The target structure contains pointers to the raw data of the
Ron Eldorcc45cd12019-05-15 10:20:09 +0300512 * parsed certificate, and its lifetime is restricted by the
513 * lifetime of the certificate.
514 *
Ron Eldor890819a2019-05-13 19:03:04 +0300515 * \return \c 0 on success
516 * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300517 * SAN type.
518 * \return Another negative value for any other failure.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200519 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
521 mbedtls_x509_subject_alternative_name *san);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522/**
523 * \brief Returns an informational string about the
524 * certificate.
525 *
526 * \param buf Buffer to write to
527 * \param size Maximum size of buffer
528 * \param prefix A line prefix
529 * \param crt The X509 certificate to represent
530 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200531 * \return The length of the string written (not including the
532 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
535 const mbedtls_x509_crt *crt);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536
537/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100538 * \brief Returns an informational string about the
539 * verification status of a certificate.
540 *
541 * \param buf Buffer to write to
542 * \param size Maximum size of buffer
543 * \param prefix A line prefix
544 * \param flags Verification flags created by mbedtls_x509_crt_verify()
545 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200546 * \return The length of the string written (not including the
547 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100548 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100549int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
550 uint32_t flags);
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200551
552/**
Hanno Becker902451d2019-03-27 11:48:40 +0000553 * \brief Verify a chain of certificates.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554 *
555 * The verify callback is a user-supplied callback that
556 * can clear / modify / add flags for a certificate. If set,
557 * the verification callback is called for each
558 * certificate in the chain (from the trust-ca down to the
559 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561 * int *flags). With the flags representing current flags for
562 * that specific certificate and the certificate depth from
563 * the bottom (Peer cert depth = 0).
564 *
565 * All flags left after returning from the callback
566 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200567 * return 0 for anything (including invalid certificates)
568 * other than fatal error, as a non-zero return code
569 * immediately aborts the verification process. For fatal
570 * errors, a specific error code should be used (different
571 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
572 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
573 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100575 * \note In case verification failed, the results can be displayed
576 * using \c mbedtls_x509_crt_verify_info()
577 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200578 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
579 * default security profile.
580 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100581 * \note It is your responsibility to provide up-to-date CRLs for
582 * all trusted CAs. If no CRL is provided for the CA that was
583 * used to sign the certificate, CRL verification is skipped
584 * silently, that is *without* setting any flag.
585 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200586 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200587 * (1) those of trusted root CAs, so that certificates
588 * chaining up to those CAs will be trusted, and (2)
589 * self-signed end-entity certificates to be trusted (for
590 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200591 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200592 *
Hanno Becker902451d2019-03-27 11:48:40 +0000593 * \param crt The certificate chain to be verified.
594 * \param trust_ca The list of trusted CAs.
595 * \param ca_crl The list of CRLs for trusted CAs.
Manuel Pégourié-Gonnardf58e5cc2020-07-24 10:31:37 +0200596 * \param cn The expected Common Name. This will be checked to be
597 * present in the certificate's subjectAltNames extension or,
598 * if this extension is absent, as a CN component in its
599 * Subject name. Currently only DNS names are supported. This
600 * may be \c NULL if the CN need not be verified.
Hanno Becker902451d2019-03-27 11:48:40 +0000601 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100602 * If the verification couldn't be completed, the flag value is
603 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000604 * \param f_vrfy The verification callback to use. See the documentation
605 * of mbedtls_x509_crt_verify() for more information.
606 * \param p_vrfy The context to be passed to \p f_vrfy.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200607 *
Hanno Becker902451d2019-03-27 11:48:40 +0000608 * \return \c 0 if the chain is valid with respect to the
609 * passed CN, CAs, CRLs and security profile.
610 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
611 * certificate chain verification failed. In this case,
612 * \c *flags will have one or more
613 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
614 * flags set.
615 * \return Another negative error code in case of a fatal error
616 * encountered during the verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100618int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
619 mbedtls_x509_crt *trust_ca,
620 mbedtls_x509_crl *ca_crl,
621 const char *cn, uint32_t *flags,
622 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
623 void *p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200625/**
Hanno Becker902451d2019-03-27 11:48:40 +0000626 * \brief Verify a chain of certificates with respect to
627 * a configurable security profile.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200628 *
629 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
630 * security profile.
631 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200632 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200633 * for ECDSA) apply to all certificates: trusted root,
634 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200635 *
Hanno Becker902451d2019-03-27 11:48:40 +0000636 * \param crt The certificate chain to be verified.
637 * \param trust_ca The list of trusted CAs.
638 * \param ca_crl The list of CRLs for trusted CAs.
639 * \param profile The security profile to use for the verification.
640 * \param cn The expected Common Name. This may be \c NULL if the
641 * CN need not be verified.
642 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100643 * If the verification couldn't be completed, the flag value is
644 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000645 * \param f_vrfy The verification callback to use. See the documentation
646 * of mbedtls_x509_crt_verify() for more information.
647 * \param p_vrfy The context to be passed to \p f_vrfy.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200648 *
Hanno Becker902451d2019-03-27 11:48:40 +0000649 * \return \c 0 if the chain is valid with respect to the
650 * passed CN, CAs, CRLs and security profile.
651 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
652 * certificate chain verification failed. In this case,
653 * \c *flags will have one or more
654 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
655 * flags set.
656 * \return Another negative error code in case of a fatal error
657 * encountered during the verification process.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200658 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
660 mbedtls_x509_crt *trust_ca,
661 mbedtls_x509_crl *ca_crl,
662 const mbedtls_x509_crt_profile *profile,
663 const char *cn, uint32_t *flags,
664 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
665 void *p_vrfy);
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200666
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200667/**
668 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
669 *
670 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
671 * but can return early and restart according to the limit
672 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
673 *
Hanno Becker902451d2019-03-27 11:48:40 +0000674 * \param crt The certificate chain to be verified.
675 * \param trust_ca The list of trusted CAs.
676 * \param ca_crl The list of CRLs for trusted CAs.
677 * \param profile The security profile to use for the verification.
678 * \param cn The expected Common Name. This may be \c NULL if the
679 * CN need not be verified.
680 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100681 * If the verification couldn't be completed, the flag value is
682 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000683 * \param f_vrfy The verification callback to use. See the documentation
684 * of mbedtls_x509_crt_verify() for more information.
685 * \param p_vrfy The context to be passed to \p f_vrfy.
686 * \param rs_ctx The restart context to use. This may be set to \c NULL
687 * to disable restartable ECC.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200688 *
689 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200690 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200691 * operations was reached: see \c mbedtls_ecp_set_max_ops().
692 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
694 mbedtls_x509_crt *trust_ca,
695 mbedtls_x509_crl *ca_crl,
696 const mbedtls_x509_crt_profile *profile,
697 const char *cn, uint32_t *flags,
698 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
699 void *p_vrfy,
700 mbedtls_x509_crt_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200701
Hanno Becker5c8df782019-03-27 11:01:17 +0000702/**
703 * \brief The type of trusted certificate callbacks.
704 *
705 * Callbacks of this type are passed to and used by the CRT
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300706 * verification routine mbedtls_x509_crt_verify_with_ca_cb()
Hanno Becker5c8df782019-03-27 11:01:17 +0000707 * when looking for trusted signers of a given certificate.
708 *
709 * On success, the callback returns a list of trusted
710 * certificates to be considered as potential signers
711 * for the input certificate.
712 *
713 * \param p_ctx An opaque context passed to the callback.
714 * \param child The certificate for which to search a potential signer.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300715 * This will point to a readable certificate.
Hanno Becker5c8df782019-03-27 11:01:17 +0000716 * \param candidate_cas The address at which to store the address of the first
717 * entry in the generated linked list of candidate signers.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300718 * This will not be \c NULL.
Hanno Becker5c8df782019-03-27 11:01:17 +0000719 *
720 * \note The callback must only return a non-zero value on a
721 * fatal error. If, in contrast, the search for a potential
722 * signer completes without a single candidate, the
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300723 * callback must return \c 0 and set \c *candidate_cas
Hanno Becker5c8df782019-03-27 11:01:17 +0000724 * to \c NULL.
725 *
726 * \return \c 0 on success. In this case, \c *candidate_cas points
727 * to a heap-allocated linked list of instances of
728 * ::mbedtls_x509_crt, and ownership of this list is passed
729 * to the caller.
730 * \return A negative error code on failure.
731 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100732typedef int (*mbedtls_x509_crt_ca_cb_t)(void *p_ctx,
733 mbedtls_x509_crt const *child,
734 mbedtls_x509_crt **candidate_cas);
Hanno Beckere15dae72019-03-28 13:55:38 +0000735
736#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker5c8df782019-03-27 11:01:17 +0000737/**
738 * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
739 * uses a callback to acquire the list of trusted CA
740 * certificates.
741 *
742 * \param crt The certificate chain to be verified.
743 * \param f_ca_cb The callback to be used to query for potential signers
744 * of a given child certificate. See the documentation of
745 * ::mbedtls_x509_crt_ca_cb_t for more information.
746 * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
747 * \param profile The security profile for the verification.
748 * \param cn The expected Common Name. This may be \c NULL if the
749 * CN need not be verified.
750 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100751 * If the verification couldn't be completed, the flag value is
752 * set to (uint32_t) -1.
Hanno Becker5c8df782019-03-27 11:01:17 +0000753 * \param f_vrfy The verification callback to use. See the documentation
754 * of mbedtls_x509_crt_verify() for more information.
755 * \param p_vrfy The context to be passed to \p f_vrfy.
756 *
757 * \return See \c mbedtls_crt_verify_with_profile().
758 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100759int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
760 mbedtls_x509_crt_ca_cb_t f_ca_cb,
761 void *p_ca_cb,
762 const mbedtls_x509_crt_profile *profile,
763 const char *cn, uint32_t *flags,
764 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
765 void *p_vrfy);
Hanno Becker5c8df782019-03-27 11:01:17 +0000766
767#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200770/**
771 * \brief Check usage of certificate against keyUsage extension.
772 *
773 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200774 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
775 * before using the certificate to perform an RSA key
776 * exchange).
777 *
778 * \note Except for decipherOnly and encipherOnly, a bit set in the
779 * usage argument means this bit MUST be set in the
780 * certificate. For decipherOnly and encipherOnly, it means
781 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200782 *
783 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200785 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200786 *
787 * \note You should only call this function on leaf certificates, on
788 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200790 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100791int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
792 unsigned int usage);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200796/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000797 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200798 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000799 * \param crt Leaf certificate used.
800 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
801 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200803 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000804 * \return 0 if this use of the certificate is allowed,
805 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200806 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000807 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200808 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100809int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
810 const char *usage_oid,
811 size_t usage_len);
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000812#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200816 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200817 *
818 * \param crt a certificate to be verified
819 * \param crl the CRL to verify against
820 *
821 * \return 1 if the certificate is revoked, 0 otherwise
822 *
823 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100824int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826
827/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200828 * \brief Initialize a certificate (chain)
829 *
830 * \param crt Certificate chain to initialize
831 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100832void mbedtls_x509_crt_init(mbedtls_x509_crt *crt);
Paul Bakker369d2eb2013-09-18 11:58:25 +0200833
834/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200835 * \brief Unallocate all certificate data
836 *
837 * \param crt Certificate chain to free
838 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839void mbedtls_x509_crt_free(mbedtls_x509_crt *crt);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200840
841#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
842/**
843 * \brief Initialize a restart context
844 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100845void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200846
847/**
848 * \brief Free the components of a restart context
849 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100850void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200851#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200853
Andrzej Kurek73afe272022-01-24 10:31:06 -0500854/** \} name Structures and functions for parsing and writing X.509 certificates */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200857/**
858 * \brief Initialize a CRT writing context
859 *
860 * \param ctx CRT context to initialize
861 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100862void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200863
864/**
Shaun Case0e7791f2021-12-20 21:14:10 -0800865 * \brief Set the version for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200867 *
868 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
870 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200871 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100872void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx, int version);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873
874/**
875 * \brief Set the serial number for a Certificate.
876 *
877 * \param ctx CRT context to use
878 * \param serial serial number to set
879 *
880 * \return 0 if successful
881 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100882int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883
884/**
885 * \brief Set the validity period for a Certificate
886 * Timestamps should be in string format for UTC timezone
887 * i.e. "YYYYMMDDhhmmss"
888 * e.g. "20131231235959" for December 31st 2013
889 * at 23:59:59
890 *
891 * \param ctx CRT context to use
892 * \param not_before not_before timestamp
893 * \param not_after not_after timestamp
894 *
895 * \return 0 if timestamp was parsed successfully, or
896 * a specific error code
897 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100898int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx, const char *not_before,
899 const char *not_after);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900
901/**
902 * \brief Set the issuer name for a Certificate
903 * Issuer names should contain a comma-separated list
904 * of OID types and values:
Gilles Peskinef08ca832023-09-12 19:21:54 +0200905 * e.g. "C=UK,O=ARM,CN=Mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906 *
907 * \param ctx CRT context to use
908 * \param issuer_name issuer name to set
909 *
910 * \return 0 if issuer name was parsed successfully, or
911 * a specific error code
912 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100913int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx,
914 const char *issuer_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915
916/**
917 * \brief Set the subject name for a Certificate
918 * Subject names should contain a comma-separated list
919 * of OID types and values:
Gilles Peskinef08ca832023-09-12 19:21:54 +0200920 * e.g. "C=UK,O=ARM,CN=Mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 *
922 * \param ctx CRT context to use
923 * \param subject_name subject name to set
924 *
925 * \return 0 if subject name was parsed successfully, or
926 * a specific error code
927 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100928int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx,
929 const char *subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930
931/**
932 * \brief Set the subject public key for the certificate
933 *
934 * \param ctx CRT context to use
935 * \param key public key to include
936 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100937void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx, mbedtls_pk_context *key);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938
939/**
940 * \brief Set the issuer key used for signing the certificate
941 *
942 * \param ctx CRT context to use
943 * \param key private key to sign with
944 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100945void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx, mbedtls_pk_context *key);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946
947/**
948 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950 *
951 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100952 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100954void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955
956/**
957 * \brief Generic function to add to or replace an extension in the
958 * CRT
959 *
960 * \param ctx CRT context to use
961 * \param oid OID of the extension
962 * \param oid_len length of the OID
963 * \param critical if the extension is critical (per the RFC's definition)
964 * \param val value of the extension OCTET STRING
965 * \param val_len length of the value data
966 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200967 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100969int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
970 const char *oid, size_t oid_len,
971 int critical,
972 const unsigned char *val, size_t val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973
974/**
975 * \brief Set the basicConstraints extension for a CRT
976 *
977 * \param ctx CRT context to use
978 * \param is_ca is this a CA certificate
979 * \param max_pathlen maximum length of certificate chains below this
980 * certificate (only for CA certificates, -1 is
Tom Cosgrove2b150752022-05-26 11:55:43 +0100981 * unlimited)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200983 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100985int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
986 int is_ca, int max_pathlen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989/**
990 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 * called before
993 *
994 * \param ctx CRT context to use
995 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200996 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100998int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999
1000/**
1001 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003 * called before
1004 *
1005 * \param ctx CRT context to use
1006 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001007 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001009int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011
1012/**
1013 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015 *
1016 * \param ctx CRT context to use
1017 * \param key_usage key usage flags to set
1018 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001019 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001021int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
1022 unsigned int key_usage);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023
1024/**
1025 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 *
1028 * \param ctx CRT context to use
1029 * \param ns_cert_type Netscape Cert Type flags to set
1030 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001031 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001033int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
1034 unsigned char ns_cert_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035
1036/**
1037 * \brief Free the contents of a CRT write context
1038 *
1039 * \param ctx CRT context to free
1040 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001041void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042
1043/**
1044 * \brief Write a built up certificate to a X509 DER structure
1045 * Note: data is written at the end of the buffer! Use the
1046 * return value to determine where you should start
1047 * using the buffer
1048 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001049 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050 * \param buf buffer to write to
1051 * \param size size of the buffer
1052 * \param f_rng RNG function (for signature, see note)
1053 * \param p_rng RNG parameter
1054 *
1055 * \return length of data written if successful, or a specific
1056 * error code
1057 *
1058 * \note f_rng may be NULL if RSA is used for signature and the
1059 * signature is made offline (otherwise f_rng is desirable
1060 * for countermeasures against timing attacks).
1061 * ECDSA signatures always require a non-NULL f_rng.
1062 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001063int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
1064 int (*f_rng)(void *, unsigned char *, size_t),
1065 void *p_rng);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068/**
1069 * \brief Write a built up certificate to a X509 PEM string
1070 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001071 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072 * \param buf buffer to write to
1073 * \param size size of the buffer
1074 * \param f_rng RNG function (for signature, see note)
1075 * \param p_rng RNG parameter
1076 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001077 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078 *
1079 * \note f_rng may be NULL if RSA is used for signature and the
1080 * signature is made offline (otherwise f_rng is desirable
1081 * for countermeasures against timing attacks).
1082 * ECDSA signatures always require a non-NULL f_rng.
1083 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001084int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
1085 int (*f_rng)(void *, unsigned char *, size_t),
1086 void *p_rng);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#endif /* MBEDTLS_PEM_WRITE_C */
1088#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089
Andrzej Kurekff632d52022-01-24 10:32:00 -05001090/** \} addtogroup x509_module */
1091
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092#ifdef __cplusplus
1093}
1094#endif
1095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#endif /* mbedtls_x509_crt.h */