blob: 4a796173f610bf6b47949e95fe03e9f9919cc982 [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 Becker1a65dcd2019-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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
68 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
71 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072
Hanno Becker494dd7a2019-02-06 16:13:41 +000073 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
77 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
78 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +020079 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName and OtherName supported). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080
81 int ext_types; /**< Bit string containing detected and parsed extensions */
82 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
83 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+ */
84
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +010085 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020089 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
92 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
93 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
94 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 +020095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200100/*
101 * From RFC 5280 section 4.2.1.6:
102 * OtherName ::= SEQUENCE {
103 * type-id OBJECT IDENTIFIER,
104 * value [0] EXPLICIT ANY DEFINED BY type-id }
105 */
106typedef struct mbedtls_x509_san_other_name
107{
108 mbedtls_x509_buf type_id; /**< The type id. */
109 union
110 {
111 /*
112 * From RFC 4108 section 5:
113 * HardwareModuleName ::= SEQUENCE {
114 * hwType OBJECT IDENTIFIER,
115 * hwSerialNum OCTET STRING }
116 */
117 mbedtls_x509_name hardware_module_name;
118 }
119 value;
120}
121mbedtls_x509_san_other_name;
122
123/*
124 * A structure for holding the parsed Subject Alternative Name, according to type
125 */
126typedef struct mbedtls_x509_subject_alternative_name
127{
128 int type; /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */
129 union {
130 mbedtls_x509_san_other_name other_name; /**< The otherName supported type. */
131 mbedtls_x509_buf unstructured_name; /**< The buffer for the un constructed types. Only dnsName currently supported */
132 }
133 san; /**< A union of the supported SAN types */
134 struct mbedtls_x509_subject_alternative_name *next; /**< The next SAN in the list. */
135}
136mbedtls_x509_subject_alternative_name;
137
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200138/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200139 * Build flag from an algorithm/curve identifier (pk, md, ecp)
140 * Since 0 is always XXX_NONE, ignore it.
141 */
Hanno Becker1eeca412018-10-15 12:01:35 +0100142#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200143
144/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200145 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200146 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200147 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200148 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200149typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200150{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200151 uint32_t allowed_mds; /**< MDs for signatures */
152 uint32_t allowed_pks; /**< PK algs for signatures */
153 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
154 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200155}
156mbedtls_x509_crt_profile;
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#define MBEDTLS_X509_CRT_VERSION_1 0
159#define MBEDTLS_X509_CRT_VERSION_2 1
160#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
163#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200164
Andres AGf9113192016-09-02 14:06:04 +0100165#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
166#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
167#endif
168
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200169/**
170 * Container for writing a certificate (CRT)
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200173{
174 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_mpi serial;
176 mbedtls_pk_context *subject_key;
177 mbedtls_pk_context *issuer_key;
178 mbedtls_asn1_named_data *subject;
179 mbedtls_asn1_named_data *issuer;
180 mbedtls_md_type_t md_alg;
181 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
182 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
183 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200184}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200187/**
188 * Item in a verification chain: cert and flags for it
189 */
190typedef struct {
191 mbedtls_x509_crt *crt;
192 uint32_t flags;
193} mbedtls_x509_crt_verify_chain_item;
194
195/**
196 * Max size of verification chain: end-entity + intermediates + trusted root
197 */
198#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
199
200/**
201 * Verification chain as built by \c mbedtls_crt_verify_chain()
202 */
203typedef struct
204{
205 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200206 unsigned len;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000207
208#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
209 /* This stores the list of potential trusted signers obtained from
210 * the CA callback used for the CRT verification, if configured.
211 * We must track it somewhere because the callback passes its
212 * ownership to the caller. */
213 mbedtls_x509_crt *trust_ca_cb_result;
214#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200215} mbedtls_x509_crt_verify_chain;
216
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200217#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
218
219/**
220 * \brief Context for resuming X.509 verify operations
221 */
222typedef struct
223{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200224 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200225 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200226
227 /* for find_parent_in() */
228 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
229 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200230 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200231
232 /* for find_parent() */
233 int parent_is_trusted; /* -1 if find_parent is not in progress */
234
235 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200236 enum {
237 x509_crt_rs_none,
238 x509_crt_rs_find_parent,
239 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200240 int self_cnt;
241 mbedtls_x509_crt_verify_chain ver_chain;
242
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200243} mbedtls_x509_crt_restart_ctx;
244
245#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
246
247/* Now we can declare functions that take a pointer to that */
248typedef void mbedtls_x509_crt_restart_ctx;
249
250#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200253/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200254 * Default security profile. Should provide a good balance between security
255 * and compatibility with current deployments.
256 */
257extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
258
259/**
260 * Expected next default profile. Recommended for new deployments.
261 * Currently targets a 128-bit security level, except for RSA-2048.
262 */
263extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
264
265/**
266 * NSA Suite B profile.
267 */
268extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
269
270/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271 * \brief Parse a single DER formatted certificate and add it
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000272 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200273 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000274 * \param chain The pointer to the start of the CRT chain to attach to.
275 * When parsing the first CRT in a chain, this should point
276 * to an instance of ::mbedtls_x509_crt initialized through
277 * mbedtls_x509_crt_init().
278 * \param buf The buffer holding the DER encoded certificate.
279 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000281 * \note This function makes an internal copy of the CRT buffer
282 * \p buf. In particular, \p buf may be destroyed or reused
283 * after this call returns. To avoid duplicating the CRT
284 * buffer (at the cost of stricter lifetime constraints),
285 * use mbedtls_x509_crt_parse_der_nocopy() instead.
286 *
287 * \return \c 0 if successful.
288 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000290int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
291 const unsigned char *buf,
292 size_t buflen );
293
294/**
295 * \brief Parse a single DER formatted certificate and add it
296 * to the end of the provided chained list. This is a
297 * variant of mbedtls_x509_crt_parse_der() which takes
298 * temporary ownership of the CRT buffer until the CRT
299 * is destroyed.
300 *
301 * \param chain The pointer to the start of the CRT chain to attach to.
302 * When parsing the first CRT in a chain, this should point
303 * to an instance of ::mbedtls_x509_crt initialized through
304 * mbedtls_x509_crt_init().
305 * \param buf The address of the readable buffer holding the DER encoded
306 * certificate to use. On success, this buffer must be
307 * retained and not be changed for the liftetime of the
308 * CRT chain \p chain, that is, until \p chain is destroyed
309 * through a call to mbedtls_x509_crt_free().
310 * \param buflen The size in Bytes of \p buf.
311 *
312 * \note This call is functionally equivalent to
313 * mbedtls_x509_crt_parse_der(), but it avoids creating a
314 * copy of the input buffer at the cost of stronger lifetime
315 * constraints. This is useful in constrained environments
316 * where duplication of the CRT cannot be tolerated.
317 *
318 * \return \c 0 if successful.
319 * \return A negative error code on failure.
320 */
321int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
322 const unsigned char *buf,
323 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200324
325/**
Hanno Becker89a91122018-08-23 16:14:00 +0100326 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100327 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100329 * For CRTs in PEM encoding, the function parses permissively:
330 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100331 * returns the number of certificates for which parsing failed
332 * (hence \c 0 if all certificates were parsed successfully).
333 * If no certificate could be parsed, the function returns
334 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100336 * PEM encoded certificates may be interleaved by other data
337 * such as human readable descriptions of their content, as
338 * long as the certificates are enclosed in the PEM specific
339 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
340 *
341 * \param chain The chain to which to add the parsed certificates.
342 * \param buf The buffer holding the certificate data in PEM or DER format.
343 * For certificates in PEM encoding, this may be a concatenation
344 * of multiple certificates; for DER encoding, the buffer must
345 * comprise exactly one certificate.
346 * \param buflen The size of \p buf, including the terminating \c NULL byte
347 * in case of PEM encoded data.
348 *
349 * \return \c 0 if all certificates were parsed successfully.
350 * \return The (positive) number of certificates that couldn't
351 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100352 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100353 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358/**
359 * \brief Load one or more certificates and add them
360 * to the chained list. Parses permissively. If some
361 * certificates can be parsed, the result is the number
362 * of failed certificates it encountered. If none complete
363 * correctly, the first error is returned.
364 *
365 * \param chain points to the start of the chain
366 * \param path filename to read the certificates from
367 *
368 * \return 0 if all certificates parsed successfully, a positive number
369 * if partly successful or a specific X509 or PEM error code
370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372
373/**
374 * \brief Load one or more certificate files from a path and add them
375 * to the chained list. Parses permissively. If some
376 * certificates can be parsed, the result is the number
377 * of failed certificates it encountered. If none complete
378 * correctly, the first error is returned.
379 *
380 * \param chain points to the start of the chain
381 * \param path directory / folder to read the certificate files from
382 *
383 * \return 0 if all certificates parsed successfully, a positive number
384 * if partly successful or a specific X509 or PEM error code
385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
387#endif /* MBEDTLS_FS_IO */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200388/**
389 * \brief Parses the subject alternative name list of a given certificate;
390 *
391 * \param crt The X509 certificate to parse.
392 *
393 * \param san A list holding the parsed certificate.
394 *
395 * \note Only "dnsName" and "otherName" of type hardware_module_name,
396 * as defined in RFC 4180 is supported.
397 *
398 * \note Any unsupported san type is ignored.
399 *
400 * \note The function allocates a list of mbedtls_x509_subject_alternative_name
401 * and it is the caller's responsibility to free it.
402 *
403 * \return Zero for success and negative
404 * value for any other failure.
405 */
406int mbedtls_x509_parse_subject_alternative_name( const mbedtls_x509_crt *crt,
407 mbedtls_x509_subject_alternative_name **san );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408/**
409 * \brief Returns an informational string about the
410 * certificate.
411 *
412 * \param buf Buffer to write to
413 * \param size Maximum size of buffer
414 * \param prefix A line prefix
415 * \param crt The X509 certificate to represent
416 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200417 * \return The length of the string written (not including the
418 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
421 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422
423/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100424 * \brief Returns an informational string about the
425 * verification status of a certificate.
426 *
427 * \param buf Buffer to write to
428 * \param size Maximum size of buffer
429 * \param prefix A line prefix
430 * \param flags Verification flags created by mbedtls_x509_crt_verify()
431 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200432 * \return The length of the string written (not including the
433 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100434 */
435int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200436 uint32_t flags );
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200437
438/**
Hanno Becker902451d2019-03-27 11:48:40 +0000439 * \brief Verify a chain of certificates.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440 *
441 * The verify callback is a user-supplied callback that
442 * can clear / modify / add flags for a certificate. If set,
443 * the verification callback is called for each
444 * certificate in the chain (from the trust-ca down to the
445 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447 * int *flags). With the flags representing current flags for
448 * that specific certificate and the certificate depth from
449 * the bottom (Peer cert depth = 0).
450 *
451 * All flags left after returning from the callback
452 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200453 * return 0 for anything (including invalid certificates)
454 * other than fatal error, as a non-zero return code
455 * immediately aborts the verification process. For fatal
456 * errors, a specific error code should be used (different
457 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
458 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
459 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100461 * \note In case verification failed, the results can be displayed
462 * using \c mbedtls_x509_crt_verify_info()
463 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200464 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
465 * default security profile.
466 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100467 * \note It is your responsibility to provide up-to-date CRLs for
468 * all trusted CAs. If no CRL is provided for the CA that was
469 * used to sign the certificate, CRL verification is skipped
470 * silently, that is *without* setting any flag.
471 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200472 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200473 * (1) those of trusted root CAs, so that certificates
474 * chaining up to those CAs will be trusted, and (2)
475 * self-signed end-entity certificates to be trusted (for
476 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200477 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200478 *
Hanno Becker902451d2019-03-27 11:48:40 +0000479 * \param crt The certificate chain to be verified.
480 * \param trust_ca The list of trusted CAs.
481 * \param ca_crl The list of CRLs for trusted CAs.
482 * \param cn The expected Common Name. This may be \c NULL if the
483 * CN need not be verified.
484 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100485 * If the verification couldn't be completed, the flag value is
486 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000487 * \param f_vrfy The verification callback to use. See the documentation
488 * of mbedtls_x509_crt_verify() for more information.
489 * \param p_vrfy The context to be passed to \p f_vrfy.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 *
Hanno Becker902451d2019-03-27 11:48:40 +0000491 * \return \c 0 if the chain is valid with respect to the
492 * passed CN, CAs, CRLs and security profile.
493 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
494 * certificate chain verification failed. In this case,
495 * \c *flags will have one or more
496 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
497 * flags set.
498 * \return Another negative error code in case of a fatal error
499 * encountered during the verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
502 mbedtls_x509_crt *trust_ca,
503 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200504 const char *cn, uint32_t *flags,
505 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200506 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200508/**
Hanno Becker902451d2019-03-27 11:48:40 +0000509 * \brief Verify a chain of certificates with respect to
510 * a configurable security profile.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200511 *
512 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
513 * security profile.
514 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200515 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200516 * for ECDSA) apply to all certificates: trusted root,
517 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200518 *
Hanno Becker902451d2019-03-27 11:48:40 +0000519 * \param crt The certificate chain to be verified.
520 * \param trust_ca The list of trusted CAs.
521 * \param ca_crl The list of CRLs for trusted CAs.
522 * \param profile The security profile to use for the verification.
523 * \param cn The expected Common Name. This may be \c NULL if the
524 * CN need not be verified.
525 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100526 * If the verification couldn't be completed, the flag value is
527 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000528 * \param f_vrfy The verification callback to use. See the documentation
529 * of mbedtls_x509_crt_verify() for more information.
530 * \param p_vrfy The context to be passed to \p f_vrfy.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200531 *
Hanno Becker902451d2019-03-27 11:48:40 +0000532 * \return \c 0 if the chain is valid with respect to the
533 * passed CN, CAs, CRLs and security profile.
534 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
535 * certificate chain verification failed. In this case,
536 * \c *flags will have one or more
537 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
538 * flags set.
539 * \return Another negative error code in case of a fatal error
540 * encountered during the verification process.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200541 */
542int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
543 mbedtls_x509_crt *trust_ca,
544 mbedtls_x509_crl *ca_crl,
545 const mbedtls_x509_crt_profile *profile,
546 const char *cn, uint32_t *flags,
547 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
548 void *p_vrfy );
549
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200550/**
551 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
552 *
553 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
554 * but can return early and restart according to the limit
555 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
556 *
Hanno Becker902451d2019-03-27 11:48:40 +0000557 * \param crt The certificate chain to be verified.
558 * \param trust_ca The list of trusted CAs.
559 * \param ca_crl The list of CRLs for trusted CAs.
560 * \param profile The security profile to use for the verification.
561 * \param cn The expected Common Name. This may be \c NULL if the
562 * CN need not be verified.
563 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100564 * If the verification couldn't be completed, the flag value is
565 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000566 * \param f_vrfy The verification callback to use. See the documentation
567 * of mbedtls_x509_crt_verify() for more information.
568 * \param p_vrfy The context to be passed to \p f_vrfy.
569 * \param rs_ctx The restart context to use. This may be set to \c NULL
570 * to disable restartable ECC.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200571 *
572 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200573 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200574 * operations was reached: see \c mbedtls_ecp_set_max_ops().
575 */
576int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
577 mbedtls_x509_crt *trust_ca,
578 mbedtls_x509_crl *ca_crl,
579 const mbedtls_x509_crt_profile *profile,
580 const char *cn, uint32_t *flags,
581 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
582 void *p_vrfy,
583 mbedtls_x509_crt_restart_ctx *rs_ctx );
584
Hanno Becker5c8df782019-03-27 11:01:17 +0000585/**
586 * \brief The type of trusted certificate callbacks.
587 *
588 * Callbacks of this type are passed to and used by the CRT
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300589 * verification routine mbedtls_x509_crt_verify_with_ca_cb()
Hanno Becker5c8df782019-03-27 11:01:17 +0000590 * when looking for trusted signers of a given certificate.
591 *
592 * On success, the callback returns a list of trusted
593 * certificates to be considered as potential signers
594 * for the input certificate.
595 *
596 * \param p_ctx An opaque context passed to the callback.
597 * \param child The certificate for which to search a potential signer.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300598 * This will point to a readable certificate.
Hanno Becker5c8df782019-03-27 11:01:17 +0000599 * \param candidate_cas The address at which to store the address of the first
600 * entry in the generated linked list of candidate signers.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300601 * This will not be \c NULL.
Hanno Becker5c8df782019-03-27 11:01:17 +0000602 *
603 * \note The callback must only return a non-zero value on a
604 * fatal error. If, in contrast, the search for a potential
605 * signer completes without a single candidate, the
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300606 * callback must return \c 0 and set \c *candidate_cas
Hanno Becker5c8df782019-03-27 11:01:17 +0000607 * to \c NULL.
608 *
609 * \return \c 0 on success. In this case, \c *candidate_cas points
610 * to a heap-allocated linked list of instances of
611 * ::mbedtls_x509_crt, and ownership of this list is passed
612 * to the caller.
613 * \return A negative error code on failure.
614 */
615typedef int (*mbedtls_x509_crt_ca_cb_t)( void *p_ctx,
616 mbedtls_x509_crt const *child,
617 mbedtls_x509_crt **candidate_cas );
Hanno Beckere15dae72019-03-28 13:55:38 +0000618
619#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker5c8df782019-03-27 11:01:17 +0000620/**
621 * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
622 * uses a callback to acquire the list of trusted CA
623 * certificates.
624 *
625 * \param crt The certificate chain to be verified.
626 * \param f_ca_cb The callback to be used to query for potential signers
627 * of a given child certificate. See the documentation of
628 * ::mbedtls_x509_crt_ca_cb_t for more information.
629 * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
630 * \param profile The security profile for the verification.
631 * \param cn The expected Common Name. This may be \c NULL if the
632 * CN need not be verified.
633 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100634 * If the verification couldn't be completed, the flag value is
635 * set to (uint32_t) -1.
Hanno Becker5c8df782019-03-27 11:01:17 +0000636 * \param f_vrfy The verification callback to use. See the documentation
637 * of mbedtls_x509_crt_verify() for more information.
638 * \param p_vrfy The context to be passed to \p f_vrfy.
639 *
640 * \return See \c mbedtls_crt_verify_with_profile().
641 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300642int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker5c8df782019-03-27 11:01:17 +0000643 mbedtls_x509_crt_ca_cb_t f_ca_cb,
644 void *p_ca_cb,
645 const mbedtls_x509_crt_profile *profile,
646 const char *cn, uint32_t *flags,
647 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
648 void *p_vrfy );
649
650#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200653/**
654 * \brief Check usage of certificate against keyUsage extension.
655 *
656 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200657 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
658 * before using the certificate to perform an RSA key
659 * exchange).
660 *
661 * \note Except for decipherOnly and encipherOnly, a bit set in the
662 * usage argument means this bit MUST be set in the
663 * certificate. For decipherOnly and encipherOnly, it means
664 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200665 *
666 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200668 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200669 *
670 * \note You should only call this function on leaf certificates, on
671 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200673 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200674int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
675 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200679/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000680 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200681 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000682 * \param crt Leaf certificate used.
683 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
684 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200686 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000687 * \return 0 if this use of the certificate is allowed,
688 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200689 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000690 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000693 const char *usage_oid,
694 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000695#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200699 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700 *
701 * \param crt a certificate to be verified
702 * \param crl the CRL to verify against
703 *
704 * \return 1 if the certificate is revoked, 0 otherwise
705 *
706 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100707int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709
710/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200711 * \brief Initialize a certificate (chain)
712 *
713 * \param crt Certificate chain to initialize
714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200716
717/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718 * \brief Unallocate all certificate data
719 *
720 * \param crt Certificate chain to free
721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200723
724#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
725/**
726 * \brief Initialize a restart context
727 */
728void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
729
730/**
731 * \brief Free the components of a restart context
732 */
733void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
734#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736
737/* \} name */
738/* \} addtogroup x509_module */
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741/**
742 * \brief Initialize a CRT writing context
743 *
744 * \param ctx CRT context to initialize
745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200747
748/**
749 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200751 *
752 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
754 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757
758/**
759 * \brief Set the serial number for a Certificate.
760 *
761 * \param ctx CRT context to use
762 * \param serial serial number to set
763 *
764 * \return 0 if successful
765 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200767
768/**
769 * \brief Set the validity period for a Certificate
770 * Timestamps should be in string format for UTC timezone
771 * i.e. "YYYYMMDDhhmmss"
772 * e.g. "20131231235959" for December 31st 2013
773 * at 23:59:59
774 *
775 * \param ctx CRT context to use
776 * \param not_before not_before timestamp
777 * \param not_after not_after timestamp
778 *
779 * \return 0 if timestamp was parsed successfully, or
780 * a specific error code
781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100783 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784
785/**
786 * \brief Set the issuer name for a Certificate
787 * Issuer names should contain a comma-separated list
788 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000789 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200790 *
791 * \param ctx CRT context to use
792 * \param issuer_name issuer name to set
793 *
794 * \return 0 if issuer name was parsed successfully, or
795 * a specific error code
796 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100798 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799
800/**
801 * \brief Set the subject name for a Certificate
802 * Subject names should contain a comma-separated list
803 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000804 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200805 *
806 * \param ctx CRT context to use
807 * \param subject_name subject name to set
808 *
809 * \return 0 if subject name was parsed successfully, or
810 * a specific error code
811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100813 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200814
815/**
816 * \brief Set the subject public key for the certificate
817 *
818 * \param ctx CRT context to use
819 * \param key public key to include
820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200822
823/**
824 * \brief Set the issuer key used for signing the certificate
825 *
826 * \param ctx CRT context to use
827 * \param key private key to sign with
828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830
831/**
832 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200834 *
835 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100836 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839
840/**
841 * \brief Generic function to add to or replace an extension in the
842 * CRT
843 *
844 * \param ctx CRT context to use
845 * \param oid OID of the extension
846 * \param oid_len length of the OID
847 * \param critical if the extension is critical (per the RFC's definition)
848 * \param val value of the extension OCTET STRING
849 * \param val_len length of the value data
850 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200851 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200854 const char *oid, size_t oid_len,
855 int critical,
856 const unsigned char *val, size_t val_len );
857
858/**
859 * \brief Set the basicConstraints extension for a CRT
860 *
861 * \param ctx CRT context to use
862 * \param is_ca is this a CA certificate
863 * \param max_pathlen maximum length of certificate chains below this
864 * certificate (only for CA certificates, -1 is
865 * inlimited)
866 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200867 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870 int is_ca, int max_pathlen );
871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873/**
874 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876 * called before
877 *
878 * \param ctx CRT context to use
879 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200880 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883
884/**
885 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 * called before
888 *
889 * \param ctx CRT context to use
890 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200891 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
894#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895
896/**
897 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 *
900 * \param ctx CRT context to use
901 * \param key_usage key usage flags to set
902 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200903 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200905int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
906 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907
908/**
909 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911 *
912 * \param ctx CRT context to use
913 * \param ns_cert_type Netscape Cert Type flags to set
914 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200915 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918 unsigned char ns_cert_type );
919
920/**
921 * \brief Free the contents of a CRT write context
922 *
923 * \param ctx CRT context to free
924 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926
927/**
928 * \brief Write a built up certificate to a X509 DER structure
929 * Note: data is written at the end of the buffer! Use the
930 * return value to determine where you should start
931 * using the buffer
932 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100933 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934 * \param buf buffer to write to
935 * \param size size of the buffer
936 * \param f_rng RNG function (for signature, see note)
937 * \param p_rng RNG parameter
938 *
939 * \return length of data written if successful, or a specific
940 * error code
941 *
942 * \note f_rng may be NULL if RSA is used for signature and the
943 * signature is made offline (otherwise f_rng is desirable
944 * for countermeasures against timing attacks).
945 * ECDSA signatures always require a non-NULL f_rng.
946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948 int (*f_rng)(void *, unsigned char *, size_t),
949 void *p_rng );
950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952/**
953 * \brief Write a built up certificate to a X509 PEM string
954 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100955 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956 * \param buf buffer to write to
957 * \param size size of the buffer
958 * \param f_rng RNG function (for signature, see note)
959 * \param p_rng RNG parameter
960 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200961 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 *
963 * \note f_rng may be NULL if RSA is used for signature and the
964 * signature is made offline (otherwise f_rng is desirable
965 * for countermeasures against timing attacks).
966 * ECDSA signatures always require a non-NULL f_rng.
967 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969 int (*f_rng)(void *, unsigned char *, size_t),
970 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#endif /* MBEDTLS_PEM_WRITE_C */
972#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973
974#ifdef __cplusplus
975}
976#endif
977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978#endif /* mbedtls_x509_crt.h */