blob: 3f8350a4a91cb2e1c96ee13a897d3e65603e0f2c [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
125 * by the structure or not. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
127 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200128
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +0200129 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
131 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
134 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135
Hanno Beckerf8a42862019-02-20 13:45:16 +0000136 mbedtls_x509_buf_raw subject_raw_no_hdr;
137 mbedtls_x509_buf_raw issuer_raw_no_hdr;
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
140 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
143 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200144
Hanno Becker32c530e2019-02-06 16:13:41 +0000145 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
149 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
150 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
151 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Hanno Beckerded167e2019-02-21 14:34:46 +0000152 mbedtls_x509_buf_raw subject_alt_raw; /**< Raw data for SubjectAlternativeNames extension. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153
154 int ext_types; /**< Bit string containing detected and parsed extensions */
155 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
156 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+ */
157
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100158 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200159
Hanno Becker7ec9c362019-02-21 14:24:05 +0000160 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
161 mbedtls_x509_buf_raw ext_key_usage_raw; /**< Raw data of ExtendedKeyUsage extensions. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200162
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +0200163 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
166 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
167 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
168 void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200169
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000170 mbedtls_x509_crt_cache *cache; /**< Internal parsing cache. */
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200175
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200176/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200177 * Build flag from an algorithm/curve identifier (pk, md, ecp)
178 * Since 0 is always XXX_NONE, ignore it.
179 */
Hanno Beckerd6028a12018-10-15 12:01:35 +0100180#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200181
182/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200183 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200184 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200185 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200186 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200187typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200188{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200189 uint32_t allowed_mds; /**< MDs for signatures */
190 uint32_t allowed_pks; /**< PK algs for signatures */
191 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
192 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200193}
194mbedtls_x509_crt_profile;
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#define MBEDTLS_X509_CRT_VERSION_1 0
197#define MBEDTLS_X509_CRT_VERSION_2 1
198#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
201#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200202
Andres AGf9113192016-09-02 14:06:04 +0100203#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
204#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
205#endif
206
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200207/**
208 * Container for writing a certificate (CRT)
209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200211{
212 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_mpi serial;
214 mbedtls_pk_context *subject_key;
215 mbedtls_pk_context *issuer_key;
216 mbedtls_asn1_named_data *subject;
217 mbedtls_asn1_named_data *issuer;
218 mbedtls_md_type_t md_alg;
219 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
220 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
221 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200222}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200224
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200225/**
226 * Item in a verification chain: cert and flags for it
227 */
228typedef struct {
229 mbedtls_x509_crt *crt;
230 uint32_t flags;
231} mbedtls_x509_crt_verify_chain_item;
232
233/**
234 * Max size of verification chain: end-entity + intermediates + trusted root
235 */
236#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
237
238/**
239 * Verification chain as built by \c mbedtls_crt_verify_chain()
240 */
241typedef struct
242{
243 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200244 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200245} mbedtls_x509_crt_verify_chain;
246
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200247#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
248
249/**
250 * \brief Context for resuming X.509 verify operations
251 */
252typedef struct
253{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200254 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200255 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200256
257 /* for find_parent_in() */
258 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
259 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200260 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200261
262 /* for find_parent() */
263 int parent_is_trusted; /* -1 if find_parent is not in progress */
264
265 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200266 enum {
267 x509_crt_rs_none,
268 x509_crt_rs_find_parent,
269 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200270 int self_cnt;
271 mbedtls_x509_crt_verify_chain ver_chain;
272
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200273} mbedtls_x509_crt_restart_ctx;
274
275#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
276
277/* Now we can declare functions that take a pointer to that */
278typedef void mbedtls_x509_crt_restart_ctx;
279
280#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200284 * Default security profile. Should provide a good balance between security
285 * and compatibility with current deployments.
286 */
287extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
288
289/**
290 * Expected next default profile. Recommended for new deployments.
291 * Currently targets a 128-bit security level, except for RSA-2048.
292 */
293extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
294
295/**
296 * NSA Suite B profile.
297 */
298extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
299
300/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200301 * \brief Parse a single DER formatted certificate and add it
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000302 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200303 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000304 * \param chain The pointer to the start of the CRT chain to attach to.
305 * When parsing the first CRT in a chain, this should point
306 * to an instance of ::mbedtls_x509_crt initialized through
307 * mbedtls_x509_crt_init().
308 * \param buf The buffer holding the DER encoded certificate.
309 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200310 *
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000311 * \note This function makes an internal copy of the CRT buffer
312 * \p buf. In particular, \p buf may be destroyed or reused
313 * after this call returns. To avoid duplicating the CRT
314 * buffer (at the cost of stricter lifetime constraints),
315 * use mbedtls_x509_crt_parse_der_nocopy() instead.
316 *
317 * \return \c 0 if successful.
318 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200319 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +0000320int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
321 const unsigned char *buf,
322 size_t buflen );
323
324/**
325 * \brief Parse a single DER formatted certificate and add it
326 * to the end of the provided chained list. This is a
327 * variant of mbedtls_x509_crt_parse_der() which takes
328 * temporary ownership of the CRT buffer until the CRT
329 * is destroyed.
330 *
331 * \param chain The pointer to the start of the CRT chain to attach to.
332 * When parsing the first CRT in a chain, this should point
333 * to an instance of ::mbedtls_x509_crt initialized through
334 * mbedtls_x509_crt_init().
335 * \param buf The address of the readable buffer holding the DER encoded
336 * certificate to use. On success, this buffer must be
337 * retained and not be changed for the liftetime of the
338 * CRT chain \p chain, that is, until \p chain is destroyed
339 * through a call to mbedtls_x509_crt_free().
340 * \param buflen The size in Bytes of \p buf.
341 *
342 * \note This call is functionally equivalent to
343 * mbedtls_x509_crt_parse_der(), but it avoids creating a
344 * copy of the input buffer at the cost of stronger lifetime
345 * constraints. This is useful in constrained environments
346 * where duplication of the CRT cannot be tolerated.
347 *
348 * \return \c 0 if successful.
349 * \return A negative error code on failure.
350 */
351int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
352 const unsigned char *buf,
353 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354
355/**
Hanno Becker89a91122018-08-23 16:14:00 +0100356 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100357 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100359 * For CRTs in PEM encoding, the function parses permissively:
360 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100361 * returns the number of certificates for which parsing failed
362 * (hence \c 0 if all certificates were parsed successfully).
363 * If no certificate could be parsed, the function returns
364 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100366 * PEM encoded certificates may be interleaved by other data
367 * such as human readable descriptions of their content, as
368 * long as the certificates are enclosed in the PEM specific
369 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
370 *
371 * \param chain The chain to which to add the parsed certificates.
372 * \param buf The buffer holding the certificate data in PEM or DER format.
373 * For certificates in PEM encoding, this may be a concatenation
374 * of multiple certificates; for DER encoding, the buffer must
375 * comprise exactly one certificate.
376 * \param buflen The size of \p buf, including the terminating \c NULL byte
377 * in case of PEM encoded data.
378 *
379 * \return \c 0 if all certificates were parsed successfully.
380 * \return The (positive) number of certificates that couldn't
381 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100382 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100383 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388/**
389 * \brief Load one or more certificates and add them
390 * to the chained list. Parses permissively. If some
391 * certificates can be parsed, the result is the number
392 * of failed certificates it encountered. If none complete
393 * correctly, the first error is returned.
394 *
395 * \param chain points to the start of the chain
396 * \param path filename to read the certificates from
397 *
398 * \return 0 if all certificates parsed successfully, a positive number
399 * if partly successful or a specific X509 or PEM error code
400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402
403/**
404 * \brief Load one or more certificate files from a path and add them
405 * to the chained list. Parses permissively. If some
406 * certificates can be parsed, the result is the number
407 * of failed certificates it encountered. If none complete
408 * correctly, the first error is returned.
409 *
410 * \param chain points to the start of the chain
411 * \param path directory / folder to read the certificate files from
412 *
413 * \return 0 if all certificates parsed successfully, a positive number
414 * if partly successful or a specific X509 or PEM error code
415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
417#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418
Hanno Becker02a21932019-06-10 15:08:43 +0100419#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420/**
421 * \brief Returns an informational string about the
422 * certificate.
423 *
424 * \param buf Buffer to write to
425 * \param size Maximum size of buffer
426 * \param prefix A line prefix
427 * \param crt The X509 certificate to represent
428 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200429 * \return The length of the string written (not including the
430 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
433 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434
435/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100436 * \brief Returns an informational string about the
437 * verification status of a certificate.
438 *
439 * \param buf Buffer to write to
440 * \param size Maximum size of buffer
441 * \param prefix A line prefix
442 * \param flags Verification flags created by mbedtls_x509_crt_verify()
443 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200444 * \return The length of the string written (not including the
445 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100446 */
447int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200448 uint32_t flags );
Hanno Beckerc6043f22019-06-14 17:21:24 +0100449#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200450
451/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452 * \brief Verify the certificate signature
453 *
454 * The verify callback is a user-supplied callback that
455 * can clear / modify / add flags for a certificate. If set,
456 * the verification callback is called for each
457 * certificate in the chain (from the trust-ca down to the
458 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460 * int *flags). With the flags representing current flags for
461 * that specific certificate and the certificate depth from
462 * the bottom (Peer cert depth = 0).
463 *
464 * All flags left after returning from the callback
465 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200466 * return 0 for anything (including invalid certificates)
467 * other than fatal error, as a non-zero return code
468 * immediately aborts the verification process. For fatal
469 * errors, a specific error code should be used (different
470 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
471 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
472 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100474 * \note In case verification failed, the results can be displayed
475 * using \c mbedtls_x509_crt_verify_info()
476 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200477 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
478 * default security profile.
479 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100480 * \note It is your responsibility to provide up-to-date CRLs for
481 * all trusted CAs. If no CRL is provided for the CA that was
482 * used to sign the certificate, CRL verification is skipped
483 * silently, that is *without* setting any flag.
484 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200485 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200486 * (1) those of trusted root CAs, so that certificates
487 * chaining up to those CAs will be trusted, and (2)
488 * self-signed end-entity certificates to be trusted (for
489 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200490 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200491 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100492 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200493 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100494 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495 * \param cn expected Common Name (can be set to
496 * NULL if the CN must not be verified)
497 * \param flags result of the verification
498 * \param f_vrfy verification function
499 * \param p_vrfy verification parameter
500 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200501 * \return 0 (and flags set to 0) if the chain was verified and valid,
502 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
503 * but found to be invalid, in which case *flags will have one
504 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
505 * flags set, or another error (and flags set to 0xffffffff)
506 * in case of a fatal error encountered during the
507 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
510 mbedtls_x509_crt *trust_ca,
511 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200512 const char *cn, uint32_t *flags,
513 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200514 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200516/**
517 * \brief Verify the certificate signature according to profile
518 *
519 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
520 * security profile.
521 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200522 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200523 * for ECDSA) apply to all certificates: trusted root,
524 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200525 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100526 * \param crt a certificate (chain) to be verified
527 * \param trust_ca the list of trusted CAs
528 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200529 * \param profile security profile for verification
530 * \param cn expected Common Name (can be set to
531 * NULL if the CN must not be verified)
532 * \param flags result of the verification
533 * \param f_vrfy verification function
534 * \param p_vrfy verification parameter
535 *
536 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
537 * in which case *flags will have one or more
538 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
539 * set,
540 * or another error in case of a fatal error encountered
541 * during the verification process.
542 */
543int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
544 mbedtls_x509_crt *trust_ca,
545 mbedtls_x509_crl *ca_crl,
546 const mbedtls_x509_crt_profile *profile,
547 const char *cn, uint32_t *flags,
548 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
549 void *p_vrfy );
550
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200551/**
552 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
553 *
554 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
555 * but can return early and restart according to the limit
556 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
557 *
558 * \param crt a certificate (chain) to be verified
559 * \param trust_ca the list of trusted CAs
560 * \param ca_crl the list of CRLs for trusted CAs
561 * \param profile security profile for verification
562 * \param cn expected Common Name (can be set to
563 * NULL if the CN must not be verified)
564 * \param flags result of the verification
565 * \param f_vrfy verification function
566 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200567 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200568 *
569 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200570 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200571 * operations was reached: see \c mbedtls_ecp_set_max_ops().
572 */
573int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
574 mbedtls_x509_crt *trust_ca,
575 mbedtls_x509_crl *ca_crl,
576 const mbedtls_x509_crt_profile *profile,
577 const char *cn, uint32_t *flags,
578 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
579 void *p_vrfy,
580 mbedtls_x509_crt_restart_ctx *rs_ctx );
581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200583/**
584 * \brief Check usage of certificate against keyUsage extension.
585 *
586 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200587 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
588 * before using the certificate to perform an RSA key
589 * exchange).
590 *
591 * \note Except for decipherOnly and encipherOnly, a bit set in the
592 * usage argument means this bit MUST be set in the
593 * certificate. For decipherOnly and encipherOnly, it means
594 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200595 *
596 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200598 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200599 *
600 * \note You should only call this function on leaf certificates, on
601 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200603 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200604int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
605 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200609/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000610 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200611 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000612 * \param crt Leaf certificate used.
613 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
614 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200616 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000617 * \return 0 if this use of the certificate is allowed,
618 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200619 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000620 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000623 const char *usage_oid,
624 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000625#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200629 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 *
631 * \param crt a certificate to be verified
632 * \param crl the CRL to verify against
633 *
634 * \return 1 if the certificate is revoked, 0 otherwise
635 *
636 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100637int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639
640/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200641 * \brief Initialize a certificate (chain)
642 *
643 * \param crt Certificate chain to initialize
644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200646
647/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 * \brief Unallocate all certificate data
649 *
650 * \param crt Certificate chain to free
651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200653
654#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
655/**
656 * \brief Initialize a restart context
657 */
658void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
659
660/**
661 * \brief Free the components of a restart context
662 */
663void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
664#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000665
666
667/**
668 * \brief Flush internal X.509 CRT parsing cache, if present.
669 *
670 * \param crt The CRT structure whose cache to flush.
671 *
672 * \note Calling this function frequently reduces RAM usage
673 * at the cost of performance.
674 *
675 * \return \c 0 on success.
676 * \return A negative error code on failure.
677 */
678int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt );
679
680/* Internal X.509 CRT cache handling functions.
681 * They are not part of the public API and may change
682 * at any time. */
683
684int mbedtls_x509_crt_cache_provide_frame( mbedtls_x509_crt const *crt );
685int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt );
686
687static inline int mbedtls_x509_crt_cache_frame_set(
688 mbedtls_x509_crt_cache *cache )
689{
690 return( cache->frame != NULL );
691}
692
693static inline mbedtls_x509_crt_frame* mbedtls_x509_crt_cache_get_frame(
694 mbedtls_x509_crt_cache *cache )
695{
696 return( cache->frame );
697}
698
699static inline int mbedtls_x509_crt_cache_pk_set(
700 mbedtls_x509_crt_cache *cache )
701{
702 return( cache->pk != NULL );
703}
704
705static inline mbedtls_pk_context* mbedtls_x509_crt_cache_get_pk(
706 mbedtls_x509_crt_cache *cache )
707{
708 return( cache->pk );
709}
710
711static inline int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
712 mbedtls_x509_crt_frame **frame_ptr )
713{
714#if defined(MBEDTLS_THREADING_C)
715 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
716 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
717#endif
718
719 if( !mbedtls_x509_crt_cache_frame_set( crt->cache ) )
720 {
721 int ret;
722 ret = mbedtls_x509_crt_cache_provide_frame( crt );
723 if( ret != 0 )
724 {
725#if defined(MBEDTLS_THREADING_C)
726 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
727 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
728#endif
729 return( ret );
730 }
731 }
732
733 *frame_ptr = mbedtls_x509_crt_cache_get_frame( crt->cache );
734 return( 0 );
735}
736
737static inline void mbedtls_x509_crt_frame_release(
738 mbedtls_x509_crt const *crt,
739 mbedtls_x509_crt_frame *frame )
740{
741 ((void) frame);
742 ((void) crt);
743
744#if defined(MBEDTLS_THREADING_C)
745 mbedtls_mutex_unlock( &crt->cache->frame_mutex );
746#endif
747}
748
749static inline int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt *crt,
750 mbedtls_pk_context **pk_ptr )
751{
752#if defined(MBEDTLS_THREADING_C)
753 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
754 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
755#endif
756
757 if( !mbedtls_x509_crt_cache_pk_set( crt->cache ) )
758 {
759 int ret;
760 ret = mbedtls_x509_crt_cache_provide_pk( crt );
761 if( ret != 0 )
762 {
763#if defined(MBEDTLS_THREADING_C)
764 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
765 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
766#endif
767 return( ret );
768 }
769 }
770
771 *pk_ptr = mbedtls_x509_crt_cache_get_pk( crt->cache );
772 return( 0 );
773}
774
775static inline void mbedtls_x509_crt_pk_release( mbedtls_x509_crt *crt,
776 mbedtls_pk_context *pk )
777{
778 ((void) pk);
779 ((void) crt);
780
781#if defined(MBEDTLS_THREADING_C)
782 mbedtls_mutex_unlock( &crt->cache->pk_mutex );
783#endif
784}
785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200787
788/* \} name */
789/* \} addtogroup x509_module */
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792/**
793 * \brief Initialize a CRT writing context
794 *
795 * \param ctx CRT context to initialize
796 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798
799/**
800 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200802 *
803 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
805 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200806 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200808
809/**
810 * \brief Set the serial number for a Certificate.
811 *
812 * \param ctx CRT context to use
813 * \param serial serial number to set
814 *
815 * \return 0 if successful
816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818
819/**
820 * \brief Set the validity period for a Certificate
821 * Timestamps should be in string format for UTC timezone
822 * i.e. "YYYYMMDDhhmmss"
823 * e.g. "20131231235959" for December 31st 2013
824 * at 23:59:59
825 *
826 * \param ctx CRT context to use
827 * \param not_before not_before timestamp
828 * \param not_after not_after timestamp
829 *
830 * \return 0 if timestamp was parsed successfully, or
831 * a specific error code
832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100834 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200835
836/**
837 * \brief Set the issuer name for a Certificate
838 * Issuer names should contain a comma-separated list
839 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000840 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200841 *
842 * \param ctx CRT context to use
843 * \param issuer_name issuer name to set
844 *
845 * \return 0 if issuer name was parsed successfully, or
846 * a specific error code
847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100849 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200850
851/**
852 * \brief Set the subject name for a Certificate
853 * Subject names should contain a comma-separated list
854 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000855 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856 *
857 * \param ctx CRT context to use
858 * \param subject_name subject name to set
859 *
860 * \return 0 if subject name was parsed successfully, or
861 * a specific error code
862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100864 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200865
866/**
867 * \brief Set the subject public key for the certificate
868 *
869 * \param ctx CRT context to use
870 * \param key public key to include
871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873
874/**
875 * \brief Set the issuer key used for signing the certificate
876 *
877 * \param ctx CRT context to use
878 * \param key private key to sign with
879 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881
882/**
883 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885 *
886 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100887 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890
891/**
892 * \brief Generic function to add to or replace an extension in the
893 * CRT
894 *
895 * \param ctx CRT context to use
896 * \param oid OID of the extension
897 * \param oid_len length of the OID
898 * \param critical if the extension is critical (per the RFC's definition)
899 * \param val value of the extension OCTET STRING
900 * \param val_len length of the value data
901 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200902 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 const char *oid, size_t oid_len,
906 int critical,
907 const unsigned char *val, size_t val_len );
908
909/**
910 * \brief Set the basicConstraints extension for a CRT
911 *
912 * \param ctx CRT context to use
913 * \param is_ca is this a CA certificate
914 * \param max_pathlen maximum length of certificate chains below this
915 * certificate (only for CA certificates, -1 is
916 * inlimited)
917 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200918 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 int is_ca, int max_pathlen );
922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924/**
925 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927 * called before
928 *
929 * \param ctx CRT context to use
930 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200931 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934
935/**
936 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938 * called before
939 *
940 * \param ctx CRT context to use
941 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200942 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
945#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946
947/**
948 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950 *
951 * \param ctx CRT context to use
952 * \param key_usage key usage flags to set
953 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200954 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200956int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
957 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958
959/**
960 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 *
963 * \param ctx CRT context to use
964 * \param ns_cert_type Netscape Cert Type flags to set
965 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200966 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969 unsigned char ns_cert_type );
970
971/**
972 * \brief Free the contents of a CRT write context
973 *
974 * \param ctx CRT context to free
975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977
978/**
979 * \brief Write a built up certificate to a X509 DER structure
980 * Note: data is written at the end of the buffer! Use the
981 * return value to determine where you should start
982 * using the buffer
983 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100984 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 * \param buf buffer to write to
986 * \param size size of the buffer
987 * \param f_rng RNG function (for signature, see note)
988 * \param p_rng RNG parameter
989 *
990 * \return length of data written if successful, or a specific
991 * error code
992 *
993 * \note f_rng may be NULL if RSA is used for signature and the
994 * signature is made offline (otherwise f_rng is desirable
995 * for countermeasures against timing attacks).
996 * ECDSA signatures always require a non-NULL f_rng.
997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999 int (*f_rng)(void *, unsigned char *, size_t),
1000 void *p_rng );
1001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003/**
1004 * \brief Write a built up certificate to a X509 PEM string
1005 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001006 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007 * \param buf buffer to write to
1008 * \param size size of the buffer
1009 * \param f_rng RNG function (for signature, see note)
1010 * \param p_rng RNG parameter
1011 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001012 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 *
1014 * \note f_rng may be NULL if RSA is used for signature and the
1015 * signature is made offline (otherwise f_rng is desirable
1016 * for countermeasures against timing attacks).
1017 * ECDSA signatures always require a non-NULL f_rng.
1018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 int (*f_rng)(void *, unsigned char *, size_t),
1021 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#endif /* MBEDTLS_PEM_WRITE_C */
1023#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024
1025#ifdef __cplusplus
1026}
1027#endif
1028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029#endif /* mbedtls_x509_crt.h */