blob: 88e3e7b47379022229bd75273b3da04994ef8738 [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 Becker843b71a2019-06-25 09:39:21 +010073#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker21f55672019-02-15 15:27:59 +000074 mbedtls_x509_time valid_from; /**< The start time of certificate validity. */
75 mbedtls_x509_time valid_to; /**< The end time of certificate validity. */
Hanno Becker843b71a2019-06-25 09:39:21 +010076#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker21f55672019-02-15 15:27:59 +000077
78 mbedtls_x509_buf_raw raw; /**< The raw certificate data in DER. */
79 mbedtls_x509_buf_raw tbs; /**< The part of the CRT that is [T]o [B]e [S]igned. */
80
81 mbedtls_x509_buf_raw serial; /**< The unique ID for certificate issued by a specific CA. */
82
83 mbedtls_x509_buf_raw pubkey_raw; /**< The raw public key data (DER). */
84
85 mbedtls_x509_buf_raw issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
86 mbedtls_x509_buf_raw issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
87
88 mbedtls_x509_buf_raw subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
89 mbedtls_x509_buf_raw subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
90
91 mbedtls_x509_buf_raw sig; /**< Signature: hash of the tbs part signed with the private key. */
92 mbedtls_x509_buf_raw sig_alg; /**< The signature algorithm used for \p sig. */
93
94 mbedtls_x509_buf_raw v3_ext; /**< The raw data for the extension list in the certificate.
95 * Might be useful for manual inspection of extensions that
96 * Mbed TLS doesn't yet support. */
97 mbedtls_x509_buf_raw subject_alt_raw; /**< The raw data for the SubjectAlternativeNames extension. */
98 mbedtls_x509_buf_raw ext_key_usage_raw; /**< The raw data for the ExtendedKeyUsage extension. */
99
Hanno Becker21f55672019-02-15 15:27:59 +0000100} mbedtls_x509_crt_frame;
101
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102/**
103 * Container for an X.509 certificate. The certificate may be chained.
104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200106{
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000107 int own_buffer; /**< Indicates if \c raw is owned
Hanno Becker180f7bf2019-02-28 13:23:38 +0000108 * by the structure or not. */
109 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
Hanno Becker00d39032019-05-13 12:39:44 +0100110 mbedtls_x509_crt_cache *cache; /**< Internal parsing cache. */
Hanno Becker180f7bf2019-02-28 13:23:38 +0000111
112 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
113
114 /* Legacy fields */
115#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +0200118 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
120 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
123 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
126 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127
Hanno Becker843b71a2019-06-25 09:39:21 +0100128#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
130 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Hanno Becker843b71a2019-06-25 09:39:21 +0100131#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200132
Hanno Becker32c530e2019-02-06 16:13:41 +0000133 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
137 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
138 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
139 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200140
141 int ext_types; /**< Bit string containing detected and parsed extensions */
142 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
143 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+ */
144
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100145 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200146
Hanno Becker7ec9c362019-02-21 14:24:05 +0000147 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200148
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +0200149 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
152 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
153 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
154 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 +0000155#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200158
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200159/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200160 * Build flag from an algorithm/curve identifier (pk, md, ecp)
161 * Since 0 is always XXX_NONE, ignore it.
162 */
Hanno Beckerd6028a12018-10-15 12:01:35 +0100163#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200164
165/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200166 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200167 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200168 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200169 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200170typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200171{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200172 uint32_t allowed_mds; /**< MDs for signatures */
173 uint32_t allowed_pks; /**< PK algs for signatures */
174 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
175 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200176}
177mbedtls_x509_crt_profile;
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#define MBEDTLS_X509_CRT_VERSION_1 0
180#define MBEDTLS_X509_CRT_VERSION_2 1
181#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
184#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200185
Andres AGf9113192016-09-02 14:06:04 +0100186#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
187#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
188#endif
189
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200190/**
191 * Container for writing a certificate (CRT)
192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200194{
195 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_mpi serial;
197 mbedtls_pk_context *subject_key;
198 mbedtls_pk_context *issuer_key;
199 mbedtls_asn1_named_data *subject;
200 mbedtls_asn1_named_data *issuer;
201 mbedtls_md_type_t md_alg;
202 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
203 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
204 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200205}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200207
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200208/**
209 * Item in a verification chain: cert and flags for it
210 */
211typedef struct {
212 mbedtls_x509_crt *crt;
213 uint32_t flags;
214} mbedtls_x509_crt_verify_chain_item;
215
216/**
217 * Max size of verification chain: end-entity + intermediates + trusted root
218 */
219#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
220
221/**
222 * Verification chain as built by \c mbedtls_crt_verify_chain()
223 */
224typedef struct
225{
226 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200227 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200228} mbedtls_x509_crt_verify_chain;
229
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200230#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
231
232/**
233 * \brief Context for resuming X.509 verify operations
234 */
235typedef struct
236{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200237 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200238 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200239
240 /* for find_parent_in() */
241 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
Hanno Becker6f61b7b2019-06-10 11:12:33 +0100242
243#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200244 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200245 int fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +0100246#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200247
248 /* for find_parent() */
249 int parent_is_trusted; /* -1 if find_parent is not in progress */
250
251 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200252 enum {
253 x509_crt_rs_none,
254 x509_crt_rs_find_parent,
255 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200256 int self_cnt;
257 mbedtls_x509_crt_verify_chain ver_chain;
258
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200259} mbedtls_x509_crt_restart_ctx;
260
261#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
262
263/* Now we can declare functions that take a pointer to that */
264typedef void mbedtls_x509_crt_restart_ctx;
265
266#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200270 * Default security profile. Should provide a good balance between security
271 * and compatibility with current deployments.
272 */
273extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
274
275/**
276 * Expected next default profile. Recommended for new deployments.
277 * Currently targets a 128-bit security level, except for RSA-2048.
278 */
279extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
280
281/**
282 * NSA Suite B profile.
283 */
284extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
285
286/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287 * \brief Parse a single DER formatted certificate and add it
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000288 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000290 * \param chain The pointer to the start of the CRT chain to attach to.
291 * When parsing the first CRT in a chain, this should point
292 * to an instance of ::mbedtls_x509_crt initialized through
293 * mbedtls_x509_crt_init().
294 * \param buf The buffer holding the DER encoded certificate.
295 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000297 * \note This function makes an internal copy of the CRT buffer
298 * \p buf. In particular, \p buf may be destroyed or reused
299 * after this call returns. To avoid duplicating the CRT
300 * buffer (at the cost of stricter lifetime constraints),
301 * use mbedtls_x509_crt_parse_der_nocopy() instead.
302 *
303 * \return \c 0 if successful.
304 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000306int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
307 const unsigned char *buf,
308 size_t buflen );
309
310/**
311 * \brief Parse a single DER formatted certificate and add it
312 * to the end of the provided chained list. This is a
313 * variant of mbedtls_x509_crt_parse_der() which takes
314 * temporary ownership of the CRT buffer until the CRT
315 * is destroyed.
316 *
317 * \param chain The pointer to the start of the CRT chain to attach to.
318 * When parsing the first CRT in a chain, this should point
319 * to an instance of ::mbedtls_x509_crt initialized through
320 * mbedtls_x509_crt_init().
321 * \param buf The address of the readable buffer holding the DER encoded
322 * certificate to use. On success, this buffer must be
323 * retained and not be changed for the liftetime of the
324 * CRT chain \p chain, that is, until \p chain is destroyed
325 * through a call to mbedtls_x509_crt_free().
326 * \param buflen The size in Bytes of \p buf.
327 *
328 * \note This call is functionally equivalent to
329 * mbedtls_x509_crt_parse_der(), but it avoids creating a
330 * copy of the input buffer at the cost of stronger lifetime
331 * constraints. This is useful in constrained environments
332 * where duplication of the CRT cannot be tolerated.
333 *
334 * \return \c 0 if successful.
335 * \return A negative error code on failure.
336 */
337int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
338 const unsigned char *buf,
339 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200340
341/**
Hanno Becker89a91122018-08-23 16:14:00 +0100342 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100343 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100345 * For CRTs in PEM encoding, the function parses permissively:
346 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100347 * returns the number of certificates for which parsing failed
348 * (hence \c 0 if all certificates were parsed successfully).
349 * If no certificate could be parsed, the function returns
350 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100352 * PEM encoded certificates may be interleaved by other data
353 * such as human readable descriptions of their content, as
354 * long as the certificates are enclosed in the PEM specific
355 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
356 *
357 * \param chain The chain to which to add the parsed certificates.
358 * \param buf The buffer holding the certificate data in PEM or DER format.
359 * For certificates in PEM encoding, this may be a concatenation
360 * of multiple certificates; for DER encoding, the buffer must
361 * comprise exactly one certificate.
362 * \param buflen The size of \p buf, including the terminating \c NULL byte
363 * in case of PEM encoded data.
364 *
365 * \return \c 0 if all certificates were parsed successfully.
366 * \return The (positive) number of certificates that couldn't
367 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100368 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100369 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374/**
375 * \brief Load one or more certificates and add them
376 * to the chained list. Parses permissively. If some
377 * certificates can be parsed, the result is the number
378 * of failed certificates it encountered. If none complete
379 * correctly, the first error is returned.
380 *
381 * \param chain points to the start of the chain
382 * \param path filename to read the certificates from
383 *
384 * \return 0 if all certificates parsed successfully, a positive number
385 * if partly successful or a specific X509 or PEM error code
386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388
389/**
390 * \brief Load one or more certificate files from a path and add them
391 * to the chained list. Parses permissively. If some
392 * certificates can be parsed, the result is the number
393 * of failed certificates it encountered. If none complete
394 * correctly, the first error is returned.
395 *
396 * \param chain points to the start of the chain
397 * \param path directory / folder to read the certificate files from
398 *
399 * \return 0 if all certificates parsed successfully, a positive number
400 * if partly successful or a specific X509 or PEM error code
401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
403#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404
Hanno Becker02a21932019-06-10 15:08:43 +0100405#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406/**
407 * \brief Returns an informational string about the
408 * certificate.
409 *
410 * \param buf Buffer to write to
411 * \param size Maximum size of buffer
412 * \param prefix A line prefix
413 * \param crt The X509 certificate to represent
414 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200415 * \return The length of the string written (not including the
416 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
419 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420
421/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100422 * \brief Returns an informational string about the
423 * verification status of a certificate.
424 *
425 * \param buf Buffer to write to
426 * \param size Maximum size of buffer
427 * \param prefix A line prefix
428 * \param flags Verification flags created by mbedtls_x509_crt_verify()
429 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200430 * \return The length of the string written (not including the
431 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100432 */
433int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200434 uint32_t flags );
Hanno Beckerc6043f22019-06-14 17:21:24 +0100435#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200436
437/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438 * \brief Verify the certificate signature
439 *
440 * The verify callback is a user-supplied callback that
441 * can clear / modify / add flags for a certificate. If set,
442 * the verification callback is called for each
443 * certificate in the chain (from the trust-ca down to the
444 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446 * int *flags). With the flags representing current flags for
447 * that specific certificate and the certificate depth from
448 * the bottom (Peer cert depth = 0).
449 *
450 * All flags left after returning from the callback
451 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200452 * return 0 for anything (including invalid certificates)
453 * other than fatal error, as a non-zero return code
454 * immediately aborts the verification process. For fatal
455 * errors, a specific error code should be used (different
456 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
457 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
458 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100460 * \note In case verification failed, the results can be displayed
461 * using \c mbedtls_x509_crt_verify_info()
462 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200463 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
464 * default security profile.
465 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100466 * \note It is your responsibility to provide up-to-date CRLs for
467 * all trusted CAs. If no CRL is provided for the CA that was
468 * used to sign the certificate, CRL verification is skipped
469 * silently, that is *without* setting any flag.
470 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200471 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200472 * (1) those of trusted root CAs, so that certificates
473 * chaining up to those CAs will be trusted, and (2)
474 * self-signed end-entity certificates to be trusted (for
475 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200476 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200477 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100478 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200479 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100480 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481 * \param cn expected Common Name (can be set to
482 * NULL if the CN must not be verified)
483 * \param flags result of the verification
484 * \param f_vrfy verification function
485 * \param p_vrfy verification parameter
486 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200487 * \return 0 (and flags set to 0) if the chain was verified and valid,
488 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
489 * but found to be invalid, in which case *flags will have one
490 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
491 * flags set, or another error (and flags set to 0xffffffff)
492 * in case of a fatal error encountered during the
493 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
496 mbedtls_x509_crt *trust_ca,
497 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200498 const char *cn, uint32_t *flags,
499 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200500 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200502/**
503 * \brief Verify the certificate signature according to profile
504 *
505 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
506 * security profile.
507 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200508 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200509 * for ECDSA) apply to all certificates: trusted root,
510 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200511 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100512 * \param crt a certificate (chain) to be verified
513 * \param trust_ca the list of trusted CAs
514 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200515 * \param profile security profile for verification
516 * \param cn expected Common Name (can be set to
517 * NULL if the CN must not be verified)
518 * \param flags result of the verification
519 * \param f_vrfy verification function
520 * \param p_vrfy verification parameter
521 *
522 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
523 * in which case *flags will have one or more
524 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
525 * set,
526 * or another error in case of a fatal error encountered
527 * during the verification process.
528 */
529int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
530 mbedtls_x509_crt *trust_ca,
531 mbedtls_x509_crl *ca_crl,
532 const mbedtls_x509_crt_profile *profile,
533 const char *cn, uint32_t *flags,
534 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
535 void *p_vrfy );
536
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200537/**
538 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
539 *
540 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
541 * but can return early and restart according to the limit
542 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
543 *
544 * \param crt a certificate (chain) to be verified
545 * \param trust_ca the list of trusted CAs
546 * \param ca_crl the list of CRLs for trusted CAs
547 * \param profile security profile for verification
548 * \param cn expected Common Name (can be set to
549 * NULL if the CN must not be verified)
550 * \param flags result of the verification
551 * \param f_vrfy verification function
552 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200553 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200554 *
555 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200556 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200557 * operations was reached: see \c mbedtls_ecp_set_max_ops().
558 */
559int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
560 mbedtls_x509_crt *trust_ca,
561 mbedtls_x509_crl *ca_crl,
562 const mbedtls_x509_crt_profile *profile,
563 const char *cn, uint32_t *flags,
564 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
565 void *p_vrfy,
566 mbedtls_x509_crt_restart_ctx *rs_ctx );
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200569/**
570 * \brief Check usage of certificate against keyUsage extension.
571 *
572 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200573 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
574 * before using the certificate to perform an RSA key
575 * exchange).
576 *
577 * \note Except for decipherOnly and encipherOnly, a bit set in the
578 * usage argument means this bit MUST be set in the
579 * certificate. For decipherOnly and encipherOnly, it means
580 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200581 *
582 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200584 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200585 *
586 * \note You should only call this function on leaf certificates, on
587 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200589 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200590int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
591 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200595/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000596 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200597 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000598 * \param crt Leaf certificate used.
599 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
600 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200602 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000603 * \return 0 if this use of the certificate is allowed,
604 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200605 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000606 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000609 const char *usage_oid,
610 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000611#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200615 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200616 *
617 * \param crt a certificate to be verified
618 * \param crl the CRL to verify against
619 *
620 * \return 1 if the certificate is revoked, 0 otherwise
621 *
622 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100623int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625
626/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200627 * \brief Initialize a certificate (chain)
628 *
629 * \param crt Certificate chain to initialize
630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200632
633/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634 * \brief Unallocate all certificate data
635 *
636 * \param crt Certificate chain to free
637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200639
640#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
641/**
642 * \brief Initialize a restart context
643 */
644void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
645
646/**
647 * \brief Free the components of a restart context
648 */
649void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
650#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000651
Hanno Becker823efad2019-02-28 13:23:58 +0000652/**
653 * \brief Request CRT frame giving access to basic CRT fields
654 * and raw ASN.1 data of complex fields.
655 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100656 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker823efad2019-02-28 13:23:58 +0000657 * \param dst The address of the destination frame structure.
658 * This need not be initialized.
659 *
660 * \note ::mbedtls_x509_crt_frame does not contain pointers to
661 * dynamically allocated memory, and hence need not be freed.
662 * Users may e.g. allocate an instance of
663 * ::mbedtls_x509_crt_frame on the stack and call this function
664 * on it, in which case no allocation/freeing has to be done.
665 *
Hanno Becker608de6a2019-06-28 10:48:01 +0100666 * \note You may also use mbedtls_x509_crt_frame_acquire() and
667 * mbedtls_x509_crt_frame_release() for copy-less variants
668 * of this function.
669 *
Hanno Becker823efad2019-02-28 13:23:58 +0000670 * \return \c 0 on success. In this case, \p dst is updated
671 * to hold the frame for the given CRT.
672 * \return A negative error code on failure.
673 */
674int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
675 mbedtls_x509_crt_frame *dst );
676
677/**
Hanno Becker9219f9e2019-05-14 12:40:58 +0100678 * \brief Set up a PK context with the public key in a certificate.
Hanno Becker823efad2019-02-28 13:23:58 +0000679 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100680 * \param crt The certificate to use. This must be initialized and set up.
Hanno Becker823efad2019-02-28 13:23:58 +0000681 * \param pk The address of the destination PK context to fill.
682 * This must be initialized via mbedtls_pk_init().
683 *
Hanno Becker608de6a2019-06-28 10:48:01 +0100684 * \note You may also use mbedtls_x509_crt_pk_acquire() and
685 * mbedtls_x509_crt_pk_release() for copy-less variants
686 * of this function.
687 *
Hanno Becker823efad2019-02-28 13:23:58 +0000688 * \return \c 0 on success. In this case, the user takes ownership
689 * of the destination PK context, and is responsible for
690 * calling mbedtls_pk_free() on it once it's no longer needed.
691 * \return A negative error code on failure.
692 */
693int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
694 mbedtls_pk_context *pk );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000695
696/**
Hanno Becker63e69982019-02-26 18:50:49 +0000697 * \brief Request the subject name of a CRT, presented
698 * as a dynamically allocated linked list.
699 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100700 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker63e69982019-02-26 18:50:49 +0000701 * \param subject The address at which to store the address of the
702 * first entry of the generated linked list holding
703 * the subject name.
704 *
Hanno Beckerd92078f2019-06-28 10:48:57 +0100705 * \note Depending on your use case, consider using the raw ASN.1
Hanno Becker63e69982019-02-26 18:50:49 +0000706 * describing the subject name instead of the heap-allocated
707 * linked list generated by this call. The pointers to the
708 * raw ASN.1 data are part of the CRT frame that can be queried
709 * via mbedtls_x509_crt_get_frame(), and they can be traversed
710 * via mbedtls_asn1_traverse_sequence_of().
711 *
712 * \return \c 0 on success. In this case, the user takes ownership
713 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000714 * through a call to mbedtls_x509_name_free() once it's no
715 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000716 * \return A negative error code on failure.
717 */
718int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
719 mbedtls_x509_name **subject );
720
721/**
722 * \brief Request the subject name of a CRT, presented
723 * as a dynamically allocated linked list.
724 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100725 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker63e69982019-02-26 18:50:49 +0000726 * \param issuer The address at which to store the address of the
727 * first entry of the generated linked list holding
728 * the subject name.
729 *
Hanno Beckerd92078f2019-06-28 10:48:57 +0100730 * \note Depending on your use case, consider using the raw ASN.1
731 * describing the issuer name instead of the heap-allocated
Hanno Becker63e69982019-02-26 18:50:49 +0000732 * linked list generated by this call. The pointers to the
733 * raw ASN.1 data are part of the CRT frame that can be queried
734 * via mbedtls_x509_crt_get_frame(), and they can be traversed
735 * via mbedtls_asn1_traverse_sequence_of().
736 *
737 * \return \c 0 on success. In this case, the user takes ownership
738 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000739 * through a call to mbedtls_x509_name_free() once it's no
740 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000741 * \return A negative error code on failure.
742 */
743int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
744 mbedtls_x509_name **issuer );
745
746/**
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000747 * \brief Request the subject alternative name of a CRT, presented
748 * as a dynamically allocated linked list.
749 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100750 * \param crt The CRT to use. This must be initialized and set up.
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000751 * \param subj_alt The address at which to store the address of the
752 * first component of the subject alternative names list.
753 *
754 * \note Depending in your use case, consider using the raw ASN.1
755 * describing the subject alternative names extension
756 * instead of the heap-allocated linked list generated by this
757 * call. The pointers to the raw ASN.1 data are part of the CRT
758 * frame that can be queried via mbedtls_x509_crt_get_frame(),
759 * and mbedtls_asn1_traverse_sequence_of() can be used to
760 * traverse the list of subject alternative names.
761 *
762 * \return \c 0 on success. In this case, the user takes ownership
763 * of the name context, and is responsible for freeing it
764 * through a call to mbedtls_x509_sequence_free() once it's
765 * no longer needed.
766 * \return A negative error code on failure.
767 */
768int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
769 mbedtls_x509_sequence **subj_alt );
770
771/**
772 * \brief Request the ExtendedKeyUsage extension of a CRT,
773 * presented as a dynamically allocated linked list.
774 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100775 * \param crt The CRT to use. This must be initialized and set up.
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000776 * \param ext_key_usage The address at which to store the address of the
777 * first entry of the ExtendedKeyUsage extension.
778 *
779 * \note Depending in your use case, consider using the raw ASN.1
780 * describing the extended key usage extension instead of
781 * the heap-allocated linked list generated by this call.
782 * The pointers to the raw ASN.1 data are part of the CRT
783 * frame that can be queried via mbedtls_x509_crt_get_frame(),
784 * and mbedtls_asn1_traverse_sequence_of() can be used to
785 * traverse the entries in the extended key usage extension.
786 *
787 * \return \c 0 on success. In this case, the user takes ownership
788 * of the name context, and is responsible for freeing it
789 * through a call to mbedtls_x509_sequence_free() once it's
790 * no longer needed.
791 * \return A negative error code on failure.
792 */
793int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
794 mbedtls_x509_sequence **ext_key_usage );
795
796/**
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000797 * \brief Flush internal X.509 CRT parsing cache, if present.
798 *
799 * \param crt The CRT structure whose cache to flush.
800 *
801 * \note Calling this function frequently reduces RAM usage
802 * at the cost of performance.
803 *
804 * \return \c 0 on success.
805 * \return A negative error code on failure.
806 */
807int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt );
808
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100809/**
810 * \brief Request temporary read-access to a certificate frame
811 * for a given certificate.
812 *
813 * Once no longer needed, the frame must be released
814 * through a call to mbedtls_x509_crt_frame_release().
815 *
816 * This is a copy-less version of mbedtls_x509_crt_get_frame().
817 * See there for more information.
818 *
819 * \param crt The certificate to use. This must be initialized and set up.
820 * \param dst The address at which to store the address of a readable
821 * certificate frame for the input certificate \p crt which the
822 * caller can use until calling mbedtls_x509_crt_frame_release().
823 *
824 * \note The certificate frame `**frame_ptr` returned by this function
825 * is owned by the X.509 module and must not be freed or modified
826 * by the caller. The X.509 module guarantees its validity as long
827 * as \p crt is valid and mbedtls_x509_crt_frame_release() hasn't
828 * been issued.
829 *
Hanno Beckerfc99a092019-06-28 14:45:26 +0100830 * \note In a single-threaded application using
831 * MBEDTLS_X509_ALWAYS_FLUSH, nested calls to this function
832 * are not allowed and will fail gracefully with
833 * MBEDTLS_ERR_X509_FATAL_ERROR.
834 *
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100835 * \return \c 0 on success. In this case, `*frame_ptr` is updated
836 * to hold the address of a frame for the given CRT.
837 * \return A negative error code on failure.
838 */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000839static inline int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100840 mbedtls_x509_crt_frame const **dst )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000841{
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100842 int ret = 0;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000843#if defined(MBEDTLS_THREADING_C)
844 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
845 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100846#endif /* MBEDTLS_THREADING_C */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000847
Hanno Beckerfc99a092019-06-28 14:45:26 +0100848#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
849 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100850 if( crt->cache->frame_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100851#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100852 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000853
Hanno Beckerfc99a092019-06-28 14:45:26 +0100854#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
855 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100856 if( crt->cache->frame_readers == MBEDTLS_X509_CACHE_FRAME_READERS_MAX )
857 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
858
859 crt->cache->frame_readers++;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100860#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100861
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100862#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100863 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
864 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
865#endif /* MBEDTLS_THREADING_C */
866
867 *dst = crt->cache->frame;
868 return( ret );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000869}
870
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100871/**
872 * \brief Release access to a certificate frame acquired
873 * through a prior call to mbedtls_x509_crt_frame_acquire().
874 *
875 * \param crt The certificate for which a certificate frame has
876 * previously been acquired.
877 */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100878static inline int mbedtls_x509_crt_frame_release( mbedtls_x509_crt const *crt )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000879{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000880#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100881 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
882 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100883#endif /* MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100884
Hanno Beckerfc99a092019-06-28 14:45:26 +0100885#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
886 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100887 if( crt->cache->frame_readers == 0 )
Hanno Becker69c30332019-06-28 15:47:53 +0100888 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100889
890 crt->cache->frame_readers--;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100891#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100892
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100893#if defined(MBEDTLS_THREADING_C)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000894 mbedtls_mutex_unlock( &crt->cache->frame_mutex );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100895#endif /* MBEDTLS_THREADING_C */
Hanno Beckerbc685192019-03-05 15:35:31 +0000896
897#if defined(MBEDTLS_X509_ALWAYS_FLUSH)
Hanno Becker60785d12019-06-07 17:49:11 +0100898 (void) mbedtls_x509_crt_flush_cache_frame( crt );
Hanno Beckerbc685192019-03-05 15:35:31 +0000899#endif /* MBEDTLS_X509_ALWAYS_FLUSH */
Hanno Becker60785d12019-06-07 17:49:11 +0100900
901#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
902 !defined(MBEDTLS_THREADING_C)
903 ((void) crt);
904#endif
905
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100906 return( 0 );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000907}
908
Hanno Becker4b70e122019-05-20 16:51:01 +0100909/**
910 * \brief Request temporary access to a public key context
911 * for a given certificate.
912 *
913 * Once no longer needed, the frame must be released
914 * through a call to mbedtls_x509_crt_pk_release().
915 *
916 * This is a copy-less version of mbedtls_x509_crt_get_pk().
917 * See there for more information.
918 *
919 * \param crt The certificate to use. This must be initialized and set up.
920 * \param dst The address at which to store the address of a public key
921 * context for the public key in the input certificate \p crt.
922 *
923 * \warning The public key context `**pk_ptr` returned by this function
924 * is owned by the X.509 module and must be used by the caller
925 * in a thread-safe way. In particular, the caller must only
926 * use the context with functions which are `const` on the input
927 * context, or those which are known to be thread-safe. The latter
Hanno Beckerb653aa32019-06-28 10:53:55 +0100928 * for example includes mbedtls_pk_verify() for ECC or RSA public
929 * key contexts.
Hanno Becker4b70e122019-05-20 16:51:01 +0100930 *
Hanno Beckerfc99a092019-06-28 14:45:26 +0100931 * \note In a single-threaded application using
932 * MBEDTLS_X509_ALWAYS_FLUSH, nested calls to this function
933 * are not allowed and will fail gracefully with
934 * MBEDTLS_ERR_X509_FATAL_ERROR.
935 *
Hanno Becker4b70e122019-05-20 16:51:01 +0100936 * \return \c 0 on success. In this case, `*pk_ptr` is updated
937 * to hold the address of a public key context for the given
938 * certificate.
939 * \return A negative error code on failure.
940 */
Hanno Becker180f7bf2019-02-28 13:23:38 +0000941static inline int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt const *crt,
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100942 mbedtls_pk_context **dst )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000943{
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100944 int ret = 0;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000945#if defined(MBEDTLS_THREADING_C)
946 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
947 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100948#endif /* MBEDTLS_THREADING_C */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000949
Hanno Beckerfc99a092019-06-28 14:45:26 +0100950#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
951 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100952 if( crt->cache->pk_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100953#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100954 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000955
Hanno Beckerfc99a092019-06-28 14:45:26 +0100956#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
957 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100958 if( crt->cache->pk_readers == MBEDTLS_X509_CACHE_PK_READERS_MAX )
959 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
960
961 crt->cache->pk_readers++;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100962#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100963
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100964#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100965 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
966 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
967#endif /* MBEDTLS_THREADING_C */
968
969 *dst = crt->cache->pk;
970 return( ret );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000971}
972
Hanno Becker4b70e122019-05-20 16:51:01 +0100973/**
974 * \brief Release access to a public key context acquired
975 * through a prior call to mbedtls_x509_crt_frame_acquire().
976 *
977 * \param crt The certificate for which a certificate frame has
978 * previously been acquired.
979 */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100980static inline int mbedtls_x509_crt_pk_release( mbedtls_x509_crt const *crt )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000981{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000982#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100983 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
984 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100985#endif /* MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100986
Hanno Beckerfc99a092019-06-28 14:45:26 +0100987#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
988 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100989 if( crt->cache->pk_readers == 0 )
Hanno Becker69c30332019-06-28 15:47:53 +0100990 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100991
992 crt->cache->pk_readers--;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100993#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100994
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100995#if defined(MBEDTLS_THREADING_C)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000996 mbedtls_mutex_unlock( &crt->cache->pk_mutex );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100997#endif /* MBEDTLS_THREADING_C */
Hanno Beckerbc685192019-03-05 15:35:31 +0000998
999#if defined(MBEDTLS_X509_ALWAYS_FLUSH)
Hanno Becker60785d12019-06-07 17:49:11 +01001000 (void) mbedtls_x509_crt_flush_cache_pk( crt );
Hanno Beckerbc685192019-03-05 15:35:31 +00001001#endif /* MBEDTLS_X509_ALWAYS_FLUSH */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001002
Hanno Becker60785d12019-06-07 17:49:11 +01001003#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
1004 !defined(MBEDTLS_THREADING_C)
1005 ((void) crt);
1006#endif
1007
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001008 return( 0 );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001009}
1010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012
1013/* \} name */
1014/* \} addtogroup x509_module */
1015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017/**
1018 * \brief Initialize a CRT writing context
1019 *
1020 * \param ctx CRT context to initialize
1021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023
1024/**
1025 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 *
1028 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
1030 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033
1034/**
1035 * \brief Set the serial number for a Certificate.
1036 *
1037 * \param ctx CRT context to use
1038 * \param serial serial number to set
1039 *
1040 * \return 0 if successful
1041 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043
1044/**
1045 * \brief Set the validity period for a Certificate
1046 * Timestamps should be in string format for UTC timezone
1047 * i.e. "YYYYMMDDhhmmss"
1048 * e.g. "20131231235959" for December 31st 2013
1049 * at 23:59:59
1050 *
1051 * \param ctx CRT context to use
1052 * \param not_before not_before timestamp
1053 * \param not_after not_after timestamp
1054 *
1055 * \return 0 if timestamp was parsed successfully, or
1056 * a specific error code
1057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +01001059 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060
1061/**
1062 * \brief Set the issuer name for a Certificate
1063 * Issuer names should contain a comma-separated list
1064 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001065 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001066 *
1067 * \param ctx CRT context to use
1068 * \param issuer_name issuer name to set
1069 *
1070 * \return 0 if issuer name was parsed successfully, or
1071 * a specific error code
1072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001074 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075
1076/**
1077 * \brief Set the subject name for a Certificate
1078 * Subject names should contain a comma-separated list
1079 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001080 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081 *
1082 * \param ctx CRT context to use
1083 * \param subject_name subject name to set
1084 *
1085 * \return 0 if subject name was parsed successfully, or
1086 * a specific error code
1087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001089 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090
1091/**
1092 * \brief Set the subject public key for the certificate
1093 *
1094 * \param ctx CRT context to use
1095 * \param key public key to include
1096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098
1099/**
1100 * \brief Set the issuer key used for signing the certificate
1101 *
1102 * \param ctx CRT context to use
1103 * \param key private key to sign with
1104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106
1107/**
1108 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110 *
1111 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001112 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001115
1116/**
1117 * \brief Generic function to add to or replace an extension in the
1118 * CRT
1119 *
1120 * \param ctx CRT context to use
1121 * \param oid OID of the extension
1122 * \param oid_len length of the OID
1123 * \param critical if the extension is critical (per the RFC's definition)
1124 * \param val value of the extension OCTET STRING
1125 * \param val_len length of the value data
1126 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001127 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001130 const char *oid, size_t oid_len,
1131 int critical,
1132 const unsigned char *val, size_t val_len );
1133
1134/**
1135 * \brief Set the basicConstraints extension for a CRT
1136 *
1137 * \param ctx CRT context to use
1138 * \param is_ca is this a CA certificate
1139 * \param max_pathlen maximum length of certificate chains below this
1140 * certificate (only for CA certificates, -1 is
1141 * inlimited)
1142 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001143 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001146 int is_ca, int max_pathlen );
1147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149/**
1150 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152 * called before
1153 *
1154 * \param ctx CRT context to use
1155 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001156 * \return 0 if successful, or a 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_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001159
1160/**
1161 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001163 * called before
1164 *
1165 * \param ctx CRT context to use
1166 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001167 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
1170#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171
1172/**
1173 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001175 *
1176 * \param ctx CRT context to use
1177 * \param key_usage key usage flags to set
1178 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001179 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +02001181int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
1182 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001183
1184/**
1185 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001187 *
1188 * \param ctx CRT context to use
1189 * \param ns_cert_type Netscape Cert Type flags to set
1190 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001191 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 unsigned char ns_cert_type );
1195
1196/**
1197 * \brief Free the contents of a CRT write context
1198 *
1199 * \param ctx CRT context to free
1200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001202
1203/**
1204 * \brief Write a built up certificate to a X509 DER structure
1205 * Note: data is written at the end of the buffer! Use the
1206 * return value to determine where you should start
1207 * using the buffer
1208 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001209 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001210 * \param buf buffer to write to
1211 * \param size size of the buffer
1212 * \param f_rng RNG function (for signature, see note)
1213 * \param p_rng RNG parameter
1214 *
1215 * \return length of data written if successful, or a specific
1216 * error code
1217 *
1218 * \note f_rng may be NULL if RSA is used for signature and the
1219 * signature is made offline (otherwise f_rng is desirable
1220 * for countermeasures against timing attacks).
1221 * ECDSA signatures always require a non-NULL f_rng.
1222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001224 int (*f_rng)(void *, unsigned char *, size_t),
1225 void *p_rng );
1226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001228/**
1229 * \brief Write a built up certificate to a X509 PEM string
1230 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001231 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001232 * \param buf buffer to write to
1233 * \param size size of the buffer
1234 * \param f_rng RNG function (for signature, see note)
1235 * \param p_rng RNG parameter
1236 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001237 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001238 *
1239 * \note f_rng may be NULL if RSA is used for signature and the
1240 * signature is made offline (otherwise f_rng is desirable
1241 * for countermeasures against timing attacks).
1242 * ECDSA signatures always require a non-NULL f_rng.
1243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245 int (*f_rng)(void *, unsigned char *, size_t),
1246 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247#endif /* MBEDTLS_PEM_WRITE_C */
1248#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001249
1250#ifdef __cplusplus
1251}
1252#endif
1253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#endif /* mbedtls_x509_crt.h */