blob: ca93e1a9227763c5286d1b89d5fbadfe571fbb1d [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"
Hanno Beckerb6c39fc2019-02-25 13:50:14 +000035#include "threading.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
Hanno Becker21f55672019-02-15 15:27:59 +000051typedef struct mbedtls_x509_crt_frame
52{
53 /* Keep these 8-bit fields at the front of the structure to allow them to
54 * be fetched in a single instruction on Thumb2; putting them at the back
55 * requires an intermediate address calculation. */
56
57 uint8_t version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
58 uint8_t ca_istrue; /**< Optional Basic Constraint extension value:
59 * 1 if this certificate belongs to a CA, 0 otherwise. */
60 uint8_t max_pathlen; /**< Optional Basic Constraint extension value:
61 * The maximum path length to the root certificate.
62 * Path length is 1 higher than RFC 5280 'meaning', so 1+ */
63 uint8_t ns_cert_type; /**< Optional Netscape certificate type extension value:
64 * See the values in x509.h */
65
66 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
67 uint32_t ext_types; /**< Bitfield indicating which extensions are present.
68 * See the values in x509.h. */
69
70 mbedtls_md_type_t sig_md; /**< The hash algorithm used to hash CRT before signing. */
71 mbedtls_pk_type_t sig_pk; /**< The signature algorithm used to sign the CRT hash. */
72
73 mbedtls_x509_time valid_from; /**< The start time of certificate validity. */
74 mbedtls_x509_time valid_to; /**< The end time of certificate validity. */
75
76 mbedtls_x509_buf_raw raw; /**< The raw certificate data in DER. */
77 mbedtls_x509_buf_raw tbs; /**< The part of the CRT that is [T]o [B]e [S]igned. */
78
79 mbedtls_x509_buf_raw serial; /**< The unique ID for certificate issued by a specific CA. */
80
81 mbedtls_x509_buf_raw pubkey_raw; /**< The raw public key data (DER). */
82
83 mbedtls_x509_buf_raw issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
84 mbedtls_x509_buf_raw issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
85
86 mbedtls_x509_buf_raw subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
87 mbedtls_x509_buf_raw subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
88
89 mbedtls_x509_buf_raw sig; /**< Signature: hash of the tbs part signed with the private key. */
90 mbedtls_x509_buf_raw sig_alg; /**< The signature algorithm used for \p sig. */
91
92 mbedtls_x509_buf_raw v3_ext; /**< The raw data for the extension list in the certificate.
93 * Might be useful for manual inspection of extensions that
94 * Mbed TLS doesn't yet support. */
95 mbedtls_x509_buf_raw subject_alt_raw; /**< The raw data for the SubjectAlternativeNames extension. */
96 mbedtls_x509_buf_raw ext_key_usage_raw; /**< The raw data for the ExtendedKeyUsage extension. */
97
98 mbedtls_x509_buf_raw issuer_raw_with_hdr;
99 mbedtls_x509_buf_raw subject_raw_with_hdr;
100
101} mbedtls_x509_crt_frame;
102
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000103/* This is an internal structure used for caching parsed data from an X.509 CRT.
104 *
105 * This structure may change at any time, and it is discouraged
106 * to access it directly.
107 */
108typedef struct mbedtls_x509_crt_cache
109{
110#if defined(MBEDTLS_THREADING_C)
111 mbedtls_threading_mutex_t frame_mutex;
112 mbedtls_threading_mutex_t pk_mutex;
113#endif
114 mbedtls_x509_buf_raw pk_raw;
115 mbedtls_x509_crt_frame *frame;
116 mbedtls_pk_context *pk;
117} mbedtls_x509_crt_cache;
118
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119/**
120 * Container for an X.509 certificate. The certificate may be chained.
121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200123{
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000124 int own_buffer; /**< Indicates if \c raw is owned
Hanno Becker180f7bf2019-02-28 13:23:38 +0000125 * by the structure or not. */
126 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
127 mbedtls_x509_crt_cache *cache; /**< Internal parsing cache. */
128
129 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
130
131 /* Legacy fields */
132#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200134
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +0200135 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
137 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
140 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200141
Hanno Beckerf8a42862019-02-20 13:45:16 +0000142 mbedtls_x509_buf_raw subject_raw_no_hdr;
143 mbedtls_x509_buf_raw issuer_raw_no_hdr;
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
146 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
149 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200150
Hanno Becker32c530e2019-02-06 16:13:41 +0000151 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
155 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
156 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
157 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Hanno Beckerded167e2019-02-21 14:34:46 +0000158 mbedtls_x509_buf_raw subject_alt_raw; /**< Raw data for SubjectAlternativeNames extension. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200159
160 int ext_types; /**< Bit string containing detected and parsed extensions */
161 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
162 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+ */
163
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100164 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165
Hanno Becker7ec9c362019-02-21 14:24:05 +0000166 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
167 mbedtls_x509_buf_raw ext_key_usage_raw; /**< Raw data of ExtendedKeyUsage extensions. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200168
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +0200169 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
172 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
173 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
174 void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
Hanno Becker180f7bf2019-02-28 13:23:38 +0000175#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200178
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200179/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200180 * Build flag from an algorithm/curve identifier (pk, md, ecp)
181 * Since 0 is always XXX_NONE, ignore it.
182 */
Hanno Beckerd6028a12018-10-15 12:01:35 +0100183#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200184
185/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200186 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200187 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200188 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200189 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200190typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200191{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200192 uint32_t allowed_mds; /**< MDs for signatures */
193 uint32_t allowed_pks; /**< PK algs for signatures */
194 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
195 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200196}
197mbedtls_x509_crt_profile;
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#define MBEDTLS_X509_CRT_VERSION_1 0
200#define MBEDTLS_X509_CRT_VERSION_2 1
201#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
204#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200205
Andres AGf9113192016-09-02 14:06:04 +0100206#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
207#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
208#endif
209
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200210/**
211 * Container for writing a certificate (CRT)
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200214{
215 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_mpi serial;
217 mbedtls_pk_context *subject_key;
218 mbedtls_pk_context *issuer_key;
219 mbedtls_asn1_named_data *subject;
220 mbedtls_asn1_named_data *issuer;
221 mbedtls_md_type_t md_alg;
222 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
223 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
224 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200225}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200227
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200228/**
229 * Item in a verification chain: cert and flags for it
230 */
231typedef struct {
232 mbedtls_x509_crt *crt;
233 uint32_t flags;
234} mbedtls_x509_crt_verify_chain_item;
235
236/**
237 * Max size of verification chain: end-entity + intermediates + trusted root
238 */
239#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
240
241/**
242 * Verification chain as built by \c mbedtls_crt_verify_chain()
243 */
244typedef struct
245{
246 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200247 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200248} mbedtls_x509_crt_verify_chain;
249
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200250#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
251
252/**
253 * \brief Context for resuming X.509 verify operations
254 */
255typedef struct
256{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200257 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200258 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200259
260 /* for find_parent_in() */
261 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
262 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200263 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200264
265 /* for find_parent() */
266 int parent_is_trusted; /* -1 if find_parent is not in progress */
267
268 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200269 enum {
270 x509_crt_rs_none,
271 x509_crt_rs_find_parent,
272 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200273 int self_cnt;
274 mbedtls_x509_crt_verify_chain ver_chain;
275
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200276} mbedtls_x509_crt_restart_ctx;
277
278#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
279
280/* Now we can declare functions that take a pointer to that */
281typedef void mbedtls_x509_crt_restart_ctx;
282
283#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200287 * Default security profile. Should provide a good balance between security
288 * and compatibility with current deployments.
289 */
290extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
291
292/**
293 * Expected next default profile. Recommended for new deployments.
294 * Currently targets a 128-bit security level, except for RSA-2048.
295 */
296extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
297
298/**
299 * NSA Suite B profile.
300 */
301extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
302
303/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200304 * \brief Parse a single DER formatted certificate and add it
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000305 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200306 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000307 * \param chain The pointer to the start of the CRT chain to attach to.
308 * When parsing the first CRT in a chain, this should point
309 * to an instance of ::mbedtls_x509_crt initialized through
310 * mbedtls_x509_crt_init().
311 * \param buf The buffer holding the DER encoded certificate.
312 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000314 * \note This function makes an internal copy of the CRT buffer
315 * \p buf. In particular, \p buf may be destroyed or reused
316 * after this call returns. To avoid duplicating the CRT
317 * buffer (at the cost of stricter lifetime constraints),
318 * use mbedtls_x509_crt_parse_der_nocopy() instead.
319 *
320 * \return \c 0 if successful.
321 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200322 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000323int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
324 const unsigned char *buf,
325 size_t buflen );
326
327/**
328 * \brief Parse a single DER formatted certificate and add it
329 * to the end of the provided chained list. This is a
330 * variant of mbedtls_x509_crt_parse_der() which takes
331 * temporary ownership of the CRT buffer until the CRT
332 * is destroyed.
333 *
334 * \param chain The pointer to the start of the CRT chain to attach to.
335 * When parsing the first CRT in a chain, this should point
336 * to an instance of ::mbedtls_x509_crt initialized through
337 * mbedtls_x509_crt_init().
338 * \param buf The address of the readable buffer holding the DER encoded
339 * certificate to use. On success, this buffer must be
340 * retained and not be changed for the liftetime of the
341 * CRT chain \p chain, that is, until \p chain is destroyed
342 * through a call to mbedtls_x509_crt_free().
343 * \param buflen The size in Bytes of \p buf.
344 *
345 * \note This call is functionally equivalent to
346 * mbedtls_x509_crt_parse_der(), but it avoids creating a
347 * copy of the input buffer at the cost of stronger lifetime
348 * constraints. This is useful in constrained environments
349 * where duplication of the CRT cannot be tolerated.
350 *
351 * \return \c 0 if successful.
352 * \return A negative error code on failure.
353 */
354int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
355 const unsigned char *buf,
356 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357
358/**
Hanno Becker89a91122018-08-23 16:14:00 +0100359 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100360 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100362 * For CRTs in PEM encoding, the function parses permissively:
363 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100364 * returns the number of certificates for which parsing failed
365 * (hence \c 0 if all certificates were parsed successfully).
366 * If no certificate could be parsed, the function returns
367 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100369 * PEM encoded certificates may be interleaved by other data
370 * such as human readable descriptions of their content, as
371 * long as the certificates are enclosed in the PEM specific
372 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
373 *
374 * \param chain The chain to which to add the parsed certificates.
375 * \param buf The buffer holding the certificate data in PEM or DER format.
376 * For certificates in PEM encoding, this may be a concatenation
377 * of multiple certificates; for DER encoding, the buffer must
378 * comprise exactly one certificate.
379 * \param buflen The size of \p buf, including the terminating \c NULL byte
380 * in case of PEM encoded data.
381 *
382 * \return \c 0 if all certificates were parsed successfully.
383 * \return The (positive) number of certificates that couldn't
384 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100385 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100386 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391/**
392 * \brief Load one or more certificates and add them
393 * to the chained list. Parses permissively. If some
394 * certificates can be parsed, the result is the number
395 * of failed certificates it encountered. If none complete
396 * correctly, the first error is returned.
397 *
398 * \param chain points to the start of the chain
399 * \param path filename to read the certificates from
400 *
401 * \return 0 if all certificates parsed successfully, a positive number
402 * if partly successful or a specific X509 or PEM error code
403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405
406/**
407 * \brief Load one or more certificate files from a path and add them
408 * to the chained list. Parses permissively. If some
409 * certificates can be parsed, the result is the number
410 * of failed certificates it encountered. If none complete
411 * correctly, the first error is returned.
412 *
413 * \param chain points to the start of the chain
414 * \param path directory / folder to read the certificate files from
415 *
416 * \return 0 if all certificates parsed successfully, a positive number
417 * if partly successful or a specific X509 or PEM error code
418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
420#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421
Hanno Becker02a21932019-06-10 15:08:43 +0100422#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423/**
424 * \brief Returns an informational string about the
425 * certificate.
426 *
427 * \param buf Buffer to write to
428 * \param size Maximum size of buffer
429 * \param prefix A line prefix
430 * \param crt The X509 certificate to represent
431 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200432 * \return The length of the string written (not including the
433 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
436 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437
438/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100439 * \brief Returns an informational string about the
440 * verification status of a certificate.
441 *
442 * \param buf Buffer to write to
443 * \param size Maximum size of buffer
444 * \param prefix A line prefix
445 * \param flags Verification flags created by mbedtls_x509_crt_verify()
446 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200447 * \return The length of the string written (not including the
448 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100449 */
450int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200451 uint32_t flags );
Hanno Beckerc6043f22019-06-14 17:21:24 +0100452#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200453
454/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 * \brief Verify the certificate signature
456 *
457 * The verify callback is a user-supplied callback that
458 * can clear / modify / add flags for a certificate. If set,
459 * the verification callback is called for each
460 * certificate in the chain (from the trust-ca down to the
461 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463 * int *flags). With the flags representing current flags for
464 * that specific certificate and the certificate depth from
465 * the bottom (Peer cert depth = 0).
466 *
467 * All flags left after returning from the callback
468 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200469 * return 0 for anything (including invalid certificates)
470 * other than fatal error, as a non-zero return code
471 * immediately aborts the verification process. For fatal
472 * errors, a specific error code should be used (different
473 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
474 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
475 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100477 * \note In case verification failed, the results can be displayed
478 * using \c mbedtls_x509_crt_verify_info()
479 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200480 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
481 * default security profile.
482 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100483 * \note It is your responsibility to provide up-to-date CRLs for
484 * all trusted CAs. If no CRL is provided for the CA that was
485 * used to sign the certificate, CRL verification is skipped
486 * silently, that is *without* setting any flag.
487 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200488 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200489 * (1) those of trusted root CAs, so that certificates
490 * chaining up to those CAs will be trusted, and (2)
491 * self-signed end-entity certificates to be trusted (for
492 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200493 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200494 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100495 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200496 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100497 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498 * \param cn expected Common Name (can be set to
499 * NULL if the CN must not be verified)
500 * \param flags result of the verification
501 * \param f_vrfy verification function
502 * \param p_vrfy verification parameter
503 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200504 * \return 0 (and flags set to 0) if the chain was verified and valid,
505 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
506 * but found to be invalid, in which case *flags will have one
507 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
508 * flags set, or another error (and flags set to 0xffffffff)
509 * in case of a fatal error encountered during the
510 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
513 mbedtls_x509_crt *trust_ca,
514 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200515 const char *cn, uint32_t *flags,
516 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200517 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200519/**
520 * \brief Verify the certificate signature according to profile
521 *
522 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
523 * security profile.
524 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200525 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200526 * for ECDSA) apply to all certificates: trusted root,
527 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200528 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100529 * \param crt a certificate (chain) to be verified
530 * \param trust_ca the list of trusted CAs
531 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200532 * \param profile security profile for verification
533 * \param cn expected Common Name (can be set to
534 * NULL if the CN must not be verified)
535 * \param flags result of the verification
536 * \param f_vrfy verification function
537 * \param p_vrfy verification parameter
538 *
539 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
540 * in which case *flags will have one or more
541 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
542 * set,
543 * or another error in case of a fatal error encountered
544 * during the verification process.
545 */
546int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
547 mbedtls_x509_crt *trust_ca,
548 mbedtls_x509_crl *ca_crl,
549 const mbedtls_x509_crt_profile *profile,
550 const char *cn, uint32_t *flags,
551 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
552 void *p_vrfy );
553
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200554/**
555 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
556 *
557 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
558 * but can return early and restart according to the limit
559 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
560 *
561 * \param crt a certificate (chain) to be verified
562 * \param trust_ca the list of trusted CAs
563 * \param ca_crl the list of CRLs for trusted CAs
564 * \param profile security profile for verification
565 * \param cn expected Common Name (can be set to
566 * NULL if the CN must not be verified)
567 * \param flags result of the verification
568 * \param f_vrfy verification function
569 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200570 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200571 *
572 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200573 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200574 * operations was reached: see \c mbedtls_ecp_set_max_ops().
575 */
576int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
577 mbedtls_x509_crt *trust_ca,
578 mbedtls_x509_crl *ca_crl,
579 const mbedtls_x509_crt_profile *profile,
580 const char *cn, uint32_t *flags,
581 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
582 void *p_vrfy,
583 mbedtls_x509_crt_restart_ctx *rs_ctx );
584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200586/**
587 * \brief Check usage of certificate against keyUsage extension.
588 *
589 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200590 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
591 * before using the certificate to perform an RSA key
592 * exchange).
593 *
594 * \note Except for decipherOnly and encipherOnly, a bit set in the
595 * usage argument means this bit MUST be set in the
596 * certificate. For decipherOnly and encipherOnly, it means
597 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200598 *
599 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200601 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200602 *
603 * \note You should only call this function on leaf certificates, on
604 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200606 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200607int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
608 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200612/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000613 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200614 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000615 * \param crt Leaf certificate used.
616 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
617 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200619 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000620 * \return 0 if this use of the certificate is allowed,
621 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200622 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000623 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000626 const char *usage_oid,
627 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000628#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200632 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633 *
634 * \param crt a certificate to be verified
635 * \param crl the CRL to verify against
636 *
637 * \return 1 if the certificate is revoked, 0 otherwise
638 *
639 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100640int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642
643/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200644 * \brief Initialize a certificate (chain)
645 *
646 * \param crt Certificate chain to initialize
647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200649
650/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651 * \brief Unallocate all certificate data
652 *
653 * \param crt Certificate chain to free
654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200656
657#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
658/**
659 * \brief Initialize a restart context
660 */
661void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
662
663/**
664 * \brief Free the components of a restart context
665 */
666void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
667#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000668
Hanno Becker823efad2019-02-28 13:23:58 +0000669/**
670 * \brief Request CRT frame giving access to basic CRT fields
671 * and raw ASN.1 data of complex fields.
672 *
673 * \param crt The CRT to use. This must be initialized and setup.
674 * \param dst The address of the destination frame structure.
675 * This need not be initialized.
676 *
677 * \note ::mbedtls_x509_crt_frame does not contain pointers to
678 * dynamically allocated memory, and hence need not be freed.
679 * Users may e.g. allocate an instance of
680 * ::mbedtls_x509_crt_frame on the stack and call this function
681 * on it, in which case no allocation/freeing has to be done.
682 *
683 * \return \c 0 on success. In this case, \p dst is updated
684 * to hold the frame for the given CRT.
685 * \return A negative error code on failure.
686 */
687int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
688 mbedtls_x509_crt_frame *dst );
689
690/**
691 * \brief Setup a PK context with the public key in a certificate.
692 *
693 * \param crt The certificate to use. This must be initialized and setup.
694 * \param pk The address of the destination PK context to fill.
695 * This must be initialized via mbedtls_pk_init().
696 *
697 * \return \c 0 on success. In this case, the user takes ownership
698 * of the destination PK context, and is responsible for
699 * calling mbedtls_pk_free() on it once it's no longer needed.
700 * \return A negative error code on failure.
701 */
702int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
703 mbedtls_pk_context *pk );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000704
705/**
Hanno Becker63e69982019-02-26 18:50:49 +0000706 * \brief Request the subject name of a CRT, presented
707 * as a dynamically allocated linked list.
708 *
709 * \param crt The CRT to use. This must be initialized and setup.
710 * \param subject The address at which to store the address of the
711 * first entry of the generated linked list holding
712 * the subject name.
713 *
714 * \note Depending in your use case, consider using the raw ASN.1
715 * describing the subject name instead of the heap-allocated
716 * linked list generated by this call. The pointers to the
717 * raw ASN.1 data are part of the CRT frame that can be queried
718 * via mbedtls_x509_crt_get_frame(), and they can be traversed
719 * via mbedtls_asn1_traverse_sequence_of().
720 *
721 * \return \c 0 on success. In this case, the user takes ownership
722 * of the name context, and is responsible for freeing it
723 * once it's no longer needed.
724 * \return A negative error code on failure.
725 */
726int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
727 mbedtls_x509_name **subject );
728
729/**
730 * \brief Request the subject name of a CRT, presented
731 * as a dynamically allocated linked list.
732 *
733 * \param crt The CRT to use. This must be initialized and setup.
734 * \param issuer The address at which to store the address of the
735 * first entry of the generated linked list holding
736 * the subject name.
737 *
738 * \note Depending in your use case, consider using the raw ASN.1
739 * describing the subject name instead of the heap-allocated
740 * linked list generated by this call. The pointers to the
741 * raw ASN.1 data are part of the CRT frame that can be queried
742 * via mbedtls_x509_crt_get_frame(), and they can be traversed
743 * via mbedtls_asn1_traverse_sequence_of().
744 *
745 * \return \c 0 on success. In this case, the user takes ownership
746 * of the name context, and is responsible for freeing it
747 * once it's no longer needed.
748 * \return A negative error code on failure.
749 */
750int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
751 mbedtls_x509_name **issuer );
752
753/**
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000754 * \brief Request the subject alternative name of a CRT, presented
755 * as a dynamically allocated linked list.
756 *
757 * \param crt The CRT to use. This must be initialized and setup.
758 * \param subj_alt The address at which to store the address of the
759 * first component of the subject alternative names list.
760 *
761 * \note Depending in your use case, consider using the raw ASN.1
762 * describing the subject alternative names extension
763 * instead of the heap-allocated linked list generated by this
764 * call. The pointers to the raw ASN.1 data are part of the CRT
765 * frame that can be queried via mbedtls_x509_crt_get_frame(),
766 * and mbedtls_asn1_traverse_sequence_of() can be used to
767 * traverse the list of subject alternative names.
768 *
769 * \return \c 0 on success. In this case, the user takes ownership
770 * of the name context, and is responsible for freeing it
771 * through a call to mbedtls_x509_sequence_free() once it's
772 * no longer needed.
773 * \return A negative error code on failure.
774 */
775int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
776 mbedtls_x509_sequence **subj_alt );
777
778/**
779 * \brief Request the ExtendedKeyUsage extension of a CRT,
780 * presented as a dynamically allocated linked list.
781 *
782 * \param crt The CRT to use. This must be initialized and setup.
783 * \param ext_key_usage The address at which to store the address of the
784 * first entry of the ExtendedKeyUsage extension.
785 *
786 * \note Depending in your use case, consider using the raw ASN.1
787 * describing the extended key usage extension instead of
788 * the heap-allocated linked list generated by this call.
789 * The pointers to the raw ASN.1 data are part of the CRT
790 * frame that can be queried via mbedtls_x509_crt_get_frame(),
791 * and mbedtls_asn1_traverse_sequence_of() can be used to
792 * traverse the entries in the extended key usage extension.
793 *
794 * \return \c 0 on success. In this case, the user takes ownership
795 * of the name context, and is responsible for freeing it
796 * through a call to mbedtls_x509_sequence_free() once it's
797 * no longer needed.
798 * \return A negative error code on failure.
799 */
800int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
801 mbedtls_x509_sequence **ext_key_usage );
802
803/**
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000804 * \brief Flush internal X.509 CRT parsing cache, if present.
805 *
806 * \param crt The CRT structure whose cache to flush.
807 *
808 * \note Calling this function frequently reduces RAM usage
809 * at the cost of performance.
810 *
811 * \return \c 0 on success.
812 * \return A negative error code on failure.
813 */
814int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt );
815
816/* Internal X.509 CRT cache handling functions.
817 * They are not part of the public API and may change
818 * at any time. */
819
820int mbedtls_x509_crt_cache_provide_frame( mbedtls_x509_crt const *crt );
821int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt );
822
823static inline int mbedtls_x509_crt_cache_frame_set(
824 mbedtls_x509_crt_cache *cache )
825{
826 return( cache->frame != NULL );
827}
828
829static inline mbedtls_x509_crt_frame* mbedtls_x509_crt_cache_get_frame(
830 mbedtls_x509_crt_cache *cache )
831{
832 return( cache->frame );
833}
834
835static inline int mbedtls_x509_crt_cache_pk_set(
836 mbedtls_x509_crt_cache *cache )
837{
838 return( cache->pk != NULL );
839}
840
841static inline mbedtls_pk_context* mbedtls_x509_crt_cache_get_pk(
842 mbedtls_x509_crt_cache *cache )
843{
844 return( cache->pk );
845}
846
847static inline int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
848 mbedtls_x509_crt_frame **frame_ptr )
849{
850#if defined(MBEDTLS_THREADING_C)
851 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
852 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
853#endif
854
855 if( !mbedtls_x509_crt_cache_frame_set( crt->cache ) )
856 {
857 int ret;
858 ret = mbedtls_x509_crt_cache_provide_frame( crt );
859 if( ret != 0 )
860 {
861#if defined(MBEDTLS_THREADING_C)
862 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
863 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
864#endif
865 return( ret );
866 }
867 }
868
869 *frame_ptr = mbedtls_x509_crt_cache_get_frame( crt->cache );
870 return( 0 );
871}
872
873static inline void mbedtls_x509_crt_frame_release(
874 mbedtls_x509_crt const *crt,
875 mbedtls_x509_crt_frame *frame )
876{
877 ((void) frame);
878 ((void) crt);
879
880#if defined(MBEDTLS_THREADING_C)
881 mbedtls_mutex_unlock( &crt->cache->frame_mutex );
882#endif
883}
884
Hanno Becker180f7bf2019-02-28 13:23:38 +0000885static inline int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt const *crt,
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000886 mbedtls_pk_context **pk_ptr )
887{
888#if defined(MBEDTLS_THREADING_C)
889 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
890 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
891#endif
892
893 if( !mbedtls_x509_crt_cache_pk_set( crt->cache ) )
894 {
895 int ret;
896 ret = mbedtls_x509_crt_cache_provide_pk( crt );
897 if( ret != 0 )
898 {
899#if defined(MBEDTLS_THREADING_C)
900 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
901 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
902#endif
903 return( ret );
904 }
905 }
906
907 *pk_ptr = mbedtls_x509_crt_cache_get_pk( crt->cache );
908 return( 0 );
909}
910
Hanno Becker180f7bf2019-02-28 13:23:38 +0000911static inline void mbedtls_x509_crt_pk_release( mbedtls_x509_crt const *crt,
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000912 mbedtls_pk_context *pk )
913{
914 ((void) pk);
915 ((void) crt);
916
917#if defined(MBEDTLS_THREADING_C)
918 mbedtls_mutex_unlock( &crt->cache->pk_mutex );
919#endif
920}
921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923
924/* \} name */
925/* \} addtogroup x509_module */
926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928/**
929 * \brief Initialize a CRT writing context
930 *
931 * \param ctx CRT context to initialize
932 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934
935/**
936 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938 *
939 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
941 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944
945/**
946 * \brief Set the serial number for a Certificate.
947 *
948 * \param ctx CRT context to use
949 * \param serial serial number to set
950 *
951 * \return 0 if successful
952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
955/**
956 * \brief Set the validity period for a Certificate
957 * Timestamps should be in string format for UTC timezone
958 * i.e. "YYYYMMDDhhmmss"
959 * e.g. "20131231235959" for December 31st 2013
960 * at 23:59:59
961 *
962 * \param ctx CRT context to use
963 * \param not_before not_before timestamp
964 * \param not_after not_after timestamp
965 *
966 * \return 0 if timestamp was parsed successfully, or
967 * a specific error code
968 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100970 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971
972/**
973 * \brief Set the issuer name for a Certificate
974 * Issuer names should contain a comma-separated list
975 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000976 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977 *
978 * \param ctx CRT context to use
979 * \param issuer_name issuer name to set
980 *
981 * \return 0 if issuer name was parsed successfully, or
982 * a specific error code
983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100985 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986
987/**
988 * \brief Set the subject name for a Certificate
989 * Subject names should contain a comma-separated list
990 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000991 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 *
993 * \param ctx CRT context to use
994 * \param subject_name subject name to set
995 *
996 * \return 0 if subject name was parsed successfully, or
997 * a specific error code
998 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001000 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001
1002/**
1003 * \brief Set the subject public key for the certificate
1004 *
1005 * \param ctx CRT context to use
1006 * \param key public key to include
1007 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009
1010/**
1011 * \brief Set the issuer key used for signing the certificate
1012 *
1013 * \param ctx CRT context to use
1014 * \param key private key to sign with
1015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017
1018/**
1019 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 *
1022 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001023 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026
1027/**
1028 * \brief Generic function to add to or replace an extension in the
1029 * CRT
1030 *
1031 * \param ctx CRT context to use
1032 * \param oid OID of the extension
1033 * \param oid_len length of the OID
1034 * \param critical if the extension is critical (per the RFC's definition)
1035 * \param val value of the extension OCTET STRING
1036 * \param val_len length of the value data
1037 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001038 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041 const char *oid, size_t oid_len,
1042 int critical,
1043 const unsigned char *val, size_t val_len );
1044
1045/**
1046 * \brief Set the basicConstraints extension for a CRT
1047 *
1048 * \param ctx CRT context to use
1049 * \param is_ca is this a CA certificate
1050 * \param max_pathlen maximum length of certificate chains below this
1051 * certificate (only for CA certificates, -1 is
1052 * inlimited)
1053 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001054 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001057 int is_ca, int max_pathlen );
1058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060/**
1061 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063 * called before
1064 *
1065 * \param ctx CRT context to use
1066 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001067 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070
1071/**
1072 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074 * called before
1075 *
1076 * \param ctx CRT context to use
1077 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001078 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
1081#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001082
1083/**
1084 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086 *
1087 * \param ctx CRT context to use
1088 * \param key_usage key usage flags to set
1089 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001090 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +02001092int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
1093 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094
1095/**
1096 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 *
1099 * \param ctx CRT context to use
1100 * \param ns_cert_type Netscape Cert Type flags to set
1101 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001102 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105 unsigned char ns_cert_type );
1106
1107/**
1108 * \brief Free the contents of a CRT write context
1109 *
1110 * \param ctx CRT context to free
1111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113
1114/**
1115 * \brief Write a built up certificate to a X509 DER structure
1116 * Note: data is written at the end of the buffer! Use the
1117 * return value to determine where you should start
1118 * using the buffer
1119 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001120 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121 * \param buf buffer to write to
1122 * \param size size of the buffer
1123 * \param f_rng RNG function (for signature, see note)
1124 * \param p_rng RNG parameter
1125 *
1126 * \return length of data written if successful, or a specific
1127 * error code
1128 *
1129 * \note f_rng may be NULL if RSA is used for signature and the
1130 * signature is made offline (otherwise f_rng is desirable
1131 * for countermeasures against timing attacks).
1132 * ECDSA signatures always require a non-NULL f_rng.
1133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001135 int (*f_rng)(void *, unsigned char *, size_t),
1136 void *p_rng );
1137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139/**
1140 * \brief Write a built up certificate to a X509 PEM string
1141 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001142 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001143 * \param buf buffer to write to
1144 * \param size size of the buffer
1145 * \param f_rng RNG function (for signature, see note)
1146 * \param p_rng RNG parameter
1147 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001148 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149 *
1150 * \note f_rng may be NULL if RSA is used for signature and the
1151 * signature is made offline (otherwise f_rng is desirable
1152 * for countermeasures against timing attacks).
1153 * ECDSA signatures always require a non-NULL f_rng.
1154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 int (*f_rng)(void *, unsigned char *, size_t),
1157 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#endif /* MBEDTLS_PEM_WRITE_C */
1159#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160
1161#ifdef __cplusplus
1162}
1163#endif
1164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165#endif /* mbedtls_x509_crt.h */