blob: f0eed056f8552f9cd93a40a37405299f36ad2323 [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"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
36/**
37 * \addtogroup x509_module
38 * \{
39 */
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/**
46 * \name Structures and functions for parsing and writing X.509 certificates
47 * \{
48 */
49
Teppo Järvelinffaba552019-09-03 12:33:16 +030050typedef struct mbedtls_x509_crt_cache
51{
52#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
53 defined(MBEDTLS_THREADING_C)
54 uint32_t frame_readers;
55 uint32_t pk_readers;
56#endif /* !MBEDTLS_X509_ALWAYS_FLUSH || MBEDTLS_THREADING_C */
57#if defined(MBEDTLS_THREADING_C)
58 mbedtls_threading_mutex_t frame_mutex;
59 mbedtls_threading_mutex_t pk_mutex;
60#endif
61 mbedtls_x509_buf_raw pk_raw;
62 struct mbedtls_x509_crt_frame *frame;
63 struct mbedtls_pk_context *pk;
64} mbedtls_x509_crt_cache;
65
Hanno Becker21f55672019-02-15 15:27:59 +000066typedef struct mbedtls_x509_crt_frame
67{
68 /* Keep these 8-bit fields at the front of the structure to allow them to
69 * be fetched in a single instruction on Thumb2; putting them at the back
70 * requires an intermediate address calculation. */
71
72 uint8_t version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
73 uint8_t ca_istrue; /**< Optional Basic Constraint extension value:
74 * 1 if this certificate belongs to a CA, 0 otherwise. */
75 uint8_t max_pathlen; /**< Optional Basic Constraint extension value:
76 * The maximum path length to the root certificate.
77 * Path length is 1 higher than RFC 5280 'meaning', so 1+ */
78 uint8_t ns_cert_type; /**< Optional Netscape certificate type extension value:
79 * See the values in x509.h */
80
Hanno Becker21f55672019-02-15 15:27:59 +000081 mbedtls_md_type_t sig_md; /**< The hash algorithm used to hash CRT before signing. */
82 mbedtls_pk_type_t sig_pk; /**< The signature algorithm used to sign the CRT hash. */
83
Hanno Beckerfd5c1852019-05-13 12:52:57 +010084 uint16_t key_usage; /**< Optional key usage extension value: See the values in x509.h */
85 uint32_t ext_types; /**< Bitfield indicating which extensions are present.
86 * See the values in x509.h. */
87
Hanno Becker843b71a2019-06-25 09:39:21 +010088#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker21f55672019-02-15 15:27:59 +000089 mbedtls_x509_time valid_from; /**< The start time of certificate validity. */
90 mbedtls_x509_time valid_to; /**< The end time of certificate validity. */
Hanno Becker843b71a2019-06-25 09:39:21 +010091#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker21f55672019-02-15 15:27:59 +000092
93 mbedtls_x509_buf_raw raw; /**< The raw certificate data in DER. */
94 mbedtls_x509_buf_raw tbs; /**< The part of the CRT that is [T]o [B]e [S]igned. */
95
96 mbedtls_x509_buf_raw serial; /**< The unique ID for certificate issued by a specific CA. */
97
98 mbedtls_x509_buf_raw pubkey_raw; /**< The raw public key data (DER). */
99
Hanno Beckerd07614c2019-06-25 10:19:58 +0100100#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Hanno Becker21f55672019-02-15 15:27:59 +0000101 mbedtls_x509_buf_raw issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
Hanno Becker21f55672019-02-15 15:27:59 +0000102 mbedtls_x509_buf_raw subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
Hanno Beckerd07614c2019-06-25 10:19:58 +0100103#endif /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
104
105 mbedtls_x509_buf_raw issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
Hanno Becker21f55672019-02-15 15:27:59 +0000106 mbedtls_x509_buf_raw subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
107
108 mbedtls_x509_buf_raw sig; /**< Signature: hash of the tbs part signed with the private key. */
109 mbedtls_x509_buf_raw sig_alg; /**< The signature algorithm used for \p sig. */
110
111 mbedtls_x509_buf_raw v3_ext; /**< The raw data for the extension list in the certificate.
112 * Might be useful for manual inspection of extensions that
113 * Mbed TLS doesn't yet support. */
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300114#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker21f55672019-02-15 15:27:59 +0000115 mbedtls_x509_buf_raw subject_alt_raw; /**< The raw data for the SubjectAlternativeNames extension. */
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300116#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker21f55672019-02-15 15:27:59 +0000117 mbedtls_x509_buf_raw ext_key_usage_raw; /**< The raw data for the ExtendedKeyUsage extension. */
Hanno Becker21f55672019-02-15 15:27:59 +0000118} mbedtls_x509_crt_frame;
119
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120/**
121 * Container for an X.509 certificate. The certificate may be chained.
122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200124{
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000125 int own_buffer; /**< Indicates if \c raw is owned
Hanno Becker180f7bf2019-02-28 13:23:38 +0000126 * by the structure or not. */
127 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
Hanno Becker00d39032019-05-13 12:39:44 +0100128 mbedtls_x509_crt_cache *cache; /**< Internal parsing cache. */
Hanno Becker180f7bf2019-02-28 13:23:38 +0000129
130 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
131
132 /* Legacy fields */
133#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +0200136 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
138 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
141 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
144 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145
Hanno Becker843b71a2019-06-25 09:39:21 +0100146#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
148 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Hanno Becker843b71a2019-06-25 09:39:21 +0100149#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200150
Hanno Becker32c530e2019-02-06 16:13:41 +0000151 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153
Hanno Beckerd07614c2019-06-25 10:19:58 +0100154#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
156 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
Hanno Beckerd07614c2019-06-25 10:19:58 +0100157#endif /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300159#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300161#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200162
163 int ext_types; /**< Bit string containing detected and parsed extensions */
164 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
165 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+ */
166
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100167 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200168
Hanno Becker7ec9c362019-02-21 14:24:05 +0000169 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200170
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +0200171 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
174 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
175 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
176 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 +0000177#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200180
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200181/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200182 * Build flag from an algorithm/curve identifier (pk, md, ecp)
183 * Since 0 is always XXX_NONE, ignore it.
184 */
Hanno Beckerd6028a12018-10-15 12:01:35 +0100185#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200186
187/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200188 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200189 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200190 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200191 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200192typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200193{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200194 uint32_t allowed_mds; /**< MDs for signatures */
195 uint32_t allowed_pks; /**< PK algs for signatures */
196 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
197 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200198}
199mbedtls_x509_crt_profile;
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#define MBEDTLS_X509_CRT_VERSION_1 0
202#define MBEDTLS_X509_CRT_VERSION_2 1
203#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
206#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200207
Andres AGf9113192016-09-02 14:06:04 +0100208#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
209#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
210#endif
211
Hanno Beckere8c52ff2019-08-23 15:23:27 +0100212#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213/**
214 * Container for writing a certificate (CRT)
215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200217{
218 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_mpi serial;
220 mbedtls_pk_context *subject_key;
221 mbedtls_pk_context *issuer_key;
222 mbedtls_asn1_named_data *subject;
223 mbedtls_asn1_named_data *issuer;
224 mbedtls_md_type_t md_alg;
225 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
226 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
227 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200228}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229mbedtls_x509write_cert;
Hanno Beckere8c52ff2019-08-23 15:23:27 +0100230#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200231
Hanno Becker8d6d3202019-08-16 17:18:15 +0100232#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
233
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200234/**
235 * Item in a verification chain: cert and flags for it
236 */
237typedef struct {
238 mbedtls_x509_crt *crt;
239 uint32_t flags;
240} mbedtls_x509_crt_verify_chain_item;
241
242/**
243 * Max size of verification chain: end-entity + intermediates + trusted root
244 */
245#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
246
247/**
248 * Verification chain as built by \c mbedtls_crt_verify_chain()
249 */
250typedef struct
251{
252 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200253 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200254} mbedtls_x509_crt_verify_chain;
255
Hanno Becker8d6d3202019-08-16 17:18:15 +0100256#else /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
257
258typedef struct
259{
260 unsigned len;
261 uint32_t flags;
262} mbedtls_x509_crt_verify_chain;
263
264#endif /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
265
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200266#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
267
268/**
269 * \brief Context for resuming X.509 verify operations
270 */
271typedef struct
272{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200273 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200274 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200275
276 /* for find_parent_in() */
277 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
Hanno Becker6f61b7b2019-06-10 11:12:33 +0100278
Hanno Beckeradc282a2019-08-16 17:14:25 +0100279 /* current child CRT */
280 mbedtls_x509_crt *cur_crt;
281
Hanno Becker6f61b7b2019-06-10 11:12:33 +0100282#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200283 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200284 int fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +0100285#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200286
287 /* for find_parent() */
288 int parent_is_trusted; /* -1 if find_parent is not in progress */
289
290 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200291 enum {
292 x509_crt_rs_none,
293 x509_crt_rs_find_parent,
294 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200295 int self_cnt;
296 mbedtls_x509_crt_verify_chain ver_chain;
297
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200298} mbedtls_x509_crt_restart_ctx;
299
300#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
301
302/* Now we can declare functions that take a pointer to that */
303typedef void mbedtls_x509_crt_restart_ctx;
304
305#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200308/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200309 * Default security profile. Should provide a good balance between security
310 * and compatibility with current deployments.
311 */
312extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
313
314/**
315 * Expected next default profile. Recommended for new deployments.
316 * Currently targets a 128-bit security level, except for RSA-2048.
317 */
318extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
319
320/**
321 * NSA Suite B profile.
322 */
323extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
324
325/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200326 * \brief Parse a single DER formatted certificate and add it
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000327 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000329 * \param chain The pointer to the start of the CRT chain to attach to.
330 * When parsing the first CRT in a chain, this should point
331 * to an instance of ::mbedtls_x509_crt initialized through
332 * mbedtls_x509_crt_init().
333 * \param buf The buffer holding the DER encoded certificate.
334 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000336 * \note This function makes an internal copy of the CRT buffer
337 * \p buf. In particular, \p buf may be destroyed or reused
338 * after this call returns. To avoid duplicating the CRT
339 * buffer (at the cost of stricter lifetime constraints),
340 * use mbedtls_x509_crt_parse_der_nocopy() instead.
341 *
342 * \return \c 0 if successful.
343 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000345int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
346 const unsigned char *buf,
347 size_t buflen );
348
349/**
350 * \brief Parse a single DER formatted certificate and add it
351 * to the end of the provided chained list. This is a
352 * variant of mbedtls_x509_crt_parse_der() which takes
353 * temporary ownership of the CRT buffer until the CRT
354 * is destroyed.
355 *
356 * \param chain The pointer to the start of the CRT chain to attach to.
357 * When parsing the first CRT in a chain, this should point
358 * to an instance of ::mbedtls_x509_crt initialized through
359 * mbedtls_x509_crt_init().
360 * \param buf The address of the readable buffer holding the DER encoded
361 * certificate to use. On success, this buffer must be
362 * retained and not be changed for the liftetime of the
363 * CRT chain \p chain, that is, until \p chain is destroyed
364 * through a call to mbedtls_x509_crt_free().
365 * \param buflen The size in Bytes of \p buf.
366 *
367 * \note This call is functionally equivalent to
368 * mbedtls_x509_crt_parse_der(), but it avoids creating a
369 * copy of the input buffer at the cost of stronger lifetime
370 * constraints. This is useful in constrained environments
371 * where duplication of the CRT cannot be tolerated.
372 *
373 * \return \c 0 if successful.
374 * \return A negative error code on failure.
375 */
376int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
377 const unsigned char *buf,
378 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379
380/**
Hanno Becker89a91122018-08-23 16:14:00 +0100381 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100382 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100384 * For CRTs in PEM encoding, the function parses permissively:
385 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100386 * returns the number of certificates for which parsing failed
387 * (hence \c 0 if all certificates were parsed successfully).
388 * If no certificate could be parsed, the function returns
389 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100391 * PEM encoded certificates may be interleaved by other data
392 * such as human readable descriptions of their content, as
393 * long as the certificates are enclosed in the PEM specific
394 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
395 *
396 * \param chain The chain to which to add the parsed certificates.
397 * \param buf The buffer holding the certificate data in PEM or DER format.
398 * For certificates in PEM encoding, this may be a concatenation
399 * of multiple certificates; for DER encoding, the buffer must
400 * comprise exactly one certificate.
401 * \param buflen The size of \p buf, including the terminating \c NULL byte
402 * in case of PEM encoded data.
403 *
404 * \return \c 0 if all certificates were parsed successfully.
405 * \return The (positive) number of certificates that couldn't
406 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100407 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100408 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413/**
414 * \brief Load one or more certificates and add them
415 * to the chained list. Parses permissively. If some
416 * certificates can be parsed, the result is the number
417 * of failed certificates it encountered. If none complete
418 * correctly, the first error is returned.
419 *
420 * \param chain points to the start of the chain
421 * \param path filename to read the certificates from
422 *
423 * \return 0 if all certificates parsed successfully, a positive number
424 * if partly successful or a specific X509 or PEM error code
425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427
428/**
429 * \brief Load one or more certificate files from a path and add them
430 * to the chained list. Parses permissively. If some
431 * certificates can be parsed, the result is the number
432 * of failed certificates it encountered. If none complete
433 * correctly, the first error is returned.
434 *
435 * \param chain points to the start of the chain
436 * \param path directory / folder to read the certificate files from
437 *
438 * \return 0 if all certificates parsed successfully, a positive number
439 * if partly successful or a specific X509 or PEM error code
440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
442#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
Hanno Becker02a21932019-06-10 15:08:43 +0100444#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445/**
446 * \brief Returns an informational string about the
447 * certificate.
448 *
449 * \param buf Buffer to write to
450 * \param size Maximum size of buffer
451 * \param prefix A line prefix
452 * \param crt The X509 certificate to represent
453 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200454 * \return The length of the string written (not including the
455 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
458 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459
460/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100461 * \brief Returns an informational string about the
462 * verification status of a certificate.
463 *
464 * \param buf Buffer to write to
465 * \param size Maximum size of buffer
466 * \param prefix A line prefix
467 * \param flags Verification flags created by mbedtls_x509_crt_verify()
468 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200469 * \return The length of the string written (not including the
470 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100471 */
472int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200473 uint32_t flags );
Hanno Beckerc6043f22019-06-14 17:21:24 +0100474#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200475
476/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477 * \brief Verify the certificate signature
478 *
479 * The verify callback is a user-supplied callback that
480 * can clear / modify / add flags for a certificate. If set,
481 * the verification callback is called for each
482 * certificate in the chain (from the trust-ca down to the
483 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 * int *flags). With the flags representing current flags for
486 * that specific certificate and the certificate depth from
487 * the bottom (Peer cert depth = 0).
488 *
489 * All flags left after returning from the callback
490 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200491 * return 0 for anything (including invalid certificates)
492 * other than fatal error, as a non-zero return code
493 * immediately aborts the verification process. For fatal
494 * errors, a specific error code should be used (different
495 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
496 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
497 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100499 * \note In case verification failed, the results can be displayed
500 * using \c mbedtls_x509_crt_verify_info()
501 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200502 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
503 * default security profile.
504 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100505 * \note It is your responsibility to provide up-to-date CRLs for
506 * all trusted CAs. If no CRL is provided for the CA that was
507 * used to sign the certificate, CRL verification is skipped
508 * silently, that is *without* setting any flag.
509 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200510 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200511 * (1) those of trusted root CAs, so that certificates
512 * chaining up to those CAs will be trusted, and (2)
513 * self-signed end-entity certificates to be trusted (for
514 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200515 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200516 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100517 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200518 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100519 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520 * \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 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200526 * \return 0 (and flags set to 0) if the chain was verified and valid,
527 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
528 * but found to be invalid, in which case *flags will have one
529 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
530 * flags set, or another error (and flags set to 0xffffffff)
531 * in case of a fatal error encountered during the
532 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100535 mbedtls_x509_crt *trust_ca,
536 mbedtls_x509_crl *ca_crl,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300537#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION) || defined(DOXYGEN_ONLY)
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100538 const char *cn,
Hanno Beckercd839c92019-08-29 11:11:00 +0100539#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION || DOXYGEN_ONLY */
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100540 uint32_t *flags
Hanno Beckercd839c92019-08-29 11:11:00 +0100541#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK) || defined(DOXYGEN_ONLY)
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100542 , int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
543 void *p_vrfy
Hanno Beckercd839c92019-08-29 11:11:00 +0100544#endif /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK || DOXYGEN_ONLY */
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100545 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200547/**
548 * \brief Verify the certificate signature according to profile
549 *
550 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
551 * security profile.
552 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200553 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200554 * for ECDSA) apply to all certificates: trusted root,
555 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200556 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100557 * \param crt a certificate (chain) to be verified
558 * \param trust_ca the list of trusted CAs
559 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200560 * \param profile security profile for verification
561 * \param cn expected Common Name (can be set to
562 * NULL if the CN must not be verified)
563 * \param flags result of the verification
564 * \param f_vrfy verification function
565 * \param p_vrfy verification parameter
566 *
567 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
568 * in which case *flags will have one or more
569 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
570 * set,
571 * or another error in case of a fatal error encountered
572 * during the verification process.
573 */
574int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
575 mbedtls_x509_crt *trust_ca,
576 mbedtls_x509_crl *ca_crl,
577 const mbedtls_x509_crt_profile *profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300578#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION) || defined(DOXYGEN_ONLY)
579 const char *cn,
Hanno Beckercd839c92019-08-29 11:11:00 +0100580#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION || DOXYGEN_ONLY */
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100581 uint32_t *flags
Hanno Beckercd839c92019-08-29 11:11:00 +0100582#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK) || defined(DOXYGEN_ONLY)
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100583 , int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
584 void *p_vrfy
Hanno Beckercd839c92019-08-29 11:11:00 +0100585#endif /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK || DOXYGEN_ONLY */
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100586 );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200587
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200588/**
589 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
590 *
591 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
592 * but can return early and restart according to the limit
593 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
594 *
595 * \param crt a certificate (chain) to be verified
596 * \param trust_ca the list of trusted CAs
597 * \param ca_crl the list of CRLs for trusted CAs
598 * \param profile security profile for verification
599 * \param cn expected Common Name (can be set to
600 * NULL if the CN must not be verified)
601 * \param flags result of the verification
602 * \param f_vrfy verification function
603 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200604 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200605 *
606 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200607 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200608 * operations was reached: see \c mbedtls_ecp_set_max_ops().
609 */
610int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
611 mbedtls_x509_crt *trust_ca,
612 mbedtls_x509_crl *ca_crl,
613 const mbedtls_x509_crt_profile *profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300614#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION) || defined(DOXYGEN_ONLY)
615 const char *cn,
Hanno Beckercd839c92019-08-29 11:11:00 +0100616#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION || DOXYGEN_ONLY */
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300617 uint32_t *flags,
Hanno Beckercd839c92019-08-29 11:11:00 +0100618#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK) || defined(DOXYGEN_ONLY)
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100619 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
620 void *p_vrfy,
Hanno Beckercd839c92019-08-29 11:11:00 +0100621#endif /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK || DOXYGEN_ONLY */
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200622 mbedtls_x509_crt_restart_ctx *rs_ctx );
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200625/**
626 * \brief Check usage of certificate against keyUsage extension.
627 *
628 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200629 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
630 * before using the certificate to perform an RSA key
631 * exchange).
632 *
633 * \note Except for decipherOnly and encipherOnly, a bit set in the
634 * usage argument means this bit MUST be set in the
635 * certificate. For decipherOnly and encipherOnly, it means
636 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200637 *
638 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200640 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200641 *
642 * \note You should only call this function on leaf certificates, on
643 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200645 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200646int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
647 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200651/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000652 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200653 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000654 * \param crt Leaf certificate used.
655 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
656 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200658 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000659 * \return 0 if this use of the certificate is allowed,
660 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200661 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000662 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000665 const char *usage_oid,
666 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000667#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200671 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672 *
673 * \param crt a certificate to be verified
674 * \param crl the CRL to verify against
675 *
676 * \return 1 if the certificate is revoked, 0 otherwise
677 *
678 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100679int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
682/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200683 * \brief Initialize a certificate (chain)
684 *
685 * \param crt Certificate chain to initialize
686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200688
689/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690 * \brief Unallocate all certificate data
691 *
692 * \param crt Certificate chain to free
693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200695
696#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
697/**
698 * \brief Initialize a restart context
699 */
700void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
701
702/**
703 * \brief Free the components of a restart context
704 */
705void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
706#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000707
Hanno Becker823efad2019-02-28 13:23:58 +0000708/**
709 * \brief Request CRT frame giving access to basic CRT fields
710 * and raw ASN.1 data of complex fields.
711 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100712 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker823efad2019-02-28 13:23:58 +0000713 * \param dst The address of the destination frame structure.
714 * This need not be initialized.
715 *
716 * \note ::mbedtls_x509_crt_frame does not contain pointers to
717 * dynamically allocated memory, and hence need not be freed.
718 * Users may e.g. allocate an instance of
719 * ::mbedtls_x509_crt_frame on the stack and call this function
720 * on it, in which case no allocation/freeing has to be done.
721 *
Hanno Becker608de6a2019-06-28 10:48:01 +0100722 * \note You may also use mbedtls_x509_crt_frame_acquire() and
723 * mbedtls_x509_crt_frame_release() for copy-less variants
724 * of this function.
725 *
Hanno Becker823efad2019-02-28 13:23:58 +0000726 * \return \c 0 on success. In this case, \p dst is updated
727 * to hold the frame for the given CRT.
728 * \return A negative error code on failure.
729 */
730int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
731 mbedtls_x509_crt_frame *dst );
732
733/**
Hanno Becker9219f9e2019-05-14 12:40:58 +0100734 * \brief Set up a PK context with the public key in a certificate.
Hanno Becker823efad2019-02-28 13:23:58 +0000735 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100736 * \param crt The certificate to use. This must be initialized and set up.
Hanno Becker823efad2019-02-28 13:23:58 +0000737 * \param pk The address of the destination PK context to fill.
738 * This must be initialized via mbedtls_pk_init().
739 *
Hanno Becker608de6a2019-06-28 10:48:01 +0100740 * \note You may also use mbedtls_x509_crt_pk_acquire() and
741 * mbedtls_x509_crt_pk_release() for copy-less variants
742 * of this function.
743 *
Hanno Becker823efad2019-02-28 13:23:58 +0000744 * \return \c 0 on success. In this case, the user takes ownership
745 * of the destination PK context, and is responsible for
746 * calling mbedtls_pk_free() on it once it's no longer needed.
747 * \return A negative error code on failure.
748 */
749int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
750 mbedtls_pk_context *pk );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000751
752/**
Hanno Becker63e69982019-02-26 18:50:49 +0000753 * \brief Request the subject name of a CRT, presented
754 * as a dynamically allocated linked list.
755 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100756 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker63e69982019-02-26 18:50:49 +0000757 * \param subject The address at which to store the address of the
758 * first entry of the generated linked list holding
759 * the subject name.
760 *
Hanno Beckerd92078f2019-06-28 10:48:57 +0100761 * \note Depending on your use case, consider using the raw ASN.1
Hanno Becker63e69982019-02-26 18:50:49 +0000762 * describing the subject name instead of the heap-allocated
763 * linked list generated by this call. The pointers to the
764 * raw ASN.1 data are part of the CRT frame that can be queried
765 * via mbedtls_x509_crt_get_frame(), and they can be traversed
766 * via mbedtls_asn1_traverse_sequence_of().
767 *
768 * \return \c 0 on success. In this case, the user takes ownership
769 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000770 * through a call to mbedtls_x509_name_free() once it's no
771 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000772 * \return A negative error code on failure.
773 */
774int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
775 mbedtls_x509_name **subject );
776
777/**
778 * \brief Request the subject name of a CRT, presented
779 * as a dynamically allocated linked list.
780 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100781 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker63e69982019-02-26 18:50:49 +0000782 * \param issuer The address at which to store the address of the
783 * first entry of the generated linked list holding
784 * the subject name.
785 *
Hanno Beckerd92078f2019-06-28 10:48:57 +0100786 * \note Depending on your use case, consider using the raw ASN.1
787 * describing the issuer name instead of the heap-allocated
Hanno Becker63e69982019-02-26 18:50:49 +0000788 * linked list generated by this call. The pointers to the
789 * raw ASN.1 data are part of the CRT frame that can be queried
790 * via mbedtls_x509_crt_get_frame(), and they can be traversed
791 * via mbedtls_asn1_traverse_sequence_of().
792 *
793 * \return \c 0 on success. In this case, the user takes ownership
794 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000795 * through a call to mbedtls_x509_name_free() once it's no
796 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000797 * \return A negative error code on failure.
798 */
799int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
800 mbedtls_x509_name **issuer );
801
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300802#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker63e69982019-02-26 18:50:49 +0000803/**
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000804 * \brief Request the subject alternative name of a CRT, presented
805 * as a dynamically allocated linked list.
806 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100807 * \param crt The CRT to use. This must be initialized and set up.
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000808 * \param subj_alt The address at which to store the address of the
809 * first component of the subject alternative names list.
810 *
811 * \note Depending in your use case, consider using the raw ASN.1
812 * describing the subject alternative names extension
813 * instead of the heap-allocated linked list generated by this
814 * call. The pointers to the raw ASN.1 data are part of the CRT
815 * frame that can be queried via mbedtls_x509_crt_get_frame(),
816 * and mbedtls_asn1_traverse_sequence_of() can be used to
817 * traverse the list of subject alternative names.
818 *
819 * \return \c 0 on success. In this case, the user takes ownership
820 * of the name context, and is responsible for freeing it
821 * through a call to mbedtls_x509_sequence_free() once it's
822 * no longer needed.
823 * \return A negative error code on failure.
824 */
825int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
826 mbedtls_x509_sequence **subj_alt );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300827#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000828
829/**
830 * \brief Request the ExtendedKeyUsage extension of a CRT,
831 * presented as a dynamically allocated linked list.
832 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100833 * \param crt The CRT to use. This must be initialized and set up.
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000834 * \param ext_key_usage The address at which to store the address of the
835 * first entry of the ExtendedKeyUsage extension.
836 *
837 * \note Depending in your use case, consider using the raw ASN.1
838 * describing the extended key usage extension instead of
839 * the heap-allocated linked list generated by this call.
840 * The pointers to the raw ASN.1 data are part of the CRT
841 * frame that can be queried via mbedtls_x509_crt_get_frame(),
842 * and mbedtls_asn1_traverse_sequence_of() can be used to
843 * traverse the entries in the extended key usage extension.
844 *
845 * \return \c 0 on success. In this case, the user takes ownership
846 * of the name context, and is responsible for freeing it
847 * through a call to mbedtls_x509_sequence_free() once it's
848 * no longer needed.
849 * \return A negative error code on failure.
850 */
851int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
852 mbedtls_x509_sequence **ext_key_usage );
853
854/**
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000855 * \brief Flush internal X.509 CRT parsing cache, if present.
856 *
857 * \param crt The CRT structure whose cache to flush.
858 *
859 * \note Calling this function frequently reduces RAM usage
860 * at the cost of performance.
861 *
862 * \return \c 0 on success.
863 * \return A negative error code on failure.
864 */
865int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt );
866
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100867/**
868 * \brief Request temporary read-access to a certificate frame
869 * for a given certificate.
870 *
871 * Once no longer needed, the frame must be released
872 * through a call to mbedtls_x509_crt_frame_release().
873 *
874 * This is a copy-less version of mbedtls_x509_crt_get_frame().
875 * See there for more information.
876 *
877 * \param crt The certificate to use. This must be initialized and set up.
878 * \param dst The address at which to store the address of a readable
879 * certificate frame for the input certificate \p crt which the
880 * caller can use until calling mbedtls_x509_crt_frame_release().
881 *
882 * \note The certificate frame `**frame_ptr` returned by this function
883 * is owned by the X.509 module and must not be freed or modified
884 * by the caller. The X.509 module guarantees its validity as long
885 * as \p crt is valid and mbedtls_x509_crt_frame_release() hasn't
886 * been issued.
887 *
Hanno Beckerfc99a092019-06-28 14:45:26 +0100888 * \note In a single-threaded application using
889 * MBEDTLS_X509_ALWAYS_FLUSH, nested calls to this function
890 * are not allowed and will fail gracefully with
891 * MBEDTLS_ERR_X509_FATAL_ERROR.
892 *
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100893 * \return \c 0 on success. In this case, `*frame_ptr` is updated
894 * to hold the address of a frame for the given CRT.
895 * \return A negative error code on failure.
896 */
Teppo Järvelinffaba552019-09-03 12:33:16 +0300897int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
898 mbedtls_x509_crt_frame const **dst );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000899
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100900/**
901 * \brief Release access to a certificate frame acquired
902 * through a prior call to mbedtls_x509_crt_frame_acquire().
903 *
904 * \param crt The certificate for which a certificate frame has
905 * previously been acquired.
906 */
Teppo Järvelinffaba552019-09-03 12:33:16 +0300907int mbedtls_x509_crt_frame_release( mbedtls_x509_crt const *crt );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000908
Hanno Becker4b70e122019-05-20 16:51:01 +0100909/**
910 * \brief Request temporary access to a public key context
911 * for a given certificate.
912 *
913 * Once no longer needed, the frame must be released
914 * through a call to mbedtls_x509_crt_pk_release().
915 *
916 * This is a copy-less version of mbedtls_x509_crt_get_pk().
917 * See there for more information.
918 *
919 * \param crt The certificate to use. This must be initialized and set up.
920 * \param dst The address at which to store the address of a public key
921 * context for the public key in the input certificate \p crt.
922 *
923 * \warning The public key context `**pk_ptr` returned by this function
924 * is owned by the X.509 module and must be used by the caller
925 * in a thread-safe way. In particular, the caller must only
926 * use the context with functions which are `const` on the input
927 * context, or those which are known to be thread-safe. The latter
Hanno Beckerb653aa32019-06-28 10:53:55 +0100928 * for example includes mbedtls_pk_verify() for ECC or RSA public
929 * key contexts.
Hanno Becker4b70e122019-05-20 16:51:01 +0100930 *
Hanno Beckerfc99a092019-06-28 14:45:26 +0100931 * \note In a single-threaded application using
932 * MBEDTLS_X509_ALWAYS_FLUSH, nested calls to this function
933 * are not allowed and will fail gracefully with
934 * MBEDTLS_ERR_X509_FATAL_ERROR.
935 *
Hanno Becker4b70e122019-05-20 16:51:01 +0100936 * \return \c 0 on success. In this case, `*pk_ptr` is updated
937 * to hold the address of a public key context for the given
938 * certificate.
939 * \return A negative error code on failure.
940 */
Teppo Järvelinffaba552019-09-03 12:33:16 +0300941int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt const *crt,
942 mbedtls_pk_context **dst );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000943
Hanno Becker4b70e122019-05-20 16:51:01 +0100944/**
945 * \brief Release access to a public key context acquired
946 * through a prior call to mbedtls_x509_crt_frame_acquire().
947 *
948 * \param crt The certificate for which a certificate frame has
949 * previously been acquired.
950 */
Teppo Järvelinffaba552019-09-03 12:33:16 +0300951int mbedtls_x509_crt_pk_release( mbedtls_x509_crt const *crt );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
955/* \} name */
956/* \} addtogroup x509_module */
957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959/**
960 * \brief Initialize a CRT writing context
961 *
962 * \param ctx CRT context to initialize
963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965
966/**
967 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969 *
970 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
972 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975
976/**
977 * \brief Set the serial number for a Certificate.
978 *
979 * \param ctx CRT context to use
980 * \param serial serial number to set
981 *
982 * \return 0 if successful
983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985
986/**
987 * \brief Set the validity period for a Certificate
988 * Timestamps should be in string format for UTC timezone
989 * i.e. "YYYYMMDDhhmmss"
990 * e.g. "20131231235959" for December 31st 2013
991 * at 23:59:59
992 *
993 * \param ctx CRT context to use
994 * \param not_before not_before timestamp
995 * \param not_after not_after timestamp
996 *
997 * \return 0 if timestamp was parsed successfully, or
998 * a specific error code
999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +01001001 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002
1003/**
1004 * \brief Set the issuer name for a Certificate
1005 * Issuer names should contain a comma-separated list
1006 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001007 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008 *
1009 * \param ctx CRT context to use
1010 * \param issuer_name issuer name to set
1011 *
1012 * \return 0 if issuer name was parsed successfully, or
1013 * a specific error code
1014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001016 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017
1018/**
1019 * \brief Set the subject name for a Certificate
1020 * Subject names should contain a comma-separated list
1021 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001022 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 *
1024 * \param ctx CRT context to use
1025 * \param subject_name subject name to set
1026 *
1027 * \return 0 if subject name was parsed successfully, or
1028 * a specific error code
1029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001031 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032
1033/**
1034 * \brief Set the subject public key for the certificate
1035 *
1036 * \param ctx CRT context to use
1037 * \param key public key to include
1038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040
1041/**
1042 * \brief Set the issuer key used for signing the certificate
1043 *
1044 * \param ctx CRT context to use
1045 * \param key private key to sign with
1046 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048
1049/**
1050 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001052 *
1053 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001054 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001057
1058/**
1059 * \brief Generic function to add to or replace an extension in the
1060 * CRT
1061 *
1062 * \param ctx CRT context to use
1063 * \param oid OID of the extension
1064 * \param oid_len length of the OID
1065 * \param critical if the extension is critical (per the RFC's definition)
1066 * \param val value of the extension OCTET STRING
1067 * \param val_len length of the value data
1068 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001069 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072 const char *oid, size_t oid_len,
1073 int critical,
1074 const unsigned char *val, size_t val_len );
1075
1076/**
1077 * \brief Set the basicConstraints extension for a CRT
1078 *
1079 * \param ctx CRT context to use
1080 * \param is_ca is this a CA certificate
1081 * \param max_pathlen maximum length of certificate chains below this
1082 * certificate (only for CA certificates, -1 is
1083 * inlimited)
1084 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001085 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088 int is_ca, int max_pathlen );
1089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091/**
1092 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094 * called before
1095 *
1096 * \param ctx CRT context to use
1097 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001098 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101
1102/**
1103 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105 * called before
1106 *
1107 * \param ctx CRT context to use
1108 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001109 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
1112#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113
1114/**
1115 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117 *
1118 * \param ctx CRT context to use
1119 * \param key_usage key usage flags to set
1120 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001121 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +02001123int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
1124 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001125
1126/**
1127 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129 *
1130 * \param ctx CRT context to use
1131 * \param ns_cert_type Netscape Cert Type flags to set
1132 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001133 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001136 unsigned char ns_cert_type );
1137
1138/**
1139 * \brief Free the contents of a CRT write context
1140 *
1141 * \param ctx CRT context to free
1142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001144
1145/**
1146 * \brief Write a built up certificate to a X509 DER structure
1147 * Note: data is written at the end of the buffer! Use the
1148 * return value to determine where you should start
1149 * using the buffer
1150 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001151 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152 * \param buf buffer to write to
1153 * \param size size of the buffer
1154 * \param f_rng RNG function (for signature, see note)
1155 * \param p_rng RNG parameter
1156 *
1157 * \return length of data written if successful, or a specific
1158 * error code
1159 *
1160 * \note f_rng may be NULL if RSA is used for signature and the
1161 * signature is made offline (otherwise f_rng is desirable
1162 * for countermeasures against timing attacks).
1163 * ECDSA signatures always require a non-NULL f_rng.
1164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166 int (*f_rng)(void *, unsigned char *, size_t),
1167 void *p_rng );
1168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170/**
1171 * \brief Write a built up certificate to a X509 PEM string
1172 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001173 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 * \param buf buffer to write to
1175 * \param size size of the buffer
1176 * \param f_rng RNG function (for signature, see note)
1177 * \param p_rng RNG parameter
1178 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001179 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 *
1181 * \note f_rng may be NULL if RSA is used for signature and the
1182 * signature is made offline (otherwise f_rng is desirable
1183 * for countermeasures against timing attacks).
1184 * ECDSA signatures always require a non-NULL f_rng.
1185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001187 int (*f_rng)(void *, unsigned char *, size_t),
1188 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#endif /* MBEDTLS_PEM_WRITE_C */
1190#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191
1192#ifdef __cplusplus
1193}
1194#endif
1195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196#endif /* mbedtls_x509_crt.h */