blob: 5212e67951d0ce643d778f5dec663c0ea32af423 [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 Becker00d39032019-05-13 12:39:44 +010035#include "x509_internal.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
Hanno Becker21f55672019-02-15 15:27:59 +000066 mbedtls_md_type_t sig_md; /**< The hash algorithm used to hash CRT before signing. */
67 mbedtls_pk_type_t sig_pk; /**< The signature algorithm used to sign the CRT hash. */
68
Hanno Beckerfd5c1852019-05-13 12:52:57 +010069 uint16_t key_usage; /**< Optional key usage extension value: See the values in x509.h */
70 uint32_t ext_types; /**< Bitfield indicating which extensions are present.
71 * See the values in x509.h. */
72
Hanno Becker21f55672019-02-15 15:27:59 +000073 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
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100/**
101 * Container for an X.509 certificate. The certificate may be chained.
102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104{
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000105 int own_buffer; /**< Indicates if \c raw is owned
Hanno Becker180f7bf2019-02-28 13:23:38 +0000106 * by the structure or not. */
107 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
Hanno Becker00d39032019-05-13 12:39:44 +0100108 mbedtls_x509_crt_cache *cache; /**< Internal parsing cache. */
Hanno Becker180f7bf2019-02-28 13:23:38 +0000109
110 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
111
112 /* Legacy fields */
113#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +0200116 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
118 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
121 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
124 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
127 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200128
Hanno Becker32c530e2019-02-06 16:13:41 +0000129 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
133 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
134 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
135 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200136
137 int ext_types; /**< Bit string containing detected and parsed extensions */
138 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
139 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+ */
140
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100141 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200142
Hanno Becker7ec9c362019-02-21 14:24:05 +0000143 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200144
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +0200145 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
148 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
149 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
150 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 +0000151#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200152}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200154
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200155/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200156 * Build flag from an algorithm/curve identifier (pk, md, ecp)
157 * Since 0 is always XXX_NONE, ignore it.
158 */
Hanno Beckerd6028a12018-10-15 12:01:35 +0100159#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200160
161/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200162 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200163 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200164 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200165 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200166typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200167{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200168 uint32_t allowed_mds; /**< MDs for signatures */
169 uint32_t allowed_pks; /**< PK algs for signatures */
170 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
171 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200172}
173mbedtls_x509_crt_profile;
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#define MBEDTLS_X509_CRT_VERSION_1 0
176#define MBEDTLS_X509_CRT_VERSION_2 1
177#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
180#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200181
Andres AGf9113192016-09-02 14:06:04 +0100182#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
183#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
184#endif
185
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186/**
187 * Container for writing a certificate (CRT)
188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200190{
191 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_mpi serial;
193 mbedtls_pk_context *subject_key;
194 mbedtls_pk_context *issuer_key;
195 mbedtls_asn1_named_data *subject;
196 mbedtls_asn1_named_data *issuer;
197 mbedtls_md_type_t md_alg;
198 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
199 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
200 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200201}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200203
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200204/**
205 * Item in a verification chain: cert and flags for it
206 */
207typedef struct {
208 mbedtls_x509_crt *crt;
209 uint32_t flags;
210} mbedtls_x509_crt_verify_chain_item;
211
212/**
213 * Max size of verification chain: end-entity + intermediates + trusted root
214 */
215#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
216
217/**
218 * Verification chain as built by \c mbedtls_crt_verify_chain()
219 */
220typedef struct
221{
222 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200223 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200224} mbedtls_x509_crt_verify_chain;
225
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200226#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
227
228/**
229 * \brief Context for resuming X.509 verify operations
230 */
231typedef struct
232{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200233 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200234 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200235
236 /* for find_parent_in() */
237 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
Hanno Becker6f61b7b2019-06-10 11:12:33 +0100238
239#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200240 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200241 int fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +0100242#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200243
244 /* for find_parent() */
245 int parent_is_trusted; /* -1 if find_parent is not in progress */
246
247 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200248 enum {
249 x509_crt_rs_none,
250 x509_crt_rs_find_parent,
251 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200252 int self_cnt;
253 mbedtls_x509_crt_verify_chain ver_chain;
254
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200255} mbedtls_x509_crt_restart_ctx;
256
257#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
258
259/* Now we can declare functions that take a pointer to that */
260typedef void mbedtls_x509_crt_restart_ctx;
261
262#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200266 * Default security profile. Should provide a good balance between security
267 * and compatibility with current deployments.
268 */
269extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
270
271/**
272 * Expected next default profile. Recommended for new deployments.
273 * Currently targets a 128-bit security level, except for RSA-2048.
274 */
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 Beckeraa8665a2019-01-31 08:57:44 +0000284 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000286 * \param chain The pointer to the start of the CRT chain to attach to.
287 * When parsing the first CRT in a chain, this should point
288 * to an instance of ::mbedtls_x509_crt initialized through
289 * mbedtls_x509_crt_init().
290 * \param buf The buffer holding the DER encoded certificate.
291 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200292 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000293 * \note This function makes an internal copy of the CRT buffer
294 * \p buf. In particular, \p buf may be destroyed or reused
295 * after this call returns. To avoid duplicating the CRT
296 * buffer (at the cost of stricter lifetime constraints),
297 * use mbedtls_x509_crt_parse_der_nocopy() instead.
298 *
299 * \return \c 0 if successful.
300 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200301 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000302int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
303 const unsigned char *buf,
304 size_t buflen );
305
306/**
307 * \brief Parse a single DER formatted certificate and add it
308 * to the end of the provided chained list. This is a
309 * variant of mbedtls_x509_crt_parse_der() which takes
310 * temporary ownership of the CRT buffer until the CRT
311 * is destroyed.
312 *
313 * \param chain The pointer to the start of the CRT chain to attach to.
314 * When parsing the first CRT in a chain, this should point
315 * to an instance of ::mbedtls_x509_crt initialized through
316 * mbedtls_x509_crt_init().
317 * \param buf The address of the readable buffer holding the DER encoded
318 * certificate to use. On success, this buffer must be
319 * retained and not be changed for the liftetime of the
320 * CRT chain \p chain, that is, until \p chain is destroyed
321 * through a call to mbedtls_x509_crt_free().
322 * \param buflen The size in Bytes of \p buf.
323 *
324 * \note This call is functionally equivalent to
325 * mbedtls_x509_crt_parse_der(), but it avoids creating a
326 * copy of the input buffer at the cost of stronger lifetime
327 * constraints. This is useful in constrained environments
328 * where duplication of the CRT cannot be tolerated.
329 *
330 * \return \c 0 if successful.
331 * \return A negative error code on failure.
332 */
333int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
334 const unsigned char *buf,
335 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336
337/**
Hanno Becker89a91122018-08-23 16:14:00 +0100338 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100339 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200340 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100341 * For CRTs in PEM encoding, the function parses permissively:
342 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100343 * returns the number of certificates for which parsing failed
344 * (hence \c 0 if all certificates were parsed successfully).
345 * If no certificate could be parsed, the function returns
346 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100348 * PEM encoded certificates may be interleaved by other data
349 * such as human readable descriptions of their content, as
350 * long as the certificates are enclosed in the PEM specific
351 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
352 *
353 * \param chain The chain to which to add the parsed certificates.
354 * \param buf The buffer holding the certificate data in PEM or DER format.
355 * For certificates in PEM encoding, this may be a concatenation
356 * of multiple certificates; for DER encoding, the buffer must
357 * comprise exactly one certificate.
358 * \param buflen The size of \p buf, including the terminating \c NULL byte
359 * in case of PEM encoded data.
360 *
361 * \return \c 0 if all certificates were parsed successfully.
362 * \return The (positive) number of certificates that couldn't
363 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100364 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100365 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370/**
371 * \brief Load one or more certificates and add them
372 * to the chained list. Parses permissively. If some
373 * certificates can be parsed, the result is the number
374 * of failed certificates it encountered. If none complete
375 * correctly, the first error is returned.
376 *
377 * \param chain points to the start of the chain
378 * \param path filename to read the certificates from
379 *
380 * \return 0 if all certificates parsed successfully, a positive number
381 * if partly successful or a specific X509 or PEM error code
382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384
385/**
386 * \brief Load one or more certificate files from a path and add them
387 * to the chained list. Parses permissively. If some
388 * certificates can be parsed, the result is the number
389 * of failed certificates it encountered. If none complete
390 * correctly, the first error is returned.
391 *
392 * \param chain points to the start of the chain
393 * \param path directory / folder to read the certificate files from
394 *
395 * \return 0 if all certificates parsed successfully, a positive number
396 * if partly successful or a specific X509 or PEM error code
397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
399#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400
Hanno Becker02a21932019-06-10 15:08:43 +0100401#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402/**
403 * \brief Returns an informational string about the
404 * certificate.
405 *
406 * \param buf Buffer to write to
407 * \param size Maximum size of buffer
408 * \param prefix A line prefix
409 * \param crt The X509 certificate to represent
410 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200411 * \return The length of the string written (not including the
412 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
415 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416
417/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100418 * \brief Returns an informational string about the
419 * verification status of a certificate.
420 *
421 * \param buf Buffer to write to
422 * \param size Maximum size of buffer
423 * \param prefix A line prefix
424 * \param flags Verification flags created by mbedtls_x509_crt_verify()
425 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200426 * \return The length of the string written (not including the
427 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100428 */
429int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200430 uint32_t flags );
Hanno Beckerc6043f22019-06-14 17:21:24 +0100431#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200432
433/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434 * \brief Verify the certificate signature
435 *
436 * The verify callback is a user-supplied callback that
437 * can clear / modify / add flags for a certificate. If set,
438 * the verification callback is called for each
439 * certificate in the chain (from the trust-ca down to the
440 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442 * int *flags). With the flags representing current flags for
443 * that specific certificate and the certificate depth from
444 * the bottom (Peer cert depth = 0).
445 *
446 * All flags left after returning from the callback
447 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200448 * return 0 for anything (including invalid certificates)
449 * other than fatal error, as a non-zero return code
450 * immediately aborts the verification process. For fatal
451 * errors, a specific error code should be used (different
452 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
453 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
454 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100456 * \note In case verification failed, the results can be displayed
457 * using \c mbedtls_x509_crt_verify_info()
458 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200459 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
460 * default security profile.
461 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100462 * \note It is your responsibility to provide up-to-date CRLs for
463 * all trusted CAs. If no CRL is provided for the CA that was
464 * used to sign the certificate, CRL verification is skipped
465 * silently, that is *without* setting any flag.
466 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200467 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200468 * (1) those of trusted root CAs, so that certificates
469 * chaining up to those CAs will be trusted, and (2)
470 * self-signed end-entity certificates to be trusted (for
471 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200472 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200473 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100474 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200475 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100476 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477 * \param cn expected Common Name (can be set to
478 * NULL if the CN must not be verified)
479 * \param flags result of the verification
480 * \param f_vrfy verification function
481 * \param p_vrfy verification parameter
482 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200483 * \return 0 (and flags set to 0) if the chain was verified and valid,
484 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
485 * but found to be invalid, in which case *flags will have one
486 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
487 * flags set, or another error (and flags set to 0xffffffff)
488 * in case of a fatal error encountered during the
489 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
492 mbedtls_x509_crt *trust_ca,
493 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200494 const char *cn, uint32_t *flags,
495 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200496 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200498/**
499 * \brief Verify the certificate signature according to profile
500 *
501 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
502 * security profile.
503 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200504 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200505 * for ECDSA) apply to all certificates: trusted root,
506 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200507 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100508 * \param crt a certificate (chain) to be verified
509 * \param trust_ca the list of trusted CAs
510 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200511 * \param profile security profile for verification
512 * \param cn expected Common Name (can be set to
513 * NULL if the CN must not be verified)
514 * \param flags result of the verification
515 * \param f_vrfy verification function
516 * \param p_vrfy verification parameter
517 *
518 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
519 * in which case *flags will have one or more
520 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
521 * set,
522 * or another error in case of a fatal error encountered
523 * during the verification process.
524 */
525int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
526 mbedtls_x509_crt *trust_ca,
527 mbedtls_x509_crl *ca_crl,
528 const mbedtls_x509_crt_profile *profile,
529 const char *cn, uint32_t *flags,
530 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
531 void *p_vrfy );
532
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200533/**
534 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
535 *
536 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
537 * but can return early and restart according to the limit
538 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
539 *
540 * \param crt a certificate (chain) to be verified
541 * \param trust_ca the list of trusted CAs
542 * \param ca_crl the list of CRLs for trusted CAs
543 * \param profile security profile for verification
544 * \param cn expected Common Name (can be set to
545 * NULL if the CN must not be verified)
546 * \param flags result of the verification
547 * \param f_vrfy verification function
548 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200549 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200550 *
551 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200552 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200553 * operations was reached: see \c mbedtls_ecp_set_max_ops().
554 */
555int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
556 mbedtls_x509_crt *trust_ca,
557 mbedtls_x509_crl *ca_crl,
558 const mbedtls_x509_crt_profile *profile,
559 const char *cn, uint32_t *flags,
560 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
561 void *p_vrfy,
562 mbedtls_x509_crt_restart_ctx *rs_ctx );
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200565/**
566 * \brief Check usage of certificate against keyUsage extension.
567 *
568 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200569 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
570 * before using the certificate to perform an RSA key
571 * exchange).
572 *
573 * \note Except for decipherOnly and encipherOnly, a bit set in the
574 * usage argument means this bit MUST be set in the
575 * certificate. For decipherOnly and encipherOnly, it means
576 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200577 *
578 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200580 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200581 *
582 * \note You should only call this function on leaf certificates, on
583 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200585 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200586int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
587 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200591/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000592 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200593 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000594 * \param crt Leaf certificate used.
595 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
596 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200598 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000599 * \return 0 if this use of the certificate is allowed,
600 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200601 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000602 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000605 const char *usage_oid,
606 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000607#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200611 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612 *
613 * \param crt a certificate to be verified
614 * \param crl the CRL to verify against
615 *
616 * \return 1 if the certificate is revoked, 0 otherwise
617 *
618 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100619int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621
622/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200623 * \brief Initialize a certificate (chain)
624 *
625 * \param crt Certificate chain to initialize
626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200628
629/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 * \brief Unallocate all certificate data
631 *
632 * \param crt Certificate chain to free
633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200635
636#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
637/**
638 * \brief Initialize a restart context
639 */
640void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
641
642/**
643 * \brief Free the components of a restart context
644 */
645void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
646#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000647
Hanno Becker823efad2019-02-28 13:23:58 +0000648/**
649 * \brief Request CRT frame giving access to basic CRT fields
650 * and raw ASN.1 data of complex fields.
651 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100652 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker823efad2019-02-28 13:23:58 +0000653 * \param dst The address of the destination frame structure.
654 * This need not be initialized.
655 *
656 * \note ::mbedtls_x509_crt_frame does not contain pointers to
657 * dynamically allocated memory, and hence need not be freed.
658 * Users may e.g. allocate an instance of
659 * ::mbedtls_x509_crt_frame on the stack and call this function
660 * on it, in which case no allocation/freeing has to be done.
661 *
Hanno Becker608de6a2019-06-28 10:48:01 +0100662 * \note You may also use mbedtls_x509_crt_frame_acquire() and
663 * mbedtls_x509_crt_frame_release() for copy-less variants
664 * of this function.
665 *
Hanno Becker823efad2019-02-28 13:23:58 +0000666 * \return \c 0 on success. In this case, \p dst is updated
667 * to hold the frame for the given CRT.
668 * \return A negative error code on failure.
669 */
670int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
671 mbedtls_x509_crt_frame *dst );
672
673/**
Hanno Becker9219f9e2019-05-14 12:40:58 +0100674 * \brief Set up a PK context with the public key in a certificate.
Hanno Becker823efad2019-02-28 13:23:58 +0000675 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100676 * \param crt The certificate to use. This must be initialized and set up.
Hanno Becker823efad2019-02-28 13:23:58 +0000677 * \param pk The address of the destination PK context to fill.
678 * This must be initialized via mbedtls_pk_init().
679 *
Hanno Becker608de6a2019-06-28 10:48:01 +0100680 * \note You may also use mbedtls_x509_crt_pk_acquire() and
681 * mbedtls_x509_crt_pk_release() for copy-less variants
682 * of this function.
683 *
Hanno Becker823efad2019-02-28 13:23:58 +0000684 * \return \c 0 on success. In this case, the user takes ownership
685 * of the destination PK context, and is responsible for
686 * calling mbedtls_pk_free() on it once it's no longer needed.
687 * \return A negative error code on failure.
688 */
689int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
690 mbedtls_pk_context *pk );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000691
692/**
Hanno Becker63e69982019-02-26 18:50:49 +0000693 * \brief Request the subject name of a CRT, presented
694 * as a dynamically allocated linked list.
695 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100696 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker63e69982019-02-26 18:50:49 +0000697 * \param subject The address at which to store the address of the
698 * first entry of the generated linked list holding
699 * the subject name.
700 *
Hanno Beckerd92078f2019-06-28 10:48:57 +0100701 * \note Depending on your use case, consider using the raw ASN.1
Hanno Becker63e69982019-02-26 18:50:49 +0000702 * describing the subject name instead of the heap-allocated
703 * linked list generated by this call. The pointers to the
704 * raw ASN.1 data are part of the CRT frame that can be queried
705 * via mbedtls_x509_crt_get_frame(), and they can be traversed
706 * via mbedtls_asn1_traverse_sequence_of().
707 *
708 * \return \c 0 on success. In this case, the user takes ownership
709 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000710 * through a call to mbedtls_x509_name_free() once it's no
711 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000712 * \return A negative error code on failure.
713 */
714int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
715 mbedtls_x509_name **subject );
716
717/**
718 * \brief Request the subject name of a CRT, presented
719 * as a dynamically allocated linked list.
720 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100721 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker63e69982019-02-26 18:50:49 +0000722 * \param issuer The address at which to store the address of the
723 * first entry of the generated linked list holding
724 * the subject name.
725 *
Hanno Beckerd92078f2019-06-28 10:48:57 +0100726 * \note Depending on your use case, consider using the raw ASN.1
727 * describing the issuer name instead of the heap-allocated
Hanno Becker63e69982019-02-26 18:50:49 +0000728 * linked list generated by this call. The pointers to the
729 * raw ASN.1 data are part of the CRT frame that can be queried
730 * via mbedtls_x509_crt_get_frame(), and they can be traversed
731 * via mbedtls_asn1_traverse_sequence_of().
732 *
733 * \return \c 0 on success. In this case, the user takes ownership
734 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000735 * through a call to mbedtls_x509_name_free() once it's no
736 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000737 * \return A negative error code on failure.
738 */
739int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
740 mbedtls_x509_name **issuer );
741
742/**
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000743 * \brief Request the subject alternative name of a CRT, presented
744 * as a dynamically allocated linked list.
745 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100746 * \param crt The CRT to use. This must be initialized and set up.
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000747 * \param subj_alt The address at which to store the address of the
748 * first component of the subject alternative names list.
749 *
750 * \note Depending in your use case, consider using the raw ASN.1
751 * describing the subject alternative names extension
752 * instead of the heap-allocated linked list generated by this
753 * call. The pointers to the raw ASN.1 data are part of the CRT
754 * frame that can be queried via mbedtls_x509_crt_get_frame(),
755 * and mbedtls_asn1_traverse_sequence_of() can be used to
756 * traverse the list of subject alternative names.
757 *
758 * \return \c 0 on success. In this case, the user takes ownership
759 * of the name context, and is responsible for freeing it
760 * through a call to mbedtls_x509_sequence_free() once it's
761 * no longer needed.
762 * \return A negative error code on failure.
763 */
764int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
765 mbedtls_x509_sequence **subj_alt );
766
767/**
768 * \brief Request the ExtendedKeyUsage extension of a CRT,
769 * presented as a dynamically allocated linked list.
770 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100771 * \param crt The CRT to use. This must be initialized and set up.
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000772 * \param ext_key_usage The address at which to store the address of the
773 * first entry of the ExtendedKeyUsage extension.
774 *
775 * \note Depending in your use case, consider using the raw ASN.1
776 * describing the extended key usage extension instead of
777 * the heap-allocated linked list generated by this call.
778 * The pointers to the raw ASN.1 data are part of the CRT
779 * frame that can be queried via mbedtls_x509_crt_get_frame(),
780 * and mbedtls_asn1_traverse_sequence_of() can be used to
781 * traverse the entries in the extended key usage extension.
782 *
783 * \return \c 0 on success. In this case, the user takes ownership
784 * of the name context, and is responsible for freeing it
785 * through a call to mbedtls_x509_sequence_free() once it's
786 * no longer needed.
787 * \return A negative error code on failure.
788 */
789int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
790 mbedtls_x509_sequence **ext_key_usage );
791
792/**
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000793 * \brief Flush internal X.509 CRT parsing cache, if present.
794 *
795 * \param crt The CRT structure whose cache to flush.
796 *
797 * \note Calling this function frequently reduces RAM usage
798 * at the cost of performance.
799 *
800 * \return \c 0 on success.
801 * \return A negative error code on failure.
802 */
803int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt );
804
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100805/**
806 * \brief Request temporary read-access to a certificate frame
807 * for a given certificate.
808 *
809 * Once no longer needed, the frame must be released
810 * through a call to mbedtls_x509_crt_frame_release().
811 *
812 * This is a copy-less version of mbedtls_x509_crt_get_frame().
813 * See there for more information.
814 *
815 * \param crt The certificate to use. This must be initialized and set up.
816 * \param dst The address at which to store the address of a readable
817 * certificate frame for the input certificate \p crt which the
818 * caller can use until calling mbedtls_x509_crt_frame_release().
819 *
820 * \note The certificate frame `**frame_ptr` returned by this function
821 * is owned by the X.509 module and must not be freed or modified
822 * by the caller. The X.509 module guarantees its validity as long
823 * as \p crt is valid and mbedtls_x509_crt_frame_release() hasn't
824 * been issued.
825 *
Hanno Beckerfc99a092019-06-28 14:45:26 +0100826 * \note In a single-threaded application using
827 * MBEDTLS_X509_ALWAYS_FLUSH, nested calls to this function
828 * are not allowed and will fail gracefully with
829 * MBEDTLS_ERR_X509_FATAL_ERROR.
830 *
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100831 * \return \c 0 on success. In this case, `*frame_ptr` is updated
832 * to hold the address of a frame for the given CRT.
833 * \return A negative error code on failure.
834 */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000835static inline int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100836 mbedtls_x509_crt_frame const **dst )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000837{
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100838 int ret = 0;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000839#if defined(MBEDTLS_THREADING_C)
840 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
841 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100842#endif /* MBEDTLS_THREADING_C */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000843
Hanno Beckerfc99a092019-06-28 14:45:26 +0100844#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
845 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100846 if( crt->cache->frame_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100847#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100848 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000849
Hanno Beckerfc99a092019-06-28 14:45:26 +0100850#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
851 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100852 if( crt->cache->frame_readers == MBEDTLS_X509_CACHE_FRAME_READERS_MAX )
853 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
854
855 crt->cache->frame_readers++;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100856#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100857
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100858#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100859 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
860 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
861#endif /* MBEDTLS_THREADING_C */
862
863 *dst = crt->cache->frame;
864 return( ret );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000865}
866
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100867/**
868 * \brief Release access to a certificate frame acquired
869 * through a prior call to mbedtls_x509_crt_frame_acquire().
870 *
871 * \param crt The certificate for which a certificate frame has
872 * previously been acquired.
873 */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100874static inline int mbedtls_x509_crt_frame_release( mbedtls_x509_crt const *crt )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000875{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000876#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100877 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
878 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100879#endif /* MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100880
Hanno Beckerfc99a092019-06-28 14:45:26 +0100881#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
882 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100883 if( crt->cache->frame_readers == 0 )
Hanno Becker69c30332019-06-28 15:47:53 +0100884 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100885
886 crt->cache->frame_readers--;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100887#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100888
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100889#if defined(MBEDTLS_THREADING_C)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000890 mbedtls_mutex_unlock( &crt->cache->frame_mutex );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100891#endif /* MBEDTLS_THREADING_C */
Hanno Beckerbc685192019-03-05 15:35:31 +0000892
893#if defined(MBEDTLS_X509_ALWAYS_FLUSH)
Hanno Becker60785d12019-06-07 17:49:11 +0100894 (void) mbedtls_x509_crt_flush_cache_frame( crt );
Hanno Beckerbc685192019-03-05 15:35:31 +0000895#endif /* MBEDTLS_X509_ALWAYS_FLUSH */
Hanno Becker60785d12019-06-07 17:49:11 +0100896
897#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
898 !defined(MBEDTLS_THREADING_C)
899 ((void) crt);
900#endif
901
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100902 return( 0 );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000903}
904
Hanno Becker4b70e122019-05-20 16:51:01 +0100905/**
906 * \brief Request temporary access to a public key context
907 * for a given certificate.
908 *
909 * Once no longer needed, the frame must be released
910 * through a call to mbedtls_x509_crt_pk_release().
911 *
912 * This is a copy-less version of mbedtls_x509_crt_get_pk().
913 * See there for more information.
914 *
915 * \param crt The certificate to use. This must be initialized and set up.
916 * \param dst The address at which to store the address of a public key
917 * context for the public key in the input certificate \p crt.
918 *
919 * \warning The public key context `**pk_ptr` returned by this function
920 * is owned by the X.509 module and must be used by the caller
921 * in a thread-safe way. In particular, the caller must only
922 * use the context with functions which are `const` on the input
923 * context, or those which are known to be thread-safe. The latter
Hanno Beckerb653aa32019-06-28 10:53:55 +0100924 * for example includes mbedtls_pk_verify() for ECC or RSA public
925 * key contexts.
Hanno Becker4b70e122019-05-20 16:51:01 +0100926 *
Hanno Beckerfc99a092019-06-28 14:45:26 +0100927 * \note In a single-threaded application using
928 * MBEDTLS_X509_ALWAYS_FLUSH, nested calls to this function
929 * are not allowed and will fail gracefully with
930 * MBEDTLS_ERR_X509_FATAL_ERROR.
931 *
Hanno Becker4b70e122019-05-20 16:51:01 +0100932 * \return \c 0 on success. In this case, `*pk_ptr` is updated
933 * to hold the address of a public key context for the given
934 * certificate.
935 * \return A negative error code on failure.
936 */
Hanno Becker180f7bf2019-02-28 13:23:38 +0000937static inline int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt const *crt,
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100938 mbedtls_pk_context **dst )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000939{
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100940 int ret = 0;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000941#if defined(MBEDTLS_THREADING_C)
942 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
943 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100944#endif /* MBEDTLS_THREADING_C */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000945
Hanno Beckerfc99a092019-06-28 14:45:26 +0100946#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
947 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100948 if( crt->cache->pk_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100949#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100950 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000951
Hanno Beckerfc99a092019-06-28 14:45:26 +0100952#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
953 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100954 if( crt->cache->pk_readers == MBEDTLS_X509_CACHE_PK_READERS_MAX )
955 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
956
957 crt->cache->pk_readers++;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100958#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100959
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100960#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100961 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
962 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
963#endif /* MBEDTLS_THREADING_C */
964
965 *dst = crt->cache->pk;
966 return( ret );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000967}
968
Hanno Becker4b70e122019-05-20 16:51:01 +0100969/**
970 * \brief Release access to a public key context acquired
971 * through a prior call to mbedtls_x509_crt_frame_acquire().
972 *
973 * \param crt The certificate for which a certificate frame has
974 * previously been acquired.
975 */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100976static inline int mbedtls_x509_crt_pk_release( mbedtls_x509_crt const *crt )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000977{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000978#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100979 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
980 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100981#endif /* MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100982
Hanno Beckerfc99a092019-06-28 14:45:26 +0100983#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
984 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100985 if( crt->cache->pk_readers == 0 )
Hanno Becker69c30332019-06-28 15:47:53 +0100986 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100987
988 crt->cache->pk_readers--;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100989#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100990
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100991#if defined(MBEDTLS_THREADING_C)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000992 mbedtls_mutex_unlock( &crt->cache->pk_mutex );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100993#endif /* MBEDTLS_THREADING_C */
Hanno Beckerbc685192019-03-05 15:35:31 +0000994
995#if defined(MBEDTLS_X509_ALWAYS_FLUSH)
Hanno Becker60785d12019-06-07 17:49:11 +0100996 (void) mbedtls_x509_crt_flush_cache_pk( crt );
Hanno Beckerbc685192019-03-05 15:35:31 +0000997#endif /* MBEDTLS_X509_ALWAYS_FLUSH */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100998
Hanno Becker60785d12019-06-07 17:49:11 +0100999#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
1000 !defined(MBEDTLS_THREADING_C)
1001 ((void) crt);
1002#endif
1003
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001004 return( 0 );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001005}
1006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008
1009/* \} name */
1010/* \} addtogroup x509_module */
1011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013/**
1014 * \brief Initialize a CRT writing context
1015 *
1016 * \param ctx CRT context to initialize
1017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019
1020/**
1021 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 *
1024 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
1026 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029
1030/**
1031 * \brief Set the serial number for a Certificate.
1032 *
1033 * \param ctx CRT context to use
1034 * \param serial serial number to set
1035 *
1036 * \return 0 if successful
1037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039
1040/**
1041 * \brief Set the validity period for a Certificate
1042 * Timestamps should be in string format for UTC timezone
1043 * i.e. "YYYYMMDDhhmmss"
1044 * e.g. "20131231235959" for December 31st 2013
1045 * at 23:59:59
1046 *
1047 * \param ctx CRT context to use
1048 * \param not_before not_before timestamp
1049 * \param not_after not_after timestamp
1050 *
1051 * \return 0 if timestamp was parsed successfully, or
1052 * a specific error code
1053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +01001055 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056
1057/**
1058 * \brief Set the issuer name for a Certificate
1059 * Issuer names should contain a comma-separated list
1060 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001061 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062 *
1063 * \param ctx CRT context to use
1064 * \param issuer_name issuer name to set
1065 *
1066 * \return 0 if issuer name was parsed successfully, or
1067 * a specific error code
1068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001070 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071
1072/**
1073 * \brief Set the subject name for a Certificate
1074 * Subject names should contain a comma-separated list
1075 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001076 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077 *
1078 * \param ctx CRT context to use
1079 * \param subject_name subject name to set
1080 *
1081 * \return 0 if subject name was parsed successfully, or
1082 * a specific error code
1083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001085 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086
1087/**
1088 * \brief Set the subject public key for the certificate
1089 *
1090 * \param ctx CRT context to use
1091 * \param key public key to include
1092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094
1095/**
1096 * \brief Set the issuer key used for signing the certificate
1097 *
1098 * \param ctx CRT context to use
1099 * \param key private key to sign with
1100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102
1103/**
1104 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106 *
1107 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001108 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111
1112/**
1113 * \brief Generic function to add to or replace an extension in the
1114 * CRT
1115 *
1116 * \param ctx CRT context to use
1117 * \param oid OID of the extension
1118 * \param oid_len length of the OID
1119 * \param critical if the extension is critical (per the RFC's definition)
1120 * \param val value of the extension OCTET STRING
1121 * \param val_len length of the value data
1122 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001123 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001126 const char *oid, size_t oid_len,
1127 int critical,
1128 const unsigned char *val, size_t val_len );
1129
1130/**
1131 * \brief Set the basicConstraints extension for a CRT
1132 *
1133 * \param ctx CRT context to use
1134 * \param is_ca is this a CA certificate
1135 * \param max_pathlen maximum length of certificate chains below this
1136 * certificate (only for CA certificates, -1 is
1137 * inlimited)
1138 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001139 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142 int is_ca, int max_pathlen );
1143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001145/**
1146 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 * called before
1149 *
1150 * \param ctx CRT context to use
1151 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001152 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155
1156/**
1157 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001159 * called before
1160 *
1161 * \param ctx CRT context to use
1162 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001163 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
1166#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001167
1168/**
1169 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 *
1172 * \param ctx CRT context to use
1173 * \param key_usage key usage flags to set
1174 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001175 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +02001177int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
1178 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001179
1180/**
1181 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001183 *
1184 * \param ctx CRT context to use
1185 * \param ns_cert_type Netscape Cert Type flags to set
1186 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001187 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001190 unsigned char ns_cert_type );
1191
1192/**
1193 * \brief Free the contents of a CRT write context
1194 *
1195 * \param ctx CRT context to free
1196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001198
1199/**
1200 * \brief Write a built up certificate to a X509 DER structure
1201 * Note: data is written at the end of the buffer! Use the
1202 * return value to determine where you should start
1203 * using the buffer
1204 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001205 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206 * \param buf buffer to write to
1207 * \param size size of the buffer
1208 * \param f_rng RNG function (for signature, see note)
1209 * \param p_rng RNG parameter
1210 *
1211 * \return length of data written if successful, or a specific
1212 * error code
1213 *
1214 * \note f_rng may be NULL if RSA is used for signature and the
1215 * signature is made offline (otherwise f_rng is desirable
1216 * for countermeasures against timing attacks).
1217 * ECDSA signatures always require a non-NULL f_rng.
1218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001220 int (*f_rng)(void *, unsigned char *, size_t),
1221 void *p_rng );
1222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001224/**
1225 * \brief Write a built up certificate to a X509 PEM string
1226 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001227 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001228 * \param buf buffer to write to
1229 * \param size size of the buffer
1230 * \param f_rng RNG function (for signature, see note)
1231 * \param p_rng RNG parameter
1232 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001233 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001234 *
1235 * \note f_rng may be NULL if RSA is used for signature and the
1236 * signature is made offline (otherwise f_rng is desirable
1237 * for countermeasures against timing attacks).
1238 * ECDSA signatures always require a non-NULL f_rng.
1239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001241 int (*f_rng)(void *, unsigned char *, size_t),
1242 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243#endif /* MBEDTLS_PEM_WRITE_C */
1244#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245
1246#ifdef __cplusplus
1247}
1248#endif
1249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250#endif /* mbedtls_x509_crt.h */