blob: 25e8077433ccdb2991b26ef0605e6f46468e51c0 [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
Hanno Becker21f55672019-02-15 15:27:59 +000050typedef struct mbedtls_x509_crt_frame
51{
52 /* Keep these 8-bit fields at the front of the structure to allow them to
53 * be fetched in a single instruction on Thumb2; putting them at the back
54 * requires an intermediate address calculation. */
55
56 uint8_t version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
57 uint8_t ca_istrue; /**< Optional Basic Constraint extension value:
58 * 1 if this certificate belongs to a CA, 0 otherwise. */
59 uint8_t max_pathlen; /**< Optional Basic Constraint extension value:
60 * The maximum path length to the root certificate.
61 * Path length is 1 higher than RFC 5280 'meaning', so 1+ */
62 uint8_t ns_cert_type; /**< Optional Netscape certificate type extension value:
63 * See the values in x509.h */
64
65 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
66 uint32_t ext_types; /**< Bitfield indicating which extensions are present.
67 * See the values in x509.h. */
68
69 mbedtls_md_type_t sig_md; /**< The hash algorithm used to hash CRT before signing. */
70 mbedtls_pk_type_t sig_pk; /**< The signature algorithm used to sign the CRT hash. */
71
72 mbedtls_x509_time valid_from; /**< The start time of certificate validity. */
73 mbedtls_x509_time valid_to; /**< The end time of certificate validity. */
74
75 mbedtls_x509_buf_raw raw; /**< The raw certificate data in DER. */
76 mbedtls_x509_buf_raw tbs; /**< The part of the CRT that is [T]o [B]e [S]igned. */
77
78 mbedtls_x509_buf_raw serial; /**< The unique ID for certificate issued by a specific CA. */
79
80 mbedtls_x509_buf_raw pubkey_raw; /**< The raw public key data (DER). */
81
82 mbedtls_x509_buf_raw issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
83 mbedtls_x509_buf_raw issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
84
85 mbedtls_x509_buf_raw subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
86 mbedtls_x509_buf_raw subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
87
88 mbedtls_x509_buf_raw sig; /**< Signature: hash of the tbs part signed with the private key. */
89 mbedtls_x509_buf_raw sig_alg; /**< The signature algorithm used for \p sig. */
90
91 mbedtls_x509_buf_raw v3_ext; /**< The raw data for the extension list in the certificate.
92 * Might be useful for manual inspection of extensions that
93 * Mbed TLS doesn't yet support. */
94 mbedtls_x509_buf_raw subject_alt_raw; /**< The raw data for the SubjectAlternativeNames extension. */
95 mbedtls_x509_buf_raw ext_key_usage_raw; /**< The raw data for the ExtendedKeyUsage extension. */
96
97 mbedtls_x509_buf_raw issuer_raw_with_hdr;
98 mbedtls_x509_buf_raw subject_raw_with_hdr;
99
100} mbedtls_x509_crt_frame;
101
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102/**
103 * Container for an X.509 certificate. The certificate may be chained.
104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200106{
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000107 int own_buffer; /**< Indicates if \c raw is owned
108 * by the structure or not. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
110 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +0200112 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
114 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
117 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200118
Hanno Beckerf8a42862019-02-20 13:45:16 +0000119 mbedtls_x509_buf_raw subject_raw_no_hdr;
120 mbedtls_x509_buf_raw issuer_raw_no_hdr;
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
123 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
126 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127
Hanno Becker32c530e2019-02-06 16:13:41 +0000128 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
132 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
133 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
134 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Hanno Beckerded167e2019-02-21 14:34:46 +0000135 mbedtls_x509_buf_raw subject_alt_raw; /**< Raw data for SubjectAlternativeNames extension. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200136
137 int ext_types; /**< Bit string containing detected and parsed extensions */
138 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
139 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+ */
140
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100141 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200142
Hanno Becker7ec9c362019-02-21 14:24:05 +0000143 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
144 mbedtls_x509_buf_raw ext_key_usage_raw; /**< Raw data of ExtendedKeyUsage extensions. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +0200146 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
149 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
150 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
151 void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200154}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200156
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200157/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200158 * Build flag from an algorithm/curve identifier (pk, md, ecp)
159 * Since 0 is always XXX_NONE, ignore it.
160 */
Hanno Beckerd6028a12018-10-15 12:01:35 +0100161#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200162
163/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200164 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200165 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200166 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200167 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200168typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200169{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200170 uint32_t allowed_mds; /**< MDs for signatures */
171 uint32_t allowed_pks; /**< PK algs for signatures */
172 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
173 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200174}
175mbedtls_x509_crt_profile;
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#define MBEDTLS_X509_CRT_VERSION_1 0
178#define MBEDTLS_X509_CRT_VERSION_2 1
179#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
182#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200183
Andres AGf9113192016-09-02 14:06:04 +0100184#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
185#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
186#endif
187
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200188/**
189 * Container for writing a certificate (CRT)
190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200192{
193 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_mpi serial;
195 mbedtls_pk_context *subject_key;
196 mbedtls_pk_context *issuer_key;
197 mbedtls_asn1_named_data *subject;
198 mbedtls_asn1_named_data *issuer;
199 mbedtls_md_type_t md_alg;
200 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
201 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
202 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200205
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200206/**
207 * Item in a verification chain: cert and flags for it
208 */
209typedef struct {
210 mbedtls_x509_crt *crt;
211 uint32_t flags;
212} mbedtls_x509_crt_verify_chain_item;
213
214/**
215 * Max size of verification chain: end-entity + intermediates + trusted root
216 */
217#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
218
219/**
220 * Verification chain as built by \c mbedtls_crt_verify_chain()
221 */
222typedef struct
223{
224 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200225 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200226} mbedtls_x509_crt_verify_chain;
227
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200228#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
229
230/**
231 * \brief Context for resuming X.509 verify operations
232 */
233typedef struct
234{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200235 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200236 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200237
238 /* for find_parent_in() */
239 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
240 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200241 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200242
243 /* for find_parent() */
244 int parent_is_trusted; /* -1 if find_parent is not in progress */
245
246 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200247 enum {
248 x509_crt_rs_none,
249 x509_crt_rs_find_parent,
250 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200251 int self_cnt;
252 mbedtls_x509_crt_verify_chain ver_chain;
253
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200254} mbedtls_x509_crt_restart_ctx;
255
256#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
257
258/* Now we can declare functions that take a pointer to that */
259typedef void mbedtls_x509_crt_restart_ctx;
260
261#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200265 * Default security profile. Should provide a good balance between security
266 * and compatibility with current deployments.
267 */
268extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
269
270/**
271 * Expected next default profile. Recommended for new deployments.
272 * Currently targets a 128-bit security level, except for RSA-2048.
273 */
274extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
275
276/**
277 * NSA Suite B profile.
278 */
279extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
280
281/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200282 * \brief Parse a single DER formatted certificate and add it
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000283 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000285 * \param chain The pointer to the start of the CRT chain to attach to.
286 * When parsing the first CRT in a chain, this should point
287 * to an instance of ::mbedtls_x509_crt initialized through
288 * mbedtls_x509_crt_init().
289 * \param buf The buffer holding the DER encoded certificate.
290 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000292 * \note This function makes an internal copy of the CRT buffer
293 * \p buf. In particular, \p buf may be destroyed or reused
294 * after this call returns. To avoid duplicating the CRT
295 * buffer (at the cost of stricter lifetime constraints),
296 * use mbedtls_x509_crt_parse_der_nocopy() instead.
297 *
298 * \return \c 0 if successful.
299 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000301int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
302 const unsigned char *buf,
303 size_t buflen );
304
305/**
306 * \brief Parse a single DER formatted certificate and add it
307 * to the end of the provided chained list. This is a
308 * variant of mbedtls_x509_crt_parse_der() which takes
309 * temporary ownership of the CRT buffer until the CRT
310 * is destroyed.
311 *
312 * \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 address of the readable buffer holding the DER encoded
317 * certificate to use. On success, this buffer must be
318 * retained and not be changed for the liftetime of the
319 * CRT chain \p chain, that is, until \p chain is destroyed
320 * through a call to mbedtls_x509_crt_free().
321 * \param buflen The size in Bytes of \p buf.
322 *
323 * \note This call is functionally equivalent to
324 * mbedtls_x509_crt_parse_der(), but it avoids creating a
325 * copy of the input buffer at the cost of stronger lifetime
326 * constraints. This is useful in constrained environments
327 * where duplication of the CRT cannot be tolerated.
328 *
329 * \return \c 0 if successful.
330 * \return A negative error code on failure.
331 */
332int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
333 const unsigned char *buf,
334 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335
336/**
Hanno Becker89a91122018-08-23 16:14:00 +0100337 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100338 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100340 * For CRTs in PEM encoding, the function parses permissively:
341 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100342 * returns the number of certificates for which parsing failed
343 * (hence \c 0 if all certificates were parsed successfully).
344 * If no certificate could be parsed, the function returns
345 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200346 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100347 * PEM encoded certificates may be interleaved by other data
348 * such as human readable descriptions of their content, as
349 * long as the certificates are enclosed in the PEM specific
350 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
351 *
352 * \param chain The chain to which to add the parsed certificates.
353 * \param buf The buffer holding the certificate data in PEM or DER format.
354 * For certificates in PEM encoding, this may be a concatenation
355 * of multiple certificates; for DER encoding, the buffer must
356 * comprise exactly one certificate.
357 * \param buflen The size of \p buf, including the terminating \c NULL byte
358 * in case of PEM encoded data.
359 *
360 * \return \c 0 if all certificates were parsed successfully.
361 * \return The (positive) number of certificates that couldn't
362 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100363 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100364 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369/**
370 * \brief Load one or more certificates and add them
371 * to the chained list. Parses permissively. If some
372 * certificates can be parsed, the result is the number
373 * of failed certificates it encountered. If none complete
374 * correctly, the first error is returned.
375 *
376 * \param chain points to the start of the chain
377 * \param path filename to read the certificates from
378 *
379 * \return 0 if all certificates parsed successfully, a positive number
380 * if partly successful or a specific X509 or PEM error code
381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383
384/**
385 * \brief Load one or more certificate files from a path and add them
386 * to the chained list. Parses permissively. If some
387 * certificates can be parsed, the result is the number
388 * of failed certificates it encountered. If none complete
389 * correctly, the first error is returned.
390 *
391 * \param chain points to the start of the chain
392 * \param path directory / folder to read the certificate files from
393 *
394 * \return 0 if all certificates parsed successfully, a positive number
395 * if partly successful or a specific X509 or PEM error code
396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
398#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399
Hanno Becker02a21932019-06-10 15:08:43 +0100400#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401/**
402 * \brief Returns an informational string about the
403 * certificate.
404 *
405 * \param buf Buffer to write to
406 * \param size Maximum size of buffer
407 * \param prefix A line prefix
408 * \param crt The X509 certificate to represent
409 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200410 * \return The length of the string written (not including the
411 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
414 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415
416/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100417 * \brief Returns an informational string about the
418 * verification status of a certificate.
419 *
420 * \param buf Buffer to write to
421 * \param size Maximum size of buffer
422 * \param prefix A line prefix
423 * \param flags Verification flags created by mbedtls_x509_crt_verify()
424 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200425 * \return The length of the string written (not including the
426 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100427 */
428int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200429 uint32_t flags );
Hanno Beckerc6043f22019-06-14 17:21:24 +0100430#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200431
432/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433 * \brief Verify the certificate signature
434 *
435 * The verify callback is a user-supplied callback that
436 * can clear / modify / add flags for a certificate. If set,
437 * the verification callback is called for each
438 * certificate in the chain (from the trust-ca down to the
439 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441 * int *flags). With the flags representing current flags for
442 * that specific certificate and the certificate depth from
443 * the bottom (Peer cert depth = 0).
444 *
445 * All flags left after returning from the callback
446 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200447 * return 0 for anything (including invalid certificates)
448 * other than fatal error, as a non-zero return code
449 * immediately aborts the verification process. For fatal
450 * errors, a specific error code should be used (different
451 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
452 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
453 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100455 * \note In case verification failed, the results can be displayed
456 * using \c mbedtls_x509_crt_verify_info()
457 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200458 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
459 * default security profile.
460 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100461 * \note It is your responsibility to provide up-to-date CRLs for
462 * all trusted CAs. If no CRL is provided for the CA that was
463 * used to sign the certificate, CRL verification is skipped
464 * silently, that is *without* setting any flag.
465 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200466 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200467 * (1) those of trusted root CAs, so that certificates
468 * chaining up to those CAs will be trusted, and (2)
469 * self-signed end-entity certificates to be trusted (for
470 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200471 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200472 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100473 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200474 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100475 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476 * \param cn expected Common Name (can be set to
477 * NULL if the CN must not be verified)
478 * \param flags result of the verification
479 * \param f_vrfy verification function
480 * \param p_vrfy verification parameter
481 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200482 * \return 0 (and flags set to 0) if the chain was verified and valid,
483 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
484 * but found to be invalid, in which case *flags will have one
485 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
486 * flags set, or another error (and flags set to 0xffffffff)
487 * in case of a fatal error encountered during the
488 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
491 mbedtls_x509_crt *trust_ca,
492 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200493 const char *cn, uint32_t *flags,
494 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200495 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200497/**
498 * \brief Verify the certificate signature according to profile
499 *
500 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
501 * security profile.
502 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200503 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200504 * for ECDSA) apply to all certificates: trusted root,
505 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200506 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100507 * \param crt a certificate (chain) to be verified
508 * \param trust_ca the list of trusted CAs
509 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200510 * \param profile security profile for verification
511 * \param cn expected Common Name (can be set to
512 * NULL if the CN must not be verified)
513 * \param flags result of the verification
514 * \param f_vrfy verification function
515 * \param p_vrfy verification parameter
516 *
517 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
518 * in which case *flags will have one or more
519 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
520 * set,
521 * or another error in case of a fatal error encountered
522 * during the verification process.
523 */
524int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
525 mbedtls_x509_crt *trust_ca,
526 mbedtls_x509_crl *ca_crl,
527 const mbedtls_x509_crt_profile *profile,
528 const char *cn, uint32_t *flags,
529 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
530 void *p_vrfy );
531
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200532/**
533 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
534 *
535 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
536 * but can return early and restart according to the limit
537 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
538 *
539 * \param crt a certificate (chain) to be verified
540 * \param trust_ca the list of trusted CAs
541 * \param ca_crl the list of CRLs for trusted CAs
542 * \param profile security profile for verification
543 * \param cn expected Common Name (can be set to
544 * NULL if the CN must not be verified)
545 * \param flags result of the verification
546 * \param f_vrfy verification function
547 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200548 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200549 *
550 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200551 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200552 * operations was reached: see \c mbedtls_ecp_set_max_ops().
553 */
554int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
555 mbedtls_x509_crt *trust_ca,
556 mbedtls_x509_crl *ca_crl,
557 const mbedtls_x509_crt_profile *profile,
558 const char *cn, uint32_t *flags,
559 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
560 void *p_vrfy,
561 mbedtls_x509_crt_restart_ctx *rs_ctx );
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200564/**
565 * \brief Check usage of certificate against keyUsage extension.
566 *
567 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200568 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
569 * before using the certificate to perform an RSA key
570 * exchange).
571 *
572 * \note Except for decipherOnly and encipherOnly, a bit set in the
573 * usage argument means this bit MUST be set in the
574 * certificate. For decipherOnly and encipherOnly, it means
575 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200576 *
577 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200579 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200580 *
581 * \note You should only call this function on leaf certificates, on
582 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200584 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200585int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
586 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200590/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000591 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200592 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000593 * \param crt Leaf certificate used.
594 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
595 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200597 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000598 * \return 0 if this use of the certificate is allowed,
599 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200600 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000601 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000604 const char *usage_oid,
605 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000606#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200609/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200610 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611 *
612 * \param crt a certificate to be verified
613 * \param crl the CRL to verify against
614 *
615 * \return 1 if the certificate is revoked, 0 otherwise
616 *
617 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100618int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620
621/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200622 * \brief Initialize a certificate (chain)
623 *
624 * \param crt Certificate chain to initialize
625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200627
628/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629 * \brief Unallocate all certificate data
630 *
631 * \param crt Certificate chain to free
632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200634
635#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
636/**
637 * \brief Initialize a restart context
638 */
639void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
640
641/**
642 * \brief Free the components of a restart context
643 */
644void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
645#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647
648/* \} name */
649/* \} addtogroup x509_module */
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652/**
653 * \brief Initialize a CRT writing context
654 *
655 * \param ctx CRT context to initialize
656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658
659/**
660 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662 *
663 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
665 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668
669/**
670 * \brief Set the serial number for a Certificate.
671 *
672 * \param ctx CRT context to use
673 * \param serial serial number to set
674 *
675 * \return 0 if successful
676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200678
679/**
680 * \brief Set the validity period for a Certificate
681 * Timestamps should be in string format for UTC timezone
682 * i.e. "YYYYMMDDhhmmss"
683 * e.g. "20131231235959" for December 31st 2013
684 * at 23:59:59
685 *
686 * \param ctx CRT context to use
687 * \param not_before not_before timestamp
688 * \param not_after not_after timestamp
689 *
690 * \return 0 if timestamp was parsed successfully, or
691 * a specific error code
692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100694 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695
696/**
697 * \brief Set the issuer name for a Certificate
698 * Issuer names should contain a comma-separated list
699 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000700 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701 *
702 * \param ctx CRT context to use
703 * \param issuer_name issuer name to set
704 *
705 * \return 0 if issuer name was parsed successfully, or
706 * a specific error code
707 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100709 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710
711/**
712 * \brief Set the subject name for a Certificate
713 * Subject names should contain a comma-separated list
714 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000715 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716 *
717 * \param ctx CRT context to use
718 * \param subject_name subject name to set
719 *
720 * \return 0 if subject name was parsed successfully, or
721 * a specific error code
722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100724 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725
726/**
727 * \brief Set the subject public key for the certificate
728 *
729 * \param ctx CRT context to use
730 * \param key public key to include
731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733
734/**
735 * \brief Set the issuer key used for signing the certificate
736 *
737 * \param ctx CRT context to use
738 * \param key private key to sign with
739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741
742/**
743 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745 *
746 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100747 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200750
751/**
752 * \brief Generic function to add to or replace an extension in the
753 * CRT
754 *
755 * \param ctx CRT context to use
756 * \param oid OID of the extension
757 * \param oid_len length of the OID
758 * \param critical if the extension is critical (per the RFC's definition)
759 * \param val value of the extension OCTET STRING
760 * \param val_len length of the value data
761 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200762 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765 const char *oid, size_t oid_len,
766 int critical,
767 const unsigned char *val, size_t val_len );
768
769/**
770 * \brief Set the basicConstraints extension for a CRT
771 *
772 * \param ctx CRT context to use
773 * \param is_ca is this a CA certificate
774 * \param max_pathlen maximum length of certificate chains below this
775 * certificate (only for CA certificates, -1 is
776 * inlimited)
777 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200778 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781 int is_ca, int max_pathlen );
782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784/**
785 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200787 * called before
788 *
789 * \param ctx CRT context to use
790 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200791 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794
795/**
796 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798 * called before
799 *
800 * \param ctx CRT context to use
801 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200802 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
805#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200806
807/**
808 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200810 *
811 * \param ctx CRT context to use
812 * \param key_usage key usage flags to set
813 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200814 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200816int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
817 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818
819/**
820 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200822 *
823 * \param ctx CRT context to use
824 * \param ns_cert_type Netscape Cert Type flags to set
825 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200826 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200829 unsigned char ns_cert_type );
830
831/**
832 * \brief Free the contents of a CRT write context
833 *
834 * \param ctx CRT context to free
835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837
838/**
839 * \brief Write a built up certificate to a X509 DER structure
840 * Note: data is written at the end of the buffer! Use the
841 * return value to determine where you should start
842 * using the buffer
843 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100844 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845 * \param buf buffer to write to
846 * \param size size of the buffer
847 * \param f_rng RNG function (for signature, see note)
848 * \param p_rng RNG parameter
849 *
850 * \return length of data written if successful, or a specific
851 * error code
852 *
853 * \note f_rng may be NULL if RSA is used for signature and the
854 * signature is made offline (otherwise f_rng is desirable
855 * for countermeasures against timing attacks).
856 * ECDSA signatures always require a non-NULL f_rng.
857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200859 int (*f_rng)(void *, unsigned char *, size_t),
860 void *p_rng );
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200863/**
864 * \brief Write a built up certificate to a X509 PEM string
865 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100866 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200867 * \param buf buffer to write to
868 * \param size size of the buffer
869 * \param f_rng RNG function (for signature, see note)
870 * \param p_rng RNG parameter
871 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200872 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873 *
874 * \note f_rng may be NULL if RSA is used for signature and the
875 * signature is made offline (otherwise f_rng is desirable
876 * for countermeasures against timing attacks).
877 * ECDSA signatures always require a non-NULL f_rng.
878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 int (*f_rng)(void *, unsigned char *, size_t),
881 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#endif /* MBEDTLS_PEM_WRITE_C */
883#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200884
885#ifdef __cplusplus
886}
887#endif
888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#endif /* mbedtls_x509_crt.h */