blob: 9b0a495dc6301e81d82e13283226f945be80d7a9 [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/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#ifndef MBEDTLS_X509_CRT_H
23#define MBEDTLS_X509_CRT_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020024#include "mbedtls/private_access.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amero6609aef2019-07-04 20:01:14 +010027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031
Jaeden Amero6609aef2019-07-04 20:01:14 +010032#include "mbedtls/x509.h"
33#include "mbedtls/x509_crl.h"
34#include "mbedtls/bignum.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
36/**
37 * \addtogroup x509_module
38 * \{
39 */
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/**
46 * \name Structures and functions for parsing and writing X.509 certificates
47 * \{
48 */
49
50/**
51 * Container for an X.509 certificate. The certificate may be chained.
52 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020055 int MBEDTLS_PRIVATE(own_buffer); /**< Indicates if \c raw is owned
Hanno Becker1a65dcd2019-01-31 08:57:44 +000056 * by the structure or not. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020057 mbedtls_x509_buf MBEDTLS_PRIVATE(raw); /**< The raw certificate data (DER). */
58 mbedtls_x509_buf MBEDTLS_PRIVATE(tbs); /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059
Mateusz Starzyk846f0212021-05-19 19:44:07 +020060 int MBEDTLS_PRIVATE(version); /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
61 mbedtls_x509_buf MBEDTLS_PRIVATE(serial); /**< Unique id for certificate issued by a specific CA. */
62 mbedtls_x509_buf MBEDTLS_PRIVATE(sig_oid); /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063
Mateusz Starzyk846f0212021-05-19 19:44:07 +020064 mbedtls_x509_buf MBEDTLS_PRIVATE(issuer_raw); /**< The raw issuer data (DER). Used for quick comparison. */
65 mbedtls_x509_buf MBEDTLS_PRIVATE(subject_raw); /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Mateusz Starzyk846f0212021-05-19 19:44:07 +020067 mbedtls_x509_name MBEDTLS_PRIVATE(issuer); /**< The parsed issuer data (named information object). */
68 mbedtls_x509_name MBEDTLS_PRIVATE(subject); /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069
Mateusz Starzyk846f0212021-05-19 19:44:07 +020070 mbedtls_x509_time MBEDTLS_PRIVATE(valid_from); /**< Start time of certificate validity. */
71 mbedtls_x509_time MBEDTLS_PRIVATE(valid_to); /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072
Mateusz Starzyk846f0212021-05-19 19:44:07 +020073 mbedtls_x509_buf MBEDTLS_PRIVATE(pk_raw);
74 mbedtls_pk_context MBEDTLS_PRIVATE(pk); /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075
Mateusz Starzyk846f0212021-05-19 19:44:07 +020076 mbedtls_x509_buf MBEDTLS_PRIVATE(issuer_id); /**< Optional X.509 v2/v3 issuer unique identifier. */
77 mbedtls_x509_buf MBEDTLS_PRIVATE(subject_id); /**< Optional X.509 v2/v3 subject unique identifier. */
78 mbedtls_x509_buf MBEDTLS_PRIVATE(v3_ext); /**< Optional X.509 v3 extensions. */
79 mbedtls_x509_sequence MBEDTLS_PRIVATE(subject_alt_names); /**< Optional list of raw entries of Subject Alternative Names extension (currently only dNSName and OtherName are listed). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080
Mateusz Starzyk846f0212021-05-19 19:44:07 +020081 mbedtls_x509_sequence MBEDTLS_PRIVATE(certificate_policies); /**< Optional list of certificate policies (Only anyPolicy is printed and enforced, however the rest of the policies are still listed). */
Ron Eldor74d9acc2019-03-21 14:00:03 +020082
Mateusz Starzyk846f0212021-05-19 19:44:07 +020083 int MBEDTLS_PRIVATE(ext_types); /**< Bit string containing detected and parsed extensions */
84 int MBEDTLS_PRIVATE(ca_istrue); /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
85 int MBEDTLS_PRIVATE(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+ */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086
Mateusz Starzyk846f0212021-05-19 19:44:07 +020087 unsigned int MBEDTLS_PRIVATE(key_usage); /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088
Mateusz Starzyk846f0212021-05-19 19:44:07 +020089 mbedtls_x509_sequence MBEDTLS_PRIVATE(ext_key_usage); /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090
Mateusz Starzyk846f0212021-05-19 19:44:07 +020091 unsigned char MBEDTLS_PRIVATE(ns_cert_type); /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092
Mateusz Starzyk846f0212021-05-19 19:44:07 +020093 mbedtls_x509_buf MBEDTLS_PRIVATE(sig); /**< Signature: hash of the tbs part signed with the private key. */
94 mbedtls_md_type_t MBEDTLS_PRIVATE(sig_md); /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
95 mbedtls_pk_type_t MBEDTLS_PRIVATE(sig_pk); /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
96 void *MBEDTLS_PRIVATE(sig_opts); /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097
Mateusz Starzyk846f0212021-05-19 19:44:07 +020098 struct mbedtls_x509_crt *MBEDTLS_PRIVATE(next); /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101
Janos Follath5091bec2019-05-08 15:23:08 +0100102/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200103 * From RFC 5280 section 4.2.1.6:
104 * OtherName ::= SEQUENCE {
105 * type-id OBJECT IDENTIFIER,
106 * value [0] EXPLICIT ANY DEFINED BY type-id }
107 */
108typedef struct mbedtls_x509_san_other_name
109{
Ron Eldor890819a2019-05-13 19:03:04 +0300110 /**
111 * The type_id is an OID as deifned in RFC 5280.
112 * To check the value of the type id, you should use
113 * \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf.
114 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200115 mbedtls_x509_buf MBEDTLS_PRIVATE(type_id); /**< The type id. */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200116 union
117 {
Janos Follath5091bec2019-05-08 15:23:08 +0100118 /**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200119 * From RFC 4108 section 5:
120 * HardwareModuleName ::= SEQUENCE {
121 * hwType OBJECT IDENTIFIER,
122 * hwSerialNum OCTET STRING }
123 */
Ron Eldorf05f5942019-05-13 19:11:31 +0300124 struct
125 {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200126 mbedtls_x509_buf MBEDTLS_PRIVATE(oid); /**< The object identifier. */
127 mbedtls_x509_buf MBEDTLS_PRIVATE(val); /**< The named value. */
Ron Eldorf05f5942019-05-13 19:11:31 +0300128 }
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200129 MBEDTLS_PRIVATE(hardware_module_name);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200130 }
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200131 MBEDTLS_PRIVATE(value);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200132}
133mbedtls_x509_san_other_name;
134
Janos Follath5091bec2019-05-08 15:23:08 +0100135/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200136 * A structure for holding the parsed Subject Alternative Name, according to type
137 */
138typedef struct mbedtls_x509_subject_alternative_name
139{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200140 int MBEDTLS_PRIVATE(type); /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200141 union {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200142 mbedtls_x509_san_other_name MBEDTLS_PRIVATE(other_name); /**< The otherName supported type. */
143 mbedtls_x509_buf MBEDTLS_PRIVATE(unstructured_name); /**< The buffer for the un constructed types. Only dnsName currently supported */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200144 }
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200145 MBEDTLS_PRIVATE(san); /**< A union of the supported SAN types */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200146}
147mbedtls_x509_subject_alternative_name;
148
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200149/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200150 * Build flag from an algorithm/curve identifier (pk, md, ecp)
151 * Since 0 is always XXX_NONE, ignore it.
152 */
Hanno Becker1eeca412018-10-15 12:01:35 +0100153#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200154
155/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200156 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200157 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200158 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard55a7fb82021-06-17 10:39:39 +0200159 *
160 * The fields of this structure are part of the public API and can be
161 * manipulated directly by applications. Future versions of the library may
162 * add extra fields or reorder existing fields.
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200163 *
164 * You can create custom profiles by starting from a copy of
165 * an existing profile, such as mbedtls_x509_crt_profile_default or
166 * mbedtls_x509_ctr_profile_none and then tune it to your needs.
167 *
168 * For example to allow SHA-224 in addition to the default:
169 *
170 * mbedtls_x509_crt_profile my_profile = mbedtls_x509_crt_profile_default;
171 * my_profile.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 );
172 *
173 * Or to allow only RSA-3072+ with SHA-256:
174 *
175 * mbedtls_x509_crt_profile my_profile = mbedtls_x509_crt_profile_none;
176 * my_profile.allowed_mds = MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 );
177 * my_profile.allowed_pks = MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA );
178 * my_profile.rsa_min_bitlen = 3072;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200179 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200180typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200181{
Manuel Pégourié-Gonnard5314e082021-06-17 10:32:01 +0200182 uint32_t allowed_mds; /**< MDs for signatures */
183 uint32_t allowed_pks; /**< PK algs for signatures */
184 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
185 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200186}
187mbedtls_x509_crt_profile;
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#define MBEDTLS_X509_CRT_VERSION_1 0
190#define MBEDTLS_X509_CRT_VERSION_2 1
191#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
194#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200195
Andres AGf9113192016-09-02 14:06:04 +0100196#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
197#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
198#endif
199
Hanno Becker7ac83f92020-10-09 10:48:22 +0100200/* This macro unfolds to the concatenation of macro invocations
201 * X509_CRT_ERROR_INFO( error code,
202 * error code as string,
203 * human readable description )
204 * where X509_CRT_ERROR_INFO is defined by the user.
205 * See x509_crt.c for an example of how to use this. */
206#define MBEDTLS_X509_CRT_ERROR_INFO_LIST \
207 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_EXPIRED, \
208 "MBEDTLS_X509_BADCERT_EXPIRED", \
209 "The certificate validity has expired" ) \
210 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_REVOKED, \
211 "MBEDTLS_X509_BADCERT_REVOKED", \
212 "The certificate has been revoked (is on a CRL)" ) \
213 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_CN_MISMATCH, \
214 "MBEDTLS_X509_BADCERT_CN_MISMATCH", \
215 "The certificate Common Name (CN) does not match with the expected CN" ) \
216 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_NOT_TRUSTED, \
217 "MBEDTLS_X509_BADCERT_NOT_TRUSTED", \
218 "The certificate is not correctly signed by the trusted CA" ) \
219 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_NOT_TRUSTED, \
220 "MBEDTLS_X509_BADCRL_NOT_TRUSTED", \
221 "The CRL is not correctly signed by the trusted CA" ) \
222 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_EXPIRED, \
223 "MBEDTLS_X509_BADCRL_EXPIRED", \
224 "The CRL is expired" ) \
225 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_MISSING, \
226 "MBEDTLS_X509_BADCERT_MISSING", \
227 "Certificate was missing" ) \
228 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_SKIP_VERIFY, \
229 "MBEDTLS_X509_BADCERT_SKIP_VERIFY", \
230 "Certificate verification was skipped" ) \
231 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_OTHER, \
232 "MBEDTLS_X509_BADCERT_OTHER", \
233 "Other reason (can be used by verify callback)" ) \
234 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_FUTURE, \
235 "MBEDTLS_X509_BADCERT_FUTURE", \
236 "The certificate validity starts in the future" ) \
237 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_FUTURE, \
238 "MBEDTLS_X509_BADCRL_FUTURE", \
239 "The CRL is from the future" ) \
240 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_KEY_USAGE, \
241 "MBEDTLS_X509_BADCERT_KEY_USAGE", \
242 "Usage does not match the keyUsage extension" ) \
243 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, \
244 "MBEDTLS_X509_BADCERT_EXT_KEY_USAGE", \
245 "Usage does not match the extendedKeyUsage extension" ) \
246 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_NS_CERT_TYPE, \
247 "MBEDTLS_X509_BADCERT_NS_CERT_TYPE", \
248 "Usage does not match the nsCertType extension" ) \
249 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_BAD_MD, \
250 "MBEDTLS_X509_BADCERT_BAD_MD", \
251 "The certificate is signed with an unacceptable hash." ) \
252 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_BAD_PK, \
253 "MBEDTLS_X509_BADCERT_BAD_PK", \
254 "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." ) \
255 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_BAD_KEY, \
256 "MBEDTLS_X509_BADCERT_BAD_KEY", \
257 "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." ) \
258 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_BAD_MD, \
259 "MBEDTLS_X509_BADCRL_BAD_MD", \
260 "The CRL is signed with an unacceptable hash." ) \
261 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_BAD_PK, \
262 "MBEDTLS_X509_BADCRL_BAD_PK", \
263 "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." ) \
264 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_BAD_KEY, \
265 "MBEDTLS_X509_BADCRL_BAD_KEY", \
266 "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." )
267
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268/**
269 * Container for writing a certificate (CRT)
270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200272{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200273 int MBEDTLS_PRIVATE(version);
274 mbedtls_mpi MBEDTLS_PRIVATE(serial);
275 mbedtls_pk_context *MBEDTLS_PRIVATE(subject_key);
276 mbedtls_pk_context *MBEDTLS_PRIVATE(issuer_key);
277 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(subject);
278 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(issuer);
279 mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg);
280 char MBEDTLS_PRIVATE(not_before)[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
281 char MBEDTLS_PRIVATE(not_after)[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
282 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(extensions);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200286/**
287 * Item in a verification chain: cert and flags for it
288 */
289typedef struct {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200290 mbedtls_x509_crt *MBEDTLS_PRIVATE(crt);
291 uint32_t MBEDTLS_PRIVATE(flags);
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200292} mbedtls_x509_crt_verify_chain_item;
293
294/**
295 * Max size of verification chain: end-entity + intermediates + trusted root
296 */
297#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
298
299/**
300 * Verification chain as built by \c mbedtls_crt_verify_chain()
301 */
302typedef struct
303{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200304 mbedtls_x509_crt_verify_chain_item MBEDTLS_PRIVATE(items)[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
305 unsigned MBEDTLS_PRIVATE(len);
Hanno Beckerf53893b2019-03-28 13:45:55 +0000306
307#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
308 /* This stores the list of potential trusted signers obtained from
309 * the CA callback used for the CRT verification, if configured.
310 * We must track it somewhere because the callback passes its
311 * ownership to the caller. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200312 mbedtls_x509_crt *MBEDTLS_PRIVATE(trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +0000313#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200314} mbedtls_x509_crt_verify_chain;
315
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200316#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
317
318/**
319 * \brief Context for resuming X.509 verify operations
320 */
321typedef struct
322{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200323 /* for check_signature() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200324 mbedtls_pk_restart_ctx MBEDTLS_PRIVATE(pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200325
326 /* for find_parent_in() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200327 mbedtls_x509_crt *MBEDTLS_PRIVATE(parent); /* non-null iff parent_in in progress */
328 mbedtls_x509_crt *MBEDTLS_PRIVATE(fallback_parent);
329 int MBEDTLS_PRIVATE(fallback_signature_is_good);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200330
331 /* for find_parent() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200332 int MBEDTLS_PRIVATE(parent_is_trusted); /* -1 if find_parent is not in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200333
334 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200335 enum {
336 x509_crt_rs_none,
337 x509_crt_rs_find_parent,
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200338 } MBEDTLS_PRIVATE(in_progress); /* none if no operation is in progress */
339 int MBEDTLS_PRIVATE(self_cnt);
340 mbedtls_x509_crt_verify_chain MBEDTLS_PRIVATE(ver_chain);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200341
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200342} mbedtls_x509_crt_restart_ctx;
343
344#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
345
346/* Now we can declare functions that take a pointer to that */
347typedef void mbedtls_x509_crt_restart_ctx;
348
349#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200353 * Default security profile. Should provide a good balance between security
354 * and compatibility with current deployments.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200355 *
356 * This profile permits:
357 * - SHA2 hashes with at least 256 bits: SHA-256, SHA-384, SHA-512.
Gilles Peskine39957502021-06-17 23:17:52 +0200358 * - Elliptic curves with 255 bits and above except secp256k1.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200359 * - RSA with 2048 bits and above.
360 *
361 * New minor versions of Mbed TLS may extend this profile, for example if
Gilles Peskine39957502021-06-17 23:17:52 +0200362 * new algorithms are added to the library. New minor versions of Mbed TLS will
Gilles Peskineffb92da2021-06-02 00:03:26 +0200363 * not reduce this profile unless serious security concerns require it.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200364 */
365extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
366
367/**
368 * Expected next default profile. Recommended for new deployments.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200369 * Currently targets a 128-bit security level, except for allowing RSA-2048.
370 * This profile may change at any time.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200371 */
372extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
373
374/**
375 * NSA Suite B profile.
376 */
377extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
378
379/**
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200380 * Empty profile that allows nothing. Useful as a basis for constructing
381 * custom profiles.
382 */
383extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none;
384
385/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386 * \brief Parse a single DER formatted certificate and add it
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000387 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000389 * \param chain The pointer to the start of the CRT chain to attach to.
390 * When parsing the first CRT in a chain, this should point
391 * to an instance of ::mbedtls_x509_crt initialized through
392 * mbedtls_x509_crt_init().
393 * \param buf The buffer holding the DER encoded certificate.
394 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200395 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000396 * \note This function makes an internal copy of the CRT buffer
397 * \p buf. In particular, \p buf may be destroyed or reused
398 * after this call returns. To avoid duplicating the CRT
399 * buffer (at the cost of stricter lifetime constraints),
400 * use mbedtls_x509_crt_parse_der_nocopy() instead.
401 *
402 * \return \c 0 if successful.
403 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000405int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
406 const unsigned char *buf,
407 size_t buflen );
408
409/**
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200410 * \brief The type of certificate extension callbacks.
411 *
412 * Callbacks of this type are passed to and used by the
Nicola Di Lietofde98f72020-05-28 08:34:33 +0200413 * mbedtls_x509_crt_parse_der_with_ext_cb() routine when
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200414 * it encounters either an unsupported extension or a
415 * "certificate policies" extension containing any
416 * unsupported certificate policies.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200417 * Future versions of the library may invoke the callback
418 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200419 *
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200420 * \param p_ctx An opaque context passed to the callback.
ndilieto6e249802020-05-28 06:17:38 +0000421 * \param crt The certificate being parsed.
422 * \param oid The OID of the extension.
423 * \param critical Whether the extension is critical.
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200424 * \param p Pointer to the start of the extension value
ndilieto6e249802020-05-28 06:17:38 +0000425 * (the content of the OCTET STRING).
ndilieto6e249802020-05-28 06:17:38 +0000426 * \param end End of extension value.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200427 *
428 * \note The callback must fail and return a negative error code
429 * if it can not parse or does not support the extension.
430 * When the callback fails to parse a critical extension
431 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
432 * When the callback fails to parse a non critical extension
433 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
434 * the extension and continues parsing.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200435 *
436 * \return \c 0 on success.
437 * \return A negative error code on failure.
438 */
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200439typedef int (*mbedtls_x509_crt_ext_cb_t)( void *p_ctx,
440 mbedtls_x509_crt const *crt,
ndilieto6e249802020-05-28 06:17:38 +0000441 mbedtls_x509_buf const *oid,
442 int critical,
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200443 const unsigned char *p,
ndilieto6e249802020-05-28 06:17:38 +0000444 const unsigned char *end );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200445
446/**
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200447 * \brief Parse a single DER formatted certificate and add it
448 * to the end of the provided chained list.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200449 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200450 * \param chain The pointer to the start of the CRT chain to attach to.
451 * When parsing the first CRT in a chain, this should point
452 * to an instance of ::mbedtls_x509_crt initialized through
453 * mbedtls_x509_crt_init().
454 * \param buf The buffer holding the DER encoded certificate.
455 * \param buflen The size in Bytes of \p buf.
456 * \param make_copy When not zero this function makes an internal copy of the
457 * CRT buffer \p buf. In particular, \p buf may be destroyed
458 * or reused after this call returns.
459 * When zero this function avoids duplicating the CRT buffer
460 * by taking temporary ownership thereof until the CRT
461 * is destroyed (like mbedtls_x509_crt_parse_der_nocopy())
462 * \param cb A callback invoked for every unsupported certificate
463 * extension.
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200464 * \param p_ctx An opaque context passed to the callback.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200465 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200466 * \note This call is functionally equivalent to
467 * mbedtls_x509_crt_parse_der(), and/or
468 * mbedtls_x509_crt_parse_der_nocopy()
469 * but it calls the callback with every unsupported
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200470 * certificate extension and additionally the
471 * "certificate policies" extension if it contains any
472 * unsupported certificate policies.
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200473 * The callback must return a negative error code if it
474 * does not know how to handle such an extension.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200475 * When the callback fails to parse a critical extension
476 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
477 * When the callback fails to parse a non critical extension
478 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
479 * the extension and continues parsing.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200480 * Future versions of the library may invoke the callback
481 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200482 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200483 * \return \c 0 if successful.
484 * \return A negative error code on failure.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200485 */
Nicola Di Lietofde98f72020-05-28 08:34:33 +0200486int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
487 const unsigned char *buf,
488 size_t buflen,
Nicola Di Lieto5f6ebde2020-05-28 19:00:47 +0200489 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200490 mbedtls_x509_crt_ext_cb_t cb,
491 void *p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200492
493/**
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000494 * \brief Parse a single DER formatted certificate and add it
495 * to the end of the provided chained list. This is a
496 * variant of mbedtls_x509_crt_parse_der() which takes
497 * temporary ownership of the CRT buffer until the CRT
498 * is destroyed.
499 *
500 * \param chain The pointer to the start of the CRT chain to attach to.
501 * When parsing the first CRT in a chain, this should point
502 * to an instance of ::mbedtls_x509_crt initialized through
503 * mbedtls_x509_crt_init().
504 * \param buf The address of the readable buffer holding the DER encoded
505 * certificate to use. On success, this buffer must be
506 * retained and not be changed for the liftetime of the
507 * CRT chain \p chain, that is, until \p chain is destroyed
508 * through a call to mbedtls_x509_crt_free().
509 * \param buflen The size in Bytes of \p buf.
510 *
511 * \note This call is functionally equivalent to
512 * mbedtls_x509_crt_parse_der(), but it avoids creating a
513 * copy of the input buffer at the cost of stronger lifetime
514 * constraints. This is useful in constrained environments
515 * where duplication of the CRT cannot be tolerated.
516 *
517 * \return \c 0 if successful.
518 * \return A negative error code on failure.
519 */
520int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
521 const unsigned char *buf,
522 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523
524/**
Hanno Becker89a91122018-08-23 16:14:00 +0100525 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100526 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100528 * For CRTs in PEM encoding, the function parses permissively:
529 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100530 * returns the number of certificates for which parsing failed
531 * (hence \c 0 if all certificates were parsed successfully).
532 * If no certificate could be parsed, the function returns
533 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100535 * PEM encoded certificates may be interleaved by other data
536 * such as human readable descriptions of their content, as
537 * long as the certificates are enclosed in the PEM specific
538 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
539 *
540 * \param chain The chain to which to add the parsed certificates.
541 * \param buf The buffer holding the certificate data in PEM or DER format.
542 * For certificates in PEM encoding, this may be a concatenation
543 * of multiple certificates; for DER encoding, the buffer must
544 * comprise exactly one certificate.
545 * \param buflen The size of \p buf, including the terminating \c NULL byte
546 * in case of PEM encoded data.
547 *
548 * \return \c 0 if all certificates were parsed successfully.
549 * \return The (positive) number of certificates that couldn't
550 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100551 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100552 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557/**
558 * \brief Load one or more certificates and add them
559 * to the chained list. Parses permissively. If some
560 * certificates can be parsed, the result is the number
561 * of failed certificates it encountered. If none complete
562 * correctly, the first error is returned.
563 *
564 * \param chain points to the start of the chain
565 * \param path filename to read the certificates from
566 *
567 * \return 0 if all certificates parsed successfully, a positive number
568 * if partly successful or a specific X509 or PEM error code
569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571
572/**
573 * \brief Load one or more certificate files from a path and add them
574 * to the chained list. Parses permissively. If some
575 * certificates can be parsed, the result is the number
576 * of failed certificates it encountered. If none complete
577 * correctly, the first error is returned.
578 *
579 * \param chain points to the start of the chain
580 * \param path directory / folder to read the certificate files from
581 *
582 * \return 0 if all certificates parsed successfully, a positive number
583 * if partly successful or a specific X509 or PEM error code
584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
Janos Follath11b41eb2019-05-08 15:26:49 +0100586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#endif /* MBEDTLS_FS_IO */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200588/**
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300589 * \brief This function parses an item in the SubjectAlternativeNames
590 * extension.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200591 *
Ron Eldor890819a2019-05-13 19:03:04 +0300592 * \param san_buf The buffer holding the raw data item of the subject
593 * alternative name.
594 * \param san The target structure to populate with the parsed presentation
595 * of the subject alternative name encoded in \p san_raw.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200596 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300597 * \note Only "dnsName" and "otherName" of type hardware_module_name
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200598 * as defined in RFC 4180 is supported.
599 *
Ron Eldor890819a2019-05-13 19:03:04 +0300600 * \note This function should be called on a single raw data of
Ron Eldorcc45cd12019-05-15 10:20:09 +0300601 * subject alternative name. For example, after successful
Ron Eldor890819a2019-05-13 19:03:04 +0300602 * certificate parsing, one must iterate on every item in the
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300603 * \p crt->subject_alt_names sequence, and pass it to
604 * this function.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200605 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300606 * \warning The target structure contains pointers to the raw data of the
Ron Eldorcc45cd12019-05-15 10:20:09 +0300607 * parsed certificate, and its lifetime is restricted by the
608 * lifetime of the certificate.
609 *
Ron Eldor890819a2019-05-13 19:03:04 +0300610 * \return \c 0 on success
611 * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300612 * SAN type.
613 * \return Another negative value for any other failure.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200614 */
Ron Eldor890819a2019-05-13 19:03:04 +0300615int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
616 mbedtls_x509_subject_alternative_name *san );
Peter Kolbus9a969b62018-12-11 13:55:56 -0600617
Hanno Becker612a2f12020-10-09 09:19:39 +0100618#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619/**
620 * \brief Returns an informational string about the
621 * certificate.
622 *
623 * \param buf Buffer to write to
624 * \param size Maximum size of buffer
625 * \param prefix A line prefix
626 * \param crt The X509 certificate to represent
627 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200628 * \return The length of the string written (not including the
629 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
632 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633
634/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100635 * \brief Returns an informational string about the
636 * verification status of a certificate.
637 *
638 * \param buf Buffer to write to
639 * \param size Maximum size of buffer
640 * \param prefix A line prefix
641 * \param flags Verification flags created by mbedtls_x509_crt_verify()
642 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200643 * \return The length of the string written (not including the
644 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100645 */
646int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200647 uint32_t flags );
Hanno Becker88c2bf32019-06-14 17:21:24 +0100648#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200649
650/**
Hanno Becker902451d2019-03-27 11:48:40 +0000651 * \brief Verify a chain of certificates.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652 *
653 * The verify callback is a user-supplied callback that
654 * can clear / modify / add flags for a certificate. If set,
655 * the verification callback is called for each
656 * certificate in the chain (from the trust-ca down to the
657 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659 * int *flags). With the flags representing current flags for
660 * that specific certificate and the certificate depth from
661 * the bottom (Peer cert depth = 0).
662 *
663 * All flags left after returning from the callback
664 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200665 * return 0 for anything (including invalid certificates)
666 * other than fatal error, as a non-zero return code
667 * immediately aborts the verification process. For fatal
668 * errors, a specific error code should be used (different
669 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
670 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
671 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100673 * \note In case verification failed, the results can be displayed
674 * using \c mbedtls_x509_crt_verify_info()
675 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200676 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
677 * default security profile.
678 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100679 * \note It is your responsibility to provide up-to-date CRLs for
680 * all trusted CAs. If no CRL is provided for the CA that was
681 * used to sign the certificate, CRL verification is skipped
682 * silently, that is *without* setting any flag.
683 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200684 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200685 * (1) those of trusted root CAs, so that certificates
686 * chaining up to those CAs will be trusted, and (2)
687 * self-signed end-entity certificates to be trusted (for
688 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200689 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200690 *
Hanno Becker902451d2019-03-27 11:48:40 +0000691 * \param crt The certificate chain to be verified.
692 * \param trust_ca The list of trusted CAs.
693 * \param ca_crl The list of CRLs for trusted CAs.
Manuel Pégourié-Gonnardf58e5cc2020-07-24 10:31:37 +0200694 * \param cn The expected Common Name. This will be checked to be
695 * present in the certificate's subjectAltNames extension or,
696 * if this extension is absent, as a CN component in its
697 * Subject name. Currently only DNS names are supported. This
698 * may be \c NULL if the CN need not be verified.
Hanno Becker902451d2019-03-27 11:48:40 +0000699 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100700 * If the verification couldn't be completed, the flag value is
701 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000702 * \param f_vrfy The verification callback to use. See the documentation
703 * of mbedtls_x509_crt_verify() for more information.
704 * \param p_vrfy The context to be passed to \p f_vrfy.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705 *
Hanno Becker902451d2019-03-27 11:48:40 +0000706 * \return \c 0 if the chain is valid with respect to the
707 * passed CN, CAs, CRLs and security profile.
708 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
709 * certificate chain verification failed. In this case,
710 * \c *flags will have one or more
711 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
712 * flags set.
713 * \return Another negative error code in case of a fatal error
714 * encountered during the verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
717 mbedtls_x509_crt *trust_ca,
718 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200719 const char *cn, uint32_t *flags,
720 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200721 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200723/**
Hanno Becker902451d2019-03-27 11:48:40 +0000724 * \brief Verify a chain of certificates with respect to
725 * a configurable security profile.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200726 *
727 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
728 * security profile.
729 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200730 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200731 * for ECDSA) apply to all certificates: trusted root,
732 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200733 *
Hanno Becker902451d2019-03-27 11:48:40 +0000734 * \param crt The certificate chain to be verified.
735 * \param trust_ca The list of trusted CAs.
736 * \param ca_crl The list of CRLs for trusted CAs.
737 * \param profile The security profile to use for the verification.
738 * \param cn The expected Common Name. This may be \c NULL if the
739 * CN need not be verified.
740 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100741 * If the verification couldn't be completed, the flag value is
742 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000743 * \param f_vrfy The verification callback to use. See the documentation
744 * of mbedtls_x509_crt_verify() for more information.
745 * \param p_vrfy The context to be passed to \p f_vrfy.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200746 *
Hanno Becker902451d2019-03-27 11:48:40 +0000747 * \return \c 0 if the chain is valid with respect to the
748 * passed CN, CAs, CRLs and security profile.
749 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
750 * certificate chain verification failed. In this case,
751 * \c *flags will have one or more
752 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
753 * flags set.
754 * \return Another negative error code in case of a fatal error
755 * encountered during the verification process.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200756 */
757int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
758 mbedtls_x509_crt *trust_ca,
759 mbedtls_x509_crl *ca_crl,
760 const mbedtls_x509_crt_profile *profile,
761 const char *cn, uint32_t *flags,
762 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
763 void *p_vrfy );
764
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200765/**
766 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
767 *
768 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
769 * but can return early and restart according to the limit
770 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
771 *
Hanno Becker902451d2019-03-27 11:48:40 +0000772 * \param crt The certificate chain to be verified.
773 * \param trust_ca The list of trusted CAs.
774 * \param ca_crl The list of CRLs for trusted CAs.
775 * \param profile The security profile to use for the verification.
776 * \param cn The expected Common Name. This may be \c NULL if the
777 * CN need not be verified.
778 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100779 * If the verification couldn't be completed, the flag value is
780 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000781 * \param f_vrfy The verification callback to use. See the documentation
782 * of mbedtls_x509_crt_verify() for more information.
783 * \param p_vrfy The context to be passed to \p f_vrfy.
784 * \param rs_ctx The restart context to use. This may be set to \c NULL
785 * to disable restartable ECC.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200786 *
787 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200788 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200789 * operations was reached: see \c mbedtls_ecp_set_max_ops().
790 */
791int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
792 mbedtls_x509_crt *trust_ca,
793 mbedtls_x509_crl *ca_crl,
794 const mbedtls_x509_crt_profile *profile,
795 const char *cn, uint32_t *flags,
796 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
797 void *p_vrfy,
798 mbedtls_x509_crt_restart_ctx *rs_ctx );
799
Hanno Becker5c8df782019-03-27 11:01:17 +0000800/**
801 * \brief The type of trusted certificate callbacks.
802 *
803 * Callbacks of this type are passed to and used by the CRT
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300804 * verification routine mbedtls_x509_crt_verify_with_ca_cb()
Hanno Becker5c8df782019-03-27 11:01:17 +0000805 * when looking for trusted signers of a given certificate.
806 *
807 * On success, the callback returns a list of trusted
808 * certificates to be considered as potential signers
809 * for the input certificate.
810 *
811 * \param p_ctx An opaque context passed to the callback.
812 * \param child The certificate for which to search a potential signer.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300813 * This will point to a readable certificate.
Hanno Becker5c8df782019-03-27 11:01:17 +0000814 * \param candidate_cas The address at which to store the address of the first
815 * entry in the generated linked list of candidate signers.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300816 * This will not be \c NULL.
Hanno Becker5c8df782019-03-27 11:01:17 +0000817 *
818 * \note The callback must only return a non-zero value on a
819 * fatal error. If, in contrast, the search for a potential
820 * signer completes without a single candidate, the
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300821 * callback must return \c 0 and set \c *candidate_cas
Hanno Becker5c8df782019-03-27 11:01:17 +0000822 * to \c NULL.
823 *
824 * \return \c 0 on success. In this case, \c *candidate_cas points
825 * to a heap-allocated linked list of instances of
826 * ::mbedtls_x509_crt, and ownership of this list is passed
827 * to the caller.
828 * \return A negative error code on failure.
829 */
830typedef int (*mbedtls_x509_crt_ca_cb_t)( void *p_ctx,
831 mbedtls_x509_crt const *child,
832 mbedtls_x509_crt **candidate_cas );
Hanno Beckere15dae72019-03-28 13:55:38 +0000833
834#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker5c8df782019-03-27 11:01:17 +0000835/**
836 * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
837 * uses a callback to acquire the list of trusted CA
838 * certificates.
839 *
840 * \param crt The certificate chain to be verified.
841 * \param f_ca_cb The callback to be used to query for potential signers
842 * of a given child certificate. See the documentation of
843 * ::mbedtls_x509_crt_ca_cb_t for more information.
844 * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
845 * \param profile The security profile for the verification.
846 * \param cn The expected Common Name. This may be \c NULL if the
847 * CN need not be verified.
848 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100849 * If the verification couldn't be completed, the flag value is
850 * set to (uint32_t) -1.
Hanno Becker5c8df782019-03-27 11:01:17 +0000851 * \param f_vrfy The verification callback to use. See the documentation
852 * of mbedtls_x509_crt_verify() for more information.
853 * \param p_vrfy The context to be passed to \p f_vrfy.
854 *
855 * \return See \c mbedtls_crt_verify_with_profile().
856 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300857int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker5c8df782019-03-27 11:01:17 +0000858 mbedtls_x509_crt_ca_cb_t f_ca_cb,
859 void *p_ca_cb,
860 const mbedtls_x509_crt_profile *profile,
861 const char *cn, uint32_t *flags,
862 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
863 void *p_vrfy );
864
865#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
866
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200867/**
868 * \brief Check usage of certificate against keyUsage extension.
869 *
870 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200871 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
872 * before using the certificate to perform an RSA key
873 * exchange).
874 *
875 * \note Except for decipherOnly and encipherOnly, a bit set in the
876 * usage argument means this bit MUST be set in the
877 * certificate. For decipherOnly and encipherOnly, it means
878 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200879 *
880 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200882 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200883 *
884 * \note You should only call this function on leaf certificates, on
885 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200887 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200888int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
889 unsigned int usage );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200890
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200891/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000892 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200893 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000894 * \param crt Leaf certificate used.
895 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
896 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200898 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000899 * \return 0 if this use of the certificate is allowed,
900 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200901 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000902 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000905 const char *usage_oid,
906 size_t usage_len );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200910 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911 *
912 * \param crt a certificate to be verified
913 * \param crl the CRL to verify against
914 *
915 * \return 1 if the certificate is revoked, 0 otherwise
916 *
917 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100918int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920
921/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200922 * \brief Initialize a certificate (chain)
923 *
924 * \param crt Certificate chain to initialize
925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200927
928/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929 * \brief Unallocate all certificate data
930 *
931 * \param crt Certificate chain to free
932 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200934
935#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
936/**
937 * \brief Initialize a restart context
938 */
939void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
940
941/**
942 * \brief Free the components of a restart context
943 */
944void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
945#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947
948/* \} name */
949/* \} addtogroup x509_module */
950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952/**
953 * \brief Initialize a CRT writing context
954 *
955 * \param ctx CRT context to initialize
956 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958
959/**
960 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 *
963 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
965 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968
969/**
970 * \brief Set the serial number for a Certificate.
971 *
972 * \param ctx CRT context to use
973 * \param serial serial number to set
974 *
975 * \return 0 if successful
976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978
979/**
980 * \brief Set the validity period for a Certificate
981 * Timestamps should be in string format for UTC timezone
982 * i.e. "YYYYMMDDhhmmss"
983 * e.g. "20131231235959" for December 31st 2013
984 * at 23:59:59
985 *
986 * \param ctx CRT context to use
987 * \param not_before not_before timestamp
988 * \param not_after not_after timestamp
989 *
990 * \return 0 if timestamp was parsed successfully, or
991 * a specific error code
992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100994 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995
996/**
997 * \brief Set the issuer name for a Certificate
998 * Issuer names should contain a comma-separated list
999 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001000 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001 *
1002 * \param ctx CRT context to use
1003 * \param issuer_name issuer name to set
1004 *
1005 * \return 0 if issuer name was parsed successfully, or
1006 * a specific error code
1007 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001009 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010
1011/**
1012 * \brief Set the subject name for a Certificate
1013 * Subject names should contain a comma-separated list
1014 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001015 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 *
1017 * \param ctx CRT context to use
1018 * \param subject_name subject name to set
1019 *
1020 * \return 0 if subject name was parsed successfully, or
1021 * a specific error code
1022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001024 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025
1026/**
1027 * \brief Set the subject public key for the certificate
1028 *
1029 * \param ctx CRT context to use
1030 * \param key public key to include
1031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033
1034/**
1035 * \brief Set the issuer key used for signing the certificate
1036 *
1037 * \param ctx CRT context to use
1038 * \param key private key to sign with
1039 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041
1042/**
1043 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045 *
1046 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001047 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050
1051/**
1052 * \brief Generic function to add to or replace an extension in the
1053 * CRT
1054 *
1055 * \param ctx CRT context to use
1056 * \param oid OID of the extension
1057 * \param oid_len length of the OID
1058 * \param critical if the extension is critical (per the RFC's definition)
1059 * \param val value of the extension OCTET STRING
1060 * \param val_len length of the value data
1061 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001062 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065 const char *oid, size_t oid_len,
1066 int critical,
1067 const unsigned char *val, size_t val_len );
1068
1069/**
1070 * \brief Set the basicConstraints extension for a CRT
1071 *
1072 * \param ctx CRT context to use
1073 * \param is_ca is this a CA certificate
1074 * \param max_pathlen maximum length of certificate chains below this
1075 * certificate (only for CA certificates, -1 is
1076 * inlimited)
1077 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001078 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081 int is_ca, int max_pathlen );
1082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001084/**
1085 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001087 * called before
1088 *
1089 * \param ctx CRT context to use
1090 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001091 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094
1095/**
1096 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 * called before
1099 *
1100 * \param ctx CRT context to use
1101 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001102 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
1105#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106
1107/**
1108 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110 *
1111 * \param ctx CRT context to use
1112 * \param key_usage key usage flags to set
1113 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001114 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001115 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +02001116int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
1117 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118
1119/**
1120 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122 *
1123 * \param ctx CRT context to use
1124 * \param ns_cert_type Netscape Cert Type flags to set
1125 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001126 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129 unsigned char ns_cert_type );
1130
1131/**
1132 * \brief Free the contents of a CRT write context
1133 *
1134 * \param ctx CRT context to free
1135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001137
1138/**
1139 * \brief Write a built up certificate to a X509 DER structure
1140 * Note: data is written at the end of the buffer! Use the
1141 * return value to determine where you should start
1142 * using the buffer
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
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001147 * \param f_rng RNG function. This must not be \c NULL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 * \param p_rng RNG parameter
1149 *
1150 * \return length of data written if successful, or a specific
1151 * error code
1152 *
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001153 * \note \p f_rng is used for the signature operation.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 int (*f_rng)(void *, unsigned char *, size_t),
1157 void *p_rng );
1158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160/**
1161 * \brief Write a built up certificate to a X509 PEM string
1162 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001163 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164 * \param buf buffer to write to
1165 * \param size size of the buffer
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001166 * \param f_rng RNG function. This must not be \c NULL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001167 * \param p_rng RNG parameter
1168 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001169 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170 *
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001171 * \note \p f_rng is used for the signature operation.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 int (*f_rng)(void *, unsigned char *, size_t),
1175 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#endif /* MBEDTLS_PEM_WRITE_C */
1177#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001178
1179#ifdef __cplusplus
1180}
1181#endif
1182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183#endif /* mbedtls_x509_crt.h */