blob: 466611f79782092fcb4a6a861c8c0f41973b52df [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#ifndef MBEDTLS_X509_CRT_H
23#define MBEDTLS_X509_CRT_H
Paul Bakker7c6b2c32013-09-16 13:49:26 +020024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amero6609aef2019-07-04 20:01:14 +010026#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030
Jaeden Amero6609aef2019-07-04 20:01:14 +010031#include "mbedtls/x509.h"
32#include "mbedtls/x509_crl.h"
33#include "mbedtls/bignum.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034
35/**
36 * \addtogroup x509_module
37 * \{
38 */
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/**
45 * \name Structures and functions for parsing and writing X.509 certificates
46 * \{
47 */
48
49/**
50 * Container for an X.509 certificate. The certificate may be chained.
51 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010052typedef struct mbedtls_x509_crt {
Hanno Becker1a65dcd2019-01-31 08:57:44 +000053 int own_buffer; /**< Indicates if \c raw is owned
54 * by the structure or not. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
56 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020058 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
60 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
63 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
66 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
69 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070
Hanno Becker494dd7a2019-02-06 16:13:41 +000071 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
75 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
76 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
Ron Eldor78c30402019-05-13 15:49:53 +030077 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 +020078
Ron Eldorc8b5f3f2019-05-15 15:15:55 +030079 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 +020080
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081 int ext_types; /**< Bit string containing detected and parsed extensions */
82 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
83 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+ */
84
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +010085 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020089 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
92 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
93 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
94 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 +020095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099
Janos Follath5091bec2019-05-08 15:23:08 +0100100/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200101 * From RFC 5280 section 4.2.1.6:
102 * OtherName ::= SEQUENCE {
103 * type-id OBJECT IDENTIFIER,
104 * value [0] EXPLICIT ANY DEFINED BY type-id }
105 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106typedef struct mbedtls_x509_san_other_name {
Ron Eldor890819a2019-05-13 19:03:04 +0300107 /**
Tom Cosgrove2b150752022-05-26 11:55:43 +0100108 * The type_id is an OID as defined in RFC 5280.
Ron Eldor890819a2019-05-13 19:03:04 +0300109 * To check the value of the type id, you should use
110 * \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf.
111 */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200112 mbedtls_x509_buf type_id; /**< The type id. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 union {
Janos Follath5091bec2019-05-08 15:23:08 +0100114 /**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200115 * From RFC 4108 section 5:
116 * HardwareModuleName ::= SEQUENCE {
117 * hwType OBJECT IDENTIFIER,
118 * hwSerialNum OCTET STRING }
119 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 struct {
Ron Eldorf05f5942019-05-13 19:11:31 +0300121 mbedtls_x509_buf oid; /**< The object identifier. */
122 mbedtls_x509_buf val; /**< The named value. */
123 }
124 hardware_module_name;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200125 }
126 value;
127}
128mbedtls_x509_san_other_name;
129
Janos Follath5091bec2019-05-08 15:23:08 +0100130/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200131 * A structure for holding the parsed Subject Alternative Name, according to type
132 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133typedef struct mbedtls_x509_subject_alternative_name {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200134 int type; /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */
135 union {
136 mbedtls_x509_san_other_name other_name; /**< The otherName supported type. */
137 mbedtls_x509_buf unstructured_name; /**< The buffer for the un constructed types. Only dnsName currently supported */
138 }
139 san; /**< A union of the supported SAN types */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200140}
141mbedtls_x509_subject_alternative_name;
142
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200143/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200144 * Build flag from an algorithm/curve identifier (pk, md, ecp)
145 * Since 0 is always XXX_NONE, ignore it.
146 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147#define MBEDTLS_X509_ID_FLAG(id) (1 << ((id) - 1))
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200148
149/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200150 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200151 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200152 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200153 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154typedef struct mbedtls_x509_crt_profile {
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200155 uint32_t allowed_mds; /**< MDs for signatures */
Hanno Beckerf501cb52018-10-11 11:36:29 +0100156 uint32_t allowed_pks; /**< PK algs for public keys;
Manuel Pégourié-Gonnard2b28e4e2022-04-25 10:12:01 +0200157 * this applies to all certificates
Hanno Beckerf501cb52018-10-11 11:36:29 +0100158 * in the provided chain. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200159 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
160 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200161}
162mbedtls_x509_crt_profile;
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#define MBEDTLS_X509_CRT_VERSION_1 0
165#define MBEDTLS_X509_CRT_VERSION_2 1
166#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
169#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171#if !defined(MBEDTLS_X509_MAX_FILE_PATH_LEN)
Andres AGf9113192016-09-02 14:06:04 +0100172#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
173#endif
174
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200175/**
176 * Container for writing a certificate (CRT)
177 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178typedef struct mbedtls_x509write_cert {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200179 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_mpi serial;
181 mbedtls_pk_context *subject_key;
182 mbedtls_pk_context *issuer_key;
183 mbedtls_asn1_named_data *subject;
184 mbedtls_asn1_named_data *issuer;
185 mbedtls_md_type_t md_alg;
186 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
187 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
188 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200189}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200191
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200192/**
193 * Item in a verification chain: cert and flags for it
194 */
195typedef struct {
196 mbedtls_x509_crt *crt;
197 uint32_t flags;
198} mbedtls_x509_crt_verify_chain_item;
199
200/**
201 * Max size of verification chain: end-entity + intermediates + trusted root
202 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200204
205/**
206 * Verification chain as built by \c mbedtls_crt_verify_chain()
207 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208typedef struct {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200209 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200210 unsigned len;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000211
212#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
213 /* This stores the list of potential trusted signers obtained from
214 * the CA callback used for the CRT verification, if configured.
215 * We must track it somewhere because the callback passes its
216 * ownership to the caller. */
217 mbedtls_x509_crt *trust_ca_cb_result;
218#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200219} mbedtls_x509_crt_verify_chain;
220
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200221#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
222
223/**
224 * \brief Context for resuming X.509 verify operations
225 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226typedef struct {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200227 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200228 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200229
230 /* for find_parent_in() */
231 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
232 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200233 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200234
235 /* for find_parent() */
236 int parent_is_trusted; /* -1 if find_parent is not in progress */
237
238 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200239 enum {
240 x509_crt_rs_none,
241 x509_crt_rs_find_parent,
242 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200243 int self_cnt;
244 mbedtls_x509_crt_verify_chain ver_chain;
245
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200246} mbedtls_x509_crt_restart_ctx;
247
248#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
249
250/* Now we can declare functions that take a pointer to that */
251typedef void mbedtls_x509_crt_restart_ctx;
252
253#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200257 * Default security profile. Should provide a good balance between security
258 * and compatibility with current deployments.
Gilles Peskine0ecd7192021-06-07 21:24:26 +0200259 *
260 * This profile permits:
261 * - SHA2 hashes.
262 * - All supported elliptic curves.
263 * - RSA with 2048 bits and above.
264 *
265 * New minor versions of Mbed TLS may extend this profile, for example if
266 * new curves are added to the library. New minor versions of Mbed TLS will
267 * not reduce this profile unless serious security concerns require it.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200268 */
269extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
270
271/**
272 * Expected next default profile. Recommended for new deployments.
Gilles Peskine0ecd7192021-06-07 21:24:26 +0200273 * Currently targets a 128-bit security level, except for allowing RSA-2048.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200274 */
275extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
276
277/**
278 * NSA Suite B profile.
279 */
280extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
281
282/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283 * \brief Parse a single DER formatted certificate and add it
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000284 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285 *
Gilles Peskine07ae2082023-03-07 20:22:51 +0100286 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
287 * subsystem must have been initialized by calling
288 * psa_crypto_init() before calling this function.
289 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000290 * \param chain The pointer to the start of the CRT chain to attach to.
291 * When parsing the first CRT in a chain, this should point
292 * to an instance of ::mbedtls_x509_crt initialized through
293 * mbedtls_x509_crt_init().
294 * \param buf The buffer holding the DER encoded certificate.
295 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000297 * \note This function makes an internal copy of the CRT buffer
298 * \p buf. In particular, \p buf may be destroyed or reused
299 * after this call returns. To avoid duplicating the CRT
300 * buffer (at the cost of stricter lifetime constraints),
301 * use mbedtls_x509_crt_parse_der_nocopy() instead.
302 *
303 * \return \c 0 if successful.
304 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
307 const unsigned char *buf,
308 size_t buflen);
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000309
310/**
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200311 * \brief The type of certificate extension callbacks.
312 *
313 * Callbacks of this type are passed to and used by the
Nicola Di Lietofde98f72020-05-28 08:34:33 +0200314 * mbedtls_x509_crt_parse_der_with_ext_cb() routine when
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200315 * it encounters either an unsupported extension or a
316 * "certificate policies" extension containing any
317 * unsupported certificate policies.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200318 * Future versions of the library may invoke the callback
319 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200320 *
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200321 * \param p_ctx An opaque context passed to the callback.
ndilieto6e249802020-05-28 06:17:38 +0000322 * \param crt The certificate being parsed.
323 * \param oid The OID of the extension.
324 * \param critical Whether the extension is critical.
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200325 * \param p Pointer to the start of the extension value
ndilieto6e249802020-05-28 06:17:38 +0000326 * (the content of the OCTET STRING).
ndilieto6e249802020-05-28 06:17:38 +0000327 * \param end End of extension value.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200328 *
329 * \note The callback must fail and return a negative error code
330 * if it can not parse or does not support the extension.
331 * When the callback fails to parse a critical extension
332 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
333 * When the callback fails to parse a non critical extension
334 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
335 * the extension and continues parsing.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200336 *
337 * \return \c 0 on success.
338 * \return A negative error code on failure.
339 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340typedef int (*mbedtls_x509_crt_ext_cb_t)(void *p_ctx,
341 mbedtls_x509_crt const *crt,
342 mbedtls_x509_buf const *oid,
343 int critical,
344 const unsigned char *p,
345 const unsigned char *end);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200346
347/**
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200348 * \brief Parse a single DER formatted certificate and add it
349 * to the end of the provided chained list.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200350 *
Gilles Peskine07ae2082023-03-07 20:22:51 +0100351 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
352 * subsystem must have been initialized by calling
353 * psa_crypto_init() before calling this function.
354 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200355 * \param chain The pointer to the start of the CRT chain to attach to.
356 * When parsing the first CRT in a chain, this should point
357 * to an instance of ::mbedtls_x509_crt initialized through
358 * mbedtls_x509_crt_init().
359 * \param buf The buffer holding the DER encoded certificate.
360 * \param buflen The size in Bytes of \p buf.
361 * \param make_copy When not zero this function makes an internal copy of the
362 * CRT buffer \p buf. In particular, \p buf may be destroyed
363 * or reused after this call returns.
364 * When zero this function avoids duplicating the CRT buffer
365 * by taking temporary ownership thereof until the CRT
366 * is destroyed (like mbedtls_x509_crt_parse_der_nocopy())
367 * \param cb A callback invoked for every unsupported certificate
368 * extension.
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200369 * \param p_ctx An opaque context passed to the callback.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200370 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200371 * \note This call is functionally equivalent to
372 * mbedtls_x509_crt_parse_der(), and/or
373 * mbedtls_x509_crt_parse_der_nocopy()
374 * but it calls the callback with every unsupported
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200375 * certificate extension and additionally the
376 * "certificate policies" extension if it contains any
377 * unsupported certificate policies.
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200378 * The callback must return a negative error code if it
379 * does not know how to handle such an extension.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200380 * When the callback fails to parse a critical extension
381 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
382 * When the callback fails to parse a non critical extension
383 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
384 * the extension and continues parsing.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200385 * Future versions of the library may invoke the callback
386 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200387 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200388 * \return \c 0 if successful.
389 * \return A negative error code on failure.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200390 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
392 const unsigned char *buf,
393 size_t buflen,
394 int make_copy,
395 mbedtls_x509_crt_ext_cb_t cb,
396 void *p_ctx);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200397
398/**
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000399 * \brief Parse a single DER formatted certificate and add it
400 * to the end of the provided chained list. This is a
401 * variant of mbedtls_x509_crt_parse_der() which takes
402 * temporary ownership of the CRT buffer until the CRT
403 * is destroyed.
404 *
Gilles Peskine07ae2082023-03-07 20:22:51 +0100405 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
406 * subsystem must have been initialized by calling
407 * psa_crypto_init() before calling this function.
408 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000409 * \param chain The pointer to the start of the CRT chain to attach to.
410 * When parsing the first CRT in a chain, this should point
411 * to an instance of ::mbedtls_x509_crt initialized through
412 * mbedtls_x509_crt_init().
413 * \param buf The address of the readable buffer holding the DER encoded
414 * certificate to use. On success, this buffer must be
415 * retained and not be changed for the liftetime of the
416 * CRT chain \p chain, that is, until \p chain is destroyed
417 * through a call to mbedtls_x509_crt_free().
418 * \param buflen The size in Bytes of \p buf.
419 *
420 * \note This call is functionally equivalent to
421 * mbedtls_x509_crt_parse_der(), but it avoids creating a
422 * copy of the input buffer at the cost of stronger lifetime
423 * constraints. This is useful in constrained environments
424 * where duplication of the CRT cannot be tolerated.
425 *
426 * \return \c 0 if successful.
427 * \return A negative error code on failure.
428 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
430 const unsigned char *buf,
431 size_t buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433/**
Hanno Becker89a91122018-08-23 16:14:00 +0100434 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100435 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100437 * For CRTs in PEM encoding, the function parses permissively:
438 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100439 * returns the number of certificates for which parsing failed
440 * (hence \c 0 if all certificates were parsed successfully).
441 * If no certificate could be parsed, the function returns
442 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100444 * PEM encoded certificates may be interleaved by other data
445 * such as human readable descriptions of their content, as
446 * long as the certificates are enclosed in the PEM specific
447 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
448 *
Gilles Peskine07ae2082023-03-07 20:22:51 +0100449 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
450 * subsystem must have been initialized by calling
451 * psa_crypto_init() before calling this function.
452 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100453 * \param chain The chain to which to add the parsed certificates.
454 * \param buf The buffer holding the certificate data in PEM or DER format.
455 * For certificates in PEM encoding, this may be a concatenation
456 * of multiple certificates; for DER encoding, the buffer must
457 * comprise exactly one certificate.
458 * \param buflen The size of \p buf, including the terminating \c NULL byte
459 * in case of PEM encoded data.
460 *
461 * \return \c 0 if all certificates were parsed successfully.
462 * \return The (positive) number of certificates that couldn't
463 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100464 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100465 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100467int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470/**
471 * \brief Load one or more certificates and add them
472 * to the chained list. Parses permissively. If some
473 * certificates can be parsed, the result is the number
474 * of failed certificates it encountered. If none complete
475 * correctly, the first error is returned.
476 *
Gilles Peskine07ae2082023-03-07 20:22:51 +0100477 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
478 * subsystem must have been initialized by calling
479 * psa_crypto_init() before calling this function.
480 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481 * \param chain points to the start of the chain
482 * \param path filename to read the certificates from
483 *
484 * \return 0 if all certificates parsed successfully, a positive number
485 * if partly successful or a specific X509 or PEM error code
486 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488
489/**
490 * \brief Load one or more certificate files from a path and add them
491 * to the chained list. Parses permissively. If some
492 * certificates can be parsed, the result is the number
493 * of failed certificates it encountered. If none complete
494 * correctly, the first error is returned.
495 *
496 * \param chain points to the start of the chain
497 * \param path directory / folder to read the certificate files from
498 *
499 * \return 0 if all certificates parsed successfully, a positive number
500 * if partly successful or a specific X509 or PEM error code
501 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path);
Janos Follath11b41eb2019-05-08 15:26:49 +0100503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504#endif /* MBEDTLS_FS_IO */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200505/**
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300506 * \brief This function parses an item in the SubjectAlternativeNames
507 * extension.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200508 *
Ron Eldor890819a2019-05-13 19:03:04 +0300509 * \param san_buf The buffer holding the raw data item of the subject
510 * alternative name.
511 * \param san The target structure to populate with the parsed presentation
512 * of the subject alternative name encoded in \p san_raw.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200513 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300514 * \note Only "dnsName" and "otherName" of type hardware_module_name
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200515 * as defined in RFC 4180 is supported.
516 *
Ron Eldor890819a2019-05-13 19:03:04 +0300517 * \note This function should be called on a single raw data of
Ron Eldorcc45cd12019-05-15 10:20:09 +0300518 * subject alternative name. For example, after successful
Ron Eldor890819a2019-05-13 19:03:04 +0300519 * certificate parsing, one must iterate on every item in the
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300520 * \p crt->subject_alt_names sequence, and pass it to
521 * this function.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200522 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300523 * \warning The target structure contains pointers to the raw data of the
Ron Eldorcc45cd12019-05-15 10:20:09 +0300524 * parsed certificate, and its lifetime is restricted by the
525 * lifetime of the certificate.
526 *
Ron Eldor890819a2019-05-13 19:03:04 +0300527 * \return \c 0 on success
528 * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300529 * SAN type.
530 * \return Another negative value for any other failure.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200531 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
533 mbedtls_x509_subject_alternative_name *san);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534/**
535 * \brief Returns an informational string about the
536 * certificate.
537 *
538 * \param buf Buffer to write to
539 * \param size Maximum size of buffer
540 * \param prefix A line prefix
541 * \param crt The X509 certificate to represent
542 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200543 * \return The length of the string written (not including the
544 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100546int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
547 const mbedtls_x509_crt *crt);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548
549/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100550 * \brief Returns an informational string about the
551 * verification status of a certificate.
552 *
553 * \param buf Buffer to write to
554 * \param size Maximum size of buffer
555 * \param prefix A line prefix
556 * \param flags Verification flags created by mbedtls_x509_crt_verify()
557 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200558 * \return The length of the string written (not including the
559 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100560 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100561int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
562 uint32_t flags);
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200563
564/**
Hanno Becker902451d2019-03-27 11:48:40 +0000565 * \brief Verify a chain of certificates.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566 *
567 * The verify callback is a user-supplied callback that
568 * can clear / modify / add flags for a certificate. If set,
569 * the verification callback is called for each
570 * certificate in the chain (from the trust-ca down to the
571 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573 * int *flags). With the flags representing current flags for
574 * that specific certificate and the certificate depth from
575 * the bottom (Peer cert depth = 0).
576 *
577 * All flags left after returning from the callback
578 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200579 * return 0 for anything (including invalid certificates)
580 * other than fatal error, as a non-zero return code
581 * immediately aborts the verification process. For fatal
582 * errors, a specific error code should be used (different
583 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
584 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
585 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100587 * \note In case verification failed, the results can be displayed
588 * using \c mbedtls_x509_crt_verify_info()
589 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200590 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
591 * default security profile.
592 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100593 * \note It is your responsibility to provide up-to-date CRLs for
594 * all trusted CAs. If no CRL is provided for the CA that was
595 * used to sign the certificate, CRL verification is skipped
596 * silently, that is *without* setting any flag.
597 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200598 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200599 * (1) those of trusted root CAs, so that certificates
600 * chaining up to those CAs will be trusted, and (2)
601 * self-signed end-entity certificates to be trusted (for
602 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200603 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200604 *
Hanno Becker902451d2019-03-27 11:48:40 +0000605 * \param crt The certificate chain to be verified.
606 * \param trust_ca The list of trusted CAs.
607 * \param ca_crl The list of CRLs for trusted CAs.
Manuel Pégourié-Gonnardf58e5cc2020-07-24 10:31:37 +0200608 * \param cn The expected Common Name. This will be checked to be
609 * present in the certificate's subjectAltNames extension or,
610 * if this extension is absent, as a CN component in its
611 * Subject name. Currently only DNS names are supported. This
612 * may be \c NULL if the CN need not be verified.
Hanno Becker902451d2019-03-27 11:48:40 +0000613 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100614 * If the verification couldn't be completed, the flag value is
615 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000616 * \param f_vrfy The verification callback to use. See the documentation
617 * of mbedtls_x509_crt_verify() for more information.
618 * \param p_vrfy The context to be passed to \p f_vrfy.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619 *
Hanno Becker902451d2019-03-27 11:48:40 +0000620 * \return \c 0 if the chain is valid with respect to the
621 * passed CN, CAs, CRLs and security profile.
622 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
623 * certificate chain verification failed. In this case,
624 * \c *flags will have one or more
625 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
626 * flags set.
627 * \return Another negative error code in case of a fatal error
628 * encountered during the verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100630int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
631 mbedtls_x509_crt *trust_ca,
632 mbedtls_x509_crl *ca_crl,
633 const char *cn, uint32_t *flags,
634 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
635 void *p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200637/**
Hanno Becker902451d2019-03-27 11:48:40 +0000638 * \brief Verify a chain of certificates with respect to
639 * a configurable security profile.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200640 *
641 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
642 * security profile.
643 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200644 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200645 * for ECDSA) apply to all certificates: trusted root,
646 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200647 *
Hanno Becker902451d2019-03-27 11:48:40 +0000648 * \param crt The certificate chain to be verified.
649 * \param trust_ca The list of trusted CAs.
650 * \param ca_crl The list of CRLs for trusted CAs.
651 * \param profile The security profile to use for the verification.
652 * \param cn The expected Common Name. This may be \c NULL if the
653 * CN need not be verified.
654 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100655 * If the verification couldn't be completed, the flag value is
656 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000657 * \param f_vrfy The verification callback to use. See the documentation
658 * of mbedtls_x509_crt_verify() for more information.
659 * \param p_vrfy The context to be passed to \p f_vrfy.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200660 *
Hanno Becker902451d2019-03-27 11:48:40 +0000661 * \return \c 0 if the chain is valid with respect to the
662 * passed CN, CAs, CRLs and security profile.
663 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
664 * certificate chain verification failed. In this case,
665 * \c *flags will have one or more
666 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
667 * flags set.
668 * \return Another negative error code in case of a fatal error
669 * encountered during the verification process.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200670 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100671int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
672 mbedtls_x509_crt *trust_ca,
673 mbedtls_x509_crl *ca_crl,
674 const mbedtls_x509_crt_profile *profile,
675 const char *cn, uint32_t *flags,
676 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
677 void *p_vrfy);
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200678
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200679/**
680 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
681 *
682 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
683 * but can return early and restart according to the limit
684 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
685 *
Hanno Becker902451d2019-03-27 11:48:40 +0000686 * \param crt The certificate chain to be verified.
687 * \param trust_ca The list of trusted CAs.
688 * \param ca_crl The list of CRLs for trusted CAs.
689 * \param profile The security profile to use for the verification.
690 * \param cn The expected Common Name. This may be \c NULL if the
691 * CN need not be verified.
692 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100693 * If the verification couldn't be completed, the flag value is
694 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000695 * \param f_vrfy The verification callback to use. See the documentation
696 * of mbedtls_x509_crt_verify() for more information.
697 * \param p_vrfy The context to be passed to \p f_vrfy.
698 * \param rs_ctx The restart context to use. This may be set to \c NULL
699 * to disable restartable ECC.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200700 *
701 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200702 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200703 * operations was reached: see \c mbedtls_ecp_set_max_ops().
704 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100705int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
706 mbedtls_x509_crt *trust_ca,
707 mbedtls_x509_crl *ca_crl,
708 const mbedtls_x509_crt_profile *profile,
709 const char *cn, uint32_t *flags,
710 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
711 void *p_vrfy,
712 mbedtls_x509_crt_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200713
Hanno Becker5c8df782019-03-27 11:01:17 +0000714/**
715 * \brief The type of trusted certificate callbacks.
716 *
717 * Callbacks of this type are passed to and used by the CRT
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300718 * verification routine mbedtls_x509_crt_verify_with_ca_cb()
Hanno Becker5c8df782019-03-27 11:01:17 +0000719 * when looking for trusted signers of a given certificate.
720 *
721 * On success, the callback returns a list of trusted
722 * certificates to be considered as potential signers
723 * for the input certificate.
724 *
725 * \param p_ctx An opaque context passed to the callback.
726 * \param child The certificate for which to search a potential signer.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300727 * This will point to a readable certificate.
Hanno Becker5c8df782019-03-27 11:01:17 +0000728 * \param candidate_cas The address at which to store the address of the first
729 * entry in the generated linked list of candidate signers.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300730 * This will not be \c NULL.
Hanno Becker5c8df782019-03-27 11:01:17 +0000731 *
732 * \note The callback must only return a non-zero value on a
733 * fatal error. If, in contrast, the search for a potential
734 * signer completes without a single candidate, the
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300735 * callback must return \c 0 and set \c *candidate_cas
Hanno Becker5c8df782019-03-27 11:01:17 +0000736 * to \c NULL.
737 *
738 * \return \c 0 on success. In this case, \c *candidate_cas points
739 * to a heap-allocated linked list of instances of
740 * ::mbedtls_x509_crt, and ownership of this list is passed
741 * to the caller.
742 * \return A negative error code on failure.
743 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100744typedef int (*mbedtls_x509_crt_ca_cb_t)(void *p_ctx,
745 mbedtls_x509_crt const *child,
746 mbedtls_x509_crt **candidate_cas);
Hanno Beckere15dae72019-03-28 13:55:38 +0000747
748#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker5c8df782019-03-27 11:01:17 +0000749/**
750 * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
751 * uses a callback to acquire the list of trusted CA
752 * certificates.
753 *
754 * \param crt The certificate chain to be verified.
755 * \param f_ca_cb The callback to be used to query for potential signers
756 * of a given child certificate. See the documentation of
757 * ::mbedtls_x509_crt_ca_cb_t for more information.
758 * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
759 * \param profile The security profile for the verification.
760 * \param cn The expected Common Name. This may be \c NULL if the
761 * CN need not be verified.
762 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100763 * If the verification couldn't be completed, the flag value is
764 * set to (uint32_t) -1.
Hanno Becker5c8df782019-03-27 11:01:17 +0000765 * \param f_vrfy The verification callback to use. See the documentation
766 * of mbedtls_x509_crt_verify() for more information.
767 * \param p_vrfy The context to be passed to \p f_vrfy.
768 *
769 * \return See \c mbedtls_crt_verify_with_profile().
770 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100771int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
772 mbedtls_x509_crt_ca_cb_t f_ca_cb,
773 void *p_ca_cb,
774 const mbedtls_x509_crt_profile *profile,
775 const char *cn, uint32_t *flags,
776 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
777 void *p_vrfy);
Hanno Becker5c8df782019-03-27 11:01:17 +0000778
779#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200782/**
783 * \brief Check usage of certificate against keyUsage extension.
784 *
785 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200786 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
787 * before using the certificate to perform an RSA key
788 * exchange).
789 *
790 * \note Except for decipherOnly and encipherOnly, a bit set in the
791 * usage argument means this bit MUST be set in the
792 * certificate. For decipherOnly and encipherOnly, it means
793 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200794 *
795 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200797 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200798 *
799 * \note You should only call this function on leaf certificates, on
800 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200802 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100803int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
804 unsigned int usage);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200808/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000809 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200810 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000811 * \param crt Leaf certificate used.
812 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
813 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200815 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000816 * \return 0 if this use of the certificate is allowed,
817 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200818 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000819 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200820 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
822 const char *usage_oid,
823 size_t usage_len);
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000824#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200828 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200829 *
830 * \param crt a certificate to be verified
831 * \param crl the CRL to verify against
832 *
833 * \return 1 if the certificate is revoked, 0 otherwise
834 *
835 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100836int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200838
839/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200840 * \brief Initialize a certificate (chain)
841 *
842 * \param crt Certificate chain to initialize
843 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100844void mbedtls_x509_crt_init(mbedtls_x509_crt *crt);
Paul Bakker369d2eb2013-09-18 11:58:25 +0200845
846/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200847 * \brief Unallocate all certificate data
848 *
849 * \param crt Certificate chain to free
850 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100851void mbedtls_x509_crt_free(mbedtls_x509_crt *crt);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200852
853#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
854/**
855 * \brief Initialize a restart context
856 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100857void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200858
859/**
860 * \brief Free the components of a restart context
861 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100862void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200863#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200865
Andrzej Kurek73afe272022-01-24 10:31:06 -0500866/** \} name Structures and functions for parsing and writing X.509 certificates */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869/**
870 * \brief Initialize a CRT writing context
871 *
872 * \param ctx CRT context to initialize
873 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100874void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200875
876/**
Shaun Case0e7791f2021-12-20 21:14:10 -0800877 * \brief Set the version for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879 *
880 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
882 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100884void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx, int version);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885
886/**
887 * \brief Set the serial number for a Certificate.
888 *
889 * \param ctx CRT context to use
890 * \param serial serial number to set
891 *
892 * \return 0 if successful
893 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895
896/**
897 * \brief Set the validity period for a Certificate
898 * Timestamps should be in string format for UTC timezone
899 * i.e. "YYYYMMDDhhmmss"
900 * e.g. "20131231235959" for December 31st 2013
901 * at 23:59:59
902 *
903 * \param ctx CRT context to use
904 * \param not_before not_before timestamp
905 * \param not_after not_after timestamp
906 *
907 * \return 0 if timestamp was parsed successfully, or
908 * a specific error code
909 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100910int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx, const char *not_before,
911 const char *not_after);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912
913/**
914 * \brief Set the issuer name for a Certificate
915 * Issuer names should contain a comma-separated list
916 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000917 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918 *
919 * \param ctx CRT context to use
920 * \param issuer_name issuer name to set
921 *
922 * \return 0 if issuer name was parsed successfully, or
923 * a specific error code
924 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100925int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx,
926 const char *issuer_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927
928/**
929 * \brief Set the subject name for a Certificate
930 * Subject names should contain a comma-separated list
931 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000932 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 *
934 * \param ctx CRT context to use
935 * \param subject_name subject name to set
936 *
937 * \return 0 if subject name was parsed successfully, or
938 * a specific error code
939 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100940int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx,
941 const char *subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942
943/**
944 * \brief Set the subject public key for the certificate
945 *
946 * \param ctx CRT context to use
947 * \param key public key to include
948 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100949void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx, mbedtls_pk_context *key);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950
951/**
952 * \brief Set the issuer key used for signing the certificate
953 *
954 * \param ctx CRT context to use
955 * \param key private key to sign with
956 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100957void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx, mbedtls_pk_context *key);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958
959/**
960 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 *
963 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100964 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100966void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967
968/**
969 * \brief Generic function to add to or replace an extension in the
970 * CRT
971 *
972 * \param ctx CRT context to use
973 * \param oid OID of the extension
974 * \param oid_len length of the OID
975 * \param critical if the extension is critical (per the RFC's definition)
976 * \param val value of the extension OCTET STRING
977 * \param val_len length of the value data
978 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200979 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100981int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
982 const char *oid, size_t oid_len,
983 int critical,
984 const unsigned char *val, size_t val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985
986/**
987 * \brief Set the basicConstraints extension for a CRT
988 *
989 * \param ctx CRT context to use
990 * \param is_ca is this a CA certificate
991 * \param max_pathlen maximum length of certificate chains below this
992 * certificate (only for CA certificates, -1 is
Tom Cosgrove2b150752022-05-26 11:55:43 +0100993 * unlimited)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200995 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100997int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
998 int is_ca, int max_pathlen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001/**
1002 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004 * called before
1005 *
1006 * \param ctx CRT context to use
1007 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001008 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001010int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011
1012/**
1013 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015 * called before
1016 *
1017 * \param ctx CRT context to use
1018 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001019 * \return 0 if successful, or a 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_authority_key_identifier(mbedtls_x509write_cert *ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023
1024/**
1025 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 *
1028 * \param ctx CRT context to use
1029 * \param key_usage key usage 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_key_usage(mbedtls_x509write_cert *ctx,
1034 unsigned int key_usage);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035
1036/**
1037 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039 *
1040 * \param ctx CRT context to use
1041 * \param ns_cert_type Netscape Cert Type flags to set
1042 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001043 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001045int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
1046 unsigned char ns_cert_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001047
1048/**
1049 * \brief Free the contents of a CRT write context
1050 *
1051 * \param ctx CRT context to free
1052 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001053void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001054
1055/**
1056 * \brief Write a built up certificate to a X509 DER structure
1057 * Note: data is written at the end of the buffer! Use the
1058 * return value to determine where you should start
1059 * using the buffer
1060 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001061 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062 * \param buf buffer to write to
1063 * \param size size of the buffer
1064 * \param f_rng RNG function (for signature, see note)
1065 * \param p_rng RNG parameter
1066 *
1067 * \return length of data written if successful, or a specific
1068 * error code
1069 *
1070 * \note f_rng may be NULL if RSA is used for signature and the
1071 * signature is made offline (otherwise f_rng is desirable
1072 * for countermeasures against timing attacks).
1073 * ECDSA signatures always require a non-NULL f_rng.
1074 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001075int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
1076 int (*f_rng)(void *, unsigned char *, size_t),
1077 void *p_rng);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080/**
1081 * \brief Write a built up certificate to a X509 PEM string
1082 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001083 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001084 * \param buf buffer to write to
1085 * \param size size of the buffer
1086 * \param f_rng RNG function (for signature, see note)
1087 * \param p_rng RNG parameter
1088 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001089 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090 *
1091 * \note f_rng may be NULL if RSA is used for signature and the
1092 * signature is made offline (otherwise f_rng is desirable
1093 * for countermeasures against timing attacks).
1094 * ECDSA signatures always require a non-NULL f_rng.
1095 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001096int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
1097 int (*f_rng)(void *, unsigned char *, size_t),
1098 void *p_rng);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#endif /* MBEDTLS_PEM_WRITE_C */
1100#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101
Andrzej Kurekff632d52022-01-24 10:32:00 -05001102/** \} addtogroup x509_module */
1103
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104#ifdef __cplusplus
1105}
1106#endif
1107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108#endif /* mbedtls_x509_crt.h */