blob: 6e07ac6f2d2bbe7911144e6b0c4e6ead212ffb61 [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
50/**
51 * Container for an X.509 certificate. The certificate may be chained.
52 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054{
Hanno Beckeraa8665a2019-01-31 08:57:44 +000055 int own_buffer; /**< Indicates if \c raw is owned
56 * by the structure or not. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
58 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020060 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
62 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
65 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Hanno Beckerf8a42862019-02-20 13:45:16 +000067 mbedtls_x509_buf_raw subject_raw_no_hdr;
68 mbedtls_x509_buf_raw issuer_raw_no_hdr;
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
71 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
74 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075
Hanno Becker32c530e2019-02-06 16:13:41 +000076 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
80 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
81 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
82 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083
84 int ext_types; /**< Bit string containing detected and parsed extensions */
85 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
86 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+ */
87
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +010088 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020092 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
95 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
96 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
97 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 +020098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200103/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200104 * Build flag from an algorithm/curve identifier (pk, md, ecp)
105 * Since 0 is always XXX_NONE, ignore it.
106 */
Hanno Beckerd6028a12018-10-15 12:01:35 +0100107#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200108
109/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200110 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200111 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200112 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200113 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200114typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200115{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200116 uint32_t allowed_mds; /**< MDs for signatures */
117 uint32_t allowed_pks; /**< PK algs for signatures */
118 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
119 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200120}
121mbedtls_x509_crt_profile;
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#define MBEDTLS_X509_CRT_VERSION_1 0
124#define MBEDTLS_X509_CRT_VERSION_2 1
125#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
128#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200129
Andres AGf9113192016-09-02 14:06:04 +0100130#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
131#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
132#endif
133
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200134/**
135 * Container for writing a certificate (CRT)
136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138{
139 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_mpi serial;
141 mbedtls_pk_context *subject_key;
142 mbedtls_pk_context *issuer_key;
143 mbedtls_asn1_named_data *subject;
144 mbedtls_asn1_named_data *issuer;
145 mbedtls_md_type_t md_alg;
146 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
147 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
148 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200151
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200152/**
153 * Item in a verification chain: cert and flags for it
154 */
155typedef struct {
156 mbedtls_x509_crt *crt;
157 uint32_t flags;
158} mbedtls_x509_crt_verify_chain_item;
159
160/**
161 * Max size of verification chain: end-entity + intermediates + trusted root
162 */
163#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
164
165/**
166 * Verification chain as built by \c mbedtls_crt_verify_chain()
167 */
168typedef struct
169{
170 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200171 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200172} mbedtls_x509_crt_verify_chain;
173
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200174#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
175
176/**
177 * \brief Context for resuming X.509 verify operations
178 */
179typedef struct
180{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200181 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200182 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200183
184 /* for find_parent_in() */
185 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
186 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200187 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200188
189 /* for find_parent() */
190 int parent_is_trusted; /* -1 if find_parent is not in progress */
191
192 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200193 enum {
194 x509_crt_rs_none,
195 x509_crt_rs_find_parent,
196 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200197 int self_cnt;
198 mbedtls_x509_crt_verify_chain ver_chain;
199
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200200} mbedtls_x509_crt_restart_ctx;
201
202#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
203
204/* Now we can declare functions that take a pointer to that */
205typedef void mbedtls_x509_crt_restart_ctx;
206
207#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200210/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200211 * Default security profile. Should provide a good balance between security
212 * and compatibility with current deployments.
213 */
214extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
215
216/**
217 * Expected next default profile. Recommended for new deployments.
218 * Currently targets a 128-bit security level, except for RSA-2048.
219 */
220extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
221
222/**
223 * NSA Suite B profile.
224 */
225extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
226
227/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200228 * \brief Parse a single DER formatted certificate and add it
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000229 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200230 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000231 * \param chain The pointer to the start of the CRT chain to attach to.
232 * When parsing the first CRT in a chain, this should point
233 * to an instance of ::mbedtls_x509_crt initialized through
234 * mbedtls_x509_crt_init().
235 * \param buf The buffer holding the DER encoded certificate.
236 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200237 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000238 * \note This function makes an internal copy of the CRT buffer
239 * \p buf. In particular, \p buf may be destroyed or reused
240 * after this call returns. To avoid duplicating the CRT
241 * buffer (at the cost of stricter lifetime constraints),
242 * use mbedtls_x509_crt_parse_der_nocopy() instead.
243 *
244 * \return \c 0 if successful.
245 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200246 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000247int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
248 const unsigned char *buf,
249 size_t buflen );
250
251/**
252 * \brief Parse a single DER formatted certificate and add it
253 * to the end of the provided chained list. This is a
254 * variant of mbedtls_x509_crt_parse_der() which takes
255 * temporary ownership of the CRT buffer until the CRT
256 * is destroyed.
257 *
258 * \param chain The pointer to the start of the CRT chain to attach to.
259 * When parsing the first CRT in a chain, this should point
260 * to an instance of ::mbedtls_x509_crt initialized through
261 * mbedtls_x509_crt_init().
262 * \param buf The address of the readable buffer holding the DER encoded
263 * certificate to use. On success, this buffer must be
264 * retained and not be changed for the liftetime of the
265 * CRT chain \p chain, that is, until \p chain is destroyed
266 * through a call to mbedtls_x509_crt_free().
267 * \param buflen The size in Bytes of \p buf.
268 *
269 * \note This call is functionally equivalent to
270 * mbedtls_x509_crt_parse_der(), but it avoids creating a
271 * copy of the input buffer at the cost of stronger lifetime
272 * constraints. This is useful in constrained environments
273 * where duplication of the CRT cannot be tolerated.
274 *
275 * \return \c 0 if successful.
276 * \return A negative error code on failure.
277 */
278int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
279 const unsigned char *buf,
280 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281
282/**
Hanno Becker89a91122018-08-23 16:14:00 +0100283 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100284 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100286 * For CRTs in PEM encoding, the function parses permissively:
287 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100288 * returns the number of certificates for which parsing failed
289 * (hence \c 0 if all certificates were parsed successfully).
290 * If no certificate could be parsed, the function returns
291 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200292 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100293 * PEM encoded certificates may be interleaved by other data
294 * such as human readable descriptions of their content, as
295 * long as the certificates are enclosed in the PEM specific
296 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
297 *
298 * \param chain The chain to which to add the parsed certificates.
299 * \param buf The buffer holding the certificate data in PEM or DER format.
300 * For certificates in PEM encoding, this may be a concatenation
301 * of multiple certificates; for DER encoding, the buffer must
302 * comprise exactly one certificate.
303 * \param buflen The size of \p buf, including the terminating \c NULL byte
304 * in case of PEM encoded data.
305 *
306 * \return \c 0 if all certificates were parsed successfully.
307 * \return The (positive) number of certificates that couldn't
308 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100309 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100310 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200315/**
316 * \brief Load one or more certificates and add them
317 * to the chained list. Parses permissively. If some
318 * certificates can be parsed, the result is the number
319 * of failed certificates it encountered. If none complete
320 * correctly, the first error is returned.
321 *
322 * \param chain points to the start of the chain
323 * \param path filename to read the certificates from
324 *
325 * \return 0 if all certificates parsed successfully, a positive number
326 * if partly successful or a specific X509 or PEM error code
327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329
330/**
331 * \brief Load one or more certificate files from a path and add them
332 * to the chained list. Parses permissively. If some
333 * certificates can be parsed, the result is the number
334 * of failed certificates it encountered. If none complete
335 * correctly, the first error is returned.
336 *
337 * \param chain points to the start of the chain
338 * \param path directory / folder to read the certificate files from
339 *
340 * \return 0 if all certificates parsed successfully, a positive number
341 * if partly successful or a specific X509 or PEM error code
342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
344#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345
Hanno Becker02a21932019-06-10 15:08:43 +0100346#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347/**
348 * \brief Returns an informational string about the
349 * certificate.
350 *
351 * \param buf Buffer to write to
352 * \param size Maximum size of buffer
353 * \param prefix A line prefix
354 * \param crt The X509 certificate to represent
355 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200356 * \return The length of the string written (not including the
357 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
360 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361
362/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100363 * \brief Returns an informational string about the
364 * verification status of a certificate.
365 *
366 * \param buf Buffer to write to
367 * \param size Maximum size of buffer
368 * \param prefix A line prefix
369 * \param flags Verification flags created by mbedtls_x509_crt_verify()
370 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200371 * \return The length of the string written (not including the
372 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100373 */
374int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200375 uint32_t flags );
Hanno Beckerc6043f22019-06-14 17:21:24 +0100376#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200377
378/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379 * \brief Verify the certificate signature
380 *
381 * The verify callback is a user-supplied callback that
382 * can clear / modify / add flags for a certificate. If set,
383 * the verification callback is called for each
384 * certificate in the chain (from the trust-ca down to the
385 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387 * int *flags). With the flags representing current flags for
388 * that specific certificate and the certificate depth from
389 * the bottom (Peer cert depth = 0).
390 *
391 * All flags left after returning from the callback
392 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200393 * return 0 for anything (including invalid certificates)
394 * other than fatal error, as a non-zero return code
395 * immediately aborts the verification process. For fatal
396 * errors, a specific error code should be used (different
397 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
398 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
399 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100401 * \note In case verification failed, the results can be displayed
402 * using \c mbedtls_x509_crt_verify_info()
403 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200404 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
405 * default security profile.
406 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100407 * \note It is your responsibility to provide up-to-date CRLs for
408 * all trusted CAs. If no CRL is provided for the CA that was
409 * used to sign the certificate, CRL verification is skipped
410 * silently, that is *without* setting any flag.
411 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200412 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200413 * (1) those of trusted root CAs, so that certificates
414 * chaining up to those CAs will be trusted, and (2)
415 * self-signed end-entity certificates to be trusted (for
416 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200417 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200418 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100419 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200420 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100421 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422 * \param cn expected Common Name (can be set to
423 * NULL if the CN must not be verified)
424 * \param flags result of the verification
425 * \param f_vrfy verification function
426 * \param p_vrfy verification parameter
427 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200428 * \return 0 (and flags set to 0) if the chain was verified and valid,
429 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
430 * but found to be invalid, in which case *flags will have one
431 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
432 * flags set, or another error (and flags set to 0xffffffff)
433 * in case of a fatal error encountered during the
434 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
437 mbedtls_x509_crt *trust_ca,
438 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200439 const char *cn, uint32_t *flags,
440 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200441 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200443/**
444 * \brief Verify the certificate signature according to profile
445 *
446 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
447 * security profile.
448 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200449 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200450 * for ECDSA) apply to all certificates: trusted root,
451 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200452 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100453 * \param crt a certificate (chain) to be verified
454 * \param trust_ca the list of trusted CAs
455 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200456 * \param profile security profile for verification
457 * \param cn expected Common Name (can be set to
458 * NULL if the CN must not be verified)
459 * \param flags result of the verification
460 * \param f_vrfy verification function
461 * \param p_vrfy verification parameter
462 *
463 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
464 * in which case *flags will have one or more
465 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
466 * set,
467 * or another error in case of a fatal error encountered
468 * during the verification process.
469 */
470int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
471 mbedtls_x509_crt *trust_ca,
472 mbedtls_x509_crl *ca_crl,
473 const mbedtls_x509_crt_profile *profile,
474 const char *cn, uint32_t *flags,
475 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
476 void *p_vrfy );
477
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200478/**
479 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
480 *
481 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
482 * but can return early and restart according to the limit
483 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
484 *
485 * \param crt a certificate (chain) to be verified
486 * \param trust_ca the list of trusted CAs
487 * \param ca_crl the list of CRLs for trusted CAs
488 * \param profile security profile for verification
489 * \param cn expected Common Name (can be set to
490 * NULL if the CN must not be verified)
491 * \param flags result of the verification
492 * \param f_vrfy verification function
493 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200494 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200495 *
496 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200497 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200498 * operations was reached: see \c mbedtls_ecp_set_max_ops().
499 */
500int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
501 mbedtls_x509_crt *trust_ca,
502 mbedtls_x509_crl *ca_crl,
503 const mbedtls_x509_crt_profile *profile,
504 const char *cn, uint32_t *flags,
505 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
506 void *p_vrfy,
507 mbedtls_x509_crt_restart_ctx *rs_ctx );
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200510/**
511 * \brief Check usage of certificate against keyUsage extension.
512 *
513 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200514 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
515 * before using the certificate to perform an RSA key
516 * exchange).
517 *
518 * \note Except for decipherOnly and encipherOnly, a bit set in the
519 * usage argument means this bit MUST be set in the
520 * certificate. For decipherOnly and encipherOnly, it means
521 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200522 *
523 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200525 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200526 *
527 * \note You should only call this function on leaf certificates, on
528 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200530 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200531int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
532 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200536/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000537 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200538 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000539 * \param crt Leaf certificate used.
540 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
541 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200543 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000544 * \return 0 if this use of the certificate is allowed,
545 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200546 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000547 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000550 const char *usage_oid,
551 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000552#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200556 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557 *
558 * \param crt a certificate to be verified
559 * \param crl the CRL to verify against
560 *
561 * \return 1 if the certificate is revoked, 0 otherwise
562 *
563 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100564int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566
567/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200568 * \brief Initialize a certificate (chain)
569 *
570 * \param crt Certificate chain to initialize
571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200573
574/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575 * \brief Unallocate all certificate data
576 *
577 * \param crt Certificate chain to free
578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200580
581#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
582/**
583 * \brief Initialize a restart context
584 */
585void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
586
587/**
588 * \brief Free the components of a restart context
589 */
590void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
591#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593
594/* \} name */
595/* \} addtogroup x509_module */
596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200598/**
599 * \brief Initialize a CRT writing context
600 *
601 * \param ctx CRT context to initialize
602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200604
605/**
606 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608 *
609 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
611 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614
615/**
616 * \brief Set the serial number for a Certificate.
617 *
618 * \param ctx CRT context to use
619 * \param serial serial number to set
620 *
621 * \return 0 if successful
622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624
625/**
626 * \brief Set the validity period for a Certificate
627 * Timestamps should be in string format for UTC timezone
628 * i.e. "YYYYMMDDhhmmss"
629 * e.g. "20131231235959" for December 31st 2013
630 * at 23:59:59
631 *
632 * \param ctx CRT context to use
633 * \param not_before not_before timestamp
634 * \param not_after not_after timestamp
635 *
636 * \return 0 if timestamp was parsed successfully, or
637 * a specific error code
638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100640 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641
642/**
643 * \brief Set the issuer name for a Certificate
644 * Issuer names should contain a comma-separated list
645 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000646 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647 *
648 * \param ctx CRT context to use
649 * \param issuer_name issuer name to set
650 *
651 * \return 0 if issuer name was parsed successfully, or
652 * a specific error code
653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100655 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656
657/**
658 * \brief Set the subject name for a Certificate
659 * Subject names should contain a comma-separated list
660 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000661 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662 *
663 * \param ctx CRT context to use
664 * \param subject_name subject name to set
665 *
666 * \return 0 if subject name was parsed successfully, or
667 * a specific error code
668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100670 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671
672/**
673 * \brief Set the subject public key for the certificate
674 *
675 * \param ctx CRT context to use
676 * \param key public key to include
677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200679
680/**
681 * \brief Set the issuer key used for signing the certificate
682 *
683 * \param ctx CRT context to use
684 * \param key private key to sign with
685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687
688/**
689 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 *
692 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100693 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696
697/**
698 * \brief Generic function to add to or replace an extension in the
699 * CRT
700 *
701 * \param ctx CRT context to use
702 * \param oid OID of the extension
703 * \param oid_len length of the OID
704 * \param critical if the extension is critical (per the RFC's definition)
705 * \param val value of the extension OCTET STRING
706 * \param val_len length of the value data
707 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200708 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711 const char *oid, size_t oid_len,
712 int critical,
713 const unsigned char *val, size_t val_len );
714
715/**
716 * \brief Set the basicConstraints extension for a CRT
717 *
718 * \param ctx CRT context to use
719 * \param is_ca is this a CA certificate
720 * \param max_pathlen maximum length of certificate chains below this
721 * certificate (only for CA certificates, -1 is
722 * inlimited)
723 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200724 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727 int is_ca, int max_pathlen );
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730/**
731 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733 * called before
734 *
735 * \param ctx CRT context to use
736 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200737 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740
741/**
742 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200744 * called before
745 *
746 * \param ctx CRT context to use
747 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200748 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
751#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752
753/**
754 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200756 *
757 * \param ctx CRT context to use
758 * \param key_usage key usage flags to set
759 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200760 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200762int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
763 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764
765/**
766 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 *
769 * \param ctx CRT context to use
770 * \param ns_cert_type Netscape Cert Type flags to set
771 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200772 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775 unsigned char ns_cert_type );
776
777/**
778 * \brief Free the contents of a CRT write context
779 *
780 * \param ctx CRT context to free
781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200783
784/**
785 * \brief Write a built up certificate to a X509 DER structure
786 * Note: data is written at the end of the buffer! Use the
787 * return value to determine where you should start
788 * using the buffer
789 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100790 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791 * \param buf buffer to write to
792 * \param size size of the buffer
793 * \param f_rng RNG function (for signature, see note)
794 * \param p_rng RNG parameter
795 *
796 * \return length of data written if successful, or a specific
797 * error code
798 *
799 * \note f_rng may be NULL if RSA is used for signature and the
800 * signature is made offline (otherwise f_rng is desirable
801 * for countermeasures against timing attacks).
802 * ECDSA signatures always require a non-NULL f_rng.
803 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200805 int (*f_rng)(void *, unsigned char *, size_t),
806 void *p_rng );
807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200809/**
810 * \brief Write a built up certificate to a X509 PEM string
811 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100812 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813 * \param buf buffer to write to
814 * \param size size of the buffer
815 * \param f_rng RNG function (for signature, see note)
816 * \param p_rng RNG parameter
817 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200818 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200819 *
820 * \note f_rng may be NULL if RSA is used for signature and the
821 * signature is made offline (otherwise f_rng is desirable
822 * for countermeasures against timing attacks).
823 * ECDSA signatures always require a non-NULL f_rng.
824 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826 int (*f_rng)(void *, unsigned char *, size_t),
827 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#endif /* MBEDTLS_PEM_WRITE_C */
829#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830
831#ifdef __cplusplus
832}
833#endif
834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835#endif /* mbedtls_x509_crt.h */