blob: c3ef4380a1a321b238daa3bdb1b2c12e2b898fb6 [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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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é-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_X509_CRT_H
25#define MBEDTLS_X509_CRT_H
Paul Bakker7c6b2c32013-09-16 13:49:26 +020026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032
33#include "x509.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034#include "x509_crl.h"
Hanno Beckerb6c39fc2019-02-25 13:50:14 +000035#include "threading.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036
37/**
38 * \addtogroup x509_module
39 * \{
40 */
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46/**
47 * \name Structures and functions for parsing and writing X.509 certificates
48 * \{
49 */
50
Hanno Becker21f55672019-02-15 15:27:59 +000051typedef struct mbedtls_x509_crt_frame
52{
53 /* Keep these 8-bit fields at the front of the structure to allow them to
54 * be fetched in a single instruction on Thumb2; putting them at the back
55 * requires an intermediate address calculation. */
56
57 uint8_t version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
58 uint8_t ca_istrue; /**< Optional Basic Constraint extension value:
59 * 1 if this certificate belongs to a CA, 0 otherwise. */
60 uint8_t max_pathlen; /**< Optional Basic Constraint extension value:
61 * The maximum path length to the root certificate.
62 * Path length is 1 higher than RFC 5280 'meaning', so 1+ */
63 uint8_t ns_cert_type; /**< Optional Netscape certificate type extension value:
64 * See the values in x509.h */
65
66 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
67 uint32_t ext_types; /**< Bitfield indicating which extensions are present.
68 * See the values in x509.h. */
69
70 mbedtls_md_type_t sig_md; /**< The hash algorithm used to hash CRT before signing. */
71 mbedtls_pk_type_t sig_pk; /**< The signature algorithm used to sign the CRT hash. */
72
73 mbedtls_x509_time valid_from; /**< The start time of certificate validity. */
74 mbedtls_x509_time valid_to; /**< The end time of certificate validity. */
75
76 mbedtls_x509_buf_raw raw; /**< The raw certificate data in DER. */
77 mbedtls_x509_buf_raw tbs; /**< The part of the CRT that is [T]o [B]e [S]igned. */
78
79 mbedtls_x509_buf_raw serial; /**< The unique ID for certificate issued by a specific CA. */
80
81 mbedtls_x509_buf_raw pubkey_raw; /**< The raw public key data (DER). */
82
83 mbedtls_x509_buf_raw issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
84 mbedtls_x509_buf_raw issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
85
86 mbedtls_x509_buf_raw subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
87 mbedtls_x509_buf_raw subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
88
89 mbedtls_x509_buf_raw sig; /**< Signature: hash of the tbs part signed with the private key. */
90 mbedtls_x509_buf_raw sig_alg; /**< The signature algorithm used for \p sig. */
91
92 mbedtls_x509_buf_raw v3_ext; /**< The raw data for the extension list in the certificate.
93 * Might be useful for manual inspection of extensions that
94 * Mbed TLS doesn't yet support. */
95 mbedtls_x509_buf_raw subject_alt_raw; /**< The raw data for the SubjectAlternativeNames extension. */
96 mbedtls_x509_buf_raw ext_key_usage_raw; /**< The raw data for the ExtendedKeyUsage extension. */
97
Hanno Becker21f55672019-02-15 15:27:59 +000098} mbedtls_x509_crt_frame;
99
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000100/* This is an internal structure used for caching parsed data from an X.509 CRT.
101 *
102 * This structure may change at any time, and it is discouraged
103 * to access it directly.
104 */
105typedef struct mbedtls_x509_crt_cache
106{
107#if defined(MBEDTLS_THREADING_C)
108 mbedtls_threading_mutex_t frame_mutex;
109 mbedtls_threading_mutex_t pk_mutex;
110#endif
111 mbedtls_x509_buf_raw pk_raw;
112 mbedtls_x509_crt_frame *frame;
113 mbedtls_pk_context *pk;
114} mbedtls_x509_crt_cache;
115
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116/**
117 * Container for an X.509 certificate. The certificate may be chained.
118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120{
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000121 int own_buffer; /**< Indicates if \c raw is owned
Hanno Becker180f7bf2019-02-28 13:23:38 +0000122 * by the structure or not. */
123 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
124 mbedtls_x509_crt_cache *cache; /**< Internal parsing cache. */
125
126 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
127
128 /* Legacy fields */
129#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200131
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +0200132 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
134 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
137 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
140 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
143 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200144
Hanno Becker32c530e2019-02-06 16:13:41 +0000145 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
149 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
150 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
151 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200152
153 int ext_types; /**< Bit string containing detected and parsed extensions */
154 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
155 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+ */
156
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100157 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200158
Hanno Becker7ec9c362019-02-21 14:24:05 +0000159 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200160
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +0200161 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
164 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
165 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
166 void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
Hanno Becker180f7bf2019-02-28 13:23:38 +0000167#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200170
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200171/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200172 * Build flag from an algorithm/curve identifier (pk, md, ecp)
173 * Since 0 is always XXX_NONE, ignore it.
174 */
Hanno Beckerd6028a12018-10-15 12:01:35 +0100175#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200176
177/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200178 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200179 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200180 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200181 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200182typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200183{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200184 uint32_t allowed_mds; /**< MDs for signatures */
185 uint32_t allowed_pks; /**< PK algs for signatures */
186 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
187 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200188}
189mbedtls_x509_crt_profile;
190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#define MBEDTLS_X509_CRT_VERSION_1 0
192#define MBEDTLS_X509_CRT_VERSION_2 1
193#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
196#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200197
Andres AGf9113192016-09-02 14:06:04 +0100198#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
199#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
200#endif
201
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200202/**
203 * Container for writing a certificate (CRT)
204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206{
207 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_mpi serial;
209 mbedtls_pk_context *subject_key;
210 mbedtls_pk_context *issuer_key;
211 mbedtls_asn1_named_data *subject;
212 mbedtls_asn1_named_data *issuer;
213 mbedtls_md_type_t md_alg;
214 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
215 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
216 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200217}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200220/**
221 * Item in a verification chain: cert and flags for it
222 */
223typedef struct {
224 mbedtls_x509_crt *crt;
225 uint32_t flags;
226} mbedtls_x509_crt_verify_chain_item;
227
228/**
229 * Max size of verification chain: end-entity + intermediates + trusted root
230 */
231#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
232
233/**
234 * Verification chain as built by \c mbedtls_crt_verify_chain()
235 */
236typedef struct
237{
238 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200239 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200240} mbedtls_x509_crt_verify_chain;
241
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200242#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
243
244/**
245 * \brief Context for resuming X.509 verify operations
246 */
247typedef struct
248{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200249 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200250 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200251
252 /* for find_parent_in() */
253 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
254 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200255 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200256
257 /* for find_parent() */
258 int parent_is_trusted; /* -1 if find_parent is not in progress */
259
260 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200261 enum {
262 x509_crt_rs_none,
263 x509_crt_rs_find_parent,
264 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200265 int self_cnt;
266 mbedtls_x509_crt_verify_chain ver_chain;
267
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200268} mbedtls_x509_crt_restart_ctx;
269
270#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
271
272/* Now we can declare functions that take a pointer to that */
273typedef void mbedtls_x509_crt_restart_ctx;
274
275#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200279 * Default security profile. Should provide a good balance between security
280 * and compatibility with current deployments.
281 */
282extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
283
284/**
285 * Expected next default profile. Recommended for new deployments.
286 * Currently targets a 128-bit security level, except for RSA-2048.
287 */
288extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
289
290/**
291 * NSA Suite B profile.
292 */
293extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
294
295/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296 * \brief Parse a single DER formatted certificate and add it
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000297 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200298 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000299 * \param chain The pointer to the start of the CRT chain to attach to.
300 * When parsing the first CRT in a chain, this should point
301 * to an instance of ::mbedtls_x509_crt initialized through
302 * mbedtls_x509_crt_init().
303 * \param buf The buffer holding the DER encoded certificate.
304 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000306 * \note This function makes an internal copy of the CRT buffer
307 * \p buf. In particular, \p buf may be destroyed or reused
308 * after this call returns. To avoid duplicating the CRT
309 * buffer (at the cost of stricter lifetime constraints),
310 * use mbedtls_x509_crt_parse_der_nocopy() instead.
311 *
312 * \return \c 0 if successful.
313 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200314 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000315int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
316 const unsigned char *buf,
317 size_t buflen );
318
319/**
320 * \brief Parse a single DER formatted certificate and add it
321 * to the end of the provided chained list. This is a
322 * variant of mbedtls_x509_crt_parse_der() which takes
323 * temporary ownership of the CRT buffer until the CRT
324 * is destroyed.
325 *
326 * \param chain The pointer to the start of the CRT chain to attach to.
327 * When parsing the first CRT in a chain, this should point
328 * to an instance of ::mbedtls_x509_crt initialized through
329 * mbedtls_x509_crt_init().
330 * \param buf The address of the readable buffer holding the DER encoded
331 * certificate to use. On success, this buffer must be
332 * retained and not be changed for the liftetime of the
333 * CRT chain \p chain, that is, until \p chain is destroyed
334 * through a call to mbedtls_x509_crt_free().
335 * \param buflen The size in Bytes of \p buf.
336 *
337 * \note This call is functionally equivalent to
338 * mbedtls_x509_crt_parse_der(), but it avoids creating a
339 * copy of the input buffer at the cost of stronger lifetime
340 * constraints. This is useful in constrained environments
341 * where duplication of the CRT cannot be tolerated.
342 *
343 * \return \c 0 if successful.
344 * \return A negative error code on failure.
345 */
346int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
347 const unsigned char *buf,
348 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349
350/**
Hanno Becker89a91122018-08-23 16:14:00 +0100351 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100352 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100354 * For CRTs in PEM encoding, the function parses permissively:
355 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100356 * returns the number of certificates for which parsing failed
357 * (hence \c 0 if all certificates were parsed successfully).
358 * If no certificate could be parsed, the function returns
359 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100361 * PEM encoded certificates may be interleaved by other data
362 * such as human readable descriptions of their content, as
363 * long as the certificates are enclosed in the PEM specific
364 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
365 *
366 * \param chain The chain to which to add the parsed certificates.
367 * \param buf The buffer holding the certificate data in PEM or DER format.
368 * For certificates in PEM encoding, this may be a concatenation
369 * of multiple certificates; for DER encoding, the buffer must
370 * comprise exactly one certificate.
371 * \param buflen The size of \p buf, including the terminating \c NULL byte
372 * in case of PEM encoded data.
373 *
374 * \return \c 0 if all certificates were parsed successfully.
375 * \return The (positive) number of certificates that couldn't
376 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100377 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100378 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383/**
384 * \brief Load one or more certificates and add them
385 * to the chained list. Parses permissively. If some
386 * certificates can be parsed, the result is the number
387 * of failed certificates it encountered. If none complete
388 * correctly, the first error is returned.
389 *
390 * \param chain points to the start of the chain
391 * \param path filename to read the certificates from
392 *
393 * \return 0 if all certificates parsed successfully, a positive number
394 * if partly successful or a specific X509 or PEM error code
395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397
398/**
399 * \brief Load one or more certificate files from a path and add them
400 * to the chained list. Parses permissively. If some
401 * certificates can be parsed, the result is the number
402 * of failed certificates it encountered. If none complete
403 * correctly, the first error is returned.
404 *
405 * \param chain points to the start of the chain
406 * \param path directory / folder to read the certificate files from
407 *
408 * \return 0 if all certificates parsed successfully, a positive number
409 * if partly successful or a specific X509 or PEM error code
410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
412#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413
Hanno Becker02a21932019-06-10 15:08:43 +0100414#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415/**
416 * \brief Returns an informational string about the
417 * certificate.
418 *
419 * \param buf Buffer to write to
420 * \param size Maximum size of buffer
421 * \param prefix A line prefix
422 * \param crt The X509 certificate to represent
423 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200424 * \return The length of the string written (not including the
425 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
428 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429
430/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100431 * \brief Returns an informational string about the
432 * verification status of a certificate.
433 *
434 * \param buf Buffer to write to
435 * \param size Maximum size of buffer
436 * \param prefix A line prefix
437 * \param flags Verification flags created by mbedtls_x509_crt_verify()
438 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200439 * \return The length of the string written (not including the
440 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100441 */
442int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200443 uint32_t flags );
Hanno Beckerc6043f22019-06-14 17:21:24 +0100444#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200445
446/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447 * \brief Verify the certificate signature
448 *
449 * The verify callback is a user-supplied callback that
450 * can clear / modify / add flags for a certificate. If set,
451 * the verification callback is called for each
452 * certificate in the chain (from the trust-ca down to the
453 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 * int *flags). With the flags representing current flags for
456 * that specific certificate and the certificate depth from
457 * the bottom (Peer cert depth = 0).
458 *
459 * All flags left after returning from the callback
460 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200461 * return 0 for anything (including invalid certificates)
462 * other than fatal error, as a non-zero return code
463 * immediately aborts the verification process. For fatal
464 * errors, a specific error code should be used (different
465 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
466 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
467 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100469 * \note In case verification failed, the results can be displayed
470 * using \c mbedtls_x509_crt_verify_info()
471 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200472 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
473 * default security profile.
474 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100475 * \note It is your responsibility to provide up-to-date CRLs for
476 * all trusted CAs. If no CRL is provided for the CA that was
477 * used to sign the certificate, CRL verification is skipped
478 * silently, that is *without* setting any flag.
479 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200480 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200481 * (1) those of trusted root CAs, so that certificates
482 * chaining up to those CAs will be trusted, and (2)
483 * self-signed end-entity certificates to be trusted (for
484 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200485 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200486 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100487 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200488 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100489 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 * \param cn expected Common Name (can be set to
491 * NULL if the CN must not be verified)
492 * \param flags result of the verification
493 * \param f_vrfy verification function
494 * \param p_vrfy verification parameter
495 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200496 * \return 0 (and flags set to 0) if the chain was verified and valid,
497 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
498 * but found to be invalid, in which case *flags will have one
499 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
500 * flags set, or another error (and flags set to 0xffffffff)
501 * in case of a fatal error encountered during the
502 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
505 mbedtls_x509_crt *trust_ca,
506 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200507 const char *cn, uint32_t *flags,
508 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200509 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200511/**
512 * \brief Verify the certificate signature according to profile
513 *
514 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
515 * security profile.
516 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200517 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200518 * for ECDSA) apply to all certificates: trusted root,
519 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200520 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100521 * \param crt a certificate (chain) to be verified
522 * \param trust_ca the list of trusted CAs
523 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200524 * \param profile security profile for verification
525 * \param cn expected Common Name (can be set to
526 * NULL if the CN must not be verified)
527 * \param flags result of the verification
528 * \param f_vrfy verification function
529 * \param p_vrfy verification parameter
530 *
531 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
532 * in which case *flags will have one or more
533 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
534 * set,
535 * or another error in case of a fatal error encountered
536 * during the verification process.
537 */
538int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
539 mbedtls_x509_crt *trust_ca,
540 mbedtls_x509_crl *ca_crl,
541 const mbedtls_x509_crt_profile *profile,
542 const char *cn, uint32_t *flags,
543 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
544 void *p_vrfy );
545
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200546/**
547 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
548 *
549 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
550 * but can return early and restart according to the limit
551 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
552 *
553 * \param crt a certificate (chain) to be verified
554 * \param trust_ca the list of trusted CAs
555 * \param ca_crl the list of CRLs for trusted CAs
556 * \param profile security profile for verification
557 * \param cn expected Common Name (can be set to
558 * NULL if the CN must not be verified)
559 * \param flags result of the verification
560 * \param f_vrfy verification function
561 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200562 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200563 *
564 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200565 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200566 * operations was reached: see \c mbedtls_ecp_set_max_ops().
567 */
568int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
569 mbedtls_x509_crt *trust_ca,
570 mbedtls_x509_crl *ca_crl,
571 const mbedtls_x509_crt_profile *profile,
572 const char *cn, uint32_t *flags,
573 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
574 void *p_vrfy,
575 mbedtls_x509_crt_restart_ctx *rs_ctx );
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200578/**
579 * \brief Check usage of certificate against keyUsage extension.
580 *
581 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200582 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
583 * before using the certificate to perform an RSA key
584 * exchange).
585 *
586 * \note Except for decipherOnly and encipherOnly, a bit set in the
587 * usage argument means this bit MUST be set in the
588 * certificate. For decipherOnly and encipherOnly, it means
589 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200590 *
591 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200593 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200594 *
595 * \note You should only call this function on leaf certificates, on
596 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200598 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200599int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
600 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200604/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000605 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200606 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000607 * \param crt Leaf certificate used.
608 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
609 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200611 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000612 * \return 0 if this use of the certificate is allowed,
613 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200614 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000615 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000618 const char *usage_oid,
619 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000620#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200624 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625 *
626 * \param crt a certificate to be verified
627 * \param crl the CRL to verify against
628 *
629 * \return 1 if the certificate is revoked, 0 otherwise
630 *
631 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100632int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634
635/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200636 * \brief Initialize a certificate (chain)
637 *
638 * \param crt Certificate chain to initialize
639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200641
642/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643 * \brief Unallocate all certificate data
644 *
645 * \param crt Certificate chain to free
646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200648
649#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
650/**
651 * \brief Initialize a restart context
652 */
653void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
654
655/**
656 * \brief Free the components of a restart context
657 */
658void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
659#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000660
Hanno Becker823efad2019-02-28 13:23:58 +0000661/**
662 * \brief Request CRT frame giving access to basic CRT fields
663 * and raw ASN.1 data of complex fields.
664 *
665 * \param crt The CRT to use. This must be initialized and setup.
666 * \param dst The address of the destination frame structure.
667 * This need not be initialized.
668 *
669 * \note ::mbedtls_x509_crt_frame does not contain pointers to
670 * dynamically allocated memory, and hence need not be freed.
671 * Users may e.g. allocate an instance of
672 * ::mbedtls_x509_crt_frame on the stack and call this function
673 * on it, in which case no allocation/freeing has to be done.
674 *
675 * \return \c 0 on success. In this case, \p dst is updated
676 * to hold the frame for the given CRT.
677 * \return A negative error code on failure.
678 */
679int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
680 mbedtls_x509_crt_frame *dst );
681
682/**
683 * \brief Setup a PK context with the public key in a certificate.
684 *
685 * \param crt The certificate to use. This must be initialized and setup.
686 * \param pk The address of the destination PK context to fill.
687 * This must be initialized via mbedtls_pk_init().
688 *
689 * \return \c 0 on success. In this case, the user takes ownership
690 * of the destination PK context, and is responsible for
691 * calling mbedtls_pk_free() on it once it's no longer needed.
692 * \return A negative error code on failure.
693 */
694int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
695 mbedtls_pk_context *pk );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000696
697/**
Hanno Becker63e69982019-02-26 18:50:49 +0000698 * \brief Request the subject name of a CRT, presented
699 * as a dynamically allocated linked list.
700 *
701 * \param crt The CRT to use. This must be initialized and setup.
702 * \param subject The address at which to store the address of the
703 * first entry of the generated linked list holding
704 * the subject name.
705 *
706 * \note Depending in your use case, consider using the raw ASN.1
707 * describing the subject name instead of the heap-allocated
708 * linked list generated by this call. The pointers to the
709 * raw ASN.1 data are part of the CRT frame that can be queried
710 * via mbedtls_x509_crt_get_frame(), and they can be traversed
711 * via mbedtls_asn1_traverse_sequence_of().
712 *
713 * \return \c 0 on success. In this case, the user takes ownership
714 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000715 * through a call to mbedtls_x509_name_free() once it's no
716 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000717 * \return A negative error code on failure.
718 */
719int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
720 mbedtls_x509_name **subject );
721
722/**
723 * \brief Request the subject name of a CRT, presented
724 * as a dynamically allocated linked list.
725 *
726 * \param crt The CRT to use. This must be initialized and setup.
727 * \param issuer The address at which to store the address of the
728 * first entry of the generated linked list holding
729 * the subject name.
730 *
731 * \note Depending in your use case, consider using the raw ASN.1
732 * describing the subject name instead of the heap-allocated
733 * linked list generated by this call. The pointers to the
734 * raw ASN.1 data are part of the CRT frame that can be queried
735 * via mbedtls_x509_crt_get_frame(), and they can be traversed
736 * via mbedtls_asn1_traverse_sequence_of().
737 *
738 * \return \c 0 on success. In this case, the user takes ownership
739 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000740 * through a call to mbedtls_x509_name_free() once it's no
741 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000742 * \return A negative error code on failure.
743 */
744int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
745 mbedtls_x509_name **issuer );
746
747/**
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000748 * \brief Request the subject alternative name of a CRT, presented
749 * as a dynamically allocated linked list.
750 *
751 * \param crt The CRT to use. This must be initialized and setup.
752 * \param subj_alt The address at which to store the address of the
753 * first component of the subject alternative names list.
754 *
755 * \note Depending in your use case, consider using the raw ASN.1
756 * describing the subject alternative names extension
757 * instead of the heap-allocated linked list generated by this
758 * call. The pointers to the raw ASN.1 data are part of the CRT
759 * frame that can be queried via mbedtls_x509_crt_get_frame(),
760 * and mbedtls_asn1_traverse_sequence_of() can be used to
761 * traverse the list of subject alternative names.
762 *
763 * \return \c 0 on success. In this case, the user takes ownership
764 * of the name context, and is responsible for freeing it
765 * through a call to mbedtls_x509_sequence_free() once it's
766 * no longer needed.
767 * \return A negative error code on failure.
768 */
769int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
770 mbedtls_x509_sequence **subj_alt );
771
772/**
773 * \brief Request the ExtendedKeyUsage extension of a CRT,
774 * presented as a dynamically allocated linked list.
775 *
776 * \param crt The CRT to use. This must be initialized and setup.
777 * \param ext_key_usage The address at which to store the address of the
778 * first entry of the ExtendedKeyUsage extension.
779 *
780 * \note Depending in your use case, consider using the raw ASN.1
781 * describing the extended key usage extension instead of
782 * the heap-allocated linked list generated by this call.
783 * The pointers to the raw ASN.1 data are part of the CRT
784 * frame that can be queried via mbedtls_x509_crt_get_frame(),
785 * and mbedtls_asn1_traverse_sequence_of() can be used to
786 * traverse the entries in the extended key usage extension.
787 *
788 * \return \c 0 on success. In this case, the user takes ownership
789 * of the name context, and is responsible for freeing it
790 * through a call to mbedtls_x509_sequence_free() once it's
791 * no longer needed.
792 * \return A negative error code on failure.
793 */
794int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
795 mbedtls_x509_sequence **ext_key_usage );
796
797/**
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000798 * \brief Flush internal X.509 CRT parsing cache, if present.
799 *
800 * \param crt The CRT structure whose cache to flush.
801 *
802 * \note Calling this function frequently reduces RAM usage
803 * at the cost of performance.
804 *
805 * \return \c 0 on success.
806 * \return A negative error code on failure.
807 */
808int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt );
809
810/* Internal X.509 CRT cache handling functions.
811 * They are not part of the public API and may change
812 * at any time. */
813
814int mbedtls_x509_crt_cache_provide_frame( mbedtls_x509_crt const *crt );
815int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt );
816
817static inline int mbedtls_x509_crt_cache_frame_set(
818 mbedtls_x509_crt_cache *cache )
819{
820 return( cache->frame != NULL );
821}
822
823static inline mbedtls_x509_crt_frame* mbedtls_x509_crt_cache_get_frame(
824 mbedtls_x509_crt_cache *cache )
825{
826 return( cache->frame );
827}
828
829static inline int mbedtls_x509_crt_cache_pk_set(
830 mbedtls_x509_crt_cache *cache )
831{
832 return( cache->pk != NULL );
833}
834
835static inline mbedtls_pk_context* mbedtls_x509_crt_cache_get_pk(
836 mbedtls_x509_crt_cache *cache )
837{
838 return( cache->pk );
839}
840
841static inline int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
842 mbedtls_x509_crt_frame **frame_ptr )
843{
844#if defined(MBEDTLS_THREADING_C)
845 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
846 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
847#endif
848
849 if( !mbedtls_x509_crt_cache_frame_set( crt->cache ) )
850 {
851 int ret;
852 ret = mbedtls_x509_crt_cache_provide_frame( crt );
853 if( ret != 0 )
854 {
855#if defined(MBEDTLS_THREADING_C)
856 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
857 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
858#endif
859 return( ret );
860 }
861 }
862
863 *frame_ptr = mbedtls_x509_crt_cache_get_frame( crt->cache );
864 return( 0 );
865}
866
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000867static inline void mbedtls_x509_crt_frame_release( mbedtls_x509_crt const *crt )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000868{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000869 ((void) crt);
870
871#if defined(MBEDTLS_THREADING_C)
872 mbedtls_mutex_unlock( &crt->cache->frame_mutex );
873#endif
874}
875
Hanno Becker180f7bf2019-02-28 13:23:38 +0000876static inline int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt const *crt,
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000877 mbedtls_pk_context **pk_ptr )
878{
879#if defined(MBEDTLS_THREADING_C)
880 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
881 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
882#endif
883
884 if( !mbedtls_x509_crt_cache_pk_set( crt->cache ) )
885 {
886 int ret;
887 ret = mbedtls_x509_crt_cache_provide_pk( crt );
888 if( ret != 0 )
889 {
890#if defined(MBEDTLS_THREADING_C)
891 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
892 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
893#endif
894 return( ret );
895 }
896 }
897
898 *pk_ptr = mbedtls_x509_crt_cache_get_pk( crt->cache );
899 return( 0 );
900}
901
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000902static inline void mbedtls_x509_crt_pk_release( mbedtls_x509_crt const *crt )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000903{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000904 ((void) crt);
905
906#if defined(MBEDTLS_THREADING_C)
907 mbedtls_mutex_unlock( &crt->cache->pk_mutex );
908#endif
909}
910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912
913/* \} name */
914/* \} addtogroup x509_module */
915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917/**
918 * \brief Initialize a CRT writing context
919 *
920 * \param ctx CRT context to initialize
921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923
924/**
925 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927 *
928 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
930 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
934/**
935 * \brief Set the serial number for a Certificate.
936 *
937 * \param ctx CRT context to use
938 * \param serial serial number to set
939 *
940 * \return 0 if successful
941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943
944/**
945 * \brief Set the validity period for a Certificate
946 * Timestamps should be in string format for UTC timezone
947 * i.e. "YYYYMMDDhhmmss"
948 * e.g. "20131231235959" for December 31st 2013
949 * at 23:59:59
950 *
951 * \param ctx CRT context to use
952 * \param not_before not_before timestamp
953 * \param not_after not_after timestamp
954 *
955 * \return 0 if timestamp was parsed successfully, or
956 * a specific error code
957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100959 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960
961/**
962 * \brief Set the issuer name for a Certificate
963 * Issuer names should contain a comma-separated list
964 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000965 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966 *
967 * \param ctx CRT context to use
968 * \param issuer_name issuer name to set
969 *
970 * \return 0 if issuer name was parsed successfully, or
971 * a specific error code
972 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100974 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975
976/**
977 * \brief Set the subject name for a Certificate
978 * Subject names should contain a comma-separated list
979 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000980 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 *
982 * \param ctx CRT context to use
983 * \param subject_name subject name to set
984 *
985 * \return 0 if subject name was parsed successfully, or
986 * a specific error code
987 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100989 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990
991/**
992 * \brief Set the subject public key for the certificate
993 *
994 * \param ctx CRT context to use
995 * \param key public key to include
996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998
999/**
1000 * \brief Set the issuer key used for signing the certificate
1001 *
1002 * \param ctx CRT context to use
1003 * \param key private key to sign with
1004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006
1007/**
1008 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 *
1011 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001012 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015
1016/**
1017 * \brief Generic function to add to or replace an extension in the
1018 * CRT
1019 *
1020 * \param ctx CRT context to use
1021 * \param oid OID of the extension
1022 * \param oid_len length of the OID
1023 * \param critical if the extension is critical (per the RFC's definition)
1024 * \param val value of the extension OCTET STRING
1025 * \param val_len length of the value data
1026 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001027 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 const char *oid, size_t oid_len,
1031 int critical,
1032 const unsigned char *val, size_t val_len );
1033
1034/**
1035 * \brief Set the basicConstraints extension for a CRT
1036 *
1037 * \param ctx CRT context to use
1038 * \param is_ca is this a CA certificate
1039 * \param max_pathlen maximum length of certificate chains below this
1040 * certificate (only for CA certificates, -1 is
1041 * inlimited)
1042 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001043 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046 int is_ca, int max_pathlen );
1047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001049/**
1050 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001052 * called before
1053 *
1054 * \param ctx CRT context to use
1055 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001056 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059
1060/**
1061 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063 * called before
1064 *
1065 * \param ctx CRT context to use
1066 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001067 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
1070#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071
1072/**
1073 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075 *
1076 * \param ctx CRT context to use
1077 * \param key_usage key usage flags to set
1078 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001079 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +02001081int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
1082 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083
1084/**
1085 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001087 *
1088 * \param ctx CRT context to use
1089 * \param ns_cert_type Netscape Cert Type flags to set
1090 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001091 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094 unsigned char ns_cert_type );
1095
1096/**
1097 * \brief Free the contents of a CRT write context
1098 *
1099 * \param ctx CRT context to free
1100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102
1103/**
1104 * \brief Write a built up certificate to a X509 DER structure
1105 * Note: data is written at the end of the buffer! Use the
1106 * return value to determine where you should start
1107 * using the buffer
1108 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001109 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110 * \param buf buffer to write to
1111 * \param size size of the buffer
1112 * \param f_rng RNG function (for signature, see note)
1113 * \param p_rng RNG parameter
1114 *
1115 * \return length of data written if successful, or a specific
1116 * error code
1117 *
1118 * \note f_rng may be NULL if RSA is used for signature and the
1119 * signature is made offline (otherwise f_rng is desirable
1120 * for countermeasures against timing attacks).
1121 * ECDSA signatures always require a non-NULL f_rng.
1122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124 int (*f_rng)(void *, unsigned char *, size_t),
1125 void *p_rng );
1126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128/**
1129 * \brief Write a built up certificate to a X509 PEM string
1130 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001131 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132 * \param buf buffer to write to
1133 * \param size size of the buffer
1134 * \param f_rng RNG function (for signature, see note)
1135 * \param p_rng RNG parameter
1136 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001137 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001138 *
1139 * \note f_rng may be NULL if RSA is used for signature and the
1140 * signature is made offline (otherwise f_rng is desirable
1141 * for countermeasures against timing attacks).
1142 * ECDSA signatures always require a non-NULL f_rng.
1143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001145 int (*f_rng)(void *, unsigned char *, size_t),
1146 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#endif /* MBEDTLS_PEM_WRITE_C */
1148#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149
1150#ifdef __cplusplus
1151}
1152#endif
1153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#endif /* mbedtls_x509_crt.h */