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