blob: bea165455e2586d037576b3dae99ca30ed0e4243 [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
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200212/**
213 * Container for writing a certificate (CRT)
214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216{
217 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_mpi serial;
219 mbedtls_pk_context *subject_key;
220 mbedtls_pk_context *issuer_key;
221 mbedtls_asn1_named_data *subject;
222 mbedtls_asn1_named_data *issuer;
223 mbedtls_md_type_t md_alg;
224 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
225 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
226 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200227}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200230/**
231 * Item in a verification chain: cert and flags for it
232 */
233typedef struct {
234 mbedtls_x509_crt *crt;
235 uint32_t flags;
236} mbedtls_x509_crt_verify_chain_item;
237
238/**
239 * Max size of verification chain: end-entity + intermediates + trusted root
240 */
241#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
242
243/**
244 * Verification chain as built by \c mbedtls_crt_verify_chain()
245 */
246typedef struct
247{
248 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200249 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200250} mbedtls_x509_crt_verify_chain;
251
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200252#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
253
254/**
255 * \brief Context for resuming X.509 verify operations
256 */
257typedef struct
258{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200259 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200260 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200261
262 /* for find_parent_in() */
263 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
Hanno Becker6f61b7b2019-06-10 11:12:33 +0100264
265#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200266 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200267 int fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +0100268#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200269
270 /* for find_parent() */
271 int parent_is_trusted; /* -1 if find_parent is not in progress */
272
273 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200274 enum {
275 x509_crt_rs_none,
276 x509_crt_rs_find_parent,
277 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200278 int self_cnt;
279 mbedtls_x509_crt_verify_chain ver_chain;
280
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200281} mbedtls_x509_crt_restart_ctx;
282
283#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
284
285/* Now we can declare functions that take a pointer to that */
286typedef void mbedtls_x509_crt_restart_ctx;
287
288#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200292 * Default security profile. Should provide a good balance between security
293 * and compatibility with current deployments.
294 */
295extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
296
297/**
298 * Expected next default profile. Recommended for new deployments.
299 * Currently targets a 128-bit security level, except for RSA-2048.
300 */
301extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
302
303/**
304 * NSA Suite B profile.
305 */
306extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
307
308/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200309 * \brief Parse a single DER formatted certificate and add it
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000310 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000312 * \param chain The pointer to the start of the CRT chain to attach to.
313 * When parsing the first CRT in a chain, this should point
314 * to an instance of ::mbedtls_x509_crt initialized through
315 * mbedtls_x509_crt_init().
316 * \param buf The buffer holding the DER encoded certificate.
317 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200318 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000319 * \note This function makes an internal copy of the CRT buffer
320 * \p buf. In particular, \p buf may be destroyed or reused
321 * after this call returns. To avoid duplicating the CRT
322 * buffer (at the cost of stricter lifetime constraints),
323 * use mbedtls_x509_crt_parse_der_nocopy() instead.
324 *
325 * \return \c 0 if successful.
326 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000328int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
329 const unsigned char *buf,
330 size_t buflen );
331
332/**
333 * \brief Parse a single DER formatted certificate and add it
334 * to the end of the provided chained list. This is a
335 * variant of mbedtls_x509_crt_parse_der() which takes
336 * temporary ownership of the CRT buffer until the CRT
337 * is destroyed.
338 *
339 * \param chain The pointer to the start of the CRT chain to attach to.
340 * When parsing the first CRT in a chain, this should point
341 * to an instance of ::mbedtls_x509_crt initialized through
342 * mbedtls_x509_crt_init().
343 * \param buf The address of the readable buffer holding the DER encoded
344 * certificate to use. On success, this buffer must be
345 * retained and not be changed for the liftetime of the
346 * CRT chain \p chain, that is, until \p chain is destroyed
347 * through a call to mbedtls_x509_crt_free().
348 * \param buflen The size in Bytes of \p buf.
349 *
350 * \note This call is functionally equivalent to
351 * mbedtls_x509_crt_parse_der(), but it avoids creating a
352 * copy of the input buffer at the cost of stronger lifetime
353 * constraints. This is useful in constrained environments
354 * where duplication of the CRT cannot be tolerated.
355 *
356 * \return \c 0 if successful.
357 * \return A negative error code on failure.
358 */
359int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
360 const unsigned char *buf,
361 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362
363/**
Hanno Becker89a91122018-08-23 16:14:00 +0100364 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100365 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100367 * For CRTs in PEM encoding, the function parses permissively:
368 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100369 * returns the number of certificates for which parsing failed
370 * (hence \c 0 if all certificates were parsed successfully).
371 * If no certificate could be parsed, the function returns
372 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200373 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100374 * PEM encoded certificates may be interleaved by other data
375 * such as human readable descriptions of their content, as
376 * long as the certificates are enclosed in the PEM specific
377 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
378 *
379 * \param chain The chain to which to add the parsed certificates.
380 * \param buf The buffer holding the certificate data in PEM or DER format.
381 * For certificates in PEM encoding, this may be a concatenation
382 * of multiple certificates; for DER encoding, the buffer must
383 * comprise exactly one certificate.
384 * \param buflen The size of \p buf, including the terminating \c NULL byte
385 * in case of PEM encoded data.
386 *
387 * \return \c 0 if all certificates were parsed successfully.
388 * \return The (positive) number of certificates that couldn't
389 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100390 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100391 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396/**
397 * \brief Load one or more certificates and add them
398 * to the chained list. Parses permissively. If some
399 * certificates can be parsed, the result is the number
400 * of failed certificates it encountered. If none complete
401 * correctly, the first error is returned.
402 *
403 * \param chain points to the start of the chain
404 * \param path filename to read the certificates from
405 *
406 * \return 0 if all certificates parsed successfully, a positive number
407 * if partly successful or a specific X509 or PEM error code
408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410
411/**
412 * \brief Load one or more certificate files from a path and add them
413 * to the chained list. Parses permissively. If some
414 * certificates can be parsed, the result is the number
415 * of failed certificates it encountered. If none complete
416 * correctly, the first error is returned.
417 *
418 * \param chain points to the start of the chain
419 * \param path directory / folder to read the certificate files from
420 *
421 * \return 0 if all certificates parsed successfully, a positive number
422 * if partly successful or a specific X509 or PEM error code
423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
425#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426
Hanno Becker02a21932019-06-10 15:08:43 +0100427#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428/**
429 * \brief Returns an informational string about the
430 * certificate.
431 *
432 * \param buf Buffer to write to
433 * \param size Maximum size of buffer
434 * \param prefix A line prefix
435 * \param crt The X509 certificate to represent
436 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200437 * \return The length of the string written (not including the
438 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
441 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442
443/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100444 * \brief Returns an informational string about the
445 * verification status of a certificate.
446 *
447 * \param buf Buffer to write to
448 * \param size Maximum size of buffer
449 * \param prefix A line prefix
450 * \param flags Verification flags created by mbedtls_x509_crt_verify()
451 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200452 * \return The length of the string written (not including the
453 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100454 */
455int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200456 uint32_t flags );
Hanno Beckerc6043f22019-06-14 17:21:24 +0100457#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200458
459/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460 * \brief Verify the certificate signature
461 *
462 * The verify callback is a user-supplied callback that
463 * can clear / modify / add flags for a certificate. If set,
464 * the verification callback is called for each
465 * certificate in the chain (from the trust-ca down to the
466 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468 * int *flags). With the flags representing current flags for
469 * that specific certificate and the certificate depth from
470 * the bottom (Peer cert depth = 0).
471 *
472 * All flags left after returning from the callback
473 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200474 * return 0 for anything (including invalid certificates)
475 * other than fatal error, as a non-zero return code
476 * immediately aborts the verification process. For fatal
477 * errors, a specific error code should be used (different
478 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
479 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
480 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100482 * \note In case verification failed, the results can be displayed
483 * using \c mbedtls_x509_crt_verify_info()
484 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200485 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
486 * default security profile.
487 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100488 * \note It is your responsibility to provide up-to-date CRLs for
489 * all trusted CAs. If no CRL is provided for the CA that was
490 * used to sign the certificate, CRL verification is skipped
491 * silently, that is *without* setting any flag.
492 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200493 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200494 * (1) those of trusted root CAs, so that certificates
495 * chaining up to those CAs will be trusted, and (2)
496 * self-signed end-entity certificates to be trusted (for
497 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200498 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200499 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100500 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200501 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100502 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503 * \param cn expected Common Name (can be set to
504 * NULL if the CN must not be verified)
505 * \param flags result of the verification
506 * \param f_vrfy verification function
507 * \param p_vrfy verification parameter
508 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200509 * \return 0 (and flags set to 0) if the chain was verified and valid,
510 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
511 * but found to be invalid, in which case *flags will have one
512 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
513 * flags set, or another error (and flags set to 0xffffffff)
514 * in case of a fatal error encountered during the
515 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
518 mbedtls_x509_crt *trust_ca,
519 mbedtls_x509_crl *ca_crl,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300520#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION) || defined(DOXYGEN_ONLY)
521 const char *cn,
522#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION || defined(DOXYGEN_ONLY) */
523 uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200524 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200525 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200527/**
528 * \brief Verify the certificate signature according to profile
529 *
530 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
531 * security profile.
532 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200533 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200534 * for ECDSA) apply to all certificates: trusted root,
535 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200536 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100537 * \param crt a certificate (chain) to be verified
538 * \param trust_ca the list of trusted CAs
539 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200540 * \param profile security profile for verification
541 * \param cn expected Common Name (can be set to
542 * NULL if the CN must not be verified)
543 * \param flags result of the verification
544 * \param f_vrfy verification function
545 * \param p_vrfy verification parameter
546 *
547 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
548 * in which case *flags will have one or more
549 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
550 * set,
551 * or another error in case of a fatal error encountered
552 * during the verification process.
553 */
554int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
555 mbedtls_x509_crt *trust_ca,
556 mbedtls_x509_crl *ca_crl,
557 const mbedtls_x509_crt_profile *profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300558#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION) || defined(DOXYGEN_ONLY)
559 const char *cn,
560#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION || defined(DOXYGEN_ONLY) */
561 uint32_t *flags,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200562 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
563 void *p_vrfy );
564
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200565/**
566 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
567 *
568 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
569 * but can return early and restart according to the limit
570 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
571 *
572 * \param crt a certificate (chain) to be verified
573 * \param trust_ca the list of trusted CAs
574 * \param ca_crl the list of CRLs for trusted CAs
575 * \param profile security profile for verification
576 * \param cn expected Common Name (can be set to
577 * NULL if the CN must not be verified)
578 * \param flags result of the verification
579 * \param f_vrfy verification function
580 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200581 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200582 *
583 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200584 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200585 * operations was reached: see \c mbedtls_ecp_set_max_ops().
586 */
587int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
588 mbedtls_x509_crt *trust_ca,
589 mbedtls_x509_crl *ca_crl,
590 const mbedtls_x509_crt_profile *profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300591#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION) || defined(DOXYGEN_ONLY)
592 const char *cn,
593#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION || defined(DOXYGEN_ONLY) */
594 uint32_t *flags,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200595 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
596 void *p_vrfy,
597 mbedtls_x509_crt_restart_ctx *rs_ctx );
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200600/**
601 * \brief Check usage of certificate against keyUsage extension.
602 *
603 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200604 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
605 * before using the certificate to perform an RSA key
606 * exchange).
607 *
608 * \note Except for decipherOnly and encipherOnly, a bit set in the
609 * usage argument means this bit MUST be set in the
610 * certificate. For decipherOnly and encipherOnly, it means
611 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200612 *
613 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200615 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200616 *
617 * \note You should only call this function on leaf certificates, on
618 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200620 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200621int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
622 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200626/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000627 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200628 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000629 * \param crt Leaf certificate used.
630 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
631 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200633 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000634 * \return 0 if this use of the certificate is allowed,
635 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200636 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000637 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000640 const char *usage_oid,
641 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000642#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200646 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647 *
648 * \param crt a certificate to be verified
649 * \param crl the CRL to verify against
650 *
651 * \return 1 if the certificate is revoked, 0 otherwise
652 *
653 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100654int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656
657/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200658 * \brief Initialize a certificate (chain)
659 *
660 * \param crt Certificate chain to initialize
661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200663
664/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665 * \brief Unallocate all certificate data
666 *
667 * \param crt Certificate chain to free
668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200670
671#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
672/**
673 * \brief Initialize a restart context
674 */
675void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
676
677/**
678 * \brief Free the components of a restart context
679 */
680void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
681#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000682
Hanno Becker823efad2019-02-28 13:23:58 +0000683/**
684 * \brief Request CRT frame giving access to basic CRT fields
685 * and raw ASN.1 data of complex fields.
686 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100687 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker823efad2019-02-28 13:23:58 +0000688 * \param dst The address of the destination frame structure.
689 * This need not be initialized.
690 *
691 * \note ::mbedtls_x509_crt_frame does not contain pointers to
692 * dynamically allocated memory, and hence need not be freed.
693 * Users may e.g. allocate an instance of
694 * ::mbedtls_x509_crt_frame on the stack and call this function
695 * on it, in which case no allocation/freeing has to be done.
696 *
Hanno Becker608de6a2019-06-28 10:48:01 +0100697 * \note You may also use mbedtls_x509_crt_frame_acquire() and
698 * mbedtls_x509_crt_frame_release() for copy-less variants
699 * of this function.
700 *
Hanno Becker823efad2019-02-28 13:23:58 +0000701 * \return \c 0 on success. In this case, \p dst is updated
702 * to hold the frame for the given CRT.
703 * \return A negative error code on failure.
704 */
705int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
706 mbedtls_x509_crt_frame *dst );
707
708/**
Hanno Becker9219f9e2019-05-14 12:40:58 +0100709 * \brief Set up a PK context with the public key in a certificate.
Hanno Becker823efad2019-02-28 13:23:58 +0000710 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100711 * \param crt The certificate to use. This must be initialized and set up.
Hanno Becker823efad2019-02-28 13:23:58 +0000712 * \param pk The address of the destination PK context to fill.
713 * This must be initialized via mbedtls_pk_init().
714 *
Hanno Becker608de6a2019-06-28 10:48:01 +0100715 * \note You may also use mbedtls_x509_crt_pk_acquire() and
716 * mbedtls_x509_crt_pk_release() for copy-less variants
717 * of this function.
718 *
Hanno Becker823efad2019-02-28 13:23:58 +0000719 * \return \c 0 on success. In this case, the user takes ownership
720 * of the destination PK context, and is responsible for
721 * calling mbedtls_pk_free() on it once it's no longer needed.
722 * \return A negative error code on failure.
723 */
724int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
725 mbedtls_pk_context *pk );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000726
727/**
Hanno Becker63e69982019-02-26 18:50:49 +0000728 * \brief Request the subject name of a CRT, presented
729 * as a dynamically allocated linked list.
730 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100731 * \param crt The CRT to use. This must be initialized and set up.
Hanno Becker63e69982019-02-26 18:50:49 +0000732 * \param subject The address at which to store the address of the
733 * first entry of the generated linked list holding
734 * the subject name.
735 *
Hanno Beckerd92078f2019-06-28 10:48:57 +0100736 * \note Depending on your use case, consider using the raw ASN.1
Hanno Becker63e69982019-02-26 18:50:49 +0000737 * describing the subject name instead of the heap-allocated
738 * linked list generated by this call. The pointers to the
739 * raw ASN.1 data are part of the CRT frame that can be queried
740 * via mbedtls_x509_crt_get_frame(), and they can be traversed
741 * via mbedtls_asn1_traverse_sequence_of().
742 *
743 * \return \c 0 on success. In this case, the user takes ownership
744 * of the name context, and is responsible for freeing it
Hanno Becker2bcc7642019-02-26 19:01:00 +0000745 * through a call to mbedtls_x509_name_free() once it's no
746 * longer needed.
Hanno Becker63e69982019-02-26 18:50:49 +0000747 * \return A negative error code on failure.
748 */
749int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
750 mbedtls_x509_name **subject );
751
752/**
753 * \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 issuer 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
762 * describing the issuer name instead of the heap-allocated
Hanno Becker63e69982019-02-26 18:50:49 +0000763 * 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_issuer( mbedtls_x509_crt const *crt,
775 mbedtls_x509_name **issuer );
776
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300777#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker63e69982019-02-26 18:50:49 +0000778/**
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000779 * \brief Request the subject alternative name of a CRT, presented
780 * as a dynamically allocated linked list.
781 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100782 * \param crt The CRT to use. This must be initialized and set up.
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000783 * \param subj_alt The address at which to store the address of the
784 * first component of the subject alternative names list.
785 *
786 * \note Depending in your use case, consider using the raw ASN.1
787 * describing the subject alternative names extension
788 * instead of the heap-allocated linked list generated by this
789 * call. The pointers to the raw ASN.1 data are part of the CRT
790 * frame that can be queried via mbedtls_x509_crt_get_frame(),
791 * and mbedtls_asn1_traverse_sequence_of() can be used to
792 * traverse the list of subject alternative names.
793 *
794 * \return \c 0 on success. In this case, the user takes ownership
795 * of the name context, and is responsible for freeing it
796 * through a call to mbedtls_x509_sequence_free() once it's
797 * no longer needed.
798 * \return A negative error code on failure.
799 */
800int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
801 mbedtls_x509_sequence **subj_alt );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300802#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000803
804/**
805 * \brief Request the ExtendedKeyUsage extension of a CRT,
806 * presented as a dynamically allocated linked list.
807 *
Hanno Beckerc0dab622019-05-13 13:04:53 +0100808 * \param crt The CRT to use. This must be initialized and set up.
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000809 * \param ext_key_usage The address at which to store the address of the
810 * first entry of the ExtendedKeyUsage extension.
811 *
812 * \note Depending in your use case, consider using the raw ASN.1
813 * describing the extended key usage extension instead of
814 * the heap-allocated linked list generated by this call.
815 * The pointers to the raw ASN.1 data are part of the CRT
816 * frame that can be queried via mbedtls_x509_crt_get_frame(),
817 * and mbedtls_asn1_traverse_sequence_of() can be used to
818 * traverse the entries in the extended key usage extension.
819 *
820 * \return \c 0 on success. In this case, the user takes ownership
821 * of the name context, and is responsible for freeing it
822 * through a call to mbedtls_x509_sequence_free() once it's
823 * no longer needed.
824 * \return A negative error code on failure.
825 */
826int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
827 mbedtls_x509_sequence **ext_key_usage );
828
829/**
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000830 * \brief Flush internal X.509 CRT parsing cache, if present.
831 *
832 * \param crt The CRT structure whose cache to flush.
833 *
834 * \note Calling this function frequently reduces RAM usage
835 * at the cost of performance.
836 *
837 * \return \c 0 on success.
838 * \return A negative error code on failure.
839 */
840int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt );
841
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100842/**
843 * \brief Request temporary read-access to a certificate frame
844 * for a given certificate.
845 *
846 * Once no longer needed, the frame must be released
847 * through a call to mbedtls_x509_crt_frame_release().
848 *
849 * This is a copy-less version of mbedtls_x509_crt_get_frame().
850 * See there for more information.
851 *
852 * \param crt The certificate to use. This must be initialized and set up.
853 * \param dst The address at which to store the address of a readable
854 * certificate frame for the input certificate \p crt which the
855 * caller can use until calling mbedtls_x509_crt_frame_release().
856 *
857 * \note The certificate frame `**frame_ptr` returned by this function
858 * is owned by the X.509 module and must not be freed or modified
859 * by the caller. The X.509 module guarantees its validity as long
860 * as \p crt is valid and mbedtls_x509_crt_frame_release() hasn't
861 * been issued.
862 *
Hanno Beckerfc99a092019-06-28 14:45:26 +0100863 * \note In a single-threaded application using
864 * MBEDTLS_X509_ALWAYS_FLUSH, nested calls to this function
865 * are not allowed and will fail gracefully with
866 * MBEDTLS_ERR_X509_FATAL_ERROR.
867 *
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100868 * \return \c 0 on success. In this case, `*frame_ptr` is updated
869 * to hold the address of a frame for the given CRT.
870 * \return A negative error code on failure.
871 */
Teppo Järvelinffaba552019-09-03 12:33:16 +0300872int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
873 mbedtls_x509_crt_frame const **dst );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000874
Hanno Beckerb8670fc2019-05-20 16:50:45 +0100875/**
876 * \brief Release access to a certificate frame acquired
877 * through a prior call to mbedtls_x509_crt_frame_acquire().
878 *
879 * \param crt The certificate for which a certificate frame has
880 * previously been acquired.
881 */
Teppo Järvelinffaba552019-09-03 12:33:16 +0300882int mbedtls_x509_crt_frame_release( mbedtls_x509_crt const *crt );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000883
Hanno Becker4b70e122019-05-20 16:51:01 +0100884/**
885 * \brief Request temporary access to a public key context
886 * for a given certificate.
887 *
888 * Once no longer needed, the frame must be released
889 * through a call to mbedtls_x509_crt_pk_release().
890 *
891 * This is a copy-less version of mbedtls_x509_crt_get_pk().
892 * See there for more information.
893 *
894 * \param crt The certificate to use. This must be initialized and set up.
895 * \param dst The address at which to store the address of a public key
896 * context for the public key in the input certificate \p crt.
897 *
898 * \warning The public key context `**pk_ptr` returned by this function
899 * is owned by the X.509 module and must be used by the caller
900 * in a thread-safe way. In particular, the caller must only
901 * use the context with functions which are `const` on the input
902 * context, or those which are known to be thread-safe. The latter
Hanno Beckerb653aa32019-06-28 10:53:55 +0100903 * for example includes mbedtls_pk_verify() for ECC or RSA public
904 * key contexts.
Hanno Becker4b70e122019-05-20 16:51:01 +0100905 *
Hanno Beckerfc99a092019-06-28 14:45:26 +0100906 * \note In a single-threaded application using
907 * MBEDTLS_X509_ALWAYS_FLUSH, nested calls to this function
908 * are not allowed and will fail gracefully with
909 * MBEDTLS_ERR_X509_FATAL_ERROR.
910 *
Hanno Becker4b70e122019-05-20 16:51:01 +0100911 * \return \c 0 on success. In this case, `*pk_ptr` is updated
912 * to hold the address of a public key context for the given
913 * certificate.
914 * \return A negative error code on failure.
915 */
Teppo Järvelinffaba552019-09-03 12:33:16 +0300916int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt const *crt,
917 mbedtls_pk_context **dst );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000918
Hanno Becker4b70e122019-05-20 16:51:01 +0100919/**
920 * \brief Release access to a public key context acquired
921 * through a prior call to mbedtls_x509_crt_frame_acquire().
922 *
923 * \param crt The certificate for which a certificate frame has
924 * previously been acquired.
925 */
Teppo Järvelinffaba552019-09-03 12:33:16 +0300926int mbedtls_x509_crt_pk_release( mbedtls_x509_crt const *crt );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929
930/* \} name */
931/* \} addtogroup x509_module */
932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934/**
935 * \brief Initialize a CRT writing context
936 *
937 * \param ctx CRT context to initialize
938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940
941/**
942 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944 *
945 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
947 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950
951/**
952 * \brief Set the serial number for a Certificate.
953 *
954 * \param ctx CRT context to use
955 * \param serial serial number to set
956 *
957 * \return 0 if successful
958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960
961/**
962 * \brief Set the validity period for a Certificate
963 * Timestamps should be in string format for UTC timezone
964 * i.e. "YYYYMMDDhhmmss"
965 * e.g. "20131231235959" for December 31st 2013
966 * at 23:59:59
967 *
968 * \param ctx CRT context to use
969 * \param not_before not_before timestamp
970 * \param not_after not_after timestamp
971 *
972 * \return 0 if timestamp was parsed successfully, or
973 * a specific error code
974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100976 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977
978/**
979 * \brief Set the issuer name for a Certificate
980 * Issuer names should contain a comma-separated list
981 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000982 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983 *
984 * \param ctx CRT context to use
985 * \param issuer_name issuer name to set
986 *
987 * \return 0 if issuer name was parsed successfully, or
988 * a specific error code
989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100991 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992
993/**
994 * \brief Set the subject name for a Certificate
995 * Subject names should contain a comma-separated list
996 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000997 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998 *
999 * \param ctx CRT context to use
1000 * \param subject_name subject name to set
1001 *
1002 * \return 0 if subject name was parsed successfully, or
1003 * a specific error code
1004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001006 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007
1008/**
1009 * \brief Set the subject public key for the certificate
1010 *
1011 * \param ctx CRT context to use
1012 * \param key public key to include
1013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015
1016/**
1017 * \brief Set the issuer key used for signing the certificate
1018 *
1019 * \param ctx CRT context to use
1020 * \param key private key to sign with
1021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023
1024/**
1025 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 *
1028 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001029 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032
1033/**
1034 * \brief Generic function to add to or replace an extension in the
1035 * CRT
1036 *
1037 * \param ctx CRT context to use
1038 * \param oid OID of the extension
1039 * \param oid_len length of the OID
1040 * \param critical if the extension is critical (per the RFC's definition)
1041 * \param val value of the extension OCTET STRING
1042 * \param val_len length of the value data
1043 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001044 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001047 const char *oid, size_t oid_len,
1048 int critical,
1049 const unsigned char *val, size_t val_len );
1050
1051/**
1052 * \brief Set the basicConstraints extension for a CRT
1053 *
1054 * \param ctx CRT context to use
1055 * \param is_ca is this a CA certificate
1056 * \param max_pathlen maximum length of certificate chains below this
1057 * certificate (only for CA certificates, -1 is
1058 * inlimited)
1059 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001060 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063 int is_ca, int max_pathlen );
1064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001066/**
1067 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001069 * called before
1070 *
1071 * \param ctx CRT context to use
1072 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001073 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076
1077/**
1078 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080 * called before
1081 *
1082 * \param ctx CRT context to use
1083 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001084 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
1087#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088
1089/**
1090 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092 *
1093 * \param ctx CRT context to use
1094 * \param key_usage key usage flags to set
1095 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001096 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +02001098int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
1099 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100
1101/**
1102 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104 *
1105 * \param ctx CRT context to use
1106 * \param ns_cert_type Netscape Cert Type flags to set
1107 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001108 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111 unsigned char ns_cert_type );
1112
1113/**
1114 * \brief Free the contents of a CRT write context
1115 *
1116 * \param ctx CRT context to free
1117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119
1120/**
1121 * \brief Write a built up certificate to a X509 DER structure
1122 * Note: data is written at the end of the buffer! Use the
1123 * return value to determine where you should start
1124 * using the buffer
1125 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001126 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127 * \param buf buffer to write to
1128 * \param size size of the buffer
1129 * \param f_rng RNG function (for signature, see note)
1130 * \param p_rng RNG parameter
1131 *
1132 * \return length of data written if successful, or a specific
1133 * error code
1134 *
1135 * \note f_rng may be NULL if RSA is used for signature and the
1136 * signature is made offline (otherwise f_rng is desirable
1137 * for countermeasures against timing attacks).
1138 * ECDSA signatures always require a non-NULL f_rng.
1139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001141 int (*f_rng)(void *, unsigned char *, size_t),
1142 void *p_rng );
1143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001145/**
1146 * \brief Write a built up certificate to a X509 PEM string
1147 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001148 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149 * \param buf buffer to write to
1150 * \param size size of the buffer
1151 * \param f_rng RNG function (for signature, see note)
1152 * \param p_rng RNG parameter
1153 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001154 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155 *
1156 * \note f_rng may be NULL if RSA is used for signature and the
1157 * signature is made offline (otherwise f_rng is desirable
1158 * for countermeasures against timing attacks).
1159 * ECDSA signatures always require a non-NULL f_rng.
1160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162 int (*f_rng)(void *, unsigned char *, size_t),
1163 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164#endif /* MBEDTLS_PEM_WRITE_C */
1165#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166
1167#ifdef __cplusplus
1168}
1169#endif
1170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#endif /* mbedtls_x509_crt.h */