blob: c8f488c5ce980be6a60c1e0d7d37adcab7a79855 [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
Hanno Beckerd07614c2019-06-25 10:19:58 +010085#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Hanno Becker21f55672019-02-15 15:27:59 +000086 mbedtls_x509_buf_raw issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
Hanno Becker21f55672019-02-15 15:27:59 +000087 mbedtls_x509_buf_raw subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
Hanno Beckerd07614c2019-06-25 10:19:58 +010088#endif /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
89
90 mbedtls_x509_buf_raw issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
Hanno Becker21f55672019-02-15 15:27:59 +000091 mbedtls_x509_buf_raw subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
92
93 mbedtls_x509_buf_raw sig; /**< Signature: hash of the tbs part signed with the private key. */
94 mbedtls_x509_buf_raw sig_alg; /**< The signature algorithm used for \p sig. */
95
96 mbedtls_x509_buf_raw v3_ext; /**< The raw data for the extension list in the certificate.
97 * Might be useful for manual inspection of extensions that
98 * Mbed TLS doesn't yet support. */
99 mbedtls_x509_buf_raw subject_alt_raw; /**< The raw data for the SubjectAlternativeNames extension. */
100 mbedtls_x509_buf_raw ext_key_usage_raw; /**< The raw data for the ExtendedKeyUsage extension. */
101
Hanno Becker21f55672019-02-15 15:27:59 +0000102} mbedtls_x509_crt_frame;
103
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104/**
105 * Container for an X.509 certificate. The certificate may be chained.
106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200108{
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000109 int own_buffer; /**< Indicates if \c raw is owned
Hanno Becker180f7bf2019-02-28 13:23:38 +0000110 * by the structure or not. */
111 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
Hanno Becker00d39032019-05-13 12:39:44 +0100112 mbedtls_x509_crt_cache *cache; /**< Internal parsing cache. */
Hanno Becker180f7bf2019-02-28 13:23:38 +0000113
114 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
115
116 /* Legacy fields */
117#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +0200120 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
122 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
125 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
128 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200129
Hanno Becker843b71a2019-06-25 09:39:21 +0100130#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
132 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Hanno Becker843b71a2019-06-25 09:39:21 +0100133#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200134
Hanno Becker32c530e2019-02-06 16:13:41 +0000135 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200137
Hanno Beckerd07614c2019-06-25 10:19:58 +0100138#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
140 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
Hanno Beckerd07614c2019-06-25 10:19:58 +0100141#endif /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
143 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200144
145 int ext_types; /**< Bit string containing detected and parsed extensions */
146 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
147 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+ */
148
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100149 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200150
Hanno Becker7ec9c362019-02-21 14:24:05 +0000151 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200152
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +0200153 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
156 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
157 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
158 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 +0000159#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200160}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200162
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200163/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200164 * Build flag from an algorithm/curve identifier (pk, md, ecp)
165 * Since 0 is always XXX_NONE, ignore it.
166 */
Hanno Beckerd6028a12018-10-15 12:01:35 +0100167#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200168
169/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200170 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200171 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200172 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200173 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200174typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200175{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200176 uint32_t allowed_mds; /**< MDs for signatures */
177 uint32_t allowed_pks; /**< PK algs for signatures */
178 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
179 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200180}
181mbedtls_x509_crt_profile;
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#define MBEDTLS_X509_CRT_VERSION_1 0
184#define MBEDTLS_X509_CRT_VERSION_2 1
185#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
188#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200189
Andres AGf9113192016-09-02 14:06:04 +0100190#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
191#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
192#endif
193
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200194/**
195 * Container for writing a certificate (CRT)
196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200198{
199 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_mpi serial;
201 mbedtls_pk_context *subject_key;
202 mbedtls_pk_context *issuer_key;
203 mbedtls_asn1_named_data *subject;
204 mbedtls_asn1_named_data *issuer;
205 mbedtls_md_type_t md_alg;
206 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
207 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
208 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200211
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200212/**
213 * Item in a verification chain: cert and flags for it
214 */
215typedef struct {
216 mbedtls_x509_crt *crt;
217 uint32_t flags;
218} mbedtls_x509_crt_verify_chain_item;
219
220/**
221 * Max size of verification chain: end-entity + intermediates + trusted root
222 */
223#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
224
225/**
226 * Verification chain as built by \c mbedtls_crt_verify_chain()
227 */
228typedef struct
229{
230 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200231 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200232} mbedtls_x509_crt_verify_chain;
233
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200234#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
235
236/**
237 * \brief Context for resuming X.509 verify operations
238 */
239typedef struct
240{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200241 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200242 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200243
244 /* for find_parent_in() */
245 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
Hanno Becker6f61b7b2019-06-10 11:12:33 +0100246
247#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200248 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200249 int fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +0100250#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200251
252 /* for find_parent() */
253 int parent_is_trusted; /* -1 if find_parent is not in progress */
254
255 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200256 enum {
257 x509_crt_rs_none,
258 x509_crt_rs_find_parent,
259 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200260 int self_cnt;
261 mbedtls_x509_crt_verify_chain ver_chain;
262
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200263} mbedtls_x509_crt_restart_ctx;
264
265#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
266
267/* Now we can declare functions that take a pointer to that */
268typedef void mbedtls_x509_crt_restart_ctx;
269
270#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200273/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200274 * Default security profile. Should provide a good balance between security
275 * and compatibility with current deployments.
276 */
277extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
278
279/**
280 * Expected next default profile. Recommended for new deployments.
281 * Currently targets a 128-bit security level, except for RSA-2048.
282 */
283extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
284
285/**
286 * NSA Suite B profile.
287 */
288extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
289
290/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291 * \brief Parse a single DER formatted certificate and add it
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000292 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000294 * \param chain The pointer to the start of the CRT chain to attach to.
295 * When parsing the first CRT in a chain, this should point
296 * to an instance of ::mbedtls_x509_crt initialized through
297 * mbedtls_x509_crt_init().
298 * \param buf The buffer holding the DER encoded certificate.
299 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000301 * \note This function makes an internal copy of the CRT buffer
302 * \p buf. In particular, \p buf may be destroyed or reused
303 * after this call returns. To avoid duplicating the CRT
304 * buffer (at the cost of stricter lifetime constraints),
305 * use mbedtls_x509_crt_parse_der_nocopy() instead.
306 *
307 * \return \c 0 if successful.
308 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200309 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000310int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
311 const unsigned char *buf,
312 size_t buflen );
313
314/**
315 * \brief Parse a single DER formatted certificate and add it
316 * to the end of the provided chained list. This is a
317 * variant of mbedtls_x509_crt_parse_der() which takes
318 * temporary ownership of the CRT buffer until the CRT
319 * is destroyed.
320 *
321 * \param chain The pointer to the start of the CRT chain to attach to.
322 * When parsing the first CRT in a chain, this should point
323 * to an instance of ::mbedtls_x509_crt initialized through
324 * mbedtls_x509_crt_init().
325 * \param buf The address of the readable buffer holding the DER encoded
326 * certificate to use. On success, this buffer must be
327 * retained and not be changed for the liftetime of the
328 * CRT chain \p chain, that is, until \p chain is destroyed
329 * through a call to mbedtls_x509_crt_free().
330 * \param buflen The size in Bytes of \p buf.
331 *
332 * \note This call is functionally equivalent to
333 * mbedtls_x509_crt_parse_der(), but it avoids creating a
334 * copy of the input buffer at the cost of stronger lifetime
335 * constraints. This is useful in constrained environments
336 * where duplication of the CRT cannot be tolerated.
337 *
338 * \return \c 0 if successful.
339 * \return A negative error code on failure.
340 */
341int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
342 const unsigned char *buf,
343 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344
345/**
Hanno Becker89a91122018-08-23 16:14:00 +0100346 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100347 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100349 * For CRTs in PEM encoding, the function parses permissively:
350 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100351 * returns the number of certificates for which parsing failed
352 * (hence \c 0 if all certificates were parsed successfully).
353 * If no certificate could be parsed, the function returns
354 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100356 * PEM encoded certificates may be interleaved by other data
357 * such as human readable descriptions of their content, as
358 * long as the certificates are enclosed in the PEM specific
359 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
360 *
361 * \param chain The chain to which to add the parsed certificates.
362 * \param buf The buffer holding the certificate data in PEM or DER format.
363 * For certificates in PEM encoding, this may be a concatenation
364 * of multiple certificates; for DER encoding, the buffer must
365 * comprise exactly one certificate.
366 * \param buflen The size of \p buf, including the terminating \c NULL byte
367 * in case of PEM encoded data.
368 *
369 * \return \c 0 if all certificates were parsed successfully.
370 * \return The (positive) number of certificates that couldn't
371 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100372 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100373 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378/**
379 * \brief Load one or more certificates and add them
380 * to the chained list. Parses permissively. If some
381 * certificates can be parsed, the result is the number
382 * of failed certificates it encountered. If none complete
383 * correctly, the first error is returned.
384 *
385 * \param chain points to the start of the chain
386 * \param path filename to read the certificates from
387 *
388 * \return 0 if all certificates parsed successfully, a positive number
389 * if partly successful or a specific X509 or PEM error code
390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392
393/**
394 * \brief Load one or more certificate files from a path and add them
395 * to the chained list. Parses permissively. If some
396 * certificates can be parsed, the result is the number
397 * of failed certificates it encountered. If none complete
398 * correctly, the first error is returned.
399 *
400 * \param chain points to the start of the chain
401 * \param path directory / folder to read the certificate files from
402 *
403 * \return 0 if all certificates parsed successfully, a positive number
404 * if partly successful or a specific X509 or PEM error code
405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
407#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408
Hanno Becker02a21932019-06-10 15:08:43 +0100409#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410/**
411 * \brief Returns an informational string about the
412 * certificate.
413 *
414 * \param buf Buffer to write to
415 * \param size Maximum size of buffer
416 * \param prefix A line prefix
417 * \param crt The X509 certificate to represent
418 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200419 * \return The length of the string written (not including the
420 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
423 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424
425/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100426 * \brief Returns an informational string about the
427 * verification status of a certificate.
428 *
429 * \param buf Buffer to write to
430 * \param size Maximum size of buffer
431 * \param prefix A line prefix
432 * \param flags Verification flags created by mbedtls_x509_crt_verify()
433 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200434 * \return The length of the string written (not including the
435 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100436 */
437int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200438 uint32_t flags );
Hanno Beckerc6043f22019-06-14 17:21:24 +0100439#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200440
441/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442 * \brief Verify the certificate signature
443 *
444 * The verify callback is a user-supplied callback that
445 * can clear / modify / add flags for a certificate. If set,
446 * the verification callback is called for each
447 * certificate in the chain (from the trust-ca down to the
448 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450 * int *flags). With the flags representing current flags for
451 * that specific certificate and the certificate depth from
452 * the bottom (Peer cert depth = 0).
453 *
454 * All flags left after returning from the callback
455 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200456 * return 0 for anything (including invalid certificates)
457 * other than fatal error, as a non-zero return code
458 * immediately aborts the verification process. For fatal
459 * errors, a specific error code should be used (different
460 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
461 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
462 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100464 * \note In case verification failed, the results can be displayed
465 * using \c mbedtls_x509_crt_verify_info()
466 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200467 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
468 * default security profile.
469 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100470 * \note It is your responsibility to provide up-to-date CRLs for
471 * all trusted CAs. If no CRL is provided for the CA that was
472 * used to sign the certificate, CRL verification is skipped
473 * silently, that is *without* setting any flag.
474 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200475 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200476 * (1) those of trusted root CAs, so that certificates
477 * chaining up to those CAs will be trusted, and (2)
478 * self-signed end-entity certificates to be trusted (for
479 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200480 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200481 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100482 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200483 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100484 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 * \param cn expected Common Name (can be set to
486 * NULL if the CN must not be verified)
487 * \param flags result of the verification
488 * \param f_vrfy verification function
489 * \param p_vrfy verification parameter
490 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200491 * \return 0 (and flags set to 0) if the chain was verified and valid,
492 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
493 * but found to be invalid, in which case *flags will have one
494 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
495 * flags set, or another error (and flags set to 0xffffffff)
496 * in case of a fatal error encountered during the
497 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
500 mbedtls_x509_crt *trust_ca,
501 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200502 const char *cn, uint32_t *flags,
503 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200504 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200506/**
507 * \brief Verify the certificate signature according to profile
508 *
509 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
510 * security profile.
511 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200512 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200513 * for ECDSA) apply to all certificates: trusted root,
514 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200515 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100516 * \param crt a certificate (chain) to be verified
517 * \param trust_ca the list of trusted CAs
518 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200519 * \param profile security profile for verification
520 * \param cn expected Common Name (can be set to
521 * NULL if the CN must not be verified)
522 * \param flags result of the verification
523 * \param f_vrfy verification function
524 * \param p_vrfy verification parameter
525 *
526 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
527 * in which case *flags will have one or more
528 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
529 * set,
530 * or another error in case of a fatal error encountered
531 * during the verification process.
532 */
533int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
534 mbedtls_x509_crt *trust_ca,
535 mbedtls_x509_crl *ca_crl,
536 const mbedtls_x509_crt_profile *profile,
537 const char *cn, uint32_t *flags,
538 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
539 void *p_vrfy );
540
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200541/**
542 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
543 *
544 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
545 * but can return early and restart according to the limit
546 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
547 *
548 * \param crt a certificate (chain) to be verified
549 * \param trust_ca the list of trusted CAs
550 * \param ca_crl the list of CRLs for trusted CAs
551 * \param profile security profile for verification
552 * \param cn expected Common Name (can be set to
553 * NULL if the CN must not be verified)
554 * \param flags result of the verification
555 * \param f_vrfy verification function
556 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200557 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200558 *
559 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200560 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200561 * operations was reached: see \c mbedtls_ecp_set_max_ops().
562 */
563int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
564 mbedtls_x509_crt *trust_ca,
565 mbedtls_x509_crl *ca_crl,
566 const mbedtls_x509_crt_profile *profile,
567 const char *cn, uint32_t *flags,
568 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
569 void *p_vrfy,
570 mbedtls_x509_crt_restart_ctx *rs_ctx );
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200573/**
574 * \brief Check usage of certificate against keyUsage extension.
575 *
576 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200577 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
578 * before using the certificate to perform an RSA key
579 * exchange).
580 *
581 * \note Except for decipherOnly and encipherOnly, a bit set in the
582 * usage argument means this bit MUST be set in the
583 * certificate. For decipherOnly and encipherOnly, it means
584 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200585 *
586 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200588 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200589 *
590 * \note You should only call this function on leaf certificates, on
591 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200593 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200594int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
595 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200599/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000600 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200601 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000602 * \param crt Leaf certificate used.
603 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
604 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200606 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000607 * \return 0 if this use of the certificate is allowed,
608 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200609 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000610 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000613 const char *usage_oid,
614 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000615#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200619 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620 *
621 * \param crt a certificate to be verified
622 * \param crl the CRL to verify against
623 *
624 * \return 1 if the certificate is revoked, 0 otherwise
625 *
626 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100627int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629
630/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200631 * \brief Initialize a certificate (chain)
632 *
633 * \param crt Certificate chain to initialize
634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200636
637/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638 * \brief Unallocate all certificate data
639 *
640 * \param crt Certificate chain to free
641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200643
644#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
645/**
646 * \brief Initialize a restart context
647 */
648void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
649
650/**
651 * \brief Free the components of a restart context
652 */
653void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
654#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000655
Hanno Becker823efad2019-02-28 13:23:58 +0000656/**
657 * \brief Request CRT frame giving access to basic CRT fields
658 * and raw ASN.1 data of complex fields.
659 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100660 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker823efad2019-02-28 13:23:58 +0000661 * \param dst The address of the destination frame structure.
662 * This need not be initialized.
663 *
664 * \note ::mbedtls_x509_crt_frame does not contain pointers to
665 * dynamically allocated memory, and hence need not be freed.
666 * Users may e.g. allocate an instance of
667 * ::mbedtls_x509_crt_frame on the stack and call this function
668 * on it, in which case no allocation/freeing has to be done.
669 *
Hanno Becker608de6a2019-06-28 10:48:01 +0100670 * \note You may also use mbedtls_x509_crt_frame_acquire() and
671 * mbedtls_x509_crt_frame_release() for copy-less variants
672 * of this function.
673 *
Hanno Becker823efad2019-02-28 13:23:58 +0000674 * \return \c 0 on success. In this case, \p dst is updated
675 * to hold the frame for the given CRT.
676 * \return A negative error code on failure.
677 */
678int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
679 mbedtls_x509_crt_frame *dst );
680
681/**
Hanno Becker9219f9e2019-05-14 12:40:58 +0100682 * \brief Set up a PK context with the public key in a certificate.
Hanno Becker823efad2019-02-28 13:23:58 +0000683 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100684 * \param crt The certificate to use. This must be initialized and set up.
Hanno Becker823efad2019-02-28 13:23:58 +0000685 * \param pk The address of the destination PK context to fill.
686 * This must be initialized via mbedtls_pk_init().
687 *
Hanno Becker608de6a2019-06-28 10:48:01 +0100688 * \note You may also use mbedtls_x509_crt_pk_acquire() and
689 * mbedtls_x509_crt_pk_release() for copy-less variants
690 * of this function.
691 *
Hanno Becker823efad2019-02-28 13:23:58 +0000692 * \return \c 0 on success. In this case, the user takes ownership
693 * of the destination PK context, and is responsible for
694 * calling mbedtls_pk_free() on it once it's no longer needed.
695 * \return A negative error code on failure.
696 */
697int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
698 mbedtls_pk_context *pk );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000699
700/**
Hanno Becker63e69982019-02-26 18:50:49 +0000701 * \brief Request the subject name of a CRT, presented
702 * as a dynamically allocated linked list.
703 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100704 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker63e69982019-02-26 18:50:49 +0000705 * \param subject The address at which to store the address of the
706 * first entry of the generated linked list holding
707 * the subject name.
708 *
Hanno Beckerd92078f2019-06-28 10:48:57 +0100709 * \note Depending on your use case, consider using the raw ASN.1
Hanno Becker63e69982019-02-26 18:50:49 +0000710 * describing the subject name instead of the heap-allocated
711 * linked list generated by this call. The pointers to the
712 * raw ASN.1 data are part of the CRT frame that can be queried
713 * via mbedtls_x509_crt_get_frame(), and they can be traversed
714 * via mbedtls_asn1_traverse_sequence_of().
715 *
716 * \return \c 0 on success. In this case, the user takes ownership
717 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000718 * through a call to mbedtls_x509_name_free() once it's no
719 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000720 * \return A negative error code on failure.
721 */
722int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
723 mbedtls_x509_name **subject );
724
725/**
726 * \brief Request the subject name of a CRT, presented
727 * as a dynamically allocated linked list.
728 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100729 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker63e69982019-02-26 18:50:49 +0000730 * \param issuer The address at which to store the address of the
731 * first entry of the generated linked list holding
732 * the subject name.
733 *
Hanno Beckerd92078f2019-06-28 10:48:57 +0100734 * \note Depending on your use case, consider using the raw ASN.1
735 * describing the issuer name instead of the heap-allocated
Hanno Becker63e69982019-02-26 18:50:49 +0000736 * linked list generated by this call. The pointers to the
737 * raw ASN.1 data are part of the CRT frame that can be queried
738 * via mbedtls_x509_crt_get_frame(), and they can be traversed
739 * via mbedtls_asn1_traverse_sequence_of().
740 *
741 * \return \c 0 on success. In this case, the user takes ownership
742 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000743 * through a call to mbedtls_x509_name_free() once it's no
744 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000745 * \return A negative error code on failure.
746 */
747int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
748 mbedtls_x509_name **issuer );
749
750/**
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000751 * \brief Request the subject alternative name of a CRT, presented
752 * as a dynamically allocated linked list.
753 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100754 * \param crt The CRT to use. This must be initialized and set up.
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000755 * \param subj_alt The address at which to store the address of the
756 * first component of the subject alternative names list.
757 *
758 * \note Depending in your use case, consider using the raw ASN.1
759 * describing the subject alternative names extension
760 * instead of the heap-allocated linked list generated by this
761 * call. The pointers to the raw ASN.1 data are part of the CRT
762 * frame that can be queried via mbedtls_x509_crt_get_frame(),
763 * and mbedtls_asn1_traverse_sequence_of() can be used to
764 * traverse the list of subject alternative names.
765 *
766 * \return \c 0 on success. In this case, the user takes ownership
767 * of the name context, and is responsible for freeing it
768 * through a call to mbedtls_x509_sequence_free() once it's
769 * no longer needed.
770 * \return A negative error code on failure.
771 */
772int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
773 mbedtls_x509_sequence **subj_alt );
774
775/**
776 * \brief Request the ExtendedKeyUsage extension of a CRT,
777 * presented as a dynamically allocated linked list.
778 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100779 * \param crt The CRT to use. This must be initialized and set up.
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000780 * \param ext_key_usage The address at which to store the address of the
781 * first entry of the ExtendedKeyUsage extension.
782 *
783 * \note Depending in your use case, consider using the raw ASN.1
784 * describing the extended key usage extension instead of
785 * the heap-allocated linked list generated by this call.
786 * The pointers to the raw ASN.1 data are part of the CRT
787 * frame that can be queried via mbedtls_x509_crt_get_frame(),
788 * and mbedtls_asn1_traverse_sequence_of() can be used to
789 * traverse the entries in the extended key usage extension.
790 *
791 * \return \c 0 on success. In this case, the user takes ownership
792 * of the name context, and is responsible for freeing it
793 * through a call to mbedtls_x509_sequence_free() once it's
794 * no longer needed.
795 * \return A negative error code on failure.
796 */
797int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
798 mbedtls_x509_sequence **ext_key_usage );
799
800/**
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000801 * \brief Flush internal X.509 CRT parsing cache, if present.
802 *
803 * \param crt The CRT structure whose cache to flush.
804 *
805 * \note Calling this function frequently reduces RAM usage
806 * at the cost of performance.
807 *
808 * \return \c 0 on success.
809 * \return A negative error code on failure.
810 */
811int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt );
812
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100813/**
814 * \brief Request temporary read-access to a certificate frame
815 * for a given certificate.
816 *
817 * Once no longer needed, the frame must be released
818 * through a call to mbedtls_x509_crt_frame_release().
819 *
820 * This is a copy-less version of mbedtls_x509_crt_get_frame().
821 * See there for more information.
822 *
823 * \param crt The certificate to use. This must be initialized and set up.
824 * \param dst The address at which to store the address of a readable
825 * certificate frame for the input certificate \p crt which the
826 * caller can use until calling mbedtls_x509_crt_frame_release().
827 *
828 * \note The certificate frame `**frame_ptr` returned by this function
829 * is owned by the X.509 module and must not be freed or modified
830 * by the caller. The X.509 module guarantees its validity as long
831 * as \p crt is valid and mbedtls_x509_crt_frame_release() hasn't
832 * been issued.
833 *
Hanno Beckerfc99a092019-06-28 14:45:26 +0100834 * \note In a single-threaded application using
835 * MBEDTLS_X509_ALWAYS_FLUSH, nested calls to this function
836 * are not allowed and will fail gracefully with
837 * MBEDTLS_ERR_X509_FATAL_ERROR.
838 *
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100839 * \return \c 0 on success. In this case, `*frame_ptr` is updated
840 * to hold the address of a frame for the given CRT.
841 * \return A negative error code on failure.
842 */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000843static inline int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100844 mbedtls_x509_crt_frame const **dst )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000845{
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100846 int ret = 0;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000847#if defined(MBEDTLS_THREADING_C)
848 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
849 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100850#endif /* MBEDTLS_THREADING_C */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000851
Hanno Beckerfc99a092019-06-28 14:45:26 +0100852#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
853 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100854 if( crt->cache->frame_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100855#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100856 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000857
Hanno Beckerfc99a092019-06-28 14:45:26 +0100858#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
859 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100860 if( crt->cache->frame_readers == MBEDTLS_X509_CACHE_FRAME_READERS_MAX )
861 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
862
863 crt->cache->frame_readers++;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100864#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100865
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100866#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100867 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
868 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
869#endif /* MBEDTLS_THREADING_C */
870
871 *dst = crt->cache->frame;
872 return( ret );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000873}
874
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100875/**
876 * \brief Release access to a certificate frame acquired
877 * through a prior call to mbedtls_x509_crt_frame_acquire().
878 *
879 * \param crt The certificate for which a certificate frame has
880 * previously been acquired.
881 */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100882static inline int mbedtls_x509_crt_frame_release( mbedtls_x509_crt const *crt )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000883{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000884#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100885 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
886 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100887#endif /* MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100888
Hanno Beckerfc99a092019-06-28 14:45:26 +0100889#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
890 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100891 if( crt->cache->frame_readers == 0 )
Hanno Becker69c30332019-06-28 15:47:53 +0100892 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100893
894 crt->cache->frame_readers--;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100895#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100896
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100897#if defined(MBEDTLS_THREADING_C)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000898 mbedtls_mutex_unlock( &crt->cache->frame_mutex );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100899#endif /* MBEDTLS_THREADING_C */
Hanno Beckerbc685192019-03-05 15:35:31 +0000900
901#if defined(MBEDTLS_X509_ALWAYS_FLUSH)
Hanno Becker60785d12019-06-07 17:49:11 +0100902 (void) mbedtls_x509_crt_flush_cache_frame( crt );
Hanno Beckerbc685192019-03-05 15:35:31 +0000903#endif /* MBEDTLS_X509_ALWAYS_FLUSH */
Hanno Becker60785d12019-06-07 17:49:11 +0100904
905#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
906 !defined(MBEDTLS_THREADING_C)
907 ((void) crt);
908#endif
909
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100910 return( 0 );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000911}
912
Hanno Becker4b70e122019-05-20 16:51:01 +0100913/**
914 * \brief Request temporary access to a public key context
915 * for a given certificate.
916 *
917 * Once no longer needed, the frame must be released
918 * through a call to mbedtls_x509_crt_pk_release().
919 *
920 * This is a copy-less version of mbedtls_x509_crt_get_pk().
921 * See there for more information.
922 *
923 * \param crt The certificate to use. This must be initialized and set up.
924 * \param dst The address at which to store the address of a public key
925 * context for the public key in the input certificate \p crt.
926 *
927 * \warning The public key context `**pk_ptr` returned by this function
928 * is owned by the X.509 module and must be used by the caller
929 * in a thread-safe way. In particular, the caller must only
930 * use the context with functions which are `const` on the input
931 * context, or those which are known to be thread-safe. The latter
Hanno Beckerb653aa32019-06-28 10:53:55 +0100932 * for example includes mbedtls_pk_verify() for ECC or RSA public
933 * key contexts.
Hanno Becker4b70e122019-05-20 16:51:01 +0100934 *
Hanno Beckerfc99a092019-06-28 14:45:26 +0100935 * \note In a single-threaded application using
936 * MBEDTLS_X509_ALWAYS_FLUSH, nested calls to this function
937 * are not allowed and will fail gracefully with
938 * MBEDTLS_ERR_X509_FATAL_ERROR.
939 *
Hanno Becker4b70e122019-05-20 16:51:01 +0100940 * \return \c 0 on success. In this case, `*pk_ptr` is updated
941 * to hold the address of a public key context for the given
942 * certificate.
943 * \return A negative error code on failure.
944 */
Hanno Becker180f7bf2019-02-28 13:23:38 +0000945static inline int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt const *crt,
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100946 mbedtls_pk_context **dst )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000947{
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100948 int ret = 0;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000949#if defined(MBEDTLS_THREADING_C)
950 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
951 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100952#endif /* MBEDTLS_THREADING_C */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000953
Hanno Beckerfc99a092019-06-28 14:45:26 +0100954#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
955 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100956 if( crt->cache->pk_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100957#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100958 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000959
Hanno Beckerfc99a092019-06-28 14:45:26 +0100960#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
961 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100962 if( crt->cache->pk_readers == MBEDTLS_X509_CACHE_PK_READERS_MAX )
963 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
964
965 crt->cache->pk_readers++;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100966#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100967
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100968#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100969 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
970 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
971#endif /* MBEDTLS_THREADING_C */
972
973 *dst = crt->cache->pk;
974 return( ret );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000975}
976
Hanno Becker4b70e122019-05-20 16:51:01 +0100977/**
978 * \brief Release access to a public key context acquired
979 * through a prior call to mbedtls_x509_crt_frame_acquire().
980 *
981 * \param crt The certificate for which a certificate frame has
982 * previously been acquired.
983 */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100984static inline int mbedtls_x509_crt_pk_release( mbedtls_x509_crt const *crt )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000985{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000986#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100987 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
988 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100989#endif /* MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100990
Hanno Beckerfc99a092019-06-28 14:45:26 +0100991#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
992 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100993 if( crt->cache->pk_readers == 0 )
Hanno Becker69c30332019-06-28 15:47:53 +0100994 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100995
996 crt->cache->pk_readers--;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100997#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100998
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100999#if defined(MBEDTLS_THREADING_C)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001000 mbedtls_mutex_unlock( &crt->cache->pk_mutex );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001001#endif /* MBEDTLS_THREADING_C */
Hanno Beckerbc685192019-03-05 15:35:31 +00001002
1003#if defined(MBEDTLS_X509_ALWAYS_FLUSH)
Hanno Becker60785d12019-06-07 17:49:11 +01001004 (void) mbedtls_x509_crt_flush_cache_pk( crt );
Hanno Beckerbc685192019-03-05 15:35:31 +00001005#endif /* MBEDTLS_X509_ALWAYS_FLUSH */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001006
Hanno Becker60785d12019-06-07 17:49:11 +01001007#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
1008 !defined(MBEDTLS_THREADING_C)
1009 ((void) crt);
1010#endif
1011
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001012 return( 0 );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001013}
1014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016
1017/* \} name */
1018/* \} addtogroup x509_module */
1019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021/**
1022 * \brief Initialize a CRT writing context
1023 *
1024 * \param ctx CRT context to initialize
1025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027
1028/**
1029 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 *
1032 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
1034 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037
1038/**
1039 * \brief Set the serial number for a Certificate.
1040 *
1041 * \param ctx CRT context to use
1042 * \param serial serial number to set
1043 *
1044 * \return 0 if successful
1045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001047
1048/**
1049 * \brief Set the validity period for a Certificate
1050 * Timestamps should be in string format for UTC timezone
1051 * i.e. "YYYYMMDDhhmmss"
1052 * e.g. "20131231235959" for December 31st 2013
1053 * at 23:59:59
1054 *
1055 * \param ctx CRT context to use
1056 * \param not_before not_before timestamp
1057 * \param not_after not_after timestamp
1058 *
1059 * \return 0 if timestamp was parsed successfully, or
1060 * a specific error code
1061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +01001063 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001064
1065/**
1066 * \brief Set the issuer name for a Certificate
1067 * Issuer names should contain a comma-separated list
1068 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001069 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070 *
1071 * \param ctx CRT context to use
1072 * \param issuer_name issuer name to set
1073 *
1074 * \return 0 if issuer name was parsed successfully, or
1075 * a specific error code
1076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001078 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079
1080/**
1081 * \brief Set the subject name for a Certificate
1082 * Subject names should contain a comma-separated list
1083 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001084 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085 *
1086 * \param ctx CRT context to use
1087 * \param subject_name subject name to set
1088 *
1089 * \return 0 if subject name was parsed successfully, or
1090 * a specific error code
1091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001093 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094
1095/**
1096 * \brief Set the subject public key for the certificate
1097 *
1098 * \param ctx CRT context to use
1099 * \param key public key to include
1100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102
1103/**
1104 * \brief Set the issuer key used for signing the certificate
1105 *
1106 * \param ctx CRT context to use
1107 * \param key private key to sign with
1108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110
1111/**
1112 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114 *
1115 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001116 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119
1120/**
1121 * \brief Generic function to add to or replace an extension in the
1122 * CRT
1123 *
1124 * \param ctx CRT context to use
1125 * \param oid OID of the extension
1126 * \param oid_len length of the OID
1127 * \param critical if the extension is critical (per the RFC's definition)
1128 * \param val value of the extension OCTET STRING
1129 * \param val_len length of the value data
1130 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001131 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001134 const char *oid, size_t oid_len,
1135 int critical,
1136 const unsigned char *val, size_t val_len );
1137
1138/**
1139 * \brief Set the basicConstraints extension for a CRT
1140 *
1141 * \param ctx CRT context to use
1142 * \param is_ca is this a CA certificate
1143 * \param max_pathlen maximum length of certificate chains below this
1144 * certificate (only for CA certificates, -1 is
1145 * inlimited)
1146 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001147 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150 int is_ca, int max_pathlen );
1151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001153/**
1154 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 * called before
1157 *
1158 * \param ctx CRT context to use
1159 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001160 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001163
1164/**
1165 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001167 * called before
1168 *
1169 * \param ctx CRT context to use
1170 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001171 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
1174#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001175
1176/**
1177 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001179 *
1180 * \param ctx CRT context to use
1181 * \param key_usage key usage flags to set
1182 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001183 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001184 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +02001185int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
1186 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001187
1188/**
1189 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 *
1192 * \param ctx CRT context to use
1193 * \param ns_cert_type Netscape Cert Type flags to set
1194 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001195 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001198 unsigned char ns_cert_type );
1199
1200/**
1201 * \brief Free the contents of a CRT write context
1202 *
1203 * \param ctx CRT context to free
1204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206
1207/**
1208 * \brief Write a built up certificate to a X509 DER structure
1209 * Note: data is written at the end of the buffer! Use the
1210 * return value to determine where you should start
1211 * using the buffer
1212 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001213 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001214 * \param buf buffer to write to
1215 * \param size size of the buffer
1216 * \param f_rng RNG function (for signature, see note)
1217 * \param p_rng RNG parameter
1218 *
1219 * \return length of data written if successful, or a specific
1220 * error code
1221 *
1222 * \note f_rng may be NULL if RSA is used for signature and the
1223 * signature is made offline (otherwise f_rng is desirable
1224 * for countermeasures against timing attacks).
1225 * ECDSA signatures always require a non-NULL f_rng.
1226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001228 int (*f_rng)(void *, unsigned char *, size_t),
1229 void *p_rng );
1230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001232/**
1233 * \brief Write a built up certificate to a X509 PEM string
1234 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001235 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001236 * \param buf buffer to write to
1237 * \param size size of the buffer
1238 * \param f_rng RNG function (for signature, see note)
1239 * \param p_rng RNG parameter
1240 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001241 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242 *
1243 * \note f_rng may be NULL if RSA is used for signature and the
1244 * signature is made offline (otherwise f_rng is desirable
1245 * for countermeasures against timing attacks).
1246 * ECDSA signatures always require a non-NULL f_rng.
1247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001249 int (*f_rng)(void *, unsigned char *, size_t),
1250 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#endif /* MBEDTLS_PEM_WRITE_C */
1252#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001253
1254#ifdef __cplusplus
1255}
1256#endif
1257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#endif /* mbedtls_x509_crt.h */