blob: ce1ee0539754be5b8f6ae719e1c24ed9bc4d3224 [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/**
706 * \brief Flush internal X.509 CRT parsing cache, if present.
707 *
708 * \param crt The CRT structure whose cache to flush.
709 *
710 * \note Calling this function frequently reduces RAM usage
711 * at the cost of performance.
712 *
713 * \return \c 0 on success.
714 * \return A negative error code on failure.
715 */
716int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt );
717
718/* Internal X.509 CRT cache handling functions.
719 * They are not part of the public API and may change
720 * at any time. */
721
722int mbedtls_x509_crt_cache_provide_frame( mbedtls_x509_crt const *crt );
723int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt );
724
725static inline int mbedtls_x509_crt_cache_frame_set(
726 mbedtls_x509_crt_cache *cache )
727{
728 return( cache->frame != NULL );
729}
730
731static inline mbedtls_x509_crt_frame* mbedtls_x509_crt_cache_get_frame(
732 mbedtls_x509_crt_cache *cache )
733{
734 return( cache->frame );
735}
736
737static inline int mbedtls_x509_crt_cache_pk_set(
738 mbedtls_x509_crt_cache *cache )
739{
740 return( cache->pk != NULL );
741}
742
743static inline mbedtls_pk_context* mbedtls_x509_crt_cache_get_pk(
744 mbedtls_x509_crt_cache *cache )
745{
746 return( cache->pk );
747}
748
749static inline int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
750 mbedtls_x509_crt_frame **frame_ptr )
751{
752#if defined(MBEDTLS_THREADING_C)
753 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
754 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
755#endif
756
757 if( !mbedtls_x509_crt_cache_frame_set( crt->cache ) )
758 {
759 int ret;
760 ret = mbedtls_x509_crt_cache_provide_frame( crt );
761 if( ret != 0 )
762 {
763#if defined(MBEDTLS_THREADING_C)
764 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
765 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
766#endif
767 return( ret );
768 }
769 }
770
771 *frame_ptr = mbedtls_x509_crt_cache_get_frame( crt->cache );
772 return( 0 );
773}
774
775static inline void mbedtls_x509_crt_frame_release(
776 mbedtls_x509_crt const *crt,
777 mbedtls_x509_crt_frame *frame )
778{
779 ((void) frame);
780 ((void) crt);
781
782#if defined(MBEDTLS_THREADING_C)
783 mbedtls_mutex_unlock( &crt->cache->frame_mutex );
784#endif
785}
786
Hanno Becker180f7bf2019-02-28 13:23:38 +0000787static inline int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt const *crt,
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000788 mbedtls_pk_context **pk_ptr )
789{
790#if defined(MBEDTLS_THREADING_C)
791 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
792 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
793#endif
794
795 if( !mbedtls_x509_crt_cache_pk_set( crt->cache ) )
796 {
797 int ret;
798 ret = mbedtls_x509_crt_cache_provide_pk( crt );
799 if( ret != 0 )
800 {
801#if defined(MBEDTLS_THREADING_C)
802 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
803 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
804#endif
805 return( ret );
806 }
807 }
808
809 *pk_ptr = mbedtls_x509_crt_cache_get_pk( crt->cache );
810 return( 0 );
811}
812
Hanno Becker180f7bf2019-02-28 13:23:38 +0000813static inline void mbedtls_x509_crt_pk_release( mbedtls_x509_crt const *crt,
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000814 mbedtls_pk_context *pk )
815{
816 ((void) pk);
817 ((void) crt);
818
819#if defined(MBEDTLS_THREADING_C)
820 mbedtls_mutex_unlock( &crt->cache->pk_mutex );
821#endif
822}
823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200825
826/* \} name */
827/* \} addtogroup x509_module */
828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830/**
831 * \brief Initialize a CRT writing context
832 *
833 * \param ctx CRT context to initialize
834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836
837/**
838 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200840 *
841 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
843 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200846
847/**
848 * \brief Set the serial number for a Certificate.
849 *
850 * \param ctx CRT context to use
851 * \param serial serial number to set
852 *
853 * \return 0 if successful
854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856
857/**
858 * \brief Set the validity period for a Certificate
859 * Timestamps should be in string format for UTC timezone
860 * i.e. "YYYYMMDDhhmmss"
861 * e.g. "20131231235959" for December 31st 2013
862 * at 23:59:59
863 *
864 * \param ctx CRT context to use
865 * \param not_before not_before timestamp
866 * \param not_after not_after timestamp
867 *
868 * \return 0 if timestamp was parsed successfully, or
869 * a specific error code
870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100872 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873
874/**
875 * \brief Set the issuer name for a Certificate
876 * Issuer names should contain a comma-separated list
877 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000878 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879 *
880 * \param ctx CRT context to use
881 * \param issuer_name issuer name to set
882 *
883 * \return 0 if issuer name was parsed successfully, or
884 * a specific error code
885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100887 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888
889/**
890 * \brief Set the subject name for a Certificate
891 * Subject names should contain a comma-separated list
892 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000893 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894 *
895 * \param ctx CRT context to use
896 * \param subject_name subject name to set
897 *
898 * \return 0 if subject name was parsed successfully, or
899 * a specific error code
900 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100902 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903
904/**
905 * \brief Set the subject public key for the certificate
906 *
907 * \param ctx CRT context to use
908 * \param key public key to include
909 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911
912/**
913 * \brief Set the issuer key used for signing the certificate
914 *
915 * \param ctx CRT context to use
916 * \param key private key to sign with
917 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919
920/**
921 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923 *
924 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100925 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928
929/**
930 * \brief Generic function to add to or replace an extension in the
931 * CRT
932 *
933 * \param ctx CRT context to use
934 * \param oid OID of the extension
935 * \param oid_len length of the OID
936 * \param critical if the extension is critical (per the RFC's definition)
937 * \param val value of the extension OCTET STRING
938 * \param val_len length of the value data
939 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200940 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943 const char *oid, size_t oid_len,
944 int critical,
945 const unsigned char *val, size_t val_len );
946
947/**
948 * \brief Set the basicConstraints extension for a CRT
949 *
950 * \param ctx CRT context to use
951 * \param is_ca is this a CA certificate
952 * \param max_pathlen maximum length of certificate chains below this
953 * certificate (only for CA certificates, -1 is
954 * inlimited)
955 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200956 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 int is_ca, int max_pathlen );
960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962/**
963 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965 * called before
966 *
967 * \param ctx CRT context to use
968 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200969 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972
973/**
974 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976 * called before
977 *
978 * \param ctx CRT context to use
979 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200980 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
983#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984
985/**
986 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988 *
989 * \param ctx CRT context to use
990 * \param key_usage key usage flags to set
991 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200992 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200994int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
995 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996
997/**
998 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000 *
1001 * \param ctx CRT context to use
1002 * \param ns_cert_type Netscape Cert Type flags to set
1003 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001004 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007 unsigned char ns_cert_type );
1008
1009/**
1010 * \brief Free the contents of a CRT write context
1011 *
1012 * \param ctx CRT context to free
1013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015
1016/**
1017 * \brief Write a built up certificate to a X509 DER structure
1018 * Note: data is written at the end of the buffer! Use the
1019 * return value to determine where you should start
1020 * using the buffer
1021 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001022 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 * \param buf buffer to write to
1024 * \param size size of the buffer
1025 * \param f_rng RNG function (for signature, see note)
1026 * \param p_rng RNG parameter
1027 *
1028 * \return length of data written if successful, or a specific
1029 * error code
1030 *
1031 * \note f_rng may be NULL if RSA is used for signature and the
1032 * signature is made offline (otherwise f_rng is desirable
1033 * for countermeasures against timing attacks).
1034 * ECDSA signatures always require a non-NULL f_rng.
1035 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037 int (*f_rng)(void *, unsigned char *, size_t),
1038 void *p_rng );
1039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041/**
1042 * \brief Write a built up certificate to a X509 PEM string
1043 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001044 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045 * \param buf buffer to write to
1046 * \param size size of the buffer
1047 * \param f_rng RNG function (for signature, see note)
1048 * \param p_rng RNG parameter
1049 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001050 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051 *
1052 * \note f_rng may be NULL if RSA is used for signature and the
1053 * signature is made offline (otherwise f_rng is desirable
1054 * for countermeasures against timing attacks).
1055 * ECDSA signatures always require a non-NULL f_rng.
1056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001058 int (*f_rng)(void *, unsigned char *, size_t),
1059 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#endif /* MBEDTLS_PEM_WRITE_C */
1061#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062
1063#ifdef __cplusplus
1064}
1065#endif
1066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067#endif /* mbedtls_x509_crt.h */