blob: 81a2de2be2eb6461aa0a11f2653625e64f39b54e [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 Eldor78c30402019-05-13 15:49:53 +030079 mbedtls_x509_sequence subject_alt_names; /**< Optional list of raw entries of Subject Alternative Names extension (currently only dNSName and OtherName are listed). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080
Ron Eldor78c30402019-05-13 15:49:53 +030081 mbedtls_x509_sequence certificate_policies; /**< Optional list of certificate policies (Only anyPolicy supported). */
Ron Eldor74d9acc2019-03-21 14:00:03 +020082
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083 int ext_types; /**< Bit string containing detected and parsed extensions */
84 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
85 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+ */
86
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +010087 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020091 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
94 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
95 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
96 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 +020097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101
Janos Follath5091bec2019-05-08 15:23:08 +0100102/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200103 * From RFC 5280 section 4.2.1.6:
104 * OtherName ::= SEQUENCE {
105 * type-id OBJECT IDENTIFIER,
106 * value [0] EXPLICIT ANY DEFINED BY type-id }
107 */
108typedef struct mbedtls_x509_san_other_name
109{
Ron Eldor890819a2019-05-13 19:03:04 +0300110 /**
111 * The type_id is an OID as deifned in RFC 5280.
112 * To check the value of the type id, you should use
113 * \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf.
114 */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200115 mbedtls_x509_buf type_id; /**< The type id. */
116 union
117 {
Janos Follath5091bec2019-05-08 15:23:08 +0100118 /**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200119 * From RFC 4108 section 5:
120 * HardwareModuleName ::= SEQUENCE {
121 * hwType OBJECT IDENTIFIER,
122 * hwSerialNum OCTET STRING }
123 */
Ron Eldorf05f5942019-05-13 19:11:31 +0300124 struct
125 {
126 mbedtls_x509_buf oid; /**< The object identifier. */
127 mbedtls_x509_buf val; /**< The named value. */
128 }
129 hardware_module_name;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200130 }
131 value;
132}
133mbedtls_x509_san_other_name;
134
Janos Follath5091bec2019-05-08 15:23:08 +0100135/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200136 * A structure for holding the parsed Subject Alternative Name, according to type
137 */
138typedef struct mbedtls_x509_subject_alternative_name
139{
140 int type; /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */
141 union {
142 mbedtls_x509_san_other_name other_name; /**< The otherName supported type. */
143 mbedtls_x509_buf unstructured_name; /**< The buffer for the un constructed types. Only dnsName currently supported */
144 }
145 san; /**< A union of the supported SAN types */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200146}
147mbedtls_x509_subject_alternative_name;
148
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200149/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200150 * Build flag from an algorithm/curve identifier (pk, md, ecp)
151 * Since 0 is always XXX_NONE, ignore it.
152 */
Hanno Becker1eeca412018-10-15 12:01:35 +0100153#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200154
155/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200156 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200157 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200158 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200159 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200160typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200161{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200162 uint32_t allowed_mds; /**< MDs for signatures */
163 uint32_t allowed_pks; /**< PK algs for signatures */
164 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
165 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200166}
167mbedtls_x509_crt_profile;
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#define MBEDTLS_X509_CRT_VERSION_1 0
170#define MBEDTLS_X509_CRT_VERSION_2 1
171#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
174#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200175
Andres AGf9113192016-09-02 14:06:04 +0100176#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
177#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
178#endif
179
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200180/**
181 * Container for writing a certificate (CRT)
182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200184{
185 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_mpi serial;
187 mbedtls_pk_context *subject_key;
188 mbedtls_pk_context *issuer_key;
189 mbedtls_asn1_named_data *subject;
190 mbedtls_asn1_named_data *issuer;
191 mbedtls_md_type_t md_alg;
192 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
193 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
194 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200195}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200197
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200198/**
199 * Item in a verification chain: cert and flags for it
200 */
201typedef struct {
202 mbedtls_x509_crt *crt;
203 uint32_t flags;
204} mbedtls_x509_crt_verify_chain_item;
205
206/**
207 * Max size of verification chain: end-entity + intermediates + trusted root
208 */
209#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
210
211/**
212 * Verification chain as built by \c mbedtls_crt_verify_chain()
213 */
214typedef struct
215{
216 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200217 unsigned len;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000218
219#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
220 /* This stores the list of potential trusted signers obtained from
221 * the CA callback used for the CRT verification, if configured.
222 * We must track it somewhere because the callback passes its
223 * ownership to the caller. */
224 mbedtls_x509_crt *trust_ca_cb_result;
225#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200226} mbedtls_x509_crt_verify_chain;
227
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200228#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
229
230/**
231 * \brief Context for resuming X.509 verify operations
232 */
233typedef struct
234{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200235 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200236 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200237
238 /* for find_parent_in() */
239 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
240 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200241 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200242
243 /* for find_parent() */
244 int parent_is_trusted; /* -1 if find_parent is not in progress */
245
246 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200247 enum {
248 x509_crt_rs_none,
249 x509_crt_rs_find_parent,
250 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200251 int self_cnt;
252 mbedtls_x509_crt_verify_chain ver_chain;
253
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200254} mbedtls_x509_crt_restart_ctx;
255
256#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
257
258/* Now we can declare functions that take a pointer to that */
259typedef void mbedtls_x509_crt_restart_ctx;
260
261#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200265 * Default security profile. Should provide a good balance between security
266 * and compatibility with current deployments.
267 */
268extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
269
270/**
271 * Expected next default profile. Recommended for new deployments.
272 * Currently targets a 128-bit security level, except for RSA-2048.
273 */
274extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
275
276/**
277 * NSA Suite B profile.
278 */
279extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
280
281/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200282 * \brief Parse a single DER formatted certificate and add it
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000283 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000285 * \param chain The pointer to the start of the CRT chain to attach to.
286 * When parsing the first CRT in a chain, this should point
287 * to an instance of ::mbedtls_x509_crt initialized through
288 * mbedtls_x509_crt_init().
289 * \param buf The buffer holding the DER encoded certificate.
290 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000292 * \note This function makes an internal copy of the CRT buffer
293 * \p buf. In particular, \p buf may be destroyed or reused
294 * after this call returns. To avoid duplicating the CRT
295 * buffer (at the cost of stricter lifetime constraints),
296 * use mbedtls_x509_crt_parse_der_nocopy() instead.
297 *
298 * \return \c 0 if successful.
299 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000301int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
302 const unsigned char *buf,
303 size_t buflen );
304
305/**
306 * \brief Parse a single DER formatted certificate and add it
307 * to the end of the provided chained list. This is a
308 * variant of mbedtls_x509_crt_parse_der() which takes
309 * temporary ownership of the CRT buffer until the CRT
310 * is destroyed.
311 *
312 * \param chain The pointer to the start of the CRT chain to attach to.
313 * When parsing the first CRT in a chain, this should point
314 * to an instance of ::mbedtls_x509_crt initialized through
315 * mbedtls_x509_crt_init().
316 * \param buf The address of the readable buffer holding the DER encoded
317 * certificate to use. On success, this buffer must be
318 * retained and not be changed for the liftetime of the
319 * CRT chain \p chain, that is, until \p chain is destroyed
320 * through a call to mbedtls_x509_crt_free().
321 * \param buflen The size in Bytes of \p buf.
322 *
323 * \note This call is functionally equivalent to
324 * mbedtls_x509_crt_parse_der(), but it avoids creating a
325 * copy of the input buffer at the cost of stronger lifetime
326 * constraints. This is useful in constrained environments
327 * where duplication of the CRT cannot be tolerated.
328 *
329 * \return \c 0 if successful.
330 * \return A negative error code on failure.
331 */
332int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
333 const unsigned char *buf,
334 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335
336/**
Hanno Becker89a91122018-08-23 16:14:00 +0100337 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100338 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100340 * For CRTs in PEM encoding, the function parses permissively:
341 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100342 * returns the number of certificates for which parsing failed
343 * (hence \c 0 if all certificates were parsed successfully).
344 * If no certificate could be parsed, the function returns
345 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200346 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100347 * PEM encoded certificates may be interleaved by other data
348 * such as human readable descriptions of their content, as
349 * long as the certificates are enclosed in the PEM specific
350 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
351 *
352 * \param chain The chain to which to add the parsed certificates.
353 * \param buf The buffer holding the certificate data in PEM or DER format.
354 * For certificates in PEM encoding, this may be a concatenation
355 * of multiple certificates; for DER encoding, the buffer must
356 * comprise exactly one certificate.
357 * \param buflen The size of \p buf, including the terminating \c NULL byte
358 * in case of PEM encoded data.
359 *
360 * \return \c 0 if all certificates were parsed successfully.
361 * \return The (positive) number of certificates that couldn't
362 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100363 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100364 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369/**
370 * \brief Load one or more certificates and add them
371 * to the chained list. Parses permissively. If some
372 * certificates can be parsed, the result is the number
373 * of failed certificates it encountered. If none complete
374 * correctly, the first error is returned.
375 *
376 * \param chain points to the start of the chain
377 * \param path filename to read the certificates from
378 *
379 * \return 0 if all certificates parsed successfully, a positive number
380 * if partly successful or a specific X509 or PEM error code
381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383
384/**
385 * \brief Load one or more certificate files from a path and add them
386 * to the chained list. Parses permissively. If some
387 * certificates can be parsed, the result is the number
388 * of failed certificates it encountered. If none complete
389 * correctly, the first error is returned.
390 *
391 * \param chain points to the start of the chain
392 * \param path directory / folder to read the certificate files from
393 *
394 * \return 0 if all certificates parsed successfully, a positive number
395 * if partly successful or a specific X509 or PEM error code
396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
Janos Follath11b41eb2019-05-08 15:26:49 +0100398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#endif /* MBEDTLS_FS_IO */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200400/**
Ron Eldor890819a2019-05-13 19:03:04 +0300401 * \brief Parses a subject alternative name item
402 * to an identified structure;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200403 *
Ron Eldor890819a2019-05-13 19:03:04 +0300404 * \param san_buf The buffer holding the raw data item of the subject
405 * alternative name.
406 * \param san The target structure to populate with the parsed presentation
407 * of the subject alternative name encoded in \p san_raw.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200408 *
409 * \note Only "dnsName" and "otherName" of type hardware_module_name,
410 * as defined in RFC 4180 is supported.
411 *
Ron Eldor890819a2019-05-13 19:03:04 +0300412 * \note This function should be called on a single raw data of
413 * subject alternative name. For example, after successfult
414 * certificate parsing, one must iterate on every item in the
415 * \p crt->subject_alt_names sequence, and send it as parameter
416 * to this function.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200417 *
Ron Eldor890819a2019-05-13 19:03:04 +0300418 * \return \c 0 on success
419 * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported
420 * SAN type
421 * \return Negative value for any other failure.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200422 */
Ron Eldor890819a2019-05-13 19:03:04 +0300423int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
424 mbedtls_x509_subject_alternative_name *san );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425/**
426 * \brief Returns an informational string about the
427 * certificate.
428 *
429 * \param buf Buffer to write to
430 * \param size Maximum size of buffer
431 * \param prefix A line prefix
432 * \param crt The X509 certificate to represent
433 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200434 * \return The length of the string written (not including the
435 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
438 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439
440/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100441 * \brief Returns an informational string about the
442 * verification status of a certificate.
443 *
444 * \param buf Buffer to write to
445 * \param size Maximum size of buffer
446 * \param prefix A line prefix
447 * \param flags Verification flags created by mbedtls_x509_crt_verify()
448 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200449 * \return The length of the string written (not including the
450 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100451 */
452int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200453 uint32_t flags );
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200454
455/**
Hanno Becker902451d2019-03-27 11:48:40 +0000456 * \brief Verify a chain of certificates.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457 *
458 * The verify callback is a user-supplied callback that
459 * can clear / modify / add flags for a certificate. If set,
460 * the verification callback is called for each
461 * certificate in the chain (from the trust-ca down to the
462 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464 * int *flags). With the flags representing current flags for
465 * that specific certificate and the certificate depth from
466 * the bottom (Peer cert depth = 0).
467 *
468 * All flags left after returning from the callback
469 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200470 * return 0 for anything (including invalid certificates)
471 * other than fatal error, as a non-zero return code
472 * immediately aborts the verification process. For fatal
473 * errors, a specific error code should be used (different
474 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
475 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
476 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100478 * \note In case verification failed, the results can be displayed
479 * using \c mbedtls_x509_crt_verify_info()
480 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200481 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
482 * default security profile.
483 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100484 * \note It is your responsibility to provide up-to-date CRLs for
485 * all trusted CAs. If no CRL is provided for the CA that was
486 * used to sign the certificate, CRL verification is skipped
487 * silently, that is *without* setting any flag.
488 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200489 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200490 * (1) those of trusted root CAs, so that certificates
491 * chaining up to those CAs will be trusted, and (2)
492 * self-signed end-entity certificates to be trusted (for
493 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200494 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200495 *
Hanno Becker902451d2019-03-27 11:48:40 +0000496 * \param crt The certificate chain to be verified.
497 * \param trust_ca The list of trusted CAs.
498 * \param ca_crl The list of CRLs for trusted CAs.
499 * \param cn The expected Common Name. This may be \c NULL if the
500 * CN need not be verified.
501 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100502 * If the verification couldn't be completed, the flag value is
503 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000504 * \param f_vrfy The verification callback to use. See the documentation
505 * of mbedtls_x509_crt_verify() for more information.
506 * \param p_vrfy The context to be passed to \p f_vrfy.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 *
Hanno Becker902451d2019-03-27 11:48:40 +0000508 * \return \c 0 if the chain is valid with respect to the
509 * passed CN, CAs, CRLs and security profile.
510 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
511 * certificate chain verification failed. In this case,
512 * \c *flags will have one or more
513 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
514 * flags set.
515 * \return Another negative error code in case of a fatal error
516 * encountered during the verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
519 mbedtls_x509_crt *trust_ca,
520 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200521 const char *cn, uint32_t *flags,
522 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200523 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200525/**
Hanno Becker902451d2019-03-27 11:48:40 +0000526 * \brief Verify a chain of certificates with respect to
527 * a configurable security profile.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200528 *
529 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
530 * security profile.
531 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200532 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200533 * for ECDSA) apply to all certificates: trusted root,
534 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200535 *
Hanno Becker902451d2019-03-27 11:48:40 +0000536 * \param crt The certificate chain to be verified.
537 * \param trust_ca The list of trusted CAs.
538 * \param ca_crl The list of CRLs for trusted CAs.
539 * \param profile The security profile to use for the verification.
540 * \param cn The expected Common Name. This may be \c NULL if the
541 * CN need not be verified.
542 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100543 * If the verification couldn't be completed, the flag value is
544 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000545 * \param f_vrfy The verification callback to use. See the documentation
546 * of mbedtls_x509_crt_verify() for more information.
547 * \param p_vrfy The context to be passed to \p f_vrfy.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200548 *
Hanno Becker902451d2019-03-27 11:48:40 +0000549 * \return \c 0 if the chain is valid with respect to the
550 * passed CN, CAs, CRLs and security profile.
551 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
552 * certificate chain verification failed. In this case,
553 * \c *flags will have one or more
554 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
555 * flags set.
556 * \return Another negative error code in case of a fatal error
557 * encountered during the verification process.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200558 */
559int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
560 mbedtls_x509_crt *trust_ca,
561 mbedtls_x509_crl *ca_crl,
562 const mbedtls_x509_crt_profile *profile,
563 const char *cn, uint32_t *flags,
564 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
565 void *p_vrfy );
566
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200567/**
568 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
569 *
570 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
571 * but can return early and restart according to the limit
572 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
573 *
Hanno Becker902451d2019-03-27 11:48:40 +0000574 * \param crt The certificate chain to be verified.
575 * \param trust_ca The list of trusted CAs.
576 * \param ca_crl The list of CRLs for trusted CAs.
577 * \param profile The security profile to use for the verification.
578 * \param cn The expected Common Name. This may be \c NULL if the
579 * CN need not be verified.
580 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100581 * If the verification couldn't be completed, the flag value is
582 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000583 * \param f_vrfy The verification callback to use. See the documentation
584 * of mbedtls_x509_crt_verify() for more information.
585 * \param p_vrfy The context to be passed to \p f_vrfy.
586 * \param rs_ctx The restart context to use. This may be set to \c NULL
587 * to disable restartable ECC.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200588 *
589 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200590 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200591 * operations was reached: see \c mbedtls_ecp_set_max_ops().
592 */
593int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
594 mbedtls_x509_crt *trust_ca,
595 mbedtls_x509_crl *ca_crl,
596 const mbedtls_x509_crt_profile *profile,
597 const char *cn, uint32_t *flags,
598 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
599 void *p_vrfy,
600 mbedtls_x509_crt_restart_ctx *rs_ctx );
601
Hanno Becker5c8df782019-03-27 11:01:17 +0000602/**
603 * \brief The type of trusted certificate callbacks.
604 *
605 * Callbacks of this type are passed to and used by the CRT
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300606 * verification routine mbedtls_x509_crt_verify_with_ca_cb()
Hanno Becker5c8df782019-03-27 11:01:17 +0000607 * when looking for trusted signers of a given certificate.
608 *
609 * On success, the callback returns a list of trusted
610 * certificates to be considered as potential signers
611 * for the input certificate.
612 *
613 * \param p_ctx An opaque context passed to the callback.
614 * \param child The certificate for which to search a potential signer.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300615 * This will point to a readable certificate.
Hanno Becker5c8df782019-03-27 11:01:17 +0000616 * \param candidate_cas The address at which to store the address of the first
617 * entry in the generated linked list of candidate signers.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300618 * This will not be \c NULL.
Hanno Becker5c8df782019-03-27 11:01:17 +0000619 *
620 * \note The callback must only return a non-zero value on a
621 * fatal error. If, in contrast, the search for a potential
622 * signer completes without a single candidate, the
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300623 * callback must return \c 0 and set \c *candidate_cas
Hanno Becker5c8df782019-03-27 11:01:17 +0000624 * to \c NULL.
625 *
626 * \return \c 0 on success. In this case, \c *candidate_cas points
627 * to a heap-allocated linked list of instances of
628 * ::mbedtls_x509_crt, and ownership of this list is passed
629 * to the caller.
630 * \return A negative error code on failure.
631 */
632typedef int (*mbedtls_x509_crt_ca_cb_t)( void *p_ctx,
633 mbedtls_x509_crt const *child,
634 mbedtls_x509_crt **candidate_cas );
Hanno Beckere15dae72019-03-28 13:55:38 +0000635
636#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker5c8df782019-03-27 11:01:17 +0000637/**
638 * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
639 * uses a callback to acquire the list of trusted CA
640 * certificates.
641 *
642 * \param crt The certificate chain to be verified.
643 * \param f_ca_cb The callback to be used to query for potential signers
644 * of a given child certificate. See the documentation of
645 * ::mbedtls_x509_crt_ca_cb_t for more information.
646 * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
647 * \param profile The security profile for the verification.
648 * \param cn The expected Common Name. This may be \c NULL if the
649 * CN need not be verified.
650 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100651 * If the verification couldn't be completed, the flag value is
652 * set to (uint32_t) -1.
Hanno Becker5c8df782019-03-27 11:01:17 +0000653 * \param f_vrfy The verification callback to use. See the documentation
654 * of mbedtls_x509_crt_verify() for more information.
655 * \param p_vrfy The context to be passed to \p f_vrfy.
656 *
657 * \return See \c mbedtls_crt_verify_with_profile().
658 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300659int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker5c8df782019-03-27 11:01:17 +0000660 mbedtls_x509_crt_ca_cb_t f_ca_cb,
661 void *p_ca_cb,
662 const mbedtls_x509_crt_profile *profile,
663 const char *cn, uint32_t *flags,
664 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
665 void *p_vrfy );
666
667#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200670/**
671 * \brief Check usage of certificate against keyUsage extension.
672 *
673 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200674 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
675 * before using the certificate to perform an RSA key
676 * exchange).
677 *
678 * \note Except for decipherOnly and encipherOnly, a bit set in the
679 * usage argument means this bit MUST be set in the
680 * certificate. For decipherOnly and encipherOnly, it means
681 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200682 *
683 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200685 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200686 *
687 * \note You should only call this function on leaf certificates, on
688 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200690 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200691int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
692 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200696/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000697 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200698 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000699 * \param crt Leaf certificate used.
700 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
701 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200703 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000704 * \return 0 if this use of the certificate is allowed,
705 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200706 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000707 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000710 const char *usage_oid,
711 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000712#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200716 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717 *
718 * \param crt a certificate to be verified
719 * \param crl the CRL to verify against
720 *
721 * \return 1 if the certificate is revoked, 0 otherwise
722 *
723 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100724int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726
727/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200728 * \brief Initialize a certificate (chain)
729 *
730 * \param crt Certificate chain to initialize
731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200733
734/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735 * \brief Unallocate all certificate data
736 *
737 * \param crt Certificate chain to free
738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200740
741#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
742/**
743 * \brief Initialize a restart context
744 */
745void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
746
747/**
748 * \brief Free the components of a restart context
749 */
750void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
751#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753
754/* \} name */
755/* \} addtogroup x509_module */
756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200758/**
759 * \brief Initialize a CRT writing context
760 *
761 * \param ctx CRT context to initialize
762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764
765/**
766 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 *
769 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
771 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774
775/**
776 * \brief Set the serial number for a Certificate.
777 *
778 * \param ctx CRT context to use
779 * \param serial serial number to set
780 *
781 * \return 0 if successful
782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784
785/**
786 * \brief Set the validity period for a Certificate
787 * Timestamps should be in string format for UTC timezone
788 * i.e. "YYYYMMDDhhmmss"
789 * e.g. "20131231235959" for December 31st 2013
790 * at 23:59:59
791 *
792 * \param ctx CRT context to use
793 * \param not_before not_before timestamp
794 * \param not_after not_after timestamp
795 *
796 * \return 0 if timestamp was parsed successfully, or
797 * a specific error code
798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100800 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801
802/**
803 * \brief Set the issuer name for a Certificate
804 * Issuer names should contain a comma-separated list
805 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000806 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807 *
808 * \param ctx CRT context to use
809 * \param issuer_name issuer name to set
810 *
811 * \return 0 if issuer name was parsed successfully, or
812 * a specific error code
813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100815 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816
817/**
818 * \brief Set the subject name for a Certificate
819 * Subject names should contain a comma-separated list
820 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000821 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200822 *
823 * \param ctx CRT context to use
824 * \param subject_name subject name to set
825 *
826 * \return 0 if subject name was parsed successfully, or
827 * a specific error code
828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100830 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200831
832/**
833 * \brief Set the subject public key for the certificate
834 *
835 * \param ctx CRT context to use
836 * \param key public key to include
837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839
840/**
841 * \brief Set the issuer key used for signing the certificate
842 *
843 * \param ctx CRT context to use
844 * \param key private key to sign with
845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200847
848/**
849 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851 *
852 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100853 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856
857/**
858 * \brief Generic function to add to or replace an extension in the
859 * CRT
860 *
861 * \param ctx CRT context to use
862 * \param oid OID of the extension
863 * \param oid_len length of the OID
864 * \param critical if the extension is critical (per the RFC's definition)
865 * \param val value of the extension OCTET STRING
866 * \param val_len length of the value data
867 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200868 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200871 const char *oid, size_t oid_len,
872 int critical,
873 const unsigned char *val, size_t val_len );
874
875/**
876 * \brief Set the basicConstraints extension for a CRT
877 *
878 * \param ctx CRT context to use
879 * \param is_ca is this a CA certificate
880 * \param max_pathlen maximum length of certificate chains below this
881 * certificate (only for CA certificates, -1 is
882 * inlimited)
883 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200884 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 int is_ca, int max_pathlen );
888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890/**
891 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200893 * called before
894 *
895 * \param ctx CRT context to use
896 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200897 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900
901/**
902 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 * called before
905 *
906 * \param ctx CRT context to use
907 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200908 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
911#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912
913/**
914 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916 *
917 * \param ctx CRT context to use
918 * \param key_usage key usage flags to set
919 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200920 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200922int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
923 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924
925/**
926 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928 *
929 * \param ctx CRT context to use
930 * \param ns_cert_type Netscape Cert Type flags to set
931 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200932 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935 unsigned char ns_cert_type );
936
937/**
938 * \brief Free the contents of a CRT write context
939 *
940 * \param ctx CRT context to free
941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943
944/**
945 * \brief Write a built up certificate to a X509 DER structure
946 * Note: data is written at the end of the buffer! Use the
947 * return value to determine where you should start
948 * using the buffer
949 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100950 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 * \param buf buffer to write to
952 * \param size size of the buffer
953 * \param f_rng RNG function (for signature, see note)
954 * \param p_rng RNG parameter
955 *
956 * \return length of data written if successful, or a specific
957 * error code
958 *
959 * \note f_rng may be NULL if RSA is used for signature and the
960 * signature is made offline (otherwise f_rng is desirable
961 * for countermeasures against timing attacks).
962 * ECDSA signatures always require a non-NULL f_rng.
963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965 int (*f_rng)(void *, unsigned char *, size_t),
966 void *p_rng );
967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969/**
970 * \brief Write a built up certificate to a X509 PEM string
971 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100972 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973 * \param buf buffer to write to
974 * \param size size of the buffer
975 * \param f_rng RNG function (for signature, see note)
976 * \param p_rng RNG parameter
977 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200978 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979 *
980 * \note f_rng may be NULL if RSA is used for signature and the
981 * signature is made offline (otherwise f_rng is desirable
982 * for countermeasures against timing attacks).
983 * ECDSA signatures always require a non-NULL f_rng.
984 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 int (*f_rng)(void *, unsigned char *, size_t),
987 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988#endif /* MBEDTLS_PEM_WRITE_C */
989#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990
991#ifdef __cplusplus
992}
993#endif
994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#endif /* mbedtls_x509_crt.h */