blob: 6983d60e82dd2d395ea25dc8663a6e80d27bdb45 [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"
Philippe Antoinedbc0db92018-05-28 14:27:53 +020035#include "bignum.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036
37/**
38 * \addtogroup x509_module
39 * \{
40 */
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46/**
47 * \name Structures and functions for parsing and writing X.509 certificates
48 * \{
49 */
50
51/**
52 * Container for an X.509 certificate. The certificate may be chained.
53 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055{
Hanno Becker1a65dcd2019-01-31 08:57:44 +000056 int own_buffer; /**< Indicates if \c raw is owned
57 * by the structure or not. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
59 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020061 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
63 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
66 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
69 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
72 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073
Hanno Becker494dd7a2019-02-06 16:13:41 +000074 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
78 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
79 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
Ron Eldor78c30402019-05-13 15:49:53 +030080 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 +020081
Ron Eldorc8b5f3f2019-05-15 15:15:55 +030082 mbedtls_x509_sequence certificate_policies; /**< Optional list of certificate policies (Only anyPolicy is printed and enforced, however the rest of the policies are still listed). */
Ron Eldor74d9acc2019-03-21 14:00:03 +020083
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084 int ext_types; /**< Bit string containing detected and parsed extensions */
85 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
86 int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. Path length is 1 higher than RFC 5280 'meaning', so 1+ */
87
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +010088 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020092 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
95 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
96 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
97 void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102
Janos Follath5091bec2019-05-08 15:23:08 +0100103/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200104 * From RFC 5280 section 4.2.1.6:
105 * OtherName ::= SEQUENCE {
106 * type-id OBJECT IDENTIFIER,
107 * value [0] EXPLICIT ANY DEFINED BY type-id }
108 */
109typedef struct mbedtls_x509_san_other_name
110{
Ron Eldor890819a2019-05-13 19:03:04 +0300111 /**
112 * The type_id is an OID as deifned in RFC 5280.
113 * To check the value of the type id, you should use
114 * \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf.
115 */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200116 mbedtls_x509_buf type_id; /**< The type id. */
117 union
118 {
Janos Follath5091bec2019-05-08 15:23:08 +0100119 /**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200120 * From RFC 4108 section 5:
121 * HardwareModuleName ::= SEQUENCE {
122 * hwType OBJECT IDENTIFIER,
123 * hwSerialNum OCTET STRING }
124 */
Ron Eldorf05f5942019-05-13 19:11:31 +0300125 struct
126 {
127 mbedtls_x509_buf oid; /**< The object identifier. */
128 mbedtls_x509_buf val; /**< The named value. */
129 }
130 hardware_module_name;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200131 }
132 value;
133}
134mbedtls_x509_san_other_name;
135
Janos Follath5091bec2019-05-08 15:23:08 +0100136/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200137 * A structure for holding the parsed Subject Alternative Name, according to type
138 */
139typedef struct mbedtls_x509_subject_alternative_name
140{
141 int type; /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */
142 union {
143 mbedtls_x509_san_other_name other_name; /**< The otherName supported type. */
144 mbedtls_x509_buf unstructured_name; /**< The buffer for the un constructed types. Only dnsName currently supported */
145 }
146 san; /**< A union of the supported SAN types */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200147}
148mbedtls_x509_subject_alternative_name;
149
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200150/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200151 * Build flag from an algorithm/curve identifier (pk, md, ecp)
152 * Since 0 is always XXX_NONE, ignore it.
153 */
Hanno Becker1eeca412018-10-15 12:01:35 +0100154#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200155
156/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200157 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200158 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200159 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200160 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200161typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200162{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200163 uint32_t allowed_mds; /**< MDs for signatures */
164 uint32_t allowed_pks; /**< PK algs for signatures */
165 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
166 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200167}
168mbedtls_x509_crt_profile;
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#define MBEDTLS_X509_CRT_VERSION_1 0
171#define MBEDTLS_X509_CRT_VERSION_2 1
172#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
175#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200176
Andres AGf9113192016-09-02 14:06:04 +0100177#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
178#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
179#endif
180
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200181/**
182 * Container for writing a certificate (CRT)
183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200185{
186 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_mpi serial;
188 mbedtls_pk_context *subject_key;
189 mbedtls_pk_context *issuer_key;
190 mbedtls_asn1_named_data *subject;
191 mbedtls_asn1_named_data *issuer;
192 mbedtls_md_type_t md_alg;
193 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
194 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
195 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200198
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200199/**
200 * Item in a verification chain: cert and flags for it
201 */
202typedef struct {
203 mbedtls_x509_crt *crt;
204 uint32_t flags;
205} mbedtls_x509_crt_verify_chain_item;
206
207/**
208 * Max size of verification chain: end-entity + intermediates + trusted root
209 */
210#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
211
212/**
213 * Verification chain as built by \c mbedtls_crt_verify_chain()
214 */
215typedef struct
216{
217 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200218 unsigned len;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000219
220#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
221 /* This stores the list of potential trusted signers obtained from
222 * the CA callback used for the CRT verification, if configured.
223 * We must track it somewhere because the callback passes its
224 * ownership to the caller. */
225 mbedtls_x509_crt *trust_ca_cb_result;
226#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200227} mbedtls_x509_crt_verify_chain;
228
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200229#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
230
231/**
232 * \brief Context for resuming X.509 verify operations
233 */
234typedef struct
235{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200236 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200237 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200238
239 /* for find_parent_in() */
240 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
241 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200242 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200243
244 /* for find_parent() */
245 int parent_is_trusted; /* -1 if find_parent is not in progress */
246
247 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200248 enum {
249 x509_crt_rs_none,
250 x509_crt_rs_find_parent,
251 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200252 int self_cnt;
253 mbedtls_x509_crt_verify_chain ver_chain;
254
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200255} mbedtls_x509_crt_restart_ctx;
256
257#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
258
259/* Now we can declare functions that take a pointer to that */
260typedef void mbedtls_x509_crt_restart_ctx;
261
262#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200266 * Default security profile. Should provide a good balance between security
267 * and compatibility with current deployments.
268 */
269extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
270
271/**
272 * Expected next default profile. Recommended for new deployments.
273 * Currently targets a 128-bit security level, except for RSA-2048.
274 */
275extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
276
277/**
278 * NSA Suite B profile.
279 */
280extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
281
282/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283 * \brief Parse a single DER formatted certificate and add it
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000284 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000286 * \param chain The pointer to the start of the CRT chain to attach to.
287 * When parsing the first CRT in a chain, this should point
288 * to an instance of ::mbedtls_x509_crt initialized through
289 * mbedtls_x509_crt_init().
290 * \param buf The buffer holding the DER encoded certificate.
291 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200292 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000293 * \note This function makes an internal copy of the CRT buffer
294 * \p buf. In particular, \p buf may be destroyed or reused
295 * after this call returns. To avoid duplicating the CRT
296 * buffer (at the cost of stricter lifetime constraints),
297 * use mbedtls_x509_crt_parse_der_nocopy() instead.
298 *
299 * \return \c 0 if successful.
300 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200301 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000302int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
303 const unsigned char *buf,
304 size_t buflen );
305
306/**
307 * \brief Parse a single DER formatted certificate and add it
308 * to the end of the provided chained list. This is a
309 * variant of mbedtls_x509_crt_parse_der() which takes
310 * temporary ownership of the CRT buffer until the CRT
311 * is destroyed.
312 *
313 * \param chain The pointer to the start of the CRT chain to attach to.
314 * When parsing the first CRT in a chain, this should point
315 * to an instance of ::mbedtls_x509_crt initialized through
316 * mbedtls_x509_crt_init().
317 * \param buf The address of the readable buffer holding the DER encoded
318 * certificate to use. On success, this buffer must be
319 * retained and not be changed for the liftetime of the
320 * CRT chain \p chain, that is, until \p chain is destroyed
321 * through a call to mbedtls_x509_crt_free().
322 * \param buflen The size in Bytes of \p buf.
323 *
324 * \note This call is functionally equivalent to
325 * mbedtls_x509_crt_parse_der(), but it avoids creating a
326 * copy of the input buffer at the cost of stronger lifetime
327 * constraints. This is useful in constrained environments
328 * where duplication of the CRT cannot be tolerated.
329 *
330 * \return \c 0 if successful.
331 * \return A negative error code on failure.
332 */
333int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
334 const unsigned char *buf,
335 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336
337/**
Hanno Becker89a91122018-08-23 16:14:00 +0100338 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100339 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200340 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100341 * For CRTs in PEM encoding, the function parses permissively:
342 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100343 * returns the number of certificates for which parsing failed
344 * (hence \c 0 if all certificates were parsed successfully).
345 * If no certificate could be parsed, the function returns
346 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100348 * PEM encoded certificates may be interleaved by other data
349 * such as human readable descriptions of their content, as
350 * long as the certificates are enclosed in the PEM specific
351 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
352 *
353 * \param chain The chain to which to add the parsed certificates.
354 * \param buf The buffer holding the certificate data in PEM or DER format.
355 * For certificates in PEM encoding, this may be a concatenation
356 * of multiple certificates; for DER encoding, the buffer must
357 * comprise exactly one certificate.
358 * \param buflen The size of \p buf, including the terminating \c NULL byte
359 * in case of PEM encoded data.
360 *
361 * \return \c 0 if all certificates were parsed successfully.
362 * \return The (positive) number of certificates that couldn't
363 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100364 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100365 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370/**
371 * \brief Load one or more certificates and add them
372 * to the chained list. Parses permissively. If some
373 * certificates can be parsed, the result is the number
374 * of failed certificates it encountered. If none complete
375 * correctly, the first error is returned.
376 *
377 * \param chain points to the start of the chain
378 * \param path filename to read the certificates from
379 *
380 * \return 0 if all certificates parsed successfully, a positive number
381 * if partly successful or a specific X509 or PEM error code
382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384
385/**
386 * \brief Load one or more certificate files from a path and add them
387 * to the chained list. Parses permissively. If some
388 * certificates can be parsed, the result is the number
389 * of failed certificates it encountered. If none complete
390 * correctly, the first error is returned.
391 *
392 * \param chain points to the start of the chain
393 * \param path directory / folder to read the certificate files from
394 *
395 * \return 0 if all certificates parsed successfully, a positive number
396 * if partly successful or a specific X509 or PEM error code
397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
Janos Follath11b41eb2019-05-08 15:26:49 +0100399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#endif /* MBEDTLS_FS_IO */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200401/**
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300402 * \brief This function parses an item in the SubjectAlternativeNames
403 * extension.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200404 *
Ron Eldor890819a2019-05-13 19:03:04 +0300405 * \param san_buf The buffer holding the raw data item of the subject
406 * alternative name.
407 * \param san The target structure to populate with the parsed presentation
408 * of the subject alternative name encoded in \p san_raw.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200409 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300410 * \note Only "dnsName" and "otherName" of type hardware_module_name
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200411 * as defined in RFC 4180 is supported.
412 *
Ron Eldor890819a2019-05-13 19:03:04 +0300413 * \note This function should be called on a single raw data of
Ron Eldorcc45cd12019-05-15 10:20:09 +0300414 * subject alternative name. For example, after successful
Ron Eldor890819a2019-05-13 19:03:04 +0300415 * certificate parsing, one must iterate on every item in the
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300416 * \p crt->subject_alt_names sequence, and pass it to
417 * this function.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200418 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300419 * \warning The target structure contains pointers to the raw data of the
Ron Eldorcc45cd12019-05-15 10:20:09 +0300420 * parsed certificate, and its lifetime is restricted by the
421 * lifetime of the certificate.
422 *
Ron Eldor890819a2019-05-13 19:03:04 +0300423 * \return \c 0 on success
424 * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300425 * SAN type.
426 * \return Another negative value for any other failure.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200427 */
Ron Eldor890819a2019-05-13 19:03:04 +0300428int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
429 mbedtls_x509_subject_alternative_name *san );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430/**
431 * \brief Returns an informational string about the
432 * certificate.
433 *
434 * \param buf Buffer to write to
435 * \param size Maximum size of buffer
436 * \param prefix A line prefix
437 * \param crt The X509 certificate to represent
438 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200439 * \return The length of the string written (not including the
440 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
443 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
445/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100446 * \brief Returns an informational string about the
447 * verification status of a certificate.
448 *
449 * \param buf Buffer to write to
450 * \param size Maximum size of buffer
451 * \param prefix A line prefix
452 * \param flags Verification flags created by mbedtls_x509_crt_verify()
453 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200454 * \return The length of the string written (not including the
455 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100456 */
457int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200458 uint32_t flags );
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200459
460/**
Hanno Becker902451d2019-03-27 11:48:40 +0000461 * \brief Verify a chain of certificates.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462 *
463 * The verify callback is a user-supplied callback that
464 * can clear / modify / add flags for a certificate. If set,
465 * the verification callback is called for each
466 * certificate in the chain (from the trust-ca down to the
467 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469 * int *flags). With the flags representing current flags for
470 * that specific certificate and the certificate depth from
471 * the bottom (Peer cert depth = 0).
472 *
473 * All flags left after returning from the callback
474 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200475 * return 0 for anything (including invalid certificates)
476 * other than fatal error, as a non-zero return code
477 * immediately aborts the verification process. For fatal
478 * errors, a specific error code should be used (different
479 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
480 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
481 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100483 * \note In case verification failed, the results can be displayed
484 * using \c mbedtls_x509_crt_verify_info()
485 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200486 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
487 * default security profile.
488 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100489 * \note It is your responsibility to provide up-to-date CRLs for
490 * all trusted CAs. If no CRL is provided for the CA that was
491 * used to sign the certificate, CRL verification is skipped
492 * silently, that is *without* setting any flag.
493 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200494 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200495 * (1) those of trusted root CAs, so that certificates
496 * chaining up to those CAs will be trusted, and (2)
497 * self-signed end-entity certificates to be trusted (for
498 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200499 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200500 *
Hanno Becker902451d2019-03-27 11:48:40 +0000501 * \param crt The certificate chain to be verified.
502 * \param trust_ca The list of trusted CAs.
503 * \param ca_crl The list of CRLs for trusted CAs.
504 * \param cn The expected Common Name. This may be \c NULL if the
505 * CN need not be verified.
506 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100507 * If the verification couldn't be completed, the flag value is
508 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000509 * \param f_vrfy The verification callback to use. See the documentation
510 * of mbedtls_x509_crt_verify() for more information.
511 * \param p_vrfy The context to be passed to \p f_vrfy.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512 *
Hanno Becker902451d2019-03-27 11:48:40 +0000513 * \return \c 0 if the chain is valid with respect to the
514 * passed CN, CAs, CRLs and security profile.
515 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
516 * certificate chain verification failed. In this case,
517 * \c *flags will have one or more
518 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
519 * flags set.
520 * \return Another negative error code in case of a fatal error
521 * encountered during the verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
524 mbedtls_x509_crt *trust_ca,
525 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200526 const char *cn, uint32_t *flags,
527 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200528 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200530/**
Hanno Becker902451d2019-03-27 11:48:40 +0000531 * \brief Verify a chain of certificates with respect to
532 * a configurable security profile.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200533 *
534 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
535 * security profile.
536 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200537 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200538 * for ECDSA) apply to all certificates: trusted root,
539 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200540 *
Hanno Becker902451d2019-03-27 11:48:40 +0000541 * \param crt The certificate chain to be verified.
542 * \param trust_ca The list of trusted CAs.
543 * \param ca_crl The list of CRLs for trusted CAs.
544 * \param profile The security profile to use for the verification.
545 * \param cn The expected Common Name. This may be \c NULL if the
546 * CN need not be verified.
547 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100548 * If the verification couldn't be completed, the flag value is
549 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000550 * \param f_vrfy The verification callback to use. See the documentation
551 * of mbedtls_x509_crt_verify() for more information.
552 * \param p_vrfy The context to be passed to \p f_vrfy.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200553 *
Hanno Becker902451d2019-03-27 11:48:40 +0000554 * \return \c 0 if the chain is valid with respect to the
555 * passed CN, CAs, CRLs and security profile.
556 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
557 * certificate chain verification failed. In this case,
558 * \c *flags will have one or more
559 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
560 * flags set.
561 * \return Another negative error code in case of a fatal error
562 * encountered during the verification process.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200563 */
564int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
565 mbedtls_x509_crt *trust_ca,
566 mbedtls_x509_crl *ca_crl,
567 const mbedtls_x509_crt_profile *profile,
568 const char *cn, uint32_t *flags,
569 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
570 void *p_vrfy );
571
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200572/**
573 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
574 *
575 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
576 * but can return early and restart according to the limit
577 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
578 *
Hanno Becker902451d2019-03-27 11:48:40 +0000579 * \param crt The certificate chain to be verified.
580 * \param trust_ca The list of trusted CAs.
581 * \param ca_crl The list of CRLs for trusted CAs.
582 * \param profile The security profile to use for the verification.
583 * \param cn The expected Common Name. This may be \c NULL if the
584 * CN need not be verified.
585 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100586 * If the verification couldn't be completed, the flag value is
587 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000588 * \param f_vrfy The verification callback to use. See the documentation
589 * of mbedtls_x509_crt_verify() for more information.
590 * \param p_vrfy The context to be passed to \p f_vrfy.
591 * \param rs_ctx The restart context to use. This may be set to \c NULL
592 * to disable restartable ECC.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200593 *
594 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200595 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200596 * operations was reached: see \c mbedtls_ecp_set_max_ops().
597 */
598int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
599 mbedtls_x509_crt *trust_ca,
600 mbedtls_x509_crl *ca_crl,
601 const mbedtls_x509_crt_profile *profile,
602 const char *cn, uint32_t *flags,
603 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
604 void *p_vrfy,
605 mbedtls_x509_crt_restart_ctx *rs_ctx );
606
Hanno Becker5c8df782019-03-27 11:01:17 +0000607/**
608 * \brief The type of trusted certificate callbacks.
609 *
610 * Callbacks of this type are passed to and used by the CRT
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300611 * verification routine mbedtls_x509_crt_verify_with_ca_cb()
Hanno Becker5c8df782019-03-27 11:01:17 +0000612 * when looking for trusted signers of a given certificate.
613 *
614 * On success, the callback returns a list of trusted
615 * certificates to be considered as potential signers
616 * for the input certificate.
617 *
618 * \param p_ctx An opaque context passed to the callback.
619 * \param child The certificate for which to search a potential signer.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300620 * This will point to a readable certificate.
Hanno Becker5c8df782019-03-27 11:01:17 +0000621 * \param candidate_cas The address at which to store the address of the first
622 * entry in the generated linked list of candidate signers.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300623 * This will not be \c NULL.
Hanno Becker5c8df782019-03-27 11:01:17 +0000624 *
625 * \note The callback must only return a non-zero value on a
626 * fatal error. If, in contrast, the search for a potential
627 * signer completes without a single candidate, the
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300628 * callback must return \c 0 and set \c *candidate_cas
Hanno Becker5c8df782019-03-27 11:01:17 +0000629 * to \c NULL.
630 *
631 * \return \c 0 on success. In this case, \c *candidate_cas points
632 * to a heap-allocated linked list of instances of
633 * ::mbedtls_x509_crt, and ownership of this list is passed
634 * to the caller.
635 * \return A negative error code on failure.
636 */
637typedef int (*mbedtls_x509_crt_ca_cb_t)( void *p_ctx,
638 mbedtls_x509_crt const *child,
639 mbedtls_x509_crt **candidate_cas );
Hanno Beckere15dae72019-03-28 13:55:38 +0000640
641#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker5c8df782019-03-27 11:01:17 +0000642/**
643 * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
644 * uses a callback to acquire the list of trusted CA
645 * certificates.
646 *
647 * \param crt The certificate chain to be verified.
648 * \param f_ca_cb The callback to be used to query for potential signers
649 * of a given child certificate. See the documentation of
650 * ::mbedtls_x509_crt_ca_cb_t for more information.
651 * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
652 * \param profile The security profile for the verification.
653 * \param cn The expected Common Name. This may be \c NULL if the
654 * CN need not be verified.
655 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100656 * If the verification couldn't be completed, the flag value is
657 * set to (uint32_t) -1.
Hanno Becker5c8df782019-03-27 11:01:17 +0000658 * \param f_vrfy The verification callback to use. See the documentation
659 * of mbedtls_x509_crt_verify() for more information.
660 * \param p_vrfy The context to be passed to \p f_vrfy.
661 *
662 * \return See \c mbedtls_crt_verify_with_profile().
663 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300664int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker5c8df782019-03-27 11:01:17 +0000665 mbedtls_x509_crt_ca_cb_t f_ca_cb,
666 void *p_ca_cb,
667 const mbedtls_x509_crt_profile *profile,
668 const char *cn, uint32_t *flags,
669 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
670 void *p_vrfy );
671
672#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200675/**
676 * \brief Check usage of certificate against keyUsage extension.
677 *
678 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200679 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
680 * before using the certificate to perform an RSA key
681 * exchange).
682 *
683 * \note Except for decipherOnly and encipherOnly, a bit set in the
684 * usage argument means this bit MUST be set in the
685 * certificate. For decipherOnly and encipherOnly, it means
686 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200687 *
688 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200690 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200691 *
692 * \note You should only call this function on leaf certificates, on
693 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200695 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200696int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
697 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200701/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000702 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200703 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000704 * \param crt Leaf certificate used.
705 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
706 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200708 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000709 * \return 0 if this use of the certificate is allowed,
710 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200711 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000712 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000715 const char *usage_oid,
716 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000717#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200721 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722 *
723 * \param crt a certificate to be verified
724 * \param crl the CRL to verify against
725 *
726 * \return 1 if the certificate is revoked, 0 otherwise
727 *
728 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100729int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731
732/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200733 * \brief Initialize a certificate (chain)
734 *
735 * \param crt Certificate chain to initialize
736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200738
739/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740 * \brief Unallocate all certificate data
741 *
742 * \param crt Certificate chain to free
743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200745
746#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
747/**
748 * \brief Initialize a restart context
749 */
750void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
751
752/**
753 * \brief Free the components of a restart context
754 */
755void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
756#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200758
759/* \} name */
760/* \} addtogroup x509_module */
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763/**
764 * \brief Initialize a CRT writing context
765 *
766 * \param ctx CRT context to initialize
767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769
770/**
771 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773 *
774 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
776 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779
780/**
781 * \brief Set the serial number for a Certificate.
782 *
783 * \param ctx CRT context to use
784 * \param serial serial number to set
785 *
786 * \return 0 if successful
787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789
790/**
791 * \brief Set the validity period for a Certificate
792 * Timestamps should be in string format for UTC timezone
793 * i.e. "YYYYMMDDhhmmss"
794 * e.g. "20131231235959" for December 31st 2013
795 * at 23:59:59
796 *
797 * \param ctx CRT context to use
798 * \param not_before not_before timestamp
799 * \param not_after not_after timestamp
800 *
801 * \return 0 if timestamp was parsed successfully, or
802 * a specific error code
803 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100805 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200806
807/**
808 * \brief Set the issuer name for a Certificate
809 * Issuer names should contain a comma-separated list
810 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000811 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200812 *
813 * \param ctx CRT context to use
814 * \param issuer_name issuer name to set
815 *
816 * \return 0 if issuer name was parsed successfully, or
817 * a specific error code
818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100820 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821
822/**
823 * \brief Set the subject name for a Certificate
824 * Subject names should contain a comma-separated list
825 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000826 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827 *
828 * \param ctx CRT context to use
829 * \param subject_name subject name to set
830 *
831 * \return 0 if subject name was parsed successfully, or
832 * a specific error code
833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100835 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836
837/**
838 * \brief Set the subject public key for the certificate
839 *
840 * \param ctx CRT context to use
841 * \param key public key to include
842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200844
845/**
846 * \brief Set the issuer key used for signing the certificate
847 *
848 * \param ctx CRT context to use
849 * \param key private key to sign with
850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200852
853/**
854 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856 *
857 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100858 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200861
862/**
863 * \brief Generic function to add to or replace an extension in the
864 * CRT
865 *
866 * \param ctx CRT context to use
867 * \param oid OID of the extension
868 * \param oid_len length of the OID
869 * \param critical if the extension is critical (per the RFC's definition)
870 * \param val value of the extension OCTET STRING
871 * \param val_len length of the value data
872 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200873 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876 const char *oid, size_t oid_len,
877 int critical,
878 const unsigned char *val, size_t val_len );
879
880/**
881 * \brief Set the basicConstraints extension for a CRT
882 *
883 * \param ctx CRT context to use
884 * \param is_ca is this a CA certificate
885 * \param max_pathlen maximum length of certificate chains below this
886 * certificate (only for CA certificates, -1 is
887 * inlimited)
888 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200889 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 int is_ca, int max_pathlen );
893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895/**
896 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 * called before
899 *
900 * \param ctx CRT context to use
901 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200902 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905
906/**
907 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909 * called before
910 *
911 * \param ctx CRT context to use
912 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200913 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
916#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917
918/**
919 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 *
922 * \param ctx CRT context to use
923 * \param key_usage key usage flags to set
924 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200925 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200927int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
928 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929
930/**
931 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 *
934 * \param ctx CRT context to use
935 * \param ns_cert_type Netscape Cert Type flags to set
936 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200937 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940 unsigned char ns_cert_type );
941
942/**
943 * \brief Free the contents of a CRT write context
944 *
945 * \param ctx CRT context to free
946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948
949/**
950 * \brief Write a built up certificate to a X509 DER structure
951 * Note: data is written at the end of the buffer! Use the
952 * return value to determine where you should start
953 * using the buffer
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 *
961 * \return length of data written if successful, or a specific
962 * error code
963 *
964 * \note f_rng may be NULL if RSA is used for signature and the
965 * signature is made offline (otherwise f_rng is desirable
966 * for countermeasures against timing attacks).
967 * ECDSA signatures always require a non-NULL f_rng.
968 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 int (*f_rng)(void *, unsigned char *, size_t),
971 void *p_rng );
972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974/**
975 * \brief Write a built up certificate to a X509 PEM string
976 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100977 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978 * \param buf buffer to write to
979 * \param size size of the buffer
980 * \param f_rng RNG function (for signature, see note)
981 * \param p_rng RNG parameter
982 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200983 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984 *
985 * \note f_rng may be NULL if RSA is used for signature and the
986 * signature is made offline (otherwise f_rng is desirable
987 * for countermeasures against timing attacks).
988 * ECDSA signatures always require a non-NULL f_rng.
989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991 int (*f_rng)(void *, unsigned char *, size_t),
992 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#endif /* MBEDTLS_PEM_WRITE_C */
994#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995
996#ifdef __cplusplus
997}
998#endif
999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#endif /* mbedtls_x509_crt.h */