blob: 0c71dae18ecd593e1a84c8a329528bba8afb4f6c [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
Hanno Becker7ec9c362019-02-21 14:24:05 +000090 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
91 mbedtls_x509_buf_raw ext_key_usage_raw; /**< Raw data of ExtendedKeyUsage extensions. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020093 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
96 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
97 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
98 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 +020099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200104/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200105 * Build flag from an algorithm/curve identifier (pk, md, ecp)
106 * Since 0 is always XXX_NONE, ignore it.
107 */
Hanno Beckerd6028a12018-10-15 12:01:35 +0100108#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200109
110/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200111 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200112 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200113 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200114 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200115typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200116{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200117 uint32_t allowed_mds; /**< MDs for signatures */
118 uint32_t allowed_pks; /**< PK algs for signatures */
119 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
120 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200121}
122mbedtls_x509_crt_profile;
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#define MBEDTLS_X509_CRT_VERSION_1 0
125#define MBEDTLS_X509_CRT_VERSION_2 1
126#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
129#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200130
Andres AGf9113192016-09-02 14:06:04 +0100131#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
132#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
133#endif
134
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135/**
136 * Container for writing a certificate (CRT)
137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200139{
140 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_mpi serial;
142 mbedtls_pk_context *subject_key;
143 mbedtls_pk_context *issuer_key;
144 mbedtls_asn1_named_data *subject;
145 mbedtls_asn1_named_data *issuer;
146 mbedtls_md_type_t md_alg;
147 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
148 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
149 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200150}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200152
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200153/**
154 * Item in a verification chain: cert and flags for it
155 */
156typedef struct {
157 mbedtls_x509_crt *crt;
158 uint32_t flags;
159} mbedtls_x509_crt_verify_chain_item;
160
161/**
162 * Max size of verification chain: end-entity + intermediates + trusted root
163 */
164#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
165
166/**
167 * Verification chain as built by \c mbedtls_crt_verify_chain()
168 */
169typedef struct
170{
171 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200172 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200173} mbedtls_x509_crt_verify_chain;
174
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200175#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
176
177/**
178 * \brief Context for resuming X.509 verify operations
179 */
180typedef struct
181{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200182 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200183 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200184
185 /* for find_parent_in() */
186 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
187 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200188 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200189
190 /* for find_parent() */
191 int parent_is_trusted; /* -1 if find_parent is not in progress */
192
193 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200194 enum {
195 x509_crt_rs_none,
196 x509_crt_rs_find_parent,
197 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200198 int self_cnt;
199 mbedtls_x509_crt_verify_chain ver_chain;
200
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200201} mbedtls_x509_crt_restart_ctx;
202
203#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
204
205/* Now we can declare functions that take a pointer to that */
206typedef void mbedtls_x509_crt_restart_ctx;
207
208#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200211/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200212 * Default security profile. Should provide a good balance between security
213 * and compatibility with current deployments.
214 */
215extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
216
217/**
218 * Expected next default profile. Recommended for new deployments.
219 * Currently targets a 128-bit security level, except for RSA-2048.
220 */
221extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
222
223/**
224 * NSA Suite B profile.
225 */
226extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
227
228/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229 * \brief Parse a single DER formatted certificate and add it
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000230 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200231 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000232 * \param chain The pointer to the start of the CRT chain to attach to.
233 * When parsing the first CRT in a chain, this should point
234 * to an instance of ::mbedtls_x509_crt initialized through
235 * mbedtls_x509_crt_init().
236 * \param buf The buffer holding the DER encoded certificate.
237 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200238 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000239 * \note This function makes an internal copy of the CRT buffer
240 * \p buf. In particular, \p buf may be destroyed or reused
241 * after this call returns. To avoid duplicating the CRT
242 * buffer (at the cost of stricter lifetime constraints),
243 * use mbedtls_x509_crt_parse_der_nocopy() instead.
244 *
245 * \return \c 0 if successful.
246 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000248int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
249 const unsigned char *buf,
250 size_t buflen );
251
252/**
253 * \brief Parse a single DER formatted certificate and add it
254 * to the end of the provided chained list. This is a
255 * variant of mbedtls_x509_crt_parse_der() which takes
256 * temporary ownership of the CRT buffer until the CRT
257 * is destroyed.
258 *
259 * \param chain The pointer to the start of the CRT chain to attach to.
260 * When parsing the first CRT in a chain, this should point
261 * to an instance of ::mbedtls_x509_crt initialized through
262 * mbedtls_x509_crt_init().
263 * \param buf The address of the readable buffer holding the DER encoded
264 * certificate to use. On success, this buffer must be
265 * retained and not be changed for the liftetime of the
266 * CRT chain \p chain, that is, until \p chain is destroyed
267 * through a call to mbedtls_x509_crt_free().
268 * \param buflen The size in Bytes of \p buf.
269 *
270 * \note This call is functionally equivalent to
271 * mbedtls_x509_crt_parse_der(), but it avoids creating a
272 * copy of the input buffer at the cost of stronger lifetime
273 * constraints. This is useful in constrained environments
274 * where duplication of the CRT cannot be tolerated.
275 *
276 * \return \c 0 if successful.
277 * \return A negative error code on failure.
278 */
279int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
280 const unsigned char *buf,
281 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200282
283/**
Hanno Becker89a91122018-08-23 16:14:00 +0100284 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100285 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100287 * For CRTs in PEM encoding, the function parses permissively:
288 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100289 * returns the number of certificates for which parsing failed
290 * (hence \c 0 if all certificates were parsed successfully).
291 * If no certificate could be parsed, the function returns
292 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100294 * PEM encoded certificates may be interleaved by other data
295 * such as human readable descriptions of their content, as
296 * long as the certificates are enclosed in the PEM specific
297 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
298 *
299 * \param chain The chain to which to add the parsed certificates.
300 * \param buf The buffer holding the certificate data in PEM or DER format.
301 * For certificates in PEM encoding, this may be a concatenation
302 * of multiple certificates; for DER encoding, the buffer must
303 * comprise exactly one certificate.
304 * \param buflen The size of \p buf, including the terminating \c NULL byte
305 * in case of PEM encoded data.
306 *
307 * \return \c 0 if all certificates were parsed successfully.
308 * \return The (positive) number of certificates that couldn't
309 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100310 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100311 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200316/**
317 * \brief Load one or more certificates and add them
318 * to the chained list. Parses permissively. If some
319 * certificates can be parsed, the result is the number
320 * of failed certificates it encountered. If none complete
321 * correctly, the first error is returned.
322 *
323 * \param chain points to the start of the chain
324 * \param path filename to read the certificates from
325 *
326 * \return 0 if all certificates parsed successfully, a positive number
327 * if partly successful or a specific X509 or PEM error code
328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200330
331/**
332 * \brief Load one or more certificate files from a path and add them
333 * to the chained list. Parses permissively. If some
334 * certificates can be parsed, the result is the number
335 * of failed certificates it encountered. If none complete
336 * correctly, the first error is returned.
337 *
338 * \param chain points to the start of the chain
339 * \param path directory / folder to read the certificate files from
340 *
341 * \return 0 if all certificates parsed successfully, a positive number
342 * if partly successful or a specific X509 or PEM error code
343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
345#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200346
Hanno Becker02a21932019-06-10 15:08:43 +0100347#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348/**
349 * \brief Returns an informational string about the
350 * certificate.
351 *
352 * \param buf Buffer to write to
353 * \param size Maximum size of buffer
354 * \param prefix A line prefix
355 * \param crt The X509 certificate to represent
356 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200357 * \return The length of the string written (not including the
358 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
361 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362
363/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100364 * \brief Returns an informational string about the
365 * verification status of a certificate.
366 *
367 * \param buf Buffer to write to
368 * \param size Maximum size of buffer
369 * \param prefix A line prefix
370 * \param flags Verification flags created by mbedtls_x509_crt_verify()
371 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200372 * \return The length of the string written (not including the
373 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100374 */
375int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200376 uint32_t flags );
Hanno Beckerc6043f22019-06-14 17:21:24 +0100377#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200378
379/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380 * \brief Verify the certificate signature
381 *
382 * The verify callback is a user-supplied callback that
383 * can clear / modify / add flags for a certificate. If set,
384 * the verification callback is called for each
385 * certificate in the chain (from the trust-ca down to the
386 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388 * int *flags). With the flags representing current flags for
389 * that specific certificate and the certificate depth from
390 * the bottom (Peer cert depth = 0).
391 *
392 * All flags left after returning from the callback
393 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200394 * return 0 for anything (including invalid certificates)
395 * other than fatal error, as a non-zero return code
396 * immediately aborts the verification process. For fatal
397 * errors, a specific error code should be used (different
398 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
399 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
400 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100402 * \note In case verification failed, the results can be displayed
403 * using \c mbedtls_x509_crt_verify_info()
404 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200405 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
406 * default security profile.
407 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100408 * \note It is your responsibility to provide up-to-date CRLs for
409 * all trusted CAs. If no CRL is provided for the CA that was
410 * used to sign the certificate, CRL verification is skipped
411 * silently, that is *without* setting any flag.
412 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200413 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200414 * (1) those of trusted root CAs, so that certificates
415 * chaining up to those CAs will be trusted, and (2)
416 * self-signed end-entity certificates to be trusted (for
417 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200418 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200419 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100420 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200421 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100422 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423 * \param cn expected Common Name (can be set to
424 * NULL if the CN must not be verified)
425 * \param flags result of the verification
426 * \param f_vrfy verification function
427 * \param p_vrfy verification parameter
428 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200429 * \return 0 (and flags set to 0) if the chain was verified and valid,
430 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
431 * but found to be invalid, in which case *flags will have one
432 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
433 * flags set, or another error (and flags set to 0xffffffff)
434 * in case of a fatal error encountered during the
435 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
438 mbedtls_x509_crt *trust_ca,
439 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200440 const char *cn, uint32_t *flags,
441 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200442 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200444/**
445 * \brief Verify the certificate signature according to profile
446 *
447 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
448 * security profile.
449 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200450 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200451 * for ECDSA) apply to all certificates: trusted root,
452 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200453 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100454 * \param crt a certificate (chain) to be verified
455 * \param trust_ca the list of trusted CAs
456 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200457 * \param profile security profile for verification
458 * \param cn expected Common Name (can be set to
459 * NULL if the CN must not be verified)
460 * \param flags result of the verification
461 * \param f_vrfy verification function
462 * \param p_vrfy verification parameter
463 *
464 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
465 * in which case *flags will have one or more
466 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
467 * set,
468 * or another error in case of a fatal error encountered
469 * during the verification process.
470 */
471int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
472 mbedtls_x509_crt *trust_ca,
473 mbedtls_x509_crl *ca_crl,
474 const mbedtls_x509_crt_profile *profile,
475 const char *cn, uint32_t *flags,
476 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
477 void *p_vrfy );
478
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200479/**
480 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
481 *
482 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
483 * but can return early and restart according to the limit
484 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
485 *
486 * \param crt a certificate (chain) to be verified
487 * \param trust_ca the list of trusted CAs
488 * \param ca_crl the list of CRLs for trusted CAs
489 * \param profile security profile for verification
490 * \param cn expected Common Name (can be set to
491 * NULL if the CN must not be verified)
492 * \param flags result of the verification
493 * \param f_vrfy verification function
494 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200495 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200496 *
497 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200498 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200499 * operations was reached: see \c mbedtls_ecp_set_max_ops().
500 */
501int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
502 mbedtls_x509_crt *trust_ca,
503 mbedtls_x509_crl *ca_crl,
504 const mbedtls_x509_crt_profile *profile,
505 const char *cn, uint32_t *flags,
506 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
507 void *p_vrfy,
508 mbedtls_x509_crt_restart_ctx *rs_ctx );
509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200511/**
512 * \brief Check usage of certificate against keyUsage extension.
513 *
514 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200515 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
516 * before using the certificate to perform an RSA key
517 * exchange).
518 *
519 * \note Except for decipherOnly and encipherOnly, a bit set in the
520 * usage argument means this bit MUST be set in the
521 * certificate. For decipherOnly and encipherOnly, it means
522 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200523 *
524 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200526 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200527 *
528 * \note You should only call this function on leaf certificates, on
529 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200531 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200532int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
533 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200537/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000538 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200539 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000540 * \param crt Leaf certificate used.
541 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
542 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200544 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000545 * \return 0 if this use of the certificate is allowed,
546 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200547 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000548 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000551 const char *usage_oid,
552 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000553#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200557 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558 *
559 * \param crt a certificate to be verified
560 * \param crl the CRL to verify against
561 *
562 * \return 1 if the certificate is revoked, 0 otherwise
563 *
564 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100565int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
568/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200569 * \brief Initialize a certificate (chain)
570 *
571 * \param crt Certificate chain to initialize
572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200574
575/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576 * \brief Unallocate all certificate data
577 *
578 * \param crt Certificate chain to free
579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200581
582#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
583/**
584 * \brief Initialize a restart context
585 */
586void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
587
588/**
589 * \brief Free the components of a restart context
590 */
591void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
592#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594
595/* \} name */
596/* \} addtogroup x509_module */
597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599/**
600 * \brief Initialize a CRT writing context
601 *
602 * \param ctx CRT context to initialize
603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605
606/**
607 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200609 *
610 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
612 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200615
616/**
617 * \brief Set the serial number for a Certificate.
618 *
619 * \param ctx CRT context to use
620 * \param serial serial number to set
621 *
622 * \return 0 if successful
623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625
626/**
627 * \brief Set the validity period for a Certificate
628 * Timestamps should be in string format for UTC timezone
629 * i.e. "YYYYMMDDhhmmss"
630 * e.g. "20131231235959" for December 31st 2013
631 * at 23:59:59
632 *
633 * \param ctx CRT context to use
634 * \param not_before not_before timestamp
635 * \param not_after not_after timestamp
636 *
637 * \return 0 if timestamp was parsed successfully, or
638 * a specific error code
639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100641 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642
643/**
644 * \brief Set the issuer name for a Certificate
645 * Issuer names should contain a comma-separated list
646 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000647 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 *
649 * \param ctx CRT context to use
650 * \param issuer_name issuer name to set
651 *
652 * \return 0 if issuer name was parsed successfully, or
653 * a specific error code
654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100656 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657
658/**
659 * \brief Set the subject name for a Certificate
660 * Subject names should contain a comma-separated list
661 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000662 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663 *
664 * \param ctx CRT context to use
665 * \param subject_name subject name to set
666 *
667 * \return 0 if subject name was parsed successfully, or
668 * a specific error code
669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100671 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672
673/**
674 * \brief Set the subject public key for the certificate
675 *
676 * \param ctx CRT context to use
677 * \param key public key to include
678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200680
681/**
682 * \brief Set the issuer key used for signing the certificate
683 *
684 * \param ctx CRT context to use
685 * \param key private key to sign with
686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688
689/**
690 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692 *
693 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100694 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697
698/**
699 * \brief Generic function to add to or replace an extension in the
700 * CRT
701 *
702 * \param ctx CRT context to use
703 * \param oid OID of the extension
704 * \param oid_len length of the OID
705 * \param critical if the extension is critical (per the RFC's definition)
706 * \param val value of the extension OCTET STRING
707 * \param val_len length of the value data
708 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200709 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200712 const char *oid, size_t oid_len,
713 int critical,
714 const unsigned char *val, size_t val_len );
715
716/**
717 * \brief Set the basicConstraints extension for a CRT
718 *
719 * \param ctx CRT context to use
720 * \param is_ca is this a CA certificate
721 * \param max_pathlen maximum length of certificate chains below this
722 * certificate (only for CA certificates, -1 is
723 * inlimited)
724 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200725 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728 int is_ca, int max_pathlen );
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731/**
732 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200734 * called before
735 *
736 * \param ctx CRT context to use
737 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200738 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741
742/**
743 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745 * called before
746 *
747 * \param ctx CRT context to use
748 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200749 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
752#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753
754/**
755 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757 *
758 * \param ctx CRT context to use
759 * \param key_usage key usage flags to set
760 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200761 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200763int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
764 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765
766/**
767 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 *
770 * \param ctx CRT context to use
771 * \param ns_cert_type Netscape Cert Type flags to set
772 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200773 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200776 unsigned char ns_cert_type );
777
778/**
779 * \brief Free the contents of a CRT write context
780 *
781 * \param ctx CRT context to free
782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784
785/**
786 * \brief Write a built up certificate to a X509 DER structure
787 * Note: data is written at the end of the buffer! Use the
788 * return value to determine where you should start
789 * using the buffer
790 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100791 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792 * \param buf buffer to write to
793 * \param size size of the buffer
794 * \param f_rng RNG function (for signature, see note)
795 * \param p_rng RNG parameter
796 *
797 * \return length of data written if successful, or a specific
798 * error code
799 *
800 * \note f_rng may be NULL if RSA is used for signature and the
801 * signature is made offline (otherwise f_rng is desirable
802 * for countermeasures against timing attacks).
803 * ECDSA signatures always require a non-NULL f_rng.
804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200806 int (*f_rng)(void *, unsigned char *, size_t),
807 void *p_rng );
808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200810/**
811 * \brief Write a built up certificate to a X509 PEM string
812 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100813 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200814 * \param buf buffer to write to
815 * \param size size of the buffer
816 * \param f_rng RNG function (for signature, see note)
817 * \param p_rng RNG parameter
818 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200819 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820 *
821 * \note f_rng may be NULL if RSA is used for signature and the
822 * signature is made offline (otherwise f_rng is desirable
823 * for countermeasures against timing attacks).
824 * ECDSA signatures always require a non-NULL f_rng.
825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827 int (*f_rng)(void *, unsigned char *, size_t),
828 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#endif /* MBEDTLS_PEM_WRITE_C */
830#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200831
832#ifdef __cplusplus
833}
834#endif
835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#endif /* mbedtls_x509_crt.h */