blob: c17285776ce8916b4de1a8d4e7979bfb0943f067 [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 */
238 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200239 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200240
241 /* for find_parent() */
242 int parent_is_trusted; /* -1 if find_parent is not in progress */
243
244 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200245 enum {
246 x509_crt_rs_none,
247 x509_crt_rs_find_parent,
248 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200249 int self_cnt;
250 mbedtls_x509_crt_verify_chain ver_chain;
251
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200252} mbedtls_x509_crt_restart_ctx;
253
254#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
255
256/* Now we can declare functions that take a pointer to that */
257typedef void mbedtls_x509_crt_restart_ctx;
258
259#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200262/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200263 * Default security profile. Should provide a good balance between security
264 * and compatibility with current deployments.
265 */
266extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
267
268/**
269 * Expected next default profile. Recommended for new deployments.
270 * Currently targets a 128-bit security level, except for RSA-2048.
271 */
272extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
273
274/**
275 * NSA Suite B profile.
276 */
277extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
278
279/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280 * \brief Parse a single DER formatted certificate and add it
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000281 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200282 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000283 * \param chain The pointer to the start of the CRT chain to attach to.
284 * When parsing the first CRT in a chain, this should point
285 * to an instance of ::mbedtls_x509_crt initialized through
286 * mbedtls_x509_crt_init().
287 * \param buf The buffer holding the DER encoded certificate.
288 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000290 * \note This function makes an internal copy of the CRT buffer
291 * \p buf. In particular, \p buf may be destroyed or reused
292 * after this call returns. To avoid duplicating the CRT
293 * buffer (at the cost of stricter lifetime constraints),
294 * use mbedtls_x509_crt_parse_der_nocopy() instead.
295 *
296 * \return \c 0 if successful.
297 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200298 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000299int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
300 const unsigned char *buf,
301 size_t buflen );
302
303/**
304 * \brief Parse a single DER formatted certificate and add it
305 * to the end of the provided chained list. This is a
306 * variant of mbedtls_x509_crt_parse_der() which takes
307 * temporary ownership of the CRT buffer until the CRT
308 * is destroyed.
309 *
310 * \param chain The pointer to the start of the CRT chain to attach to.
311 * When parsing the first CRT in a chain, this should point
312 * to an instance of ::mbedtls_x509_crt initialized through
313 * mbedtls_x509_crt_init().
314 * \param buf The address of the readable buffer holding the DER encoded
315 * certificate to use. On success, this buffer must be
316 * retained and not be changed for the liftetime of the
317 * CRT chain \p chain, that is, until \p chain is destroyed
318 * through a call to mbedtls_x509_crt_free().
319 * \param buflen The size in Bytes of \p buf.
320 *
321 * \note This call is functionally equivalent to
322 * mbedtls_x509_crt_parse_der(), but it avoids creating a
323 * copy of the input buffer at the cost of stronger lifetime
324 * constraints. This is useful in constrained environments
325 * where duplication of the CRT cannot be tolerated.
326 *
327 * \return \c 0 if successful.
328 * \return A negative error code on failure.
329 */
330int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
331 const unsigned char *buf,
332 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333
334/**
Hanno Becker89a91122018-08-23 16:14:00 +0100335 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100336 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100338 * For CRTs in PEM encoding, the function parses permissively:
339 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100340 * returns the number of certificates for which parsing failed
341 * (hence \c 0 if all certificates were parsed successfully).
342 * If no certificate could be parsed, the function returns
343 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100345 * PEM encoded certificates may be interleaved by other data
346 * such as human readable descriptions of their content, as
347 * long as the certificates are enclosed in the PEM specific
348 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
349 *
350 * \param chain The chain to which to add the parsed certificates.
351 * \param buf The buffer holding the certificate data in PEM or DER format.
352 * For certificates in PEM encoding, this may be a concatenation
353 * of multiple certificates; for DER encoding, the buffer must
354 * comprise exactly one certificate.
355 * \param buflen The size of \p buf, including the terminating \c NULL byte
356 * in case of PEM encoded data.
357 *
358 * \return \c 0 if all certificates were parsed successfully.
359 * \return The (positive) number of certificates that couldn't
360 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100361 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100362 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367/**
368 * \brief Load one or more certificates and add them
369 * to the chained list. Parses permissively. If some
370 * certificates can be parsed, the result is the number
371 * of failed certificates it encountered. If none complete
372 * correctly, the first error is returned.
373 *
374 * \param chain points to the start of the chain
375 * \param path filename to read the certificates from
376 *
377 * \return 0 if all certificates parsed successfully, a positive number
378 * if partly successful or a specific X509 or PEM error code
379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381
382/**
383 * \brief Load one or more certificate files from a path and add them
384 * to the chained list. Parses permissively. If some
385 * certificates can be parsed, the result is the number
386 * of failed certificates it encountered. If none complete
387 * correctly, the first error is returned.
388 *
389 * \param chain points to the start of the chain
390 * \param path directory / folder to read the certificate files from
391 *
392 * \return 0 if all certificates parsed successfully, a positive number
393 * if partly successful or a specific X509 or PEM error code
394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
396#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397
Hanno Becker02a21932019-06-10 15:08:43 +0100398#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399/**
400 * \brief Returns an informational string about the
401 * certificate.
402 *
403 * \param buf Buffer to write to
404 * \param size Maximum size of buffer
405 * \param prefix A line prefix
406 * \param crt The X509 certificate to represent
407 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200408 * \return The length of the string written (not including the
409 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
412 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413
414/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100415 * \brief Returns an informational string about the
416 * verification status of a certificate.
417 *
418 * \param buf Buffer to write to
419 * \param size Maximum size of buffer
420 * \param prefix A line prefix
421 * \param flags Verification flags created by mbedtls_x509_crt_verify()
422 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200423 * \return The length of the string written (not including the
424 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100425 */
426int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200427 uint32_t flags );
Hanno Beckerc6043f22019-06-14 17:21:24 +0100428#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200429
430/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431 * \brief Verify the certificate signature
432 *
433 * The verify callback is a user-supplied callback that
434 * can clear / modify / add flags for a certificate. If set,
435 * the verification callback is called for each
436 * certificate in the chain (from the trust-ca down to the
437 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439 * int *flags). With the flags representing current flags for
440 * that specific certificate and the certificate depth from
441 * the bottom (Peer cert depth = 0).
442 *
443 * All flags left after returning from the callback
444 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200445 * return 0 for anything (including invalid certificates)
446 * other than fatal error, as a non-zero return code
447 * immediately aborts the verification process. For fatal
448 * errors, a specific error code should be used (different
449 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
450 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
451 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100453 * \note In case verification failed, the results can be displayed
454 * using \c mbedtls_x509_crt_verify_info()
455 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200456 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
457 * default security profile.
458 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100459 * \note It is your responsibility to provide up-to-date CRLs for
460 * all trusted CAs. If no CRL is provided for the CA that was
461 * used to sign the certificate, CRL verification is skipped
462 * silently, that is *without* setting any flag.
463 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200464 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200465 * (1) those of trusted root CAs, so that certificates
466 * chaining up to those CAs will be trusted, and (2)
467 * self-signed end-entity certificates to be trusted (for
468 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200469 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200470 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100471 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200472 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100473 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474 * \param cn expected Common Name (can be set to
475 * NULL if the CN must not be verified)
476 * \param flags result of the verification
477 * \param f_vrfy verification function
478 * \param p_vrfy verification parameter
479 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200480 * \return 0 (and flags set to 0) if the chain was verified and valid,
481 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
482 * but found to be invalid, in which case *flags will have one
483 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
484 * flags set, or another error (and flags set to 0xffffffff)
485 * in case of a fatal error encountered during the
486 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
489 mbedtls_x509_crt *trust_ca,
490 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200491 const char *cn, uint32_t *flags,
492 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200493 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200495/**
496 * \brief Verify the certificate signature according to profile
497 *
498 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
499 * security profile.
500 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200501 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200502 * for ECDSA) apply to all certificates: trusted root,
503 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200504 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100505 * \param crt a certificate (chain) to be verified
506 * \param trust_ca the list of trusted CAs
507 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200508 * \param profile security profile for verification
509 * \param cn expected Common Name (can be set to
510 * NULL if the CN must not be verified)
511 * \param flags result of the verification
512 * \param f_vrfy verification function
513 * \param p_vrfy verification parameter
514 *
515 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
516 * in which case *flags will have one or more
517 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
518 * set,
519 * or another error in case of a fatal error encountered
520 * during the verification process.
521 */
522int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
523 mbedtls_x509_crt *trust_ca,
524 mbedtls_x509_crl *ca_crl,
525 const mbedtls_x509_crt_profile *profile,
526 const char *cn, uint32_t *flags,
527 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
528 void *p_vrfy );
529
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200530/**
531 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
532 *
533 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
534 * but can return early and restart according to the limit
535 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
536 *
537 * \param crt a certificate (chain) to be verified
538 * \param trust_ca the list of trusted CAs
539 * \param ca_crl the list of CRLs for trusted CAs
540 * \param profile security profile for verification
541 * \param cn expected Common Name (can be set to
542 * NULL if the CN must not be verified)
543 * \param flags result of the verification
544 * \param f_vrfy verification function
545 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200546 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200547 *
548 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200549 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200550 * operations was reached: see \c mbedtls_ecp_set_max_ops().
551 */
552int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
553 mbedtls_x509_crt *trust_ca,
554 mbedtls_x509_crl *ca_crl,
555 const mbedtls_x509_crt_profile *profile,
556 const char *cn, uint32_t *flags,
557 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
558 void *p_vrfy,
559 mbedtls_x509_crt_restart_ctx *rs_ctx );
560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200562/**
563 * \brief Check usage of certificate against keyUsage extension.
564 *
565 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200566 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
567 * before using the certificate to perform an RSA key
568 * exchange).
569 *
570 * \note Except for decipherOnly and encipherOnly, a bit set in the
571 * usage argument means this bit MUST be set in the
572 * certificate. For decipherOnly and encipherOnly, it means
573 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200574 *
575 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200577 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200578 *
579 * \note You should only call this function on leaf certificates, on
580 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200582 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200583int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
584 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200588/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000589 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200590 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000591 * \param crt Leaf certificate used.
592 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
593 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200595 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000596 * \return 0 if this use of the certificate is allowed,
597 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200598 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000599 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000602 const char *usage_oid,
603 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000604#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200607/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200608 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200609 *
610 * \param crt a certificate to be verified
611 * \param crl the CRL to verify against
612 *
613 * \return 1 if the certificate is revoked, 0 otherwise
614 *
615 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100616int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618
619/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200620 * \brief Initialize a certificate (chain)
621 *
622 * \param crt Certificate chain to initialize
623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200625
626/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627 * \brief Unallocate all certificate data
628 *
629 * \param crt Certificate chain to free
630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200632
633#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
634/**
635 * \brief Initialize a restart context
636 */
637void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
638
639/**
640 * \brief Free the components of a restart context
641 */
642void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
643#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000644
Hanno Becker823efad2019-02-28 13:23:58 +0000645/**
646 * \brief Request CRT frame giving access to basic CRT fields
647 * and raw ASN.1 data of complex fields.
648 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100649 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker823efad2019-02-28 13:23:58 +0000650 * \param dst The address of the destination frame structure.
651 * This need not be initialized.
652 *
653 * \note ::mbedtls_x509_crt_frame does not contain pointers to
654 * dynamically allocated memory, and hence need not be freed.
655 * Users may e.g. allocate an instance of
656 * ::mbedtls_x509_crt_frame on the stack and call this function
657 * on it, in which case no allocation/freeing has to be done.
658 *
Hanno Becker608de6a2019-06-28 10:48:01 +0100659 * \note You may also use mbedtls_x509_crt_frame_acquire() and
660 * mbedtls_x509_crt_frame_release() for copy-less variants
661 * of this function.
662 *
Hanno Becker823efad2019-02-28 13:23:58 +0000663 * \return \c 0 on success. In this case, \p dst is updated
664 * to hold the frame for the given CRT.
665 * \return A negative error code on failure.
666 */
667int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
668 mbedtls_x509_crt_frame *dst );
669
670/**
Hanno Becker9219f9e2019-05-14 12:40:58 +0100671 * \brief Set up a PK context with the public key in a certificate.
Hanno Becker823efad2019-02-28 13:23:58 +0000672 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100673 * \param crt The certificate to use. This must be initialized and set up.
Hanno Becker823efad2019-02-28 13:23:58 +0000674 * \param pk The address of the destination PK context to fill.
675 * This must be initialized via mbedtls_pk_init().
676 *
Hanno Becker608de6a2019-06-28 10:48:01 +0100677 * \note You may also use mbedtls_x509_crt_pk_acquire() and
678 * mbedtls_x509_crt_pk_release() for copy-less variants
679 * of this function.
680 *
Hanno Becker823efad2019-02-28 13:23:58 +0000681 * \return \c 0 on success. In this case, the user takes ownership
682 * of the destination PK context, and is responsible for
683 * calling mbedtls_pk_free() on it once it's no longer needed.
684 * \return A negative error code on failure.
685 */
686int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
687 mbedtls_pk_context *pk );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000688
689/**
Hanno Becker63e69982019-02-26 18:50:49 +0000690 * \brief Request the subject name of a CRT, presented
691 * as a dynamically allocated linked list.
692 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100693 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker63e69982019-02-26 18:50:49 +0000694 * \param subject The address at which to store the address of the
695 * first entry of the generated linked list holding
696 * the subject name.
697 *
698 * \note Depending in your use case, consider using the raw ASN.1
699 * describing the subject name instead of the heap-allocated
700 * linked list generated by this call. The pointers to the
701 * raw ASN.1 data are part of the CRT frame that can be queried
702 * via mbedtls_x509_crt_get_frame(), and they can be traversed
703 * via mbedtls_asn1_traverse_sequence_of().
704 *
705 * \return \c 0 on success. In this case, the user takes ownership
706 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000707 * through a call to mbedtls_x509_name_free() once it's no
708 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000709 * \return A negative error code on failure.
710 */
711int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
712 mbedtls_x509_name **subject );
713
714/**
715 * \brief Request the subject name of a CRT, presented
716 * as a dynamically allocated linked list.
717 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100718 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker63e69982019-02-26 18:50:49 +0000719 * \param issuer The address at which to store the address of the
720 * first entry of the generated linked list holding
721 * the subject name.
722 *
723 * \note Depending in your use case, consider using the raw ASN.1
724 * describing the subject name instead of the heap-allocated
725 * linked list generated by this call. The pointers to the
726 * raw ASN.1 data are part of the CRT frame that can be queried
727 * via mbedtls_x509_crt_get_frame(), and they can be traversed
728 * via mbedtls_asn1_traverse_sequence_of().
729 *
730 * \return \c 0 on success. In this case, the user takes ownership
731 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000732 * through a call to mbedtls_x509_name_free() once it's no
733 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000734 * \return A negative error code on failure.
735 */
736int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
737 mbedtls_x509_name **issuer );
738
739/**
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000740 * \brief Request the subject alternative name of a CRT, presented
741 * as a dynamically allocated linked list.
742 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100743 * \param crt The CRT to use. This must be initialized and set up.
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000744 * \param subj_alt The address at which to store the address of the
745 * first component of the subject alternative names list.
746 *
747 * \note Depending in your use case, consider using the raw ASN.1
748 * describing the subject alternative names extension
749 * instead of the heap-allocated linked list generated by this
750 * call. The pointers to the raw ASN.1 data are part of the CRT
751 * frame that can be queried via mbedtls_x509_crt_get_frame(),
752 * and mbedtls_asn1_traverse_sequence_of() can be used to
753 * traverse the list of subject alternative names.
754 *
755 * \return \c 0 on success. In this case, the user takes ownership
756 * of the name context, and is responsible for freeing it
757 * through a call to mbedtls_x509_sequence_free() once it's
758 * no longer needed.
759 * \return A negative error code on failure.
760 */
761int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
762 mbedtls_x509_sequence **subj_alt );
763
764/**
765 * \brief Request the ExtendedKeyUsage extension of a CRT,
766 * presented as a dynamically allocated linked list.
767 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100768 * \param crt The CRT to use. This must be initialized and set up.
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000769 * \param ext_key_usage The address at which to store the address of the
770 * first entry of the ExtendedKeyUsage extension.
771 *
772 * \note Depending in your use case, consider using the raw ASN.1
773 * describing the extended key usage extension instead of
774 * the heap-allocated linked list generated by this call.
775 * The pointers to the raw ASN.1 data are part of the CRT
776 * frame that can be queried via mbedtls_x509_crt_get_frame(),
777 * and mbedtls_asn1_traverse_sequence_of() can be used to
778 * traverse the entries in the extended key usage extension.
779 *
780 * \return \c 0 on success. In this case, the user takes ownership
781 * of the name context, and is responsible for freeing it
782 * through a call to mbedtls_x509_sequence_free() once it's
783 * no longer needed.
784 * \return A negative error code on failure.
785 */
786int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
787 mbedtls_x509_sequence **ext_key_usage );
788
789/**
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000790 * \brief Flush internal X.509 CRT parsing cache, if present.
791 *
792 * \param crt The CRT structure whose cache to flush.
793 *
794 * \note Calling this function frequently reduces RAM usage
795 * at the cost of performance.
796 *
797 * \return \c 0 on success.
798 * \return A negative error code on failure.
799 */
800int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt );
801
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100802/**
803 * \brief Request temporary read-access to a certificate frame
804 * for a given certificate.
805 *
806 * Once no longer needed, the frame must be released
807 * through a call to mbedtls_x509_crt_frame_release().
808 *
809 * This is a copy-less version of mbedtls_x509_crt_get_frame().
810 * See there for more information.
811 *
812 * \param crt The certificate to use. This must be initialized and set up.
813 * \param dst The address at which to store the address of a readable
814 * certificate frame for the input certificate \p crt which the
815 * caller can use until calling mbedtls_x509_crt_frame_release().
816 *
817 * \note The certificate frame `**frame_ptr` returned by this function
818 * is owned by the X.509 module and must not be freed or modified
819 * by the caller. The X.509 module guarantees its validity as long
820 * as \p crt is valid and mbedtls_x509_crt_frame_release() hasn't
821 * been issued.
822 *
823 * \return \c 0 on success. In this case, `*frame_ptr` is updated
824 * to hold the address of a frame for the given CRT.
825 * \return A negative error code on failure.
826 */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000827static inline int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100828 mbedtls_x509_crt_frame const **dst )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000829{
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100830 int ret = 0;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000831#if defined(MBEDTLS_THREADING_C)
832 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
833 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000834
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100835 if( crt->cache->frame_readers == 0 )
836#endif /* MBEDTLS_THREADING_C */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000837 {
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100838 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000839 }
840
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100841#if defined(MBEDTLS_THREADING_C)
842 if( crt->cache->frame_readers == MBEDTLS_X509_CACHE_FRAME_READERS_MAX )
843 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
844
845 crt->cache->frame_readers++;
846
847 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
848 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
849#endif /* MBEDTLS_THREADING_C */
850
851 *dst = crt->cache->frame;
852 return( ret );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000853}
854
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100855/**
856 * \brief Release access to a certificate frame acquired
857 * through a prior call to mbedtls_x509_crt_frame_acquire().
858 *
859 * \param crt The certificate for which a certificate frame has
860 * previously been acquired.
861 */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100862static inline int mbedtls_x509_crt_frame_release( mbedtls_x509_crt const *crt )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000863{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000864#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100865 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
866 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
867
868 if( crt->cache->frame_readers == 0 )
869 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
870
871 crt->cache->frame_readers--;
872
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000873 mbedtls_mutex_unlock( &crt->cache->frame_mutex );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100874#endif /* MBEDTLS_THREADING_C */
Hanno Beckerbc685192019-03-05 15:35:31 +0000875
876#if defined(MBEDTLS_X509_ALWAYS_FLUSH)
Hanno Becker60785d12019-06-07 17:49:11 +0100877 (void) mbedtls_x509_crt_flush_cache_frame( crt );
Hanno Beckerbc685192019-03-05 15:35:31 +0000878#endif /* MBEDTLS_X509_ALWAYS_FLUSH */
Hanno Becker60785d12019-06-07 17:49:11 +0100879
880#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
881 !defined(MBEDTLS_THREADING_C)
882 ((void) crt);
883#endif
884
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100885 return( 0 );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000886}
887
Hanno Becker4b70e122019-05-20 16:51:01 +0100888/**
889 * \brief Request temporary access to a public key context
890 * for a given certificate.
891 *
892 * Once no longer needed, the frame must be released
893 * through a call to mbedtls_x509_crt_pk_release().
894 *
895 * This is a copy-less version of mbedtls_x509_crt_get_pk().
896 * See there for more information.
897 *
898 * \param crt The certificate to use. This must be initialized and set up.
899 * \param dst The address at which to store the address of a public key
900 * context for the public key in the input certificate \p crt.
901 *
902 * \warning The public key context `**pk_ptr` returned by this function
903 * is owned by the X.509 module and must be used by the caller
904 * in a thread-safe way. In particular, the caller must only
905 * use the context with functions which are `const` on the input
906 * context, or those which are known to be thread-safe. The latter
907 * for example includes mbedtls_pk_sign() and mbedtls_pk_verify()
908 * for ECC or RSA public key contexts.
909 *
910 * \return \c 0 on success. In this case, `*pk_ptr` is updated
911 * to hold the address of a public key context for the given
912 * certificate.
913 * \return A negative error code on failure.
914 */
Hanno Becker180f7bf2019-02-28 13:23:38 +0000915static inline int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt const *crt,
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100916 mbedtls_pk_context **dst )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000917{
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100918 int ret = 0;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000919#if defined(MBEDTLS_THREADING_C)
920 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
921 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000922
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100923 if( crt->cache->pk_readers == 0 )
924#endif /* MBEDTLS_THREADING_C */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000925 {
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100926 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000927 }
928
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100929#if defined(MBEDTLS_THREADING_C)
930 if( crt->cache->pk_readers == MBEDTLS_X509_CACHE_PK_READERS_MAX )
931 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
932
933 crt->cache->pk_readers++;
934
935 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
936 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
937#endif /* MBEDTLS_THREADING_C */
938
939 *dst = crt->cache->pk;
940 return( ret );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000941}
942
Hanno Becker4b70e122019-05-20 16:51:01 +0100943/**
944 * \brief Release access to a public key context acquired
945 * through a prior call to mbedtls_x509_crt_frame_acquire().
946 *
947 * \param crt The certificate for which a certificate frame has
948 * previously been acquired.
949 */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100950static inline int mbedtls_x509_crt_pk_release( mbedtls_x509_crt const *crt )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000951{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000952#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100953 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
954 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
955
956 if( crt->cache->pk_readers == 0 )
957 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
958
959 crt->cache->pk_readers--;
960
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000961 mbedtls_mutex_unlock( &crt->cache->pk_mutex );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100962#endif /* MBEDTLS_THREADING_C */
Hanno Beckerbc685192019-03-05 15:35:31 +0000963
964#if defined(MBEDTLS_X509_ALWAYS_FLUSH)
Hanno Becker60785d12019-06-07 17:49:11 +0100965 (void) mbedtls_x509_crt_flush_cache_pk( crt );
Hanno Beckerbc685192019-03-05 15:35:31 +0000966#endif /* MBEDTLS_X509_ALWAYS_FLUSH */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100967
Hanno Becker60785d12019-06-07 17:49:11 +0100968#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
969 !defined(MBEDTLS_THREADING_C)
970 ((void) crt);
971#endif
972
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100973 return( 0 );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000974}
975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977
978/* \} name */
979/* \} addtogroup x509_module */
980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982/**
983 * \brief Initialize a CRT writing context
984 *
985 * \param ctx CRT context to initialize
986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988
989/**
990 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 *
993 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
995 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998
999/**
1000 * \brief Set the serial number for a Certificate.
1001 *
1002 * \param ctx CRT context to use
1003 * \param serial serial number to set
1004 *
1005 * \return 0 if successful
1006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008
1009/**
1010 * \brief Set the validity period for a Certificate
1011 * Timestamps should be in string format for UTC timezone
1012 * i.e. "YYYYMMDDhhmmss"
1013 * e.g. "20131231235959" for December 31st 2013
1014 * at 23:59:59
1015 *
1016 * \param ctx CRT context to use
1017 * \param not_before not_before timestamp
1018 * \param not_after not_after timestamp
1019 *
1020 * \return 0 if timestamp was parsed successfully, or
1021 * a specific error code
1022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +01001024 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025
1026/**
1027 * \brief Set the issuer name for a Certificate
1028 * Issuer names should contain a comma-separated list
1029 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001030 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 *
1032 * \param ctx CRT context to use
1033 * \param issuer_name issuer name to set
1034 *
1035 * \return 0 if issuer name was parsed successfully, or
1036 * a specific error code
1037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001039 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040
1041/**
1042 * \brief Set the subject name for a Certificate
1043 * Subject names should contain a comma-separated list
1044 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001045 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046 *
1047 * \param ctx CRT context to use
1048 * \param subject_name subject name to set
1049 *
1050 * \return 0 if subject name was parsed successfully, or
1051 * a specific error code
1052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001054 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055
1056/**
1057 * \brief Set the subject public key for the certificate
1058 *
1059 * \param ctx CRT context to use
1060 * \param key public key to include
1061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063
1064/**
1065 * \brief Set the issuer key used for signing the certificate
1066 *
1067 * \param ctx CRT context to use
1068 * \param key private key to sign with
1069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071
1072/**
1073 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075 *
1076 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001077 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080
1081/**
1082 * \brief Generic function to add to or replace an extension in the
1083 * CRT
1084 *
1085 * \param ctx CRT context to use
1086 * \param oid OID of the extension
1087 * \param oid_len length of the OID
1088 * \param critical if the extension is critical (per the RFC's definition)
1089 * \param val value of the extension OCTET STRING
1090 * \param val_len length of the value data
1091 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001092 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095 const char *oid, size_t oid_len,
1096 int critical,
1097 const unsigned char *val, size_t val_len );
1098
1099/**
1100 * \brief Set the basicConstraints extension for a CRT
1101 *
1102 * \param ctx CRT context to use
1103 * \param is_ca is this a CA certificate
1104 * \param max_pathlen maximum length of certificate chains below this
1105 * certificate (only for CA certificates, -1 is
1106 * inlimited)
1107 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001108 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111 int is_ca, int max_pathlen );
1112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114/**
1115 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117 * called before
1118 *
1119 * \param ctx CRT context to use
1120 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001121 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124
1125/**
1126 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128 * called before
1129 *
1130 * \param ctx CRT context to use
1131 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001132 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
1135#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001136
1137/**
1138 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140 *
1141 * \param ctx CRT context to use
1142 * \param key_usage key usage flags to set
1143 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001144 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001145 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +02001146int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
1147 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148
1149/**
1150 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152 *
1153 * \param ctx CRT context to use
1154 * \param ns_cert_type Netscape Cert Type flags to set
1155 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001156 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001159 unsigned char ns_cert_type );
1160
1161/**
1162 * \brief Free the contents of a CRT write context
1163 *
1164 * \param ctx CRT context to free
1165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001167
1168/**
1169 * \brief Write a built up certificate to a X509 DER structure
1170 * Note: data is written at the end of the buffer! Use the
1171 * return value to determine where you should start
1172 * using the buffer
1173 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001174 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001175 * \param buf buffer to write to
1176 * \param size size of the buffer
1177 * \param f_rng RNG function (for signature, see note)
1178 * \param p_rng RNG parameter
1179 *
1180 * \return length of data written if successful, or a specific
1181 * error code
1182 *
1183 * \note f_rng may be NULL if RSA is used for signature and the
1184 * signature is made offline (otherwise f_rng is desirable
1185 * for countermeasures against timing attacks).
1186 * ECDSA signatures always require a non-NULL f_rng.
1187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189 int (*f_rng)(void *, unsigned char *, size_t),
1190 void *p_rng );
1191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001193/**
1194 * \brief Write a built up certificate to a X509 PEM string
1195 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001196 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001197 * \param buf buffer to write to
1198 * \param size size of the buffer
1199 * \param f_rng RNG function (for signature, see note)
1200 * \param p_rng RNG parameter
1201 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001202 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203 *
1204 * \note f_rng may be NULL if RSA is used for signature and the
1205 * signature is made offline (otherwise f_rng is desirable
1206 * for countermeasures against timing attacks).
1207 * ECDSA signatures always require a non-NULL f_rng.
1208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001210 int (*f_rng)(void *, unsigned char *, size_t),
1211 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#endif /* MBEDTLS_PEM_WRITE_C */
1213#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001214
1215#ifdef __cplusplus
1216}
1217#endif
1218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219#endif /* mbedtls_x509_crt.h */