blob: f0801df796a2f333441a086d9bb32286770875e0 [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. */
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030099#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker21f55672019-02-15 15:27:59 +0000100 mbedtls_x509_buf_raw subject_alt_raw; /**< The raw data for the SubjectAlternativeNames extension. */
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300101#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker21f55672019-02-15 15:27:59 +0000102 mbedtls_x509_buf_raw ext_key_usage_raw; /**< The raw data for the ExtendedKeyUsage extension. */
Hanno Becker21f55672019-02-15 15:27:59 +0000103} mbedtls_x509_crt_frame;
104
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105/**
106 * Container for an X.509 certificate. The certificate may be chained.
107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109{
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000110 int own_buffer; /**< Indicates if \c raw is owned
Hanno Becker180f7bf2019-02-28 13:23:38 +0000111 * by the structure or not. */
112 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
Hanno Becker00d39032019-05-13 12:39:44 +0100113 mbedtls_x509_crt_cache *cache; /**< Internal parsing cache. */
Hanno Becker180f7bf2019-02-28 13:23:38 +0000114
115 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
116
117 /* Legacy fields */
118#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +0200121 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
123 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
126 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
129 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200130
Hanno Becker843b71a2019-06-25 09:39:21 +0100131#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
133 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Hanno Becker843b71a2019-06-25 09:39:21 +0100134#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135
Hanno Becker32c530e2019-02-06 16:13:41 +0000136 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138
Hanno Beckerd07614c2019-06-25 10:19:58 +0100139#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
141 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
Hanno Beckerd07614c2019-06-25 10:19:58 +0100142#endif /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300144#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300146#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147
148 int ext_types; /**< Bit string containing detected and parsed extensions */
149 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
150 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+ */
151
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100152 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153
Hanno Becker7ec9c362019-02-21 14:24:05 +0000154 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +0200156 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
159 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
160 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
161 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 +0000162#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200166/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200167 * Build flag from an algorithm/curve identifier (pk, md, ecp)
168 * Since 0 is always XXX_NONE, ignore it.
169 */
Hanno Beckerd6028a12018-10-15 12:01:35 +0100170#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200171
172/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200173 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200174 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200175 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200176 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200177typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200178{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200179 uint32_t allowed_mds; /**< MDs for signatures */
180 uint32_t allowed_pks; /**< PK algs for signatures */
181 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
182 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200183}
184mbedtls_x509_crt_profile;
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#define MBEDTLS_X509_CRT_VERSION_1 0
187#define MBEDTLS_X509_CRT_VERSION_2 1
188#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
191#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200192
Andres AGf9113192016-09-02 14:06:04 +0100193#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
194#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
195#endif
196
Hanno Beckere8c52ff2019-08-23 15:23:27 +0100197#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200198/**
199 * Container for writing a certificate (CRT)
200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200202{
203 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_mpi serial;
205 mbedtls_pk_context *subject_key;
206 mbedtls_pk_context *issuer_key;
207 mbedtls_asn1_named_data *subject;
208 mbedtls_asn1_named_data *issuer;
209 mbedtls_md_type_t md_alg;
210 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
211 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
212 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214mbedtls_x509write_cert;
Hanno Beckere8c52ff2019-08-23 15:23:27 +0100215#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200217/**
218 * Item in a verification chain: cert and flags for it
219 */
220typedef struct {
221 mbedtls_x509_crt *crt;
222 uint32_t flags;
223} mbedtls_x509_crt_verify_chain_item;
224
225/**
226 * Max size of verification chain: end-entity + intermediates + trusted root
227 */
228#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
229
230/**
231 * Verification chain as built by \c mbedtls_crt_verify_chain()
232 */
233typedef struct
234{
235 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200236 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200237} mbedtls_x509_crt_verify_chain;
238
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200239#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
240
241/**
242 * \brief Context for resuming X.509 verify operations
243 */
244typedef struct
245{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200246 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200247 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200248
249 /* for find_parent_in() */
250 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
Hanno Becker6f61b7b2019-06-10 11:12:33 +0100251
252#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200253 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200254 int fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +0100255#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200256
257 /* for find_parent() */
258 int parent_is_trusted; /* -1 if find_parent is not in progress */
259
260 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200261 enum {
262 x509_crt_rs_none,
263 x509_crt_rs_find_parent,
264 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200265 int self_cnt;
266 mbedtls_x509_crt_verify_chain ver_chain;
267
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200268} mbedtls_x509_crt_restart_ctx;
269
270#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
271
272/* Now we can declare functions that take a pointer to that */
273typedef void mbedtls_x509_crt_restart_ctx;
274
275#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200279 * Default security profile. Should provide a good balance between security
280 * and compatibility with current deployments.
281 */
282extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
283
284/**
285 * Expected next default profile. Recommended for new deployments.
286 * Currently targets a 128-bit security level, except for RSA-2048.
287 */
288extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
289
290/**
291 * NSA Suite B profile.
292 */
293extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
294
295/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296 * \brief Parse a single DER formatted certificate and add it
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000297 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200298 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000299 * \param chain The pointer to the start of the CRT chain to attach to.
300 * When parsing the first CRT in a chain, this should point
301 * to an instance of ::mbedtls_x509_crt initialized through
302 * mbedtls_x509_crt_init().
303 * \param buf The buffer holding the DER encoded certificate.
304 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000306 * \note This function makes an internal copy of the CRT buffer
307 * \p buf. In particular, \p buf may be destroyed or reused
308 * after this call returns. To avoid duplicating the CRT
309 * buffer (at the cost of stricter lifetime constraints),
310 * use mbedtls_x509_crt_parse_der_nocopy() instead.
311 *
312 * \return \c 0 if successful.
313 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200314 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000315int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
316 const unsigned char *buf,
317 size_t buflen );
318
319/**
320 * \brief Parse a single DER formatted certificate and add it
321 * to the end of the provided chained list. This is a
322 * variant of mbedtls_x509_crt_parse_der() which takes
323 * temporary ownership of the CRT buffer until the CRT
324 * is destroyed.
325 *
326 * \param chain The pointer to the start of the CRT chain to attach to.
327 * When parsing the first CRT in a chain, this should point
328 * to an instance of ::mbedtls_x509_crt initialized through
329 * mbedtls_x509_crt_init().
330 * \param buf The address of the readable buffer holding the DER encoded
331 * certificate to use. On success, this buffer must be
332 * retained and not be changed for the liftetime of the
333 * CRT chain \p chain, that is, until \p chain is destroyed
334 * through a call to mbedtls_x509_crt_free().
335 * \param buflen The size in Bytes of \p buf.
336 *
337 * \note This call is functionally equivalent to
338 * mbedtls_x509_crt_parse_der(), but it avoids creating a
339 * copy of the input buffer at the cost of stronger lifetime
340 * constraints. This is useful in constrained environments
341 * where duplication of the CRT cannot be tolerated.
342 *
343 * \return \c 0 if successful.
344 * \return A negative error code on failure.
345 */
346int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
347 const unsigned char *buf,
348 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349
350/**
Hanno Becker89a91122018-08-23 16:14:00 +0100351 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100352 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100354 * For CRTs in PEM encoding, the function parses permissively:
355 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100356 * returns the number of certificates for which parsing failed
357 * (hence \c 0 if all certificates were parsed successfully).
358 * If no certificate could be parsed, the function returns
359 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100361 * PEM encoded certificates may be interleaved by other data
362 * such as human readable descriptions of their content, as
363 * long as the certificates are enclosed in the PEM specific
364 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
365 *
366 * \param chain The chain to which to add the parsed certificates.
367 * \param buf The buffer holding the certificate data in PEM or DER format.
368 * For certificates in PEM encoding, this may be a concatenation
369 * of multiple certificates; for DER encoding, the buffer must
370 * comprise exactly one certificate.
371 * \param buflen The size of \p buf, including the terminating \c NULL byte
372 * in case of PEM encoded data.
373 *
374 * \return \c 0 if all certificates were parsed successfully.
375 * \return The (positive) number of certificates that couldn't
376 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100377 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100378 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383/**
384 * \brief Load one or more certificates and add them
385 * to the chained list. Parses permissively. If some
386 * certificates can be parsed, the result is the number
387 * of failed certificates it encountered. If none complete
388 * correctly, the first error is returned.
389 *
390 * \param chain points to the start of the chain
391 * \param path filename to read the certificates from
392 *
393 * \return 0 if all certificates parsed successfully, a positive number
394 * if partly successful or a specific X509 or PEM error code
395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397
398/**
399 * \brief Load one or more certificate files from a path and add them
400 * to the chained list. Parses permissively. If some
401 * certificates can be parsed, the result is the number
402 * of failed certificates it encountered. If none complete
403 * correctly, the first error is returned.
404 *
405 * \param chain points to the start of the chain
406 * \param path directory / folder to read the certificate files from
407 *
408 * \return 0 if all certificates parsed successfully, a positive number
409 * if partly successful or a specific X509 or PEM error code
410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
412#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413
Hanno Becker02a21932019-06-10 15:08:43 +0100414#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415/**
416 * \brief Returns an informational string about the
417 * certificate.
418 *
419 * \param buf Buffer to write to
420 * \param size Maximum size of buffer
421 * \param prefix A line prefix
422 * \param crt The X509 certificate to represent
423 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200424 * \return The length of the string written (not including the
425 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
428 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429
430/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100431 * \brief Returns an informational string about the
432 * verification status of a certificate.
433 *
434 * \param buf Buffer to write to
435 * \param size Maximum size of buffer
436 * \param prefix A line prefix
437 * \param flags Verification flags created by mbedtls_x509_crt_verify()
438 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200439 * \return The length of the string written (not including the
440 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100441 */
442int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200443 uint32_t flags );
Hanno Beckerc6043f22019-06-14 17:21:24 +0100444#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200445
446/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447 * \brief Verify the certificate signature
448 *
449 * The verify callback is a user-supplied callback that
450 * can clear / modify / add flags for a certificate. If set,
451 * the verification callback is called for each
452 * certificate in the chain (from the trust-ca down to the
453 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 * int *flags). With the flags representing current flags for
456 * that specific certificate and the certificate depth from
457 * the bottom (Peer cert depth = 0).
458 *
459 * All flags left after returning from the callback
460 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200461 * return 0 for anything (including invalid certificates)
462 * other than fatal error, as a non-zero return code
463 * immediately aborts the verification process. For fatal
464 * errors, a specific error code should be used (different
465 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
466 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
467 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100469 * \note In case verification failed, the results can be displayed
470 * using \c mbedtls_x509_crt_verify_info()
471 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200472 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
473 * default security profile.
474 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100475 * \note It is your responsibility to provide up-to-date CRLs for
476 * all trusted CAs. If no CRL is provided for the CA that was
477 * used to sign the certificate, CRL verification is skipped
478 * silently, that is *without* setting any flag.
479 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200480 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200481 * (1) those of trusted root CAs, so that certificates
482 * chaining up to those CAs will be trusted, and (2)
483 * self-signed end-entity certificates to be trusted (for
484 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200485 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200486 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100487 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200488 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100489 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 * \param cn expected Common Name (can be set to
491 * NULL if the CN must not be verified)
492 * \param flags result of the verification
493 * \param f_vrfy verification function
494 * \param p_vrfy verification parameter
495 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200496 * \return 0 (and flags set to 0) if the chain was verified and valid,
497 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
498 * but found to be invalid, in which case *flags will have one
499 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
500 * flags set, or another error (and flags set to 0xffffffff)
501 * in case of a fatal error encountered during the
502 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100505 mbedtls_x509_crt *trust_ca,
506 mbedtls_x509_crl *ca_crl,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300507#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION) || defined(DOXYGEN_ONLY)
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100508 const char *cn,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300509#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION || defined(DOXYGEN_ONLY) */
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100510 uint32_t *flags
511#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
512 , int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
513 void *p_vrfy
514#endif /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
515 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200517/**
518 * \brief Verify the certificate signature according to profile
519 *
520 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
521 * security profile.
522 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200523 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200524 * for ECDSA) apply to all certificates: trusted root,
525 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200526 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100527 * \param crt a certificate (chain) to be verified
528 * \param trust_ca the list of trusted CAs
529 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200530 * \param profile security profile for verification
531 * \param cn expected Common Name (can be set to
532 * NULL if the CN must not be verified)
533 * \param flags result of the verification
534 * \param f_vrfy verification function
535 * \param p_vrfy verification parameter
536 *
537 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
538 * in which case *flags will have one or more
539 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
540 * set,
541 * or another error in case of a fatal error encountered
542 * during the verification process.
543 */
544int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
545 mbedtls_x509_crt *trust_ca,
546 mbedtls_x509_crl *ca_crl,
547 const mbedtls_x509_crt_profile *profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300548#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION) || defined(DOXYGEN_ONLY)
549 const char *cn,
550#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION || defined(DOXYGEN_ONLY) */
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100551 uint32_t *flags
552#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
553 , int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
554 void *p_vrfy
555#endif /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
556 );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200557
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200558/**
559 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
560 *
561 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
562 * but can return early and restart according to the limit
563 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
564 *
565 * \param crt a certificate (chain) to be verified
566 * \param trust_ca the list of trusted CAs
567 * \param ca_crl the list of CRLs for trusted CAs
568 * \param profile security profile for verification
569 * \param cn expected Common Name (can be set to
570 * NULL if the CN must not be verified)
571 * \param flags result of the verification
572 * \param f_vrfy verification function
573 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200574 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200575 *
576 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200577 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200578 * operations was reached: see \c mbedtls_ecp_set_max_ops().
579 */
580int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
581 mbedtls_x509_crt *trust_ca,
582 mbedtls_x509_crl *ca_crl,
583 const mbedtls_x509_crt_profile *profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300584#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION) || defined(DOXYGEN_ONLY)
585 const char *cn,
586#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION || defined(DOXYGEN_ONLY) */
587 uint32_t *flags,
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100588#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
589 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
590 void *p_vrfy,
591#endif /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200592 mbedtls_x509_crt_restart_ctx *rs_ctx );
593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200595/**
596 * \brief Check usage of certificate against keyUsage extension.
597 *
598 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200599 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
600 * before using the certificate to perform an RSA key
601 * exchange).
602 *
603 * \note Except for decipherOnly and encipherOnly, a bit set in the
604 * usage argument means this bit MUST be set in the
605 * certificate. For decipherOnly and encipherOnly, it means
606 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200607 *
608 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200610 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200611 *
612 * \note You should only call this function on leaf certificates, on
613 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200615 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200616int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
617 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200621/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000622 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200623 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000624 * \param crt Leaf certificate used.
625 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
626 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200628 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000629 * \return 0 if this use of the certificate is allowed,
630 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200631 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000632 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000635 const char *usage_oid,
636 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000637#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200641 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 *
643 * \param crt a certificate to be verified
644 * \param crl the CRL to verify against
645 *
646 * \return 1 if the certificate is revoked, 0 otherwise
647 *
648 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100649int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651
652/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200653 * \brief Initialize a certificate (chain)
654 *
655 * \param crt Certificate chain to initialize
656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200658
659/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660 * \brief Unallocate all certificate data
661 *
662 * \param crt Certificate chain to free
663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200665
666#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
667/**
668 * \brief Initialize a restart context
669 */
670void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
671
672/**
673 * \brief Free the components of a restart context
674 */
675void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
676#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000677
Hanno Becker823efad2019-02-28 13:23:58 +0000678/**
679 * \brief Request CRT frame giving access to basic CRT fields
680 * and raw ASN.1 data of complex fields.
681 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100682 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker823efad2019-02-28 13:23:58 +0000683 * \param dst The address of the destination frame structure.
684 * This need not be initialized.
685 *
686 * \note ::mbedtls_x509_crt_frame does not contain pointers to
687 * dynamically allocated memory, and hence need not be freed.
688 * Users may e.g. allocate an instance of
689 * ::mbedtls_x509_crt_frame on the stack and call this function
690 * on it, in which case no allocation/freeing has to be done.
691 *
Hanno Becker608de6a2019-06-28 10:48:01 +0100692 * \note You may also use mbedtls_x509_crt_frame_acquire() and
693 * mbedtls_x509_crt_frame_release() for copy-less variants
694 * of this function.
695 *
Hanno Becker823efad2019-02-28 13:23:58 +0000696 * \return \c 0 on success. In this case, \p dst is updated
697 * to hold the frame for the given CRT.
698 * \return A negative error code on failure.
699 */
700int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
701 mbedtls_x509_crt_frame *dst );
702
703/**
Hanno Becker9219f9e2019-05-14 12:40:58 +0100704 * \brief Set up a PK context with the public key in a certificate.
Hanno Becker823efad2019-02-28 13:23:58 +0000705 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100706 * \param crt The certificate to use. This must be initialized and set up.
Hanno Becker823efad2019-02-28 13:23:58 +0000707 * \param pk The address of the destination PK context to fill.
708 * This must be initialized via mbedtls_pk_init().
709 *
Hanno Becker608de6a2019-06-28 10:48:01 +0100710 * \note You may also use mbedtls_x509_crt_pk_acquire() and
711 * mbedtls_x509_crt_pk_release() for copy-less variants
712 * of this function.
713 *
Hanno Becker823efad2019-02-28 13:23:58 +0000714 * \return \c 0 on success. In this case, the user takes ownership
715 * of the destination PK context, and is responsible for
716 * calling mbedtls_pk_free() on it once it's no longer needed.
717 * \return A negative error code on failure.
718 */
719int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
720 mbedtls_pk_context *pk );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000721
722/**
Hanno Becker63e69982019-02-26 18:50:49 +0000723 * \brief Request the subject name of a CRT, presented
724 * as a dynamically allocated linked list.
725 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100726 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker63e69982019-02-26 18:50:49 +0000727 * \param subject The address at which to store the address of the
728 * first entry of the generated linked list holding
729 * the subject name.
730 *
Hanno Beckerd92078f2019-06-28 10:48:57 +0100731 * \note Depending on your use case, consider using the raw ASN.1
Hanno Becker63e69982019-02-26 18:50:49 +0000732 * describing the subject name instead of the heap-allocated
733 * linked list generated by this call. The pointers to the
734 * raw ASN.1 data are part of the CRT frame that can be queried
735 * via mbedtls_x509_crt_get_frame(), and they can be traversed
736 * via mbedtls_asn1_traverse_sequence_of().
737 *
738 * \return \c 0 on success. In this case, the user takes ownership
739 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000740 * through a call to mbedtls_x509_name_free() once it's no
741 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000742 * \return A negative error code on failure.
743 */
744int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
745 mbedtls_x509_name **subject );
746
747/**
748 * \brief Request the subject name of a CRT, presented
749 * as a dynamically allocated linked list.
750 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100751 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker63e69982019-02-26 18:50:49 +0000752 * \param issuer The address at which to store the address of the
753 * first entry of the generated linked list holding
754 * the subject name.
755 *
Hanno Beckerd92078f2019-06-28 10:48:57 +0100756 * \note Depending on your use case, consider using the raw ASN.1
757 * describing the issuer name instead of the heap-allocated
Hanno Becker63e69982019-02-26 18:50:49 +0000758 * linked list generated by this call. The pointers to the
759 * raw ASN.1 data are part of the CRT frame that can be queried
760 * via mbedtls_x509_crt_get_frame(), and they can be traversed
761 * via mbedtls_asn1_traverse_sequence_of().
762 *
763 * \return \c 0 on success. In this case, the user takes ownership
764 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000765 * through a call to mbedtls_x509_name_free() once it's no
766 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000767 * \return A negative error code on failure.
768 */
769int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
770 mbedtls_x509_name **issuer );
771
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300772#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker63e69982019-02-26 18:50:49 +0000773/**
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000774 * \brief Request the subject alternative name of a CRT, presented
775 * as a dynamically allocated linked list.
776 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100777 * \param crt The CRT to use. This must be initialized and set up.
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000778 * \param subj_alt The address at which to store the address of the
779 * first component of the subject alternative names list.
780 *
781 * \note Depending in your use case, consider using the raw ASN.1
782 * describing the subject alternative names extension
783 * instead of the heap-allocated linked list generated by this
784 * call. The pointers to the raw ASN.1 data are part of the CRT
785 * frame that can be queried via mbedtls_x509_crt_get_frame(),
786 * and mbedtls_asn1_traverse_sequence_of() can be used to
787 * traverse the list of subject alternative names.
788 *
789 * \return \c 0 on success. In this case, the user takes ownership
790 * of the name context, and is responsible for freeing it
791 * through a call to mbedtls_x509_sequence_free() once it's
792 * no longer needed.
793 * \return A negative error code on failure.
794 */
795int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
796 mbedtls_x509_sequence **subj_alt );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300797#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000798
799/**
800 * \brief Request the ExtendedKeyUsage extension of a CRT,
801 * presented as a dynamically allocated linked list.
802 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100803 * \param crt The CRT to use. This must be initialized and set up.
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000804 * \param ext_key_usage The address at which to store the address of the
805 * first entry of the ExtendedKeyUsage extension.
806 *
807 * \note Depending in your use case, consider using the raw ASN.1
808 * describing the extended key usage extension instead of
809 * the heap-allocated linked list generated by this call.
810 * The pointers to the raw ASN.1 data are part of the CRT
811 * frame that can be queried via mbedtls_x509_crt_get_frame(),
812 * and mbedtls_asn1_traverse_sequence_of() can be used to
813 * traverse the entries in the extended key usage extension.
814 *
815 * \return \c 0 on success. In this case, the user takes ownership
816 * of the name context, and is responsible for freeing it
817 * through a call to mbedtls_x509_sequence_free() once it's
818 * no longer needed.
819 * \return A negative error code on failure.
820 */
821int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
822 mbedtls_x509_sequence **ext_key_usage );
823
824/**
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000825 * \brief Flush internal X.509 CRT parsing cache, if present.
826 *
827 * \param crt The CRT structure whose cache to flush.
828 *
829 * \note Calling this function frequently reduces RAM usage
830 * at the cost of performance.
831 *
832 * \return \c 0 on success.
833 * \return A negative error code on failure.
834 */
835int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt );
836
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100837/**
838 * \brief Request temporary read-access to a certificate frame
839 * for a given certificate.
840 *
841 * Once no longer needed, the frame must be released
842 * through a call to mbedtls_x509_crt_frame_release().
843 *
844 * This is a copy-less version of mbedtls_x509_crt_get_frame().
845 * See there for more information.
846 *
847 * \param crt The certificate to use. This must be initialized and set up.
848 * \param dst The address at which to store the address of a readable
849 * certificate frame for the input certificate \p crt which the
850 * caller can use until calling mbedtls_x509_crt_frame_release().
851 *
852 * \note The certificate frame `**frame_ptr` returned by this function
853 * is owned by the X.509 module and must not be freed or modified
854 * by the caller. The X.509 module guarantees its validity as long
855 * as \p crt is valid and mbedtls_x509_crt_frame_release() hasn't
856 * been issued.
857 *
Hanno Beckerfc99a092019-06-28 14:45:26 +0100858 * \note In a single-threaded application using
859 * MBEDTLS_X509_ALWAYS_FLUSH, nested calls to this function
860 * are not allowed and will fail gracefully with
861 * MBEDTLS_ERR_X509_FATAL_ERROR.
862 *
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100863 * \return \c 0 on success. In this case, `*frame_ptr` is updated
864 * to hold the address of a frame for the given CRT.
865 * \return A negative error code on failure.
866 */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000867static inline int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100868 mbedtls_x509_crt_frame const **dst )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000869{
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100870 int ret = 0;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000871#if defined(MBEDTLS_THREADING_C)
872 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
873 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100874#endif /* MBEDTLS_THREADING_C */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000875
Hanno Beckerfc99a092019-06-28 14:45:26 +0100876#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
877 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100878 if( crt->cache->frame_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100879#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100880 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000881
Hanno Beckerfc99a092019-06-28 14:45:26 +0100882#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
883 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100884 if( crt->cache->frame_readers == MBEDTLS_X509_CACHE_FRAME_READERS_MAX )
885 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
886
887 crt->cache->frame_readers++;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100888#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100889
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100890#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100891 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
892 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
893#endif /* MBEDTLS_THREADING_C */
894
895 *dst = crt->cache->frame;
896 return( ret );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000897}
898
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100899/**
900 * \brief Release access to a certificate frame acquired
901 * through a prior call to mbedtls_x509_crt_frame_acquire().
902 *
903 * \param crt The certificate for which a certificate frame has
904 * previously been acquired.
905 */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100906static inline int mbedtls_x509_crt_frame_release( mbedtls_x509_crt const *crt )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000907{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000908#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100909 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
910 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100911#endif /* MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100912
Hanno Beckerfc99a092019-06-28 14:45:26 +0100913#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
914 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100915 if( crt->cache->frame_readers == 0 )
Hanno Becker69c30332019-06-28 15:47:53 +0100916 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100917
918 crt->cache->frame_readers--;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100919#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100920
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100921#if defined(MBEDTLS_THREADING_C)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000922 mbedtls_mutex_unlock( &crt->cache->frame_mutex );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100923#endif /* MBEDTLS_THREADING_C */
Hanno Beckerbc685192019-03-05 15:35:31 +0000924
925#if defined(MBEDTLS_X509_ALWAYS_FLUSH)
Hanno Becker60785d12019-06-07 17:49:11 +0100926 (void) mbedtls_x509_crt_flush_cache_frame( crt );
Hanno Beckerbc685192019-03-05 15:35:31 +0000927#endif /* MBEDTLS_X509_ALWAYS_FLUSH */
Hanno Becker60785d12019-06-07 17:49:11 +0100928
929#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
930 !defined(MBEDTLS_THREADING_C)
931 ((void) crt);
932#endif
933
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100934 return( 0 );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000935}
936
Hanno Becker4b70e122019-05-20 16:51:01 +0100937/**
938 * \brief Request temporary access to a public key context
939 * for a given certificate.
940 *
941 * Once no longer needed, the frame must be released
942 * through a call to mbedtls_x509_crt_pk_release().
943 *
944 * This is a copy-less version of mbedtls_x509_crt_get_pk().
945 * See there for more information.
946 *
947 * \param crt The certificate to use. This must be initialized and set up.
948 * \param dst The address at which to store the address of a public key
949 * context for the public key in the input certificate \p crt.
950 *
951 * \warning The public key context `**pk_ptr` returned by this function
952 * is owned by the X.509 module and must be used by the caller
953 * in a thread-safe way. In particular, the caller must only
954 * use the context with functions which are `const` on the input
955 * context, or those which are known to be thread-safe. The latter
Hanno Beckerb653aa32019-06-28 10:53:55 +0100956 * for example includes mbedtls_pk_verify() for ECC or RSA public
957 * key contexts.
Hanno Becker4b70e122019-05-20 16:51:01 +0100958 *
Hanno Beckerfc99a092019-06-28 14:45:26 +0100959 * \note In a single-threaded application using
960 * MBEDTLS_X509_ALWAYS_FLUSH, nested calls to this function
961 * are not allowed and will fail gracefully with
962 * MBEDTLS_ERR_X509_FATAL_ERROR.
963 *
Hanno Becker4b70e122019-05-20 16:51:01 +0100964 * \return \c 0 on success. In this case, `*pk_ptr` is updated
965 * to hold the address of a public key context for the given
966 * certificate.
967 * \return A negative error code on failure.
968 */
Hanno Becker180f7bf2019-02-28 13:23:38 +0000969static inline int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt const *crt,
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100970 mbedtls_pk_context **dst )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000971{
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100972 int ret = 0;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000973#if defined(MBEDTLS_THREADING_C)
974 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
975 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100976#endif /* MBEDTLS_THREADING_C */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000977
Hanno Beckerfc99a092019-06-28 14:45:26 +0100978#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
979 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100980 if( crt->cache->pk_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100981#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100982 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000983
Hanno Beckerfc99a092019-06-28 14:45:26 +0100984#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
985 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100986 if( crt->cache->pk_readers == MBEDTLS_X509_CACHE_PK_READERS_MAX )
987 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
988
989 crt->cache->pk_readers++;
Hanno Beckerfc99a092019-06-28 14:45:26 +0100990#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100991
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100992#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100993 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
994 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
995#endif /* MBEDTLS_THREADING_C */
996
997 *dst = crt->cache->pk;
998 return( ret );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000999}
1000
Hanno Becker4b70e122019-05-20 16:51:01 +01001001/**
1002 * \brief Release access to a public key context acquired
1003 * through a prior call to mbedtls_x509_crt_frame_acquire().
1004 *
1005 * \param crt The certificate for which a certificate frame has
1006 * previously been acquired.
1007 */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001008static inline int mbedtls_x509_crt_pk_release( mbedtls_x509_crt const *crt )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001009{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001010#if defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001011 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
1012 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +01001013#endif /* MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001014
Hanno Beckerfc99a092019-06-28 14:45:26 +01001015#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
1016 defined(MBEDTLS_THREADING_C)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001017 if( crt->cache->pk_readers == 0 )
Hanno Becker69c30332019-06-28 15:47:53 +01001018 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001019
1020 crt->cache->pk_readers--;
Hanno Beckerfc99a092019-06-28 14:45:26 +01001021#endif
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001022
Hanno Beckera4bfaa82019-06-28 10:34:23 +01001023#if defined(MBEDTLS_THREADING_C)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001024 mbedtls_mutex_unlock( &crt->cache->pk_mutex );
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001025#endif /* MBEDTLS_THREADING_C */
Hanno Beckerbc685192019-03-05 15:35:31 +00001026
1027#if defined(MBEDTLS_X509_ALWAYS_FLUSH)
Hanno Becker60785d12019-06-07 17:49:11 +01001028 (void) mbedtls_x509_crt_flush_cache_pk( crt );
Hanno Beckerbc685192019-03-05 15:35:31 +00001029#endif /* MBEDTLS_X509_ALWAYS_FLUSH */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001030
Hanno Becker60785d12019-06-07 17:49:11 +01001031#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
1032 !defined(MBEDTLS_THREADING_C)
1033 ((void) crt);
1034#endif
1035
Hanno Becker2ba9fbd2019-05-28 16:11:43 +01001036 return( 0 );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001037}
1038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040
1041/* \} name */
1042/* \} addtogroup x509_module */
1043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045/**
1046 * \brief Initialize a CRT writing context
1047 *
1048 * \param ctx CRT context to initialize
1049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051
1052/**
1053 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055 *
1056 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
1058 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001061
1062/**
1063 * \brief Set the serial number for a Certificate.
1064 *
1065 * \param ctx CRT context to use
1066 * \param serial serial number to set
1067 *
1068 * \return 0 if successful
1069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071
1072/**
1073 * \brief Set the validity period for a Certificate
1074 * Timestamps should be in string format for UTC timezone
1075 * i.e. "YYYYMMDDhhmmss"
1076 * e.g. "20131231235959" for December 31st 2013
1077 * at 23:59:59
1078 *
1079 * \param ctx CRT context to use
1080 * \param not_before not_before timestamp
1081 * \param not_after not_after timestamp
1082 *
1083 * \return 0 if timestamp was parsed successfully, or
1084 * a specific error code
1085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +01001087 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088
1089/**
1090 * \brief Set the issuer name for a Certificate
1091 * Issuer names should contain a comma-separated list
1092 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001093 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094 *
1095 * \param ctx CRT context to use
1096 * \param issuer_name issuer name to set
1097 *
1098 * \return 0 if issuer name was parsed successfully, or
1099 * a specific error code
1100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001102 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103
1104/**
1105 * \brief Set the subject name for a Certificate
1106 * Subject names should contain a comma-separated list
1107 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001108 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109 *
1110 * \param ctx CRT context to use
1111 * \param subject_name subject name to set
1112 *
1113 * \return 0 if subject name was parsed successfully, or
1114 * a specific error code
1115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001117 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118
1119/**
1120 * \brief Set the subject public key for the certificate
1121 *
1122 * \param ctx CRT context to use
1123 * \param key public key to include
1124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001126
1127/**
1128 * \brief Set the issuer key used for signing the certificate
1129 *
1130 * \param ctx CRT context to use
1131 * \param key private key to sign with
1132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001134
1135/**
1136 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001138 *
1139 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001140 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001143
1144/**
1145 * \brief Generic function to add to or replace an extension in the
1146 * CRT
1147 *
1148 * \param ctx CRT context to use
1149 * \param oid OID of the extension
1150 * \param oid_len length of the OID
1151 * \param critical if the extension is critical (per the RFC's definition)
1152 * \param val value of the extension OCTET STRING
1153 * \param val_len length of the value data
1154 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001155 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158 const char *oid, size_t oid_len,
1159 int critical,
1160 const unsigned char *val, size_t val_len );
1161
1162/**
1163 * \brief Set the basicConstraints extension for a CRT
1164 *
1165 * \param ctx CRT context to use
1166 * \param is_ca is this a CA certificate
1167 * \param max_pathlen maximum length of certificate chains below this
1168 * certificate (only for CA certificates, -1 is
1169 * inlimited)
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_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 int is_ca, int max_pathlen );
1175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001177/**
1178 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 * called before
1181 *
1182 * \param ctx CRT context to use
1183 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001184 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001187
1188/**
1189 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 * called before
1192 *
1193 * \param ctx CRT context to use
1194 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001195 * \return 0 if successful, or a 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_authority_key_identifier( mbedtls_x509write_cert *ctx );
1198#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001199
1200/**
1201 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203 *
1204 * \param ctx CRT context to use
1205 * \param key_usage key usage flags to set
1206 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001207 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001208 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +02001209int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
1210 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001211
1212/**
1213 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001215 *
1216 * \param ctx CRT context to use
1217 * \param ns_cert_type Netscape Cert Type flags to set
1218 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001219 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001222 unsigned char ns_cert_type );
1223
1224/**
1225 * \brief Free the contents of a CRT write context
1226 *
1227 * \param ctx CRT context to free
1228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001230
1231/**
1232 * \brief Write a built up certificate to a X509 DER structure
1233 * Note: data is written at the end of the buffer! Use the
1234 * return value to determine where you should start
1235 * using the buffer
1236 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001237 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001238 * \param buf buffer to write to
1239 * \param size size of the buffer
1240 * \param f_rng RNG function (for signature, see note)
1241 * \param p_rng RNG parameter
1242 *
1243 * \return length of data written if successful, or a specific
1244 * error code
1245 *
1246 * \note f_rng may be NULL if RSA is used for signature and the
1247 * signature is made offline (otherwise f_rng is desirable
1248 * for countermeasures against timing attacks).
1249 * ECDSA signatures always require a non-NULL f_rng.
1250 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001252 int (*f_rng)(void *, unsigned char *, size_t),
1253 void *p_rng );
1254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001256/**
1257 * \brief Write a built up certificate to a X509 PEM string
1258 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001259 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001260 * \param buf buffer to write to
1261 * \param size size of the buffer
1262 * \param f_rng RNG function (for signature, see note)
1263 * \param p_rng RNG parameter
1264 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001265 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001266 *
1267 * \note f_rng may be NULL if RSA is used for signature and the
1268 * signature is made offline (otherwise f_rng is desirable
1269 * for countermeasures against timing attacks).
1270 * ECDSA signatures always require a non-NULL f_rng.
1271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273 int (*f_rng)(void *, unsigned char *, size_t),
1274 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#endif /* MBEDTLS_PEM_WRITE_C */
1276#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001277
1278#ifdef __cplusplus
1279}
1280#endif
1281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282#endif /* mbedtls_x509_crt.h */