blob: 49211a948fc93e82873b27c07eac257854443017 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027
Jaeden Amero6609aef2019-07-04 20:01:14 +010028#include "mbedtls/x509.h"
29#include "mbedtls/x509_crl.h"
30#include "mbedtls/bignum.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031
32/**
33 * \addtogroup x509_module
34 * \{
35 */
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/**
42 * \name Structures and functions for parsing and writing X.509 certificates
43 * \{
44 */
45
46/**
47 * Container for an X.509 certificate. The certificate may be chained.
48 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020051 int MBEDTLS_PRIVATE(own_buffer); /**< Indicates if \c raw is owned
Hanno Becker1a65dcd2019-01-31 08:57:44 +000052 * by the structure or not. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020053 mbedtls_x509_buf MBEDTLS_PRIVATE(raw); /**< The raw certificate data (DER). */
54 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 +020055
Mateusz Starzyk846f0212021-05-19 19:44:07 +020056 int MBEDTLS_PRIVATE(version); /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
57 mbedtls_x509_buf MBEDTLS_PRIVATE(serial); /**< Unique id for certificate issued by a specific CA. */
58 mbedtls_x509_buf MBEDTLS_PRIVATE(sig_oid); /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059
Mateusz Starzyk846f0212021-05-19 19:44:07 +020060 mbedtls_x509_buf MBEDTLS_PRIVATE(issuer_raw); /**< The raw issuer data (DER). Used for quick comparison. */
61 mbedtls_x509_buf MBEDTLS_PRIVATE(subject_raw); /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062
Mateusz Starzyk846f0212021-05-19 19:44:07 +020063 mbedtls_x509_name MBEDTLS_PRIVATE(issuer); /**< The parsed issuer data (named information object). */
64 mbedtls_x509_name MBEDTLS_PRIVATE(subject); /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065
Mateusz Starzyk846f0212021-05-19 19:44:07 +020066 mbedtls_x509_time MBEDTLS_PRIVATE(valid_from); /**< Start time of certificate validity. */
67 mbedtls_x509_time MBEDTLS_PRIVATE(valid_to); /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068
Mateusz Starzyk846f0212021-05-19 19:44:07 +020069 mbedtls_x509_buf MBEDTLS_PRIVATE(pk_raw);
70 mbedtls_pk_context MBEDTLS_PRIVATE(pk); /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071
Mateusz Starzyk846f0212021-05-19 19:44:07 +020072 mbedtls_x509_buf MBEDTLS_PRIVATE(issuer_id); /**< Optional X.509 v2/v3 issuer unique identifier. */
73 mbedtls_x509_buf MBEDTLS_PRIVATE(subject_id); /**< Optional X.509 v2/v3 subject unique identifier. */
74 mbedtls_x509_buf MBEDTLS_PRIVATE(v3_ext); /**< Optional X.509 v3 extensions. */
75 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 +020076
Mateusz Starzyk846f0212021-05-19 19:44:07 +020077 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 +020078
Mateusz Starzyk846f0212021-05-19 19:44:07 +020079 int MBEDTLS_PRIVATE(ext_types); /**< Bit string containing detected and parsed extensions */
80 int MBEDTLS_PRIVATE(ca_istrue); /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
81 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 +020082
Mateusz Starzyk846f0212021-05-19 19:44:07 +020083 unsigned int MBEDTLS_PRIVATE(key_usage); /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
Mateusz Starzyk846f0212021-05-19 19:44:07 +020085 mbedtls_x509_sequence MBEDTLS_PRIVATE(ext_key_usage); /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086
Mateusz Starzyk846f0212021-05-19 19:44:07 +020087 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 +020088
Mateusz Starzyk846f0212021-05-19 19:44:07 +020089 mbedtls_x509_buf MBEDTLS_PRIVATE(sig); /**< Signature: hash of the tbs part signed with the private key. */
90 mbedtls_md_type_t MBEDTLS_PRIVATE(sig_md); /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
91 mbedtls_pk_type_t MBEDTLS_PRIVATE(sig_pk); /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
92 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 +020093
Mateusz Starzyk846f0212021-05-19 19:44:07 +020094 struct mbedtls_x509_crt *MBEDTLS_PRIVATE(next); /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097
Janos Follath5091bec2019-05-08 15:23:08 +010098/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +020099 * From RFC 5280 section 4.2.1.6:
100 * OtherName ::= SEQUENCE {
101 * type-id OBJECT IDENTIFIER,
102 * value [0] EXPLICIT ANY DEFINED BY type-id }
103 */
104typedef struct mbedtls_x509_san_other_name
105{
Ron Eldor890819a2019-05-13 19:03:04 +0300106 /**
107 * The type_id is an OID as deifned in RFC 5280.
108 * To check the value of the type id, you should use
109 * \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf.
110 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200111 mbedtls_x509_buf MBEDTLS_PRIVATE(type_id); /**< The type id. */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200112 union
113 {
Janos Follath5091bec2019-05-08 15:23:08 +0100114 /**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200115 * From RFC 4108 section 5:
116 * HardwareModuleName ::= SEQUENCE {
117 * hwType OBJECT IDENTIFIER,
118 * hwSerialNum OCTET STRING }
119 */
Ron Eldorf05f5942019-05-13 19:11:31 +0300120 struct
121 {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200122 mbedtls_x509_buf MBEDTLS_PRIVATE(oid); /**< The object identifier. */
123 mbedtls_x509_buf MBEDTLS_PRIVATE(val); /**< The named value. */
Ron Eldorf05f5942019-05-13 19:11:31 +0300124 }
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200125 MBEDTLS_PRIVATE(hardware_module_name);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200126 }
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200127 MBEDTLS_PRIVATE(value);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200128}
129mbedtls_x509_san_other_name;
130
Janos Follath5091bec2019-05-08 15:23:08 +0100131/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200132 * A structure for holding the parsed Subject Alternative Name, according to type
133 */
134typedef struct mbedtls_x509_subject_alternative_name
135{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200136 int MBEDTLS_PRIVATE(type); /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200137 union {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200138 mbedtls_x509_san_other_name MBEDTLS_PRIVATE(other_name); /**< The otherName supported type. */
139 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 +0200140 }
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200141 MBEDTLS_PRIVATE(san); /**< A union of the supported SAN types */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200142}
143mbedtls_x509_subject_alternative_name;
144
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200145/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200146 * Build flag from an algorithm/curve identifier (pk, md, ecp)
147 * Since 0 is always XXX_NONE, ignore it.
148 */
Hanno Becker1eeca412018-10-15 12:01:35 +0100149#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200150
151/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200152 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200153 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200154 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard55a7fb82021-06-17 10:39:39 +0200155 *
156 * The fields of this structure are part of the public API and can be
157 * manipulated directly by applications. Future versions of the library may
158 * add extra fields or reorder existing fields.
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200159 *
160 * You can create custom profiles by starting from a copy of
161 * an existing profile, such as mbedtls_x509_crt_profile_default or
162 * mbedtls_x509_ctr_profile_none and then tune it to your needs.
163 *
164 * For example to allow SHA-224 in addition to the default:
165 *
166 * mbedtls_x509_crt_profile my_profile = mbedtls_x509_crt_profile_default;
167 * my_profile.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 );
168 *
169 * Or to allow only RSA-3072+ with SHA-256:
170 *
171 * mbedtls_x509_crt_profile my_profile = mbedtls_x509_crt_profile_none;
172 * my_profile.allowed_mds = MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 );
173 * my_profile.allowed_pks = MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA );
174 * my_profile.rsa_min_bitlen = 3072;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200175 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200176typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200177{
Manuel Pégourié-Gonnard5314e082021-06-17 10:32:01 +0200178 uint32_t allowed_mds; /**< MDs for signatures */
179 uint32_t allowed_pks; /**< PK algs for signatures */
180 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
181 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200182}
183mbedtls_x509_crt_profile;
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#define MBEDTLS_X509_CRT_VERSION_1 0
186#define MBEDTLS_X509_CRT_VERSION_2 1
187#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
190#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200191
Andres AGf9113192016-09-02 14:06:04 +0100192#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
193#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
194#endif
195
Hanno Becker7ac83f92020-10-09 10:48:22 +0100196/* This macro unfolds to the concatenation of macro invocations
197 * X509_CRT_ERROR_INFO( error code,
198 * error code as string,
199 * human readable description )
200 * where X509_CRT_ERROR_INFO is defined by the user.
201 * See x509_crt.c for an example of how to use this. */
202#define MBEDTLS_X509_CRT_ERROR_INFO_LIST \
203 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_EXPIRED, \
204 "MBEDTLS_X509_BADCERT_EXPIRED", \
205 "The certificate validity has expired" ) \
206 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_REVOKED, \
207 "MBEDTLS_X509_BADCERT_REVOKED", \
208 "The certificate has been revoked (is on a CRL)" ) \
209 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_CN_MISMATCH, \
210 "MBEDTLS_X509_BADCERT_CN_MISMATCH", \
211 "The certificate Common Name (CN) does not match with the expected CN" ) \
212 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_NOT_TRUSTED, \
213 "MBEDTLS_X509_BADCERT_NOT_TRUSTED", \
214 "The certificate is not correctly signed by the trusted CA" ) \
215 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_NOT_TRUSTED, \
216 "MBEDTLS_X509_BADCRL_NOT_TRUSTED", \
217 "The CRL is not correctly signed by the trusted CA" ) \
218 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_EXPIRED, \
219 "MBEDTLS_X509_BADCRL_EXPIRED", \
220 "The CRL is expired" ) \
221 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_MISSING, \
222 "MBEDTLS_X509_BADCERT_MISSING", \
223 "Certificate was missing" ) \
224 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_SKIP_VERIFY, \
225 "MBEDTLS_X509_BADCERT_SKIP_VERIFY", \
226 "Certificate verification was skipped" ) \
227 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_OTHER, \
228 "MBEDTLS_X509_BADCERT_OTHER", \
229 "Other reason (can be used by verify callback)" ) \
230 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_FUTURE, \
231 "MBEDTLS_X509_BADCERT_FUTURE", \
232 "The certificate validity starts in the future" ) \
233 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_FUTURE, \
234 "MBEDTLS_X509_BADCRL_FUTURE", \
235 "The CRL is from the future" ) \
236 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_KEY_USAGE, \
237 "MBEDTLS_X509_BADCERT_KEY_USAGE", \
238 "Usage does not match the keyUsage extension" ) \
239 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, \
240 "MBEDTLS_X509_BADCERT_EXT_KEY_USAGE", \
241 "Usage does not match the extendedKeyUsage extension" ) \
242 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_NS_CERT_TYPE, \
243 "MBEDTLS_X509_BADCERT_NS_CERT_TYPE", \
244 "Usage does not match the nsCertType extension" ) \
245 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_BAD_MD, \
246 "MBEDTLS_X509_BADCERT_BAD_MD", \
247 "The certificate is signed with an unacceptable hash." ) \
248 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_BAD_PK, \
249 "MBEDTLS_X509_BADCERT_BAD_PK", \
250 "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." ) \
251 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_BAD_KEY, \
252 "MBEDTLS_X509_BADCERT_BAD_KEY", \
253 "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." ) \
254 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_BAD_MD, \
255 "MBEDTLS_X509_BADCRL_BAD_MD", \
256 "The CRL is signed with an unacceptable hash." ) \
257 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_BAD_PK, \
258 "MBEDTLS_X509_BADCRL_BAD_PK", \
259 "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." ) \
260 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_BAD_KEY, \
261 "MBEDTLS_X509_BADCRL_BAD_KEY", \
262 "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." )
263
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264/**
265 * Container for writing a certificate (CRT)
266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200269 int MBEDTLS_PRIVATE(version);
270 mbedtls_mpi MBEDTLS_PRIVATE(serial);
271 mbedtls_pk_context *MBEDTLS_PRIVATE(subject_key);
272 mbedtls_pk_context *MBEDTLS_PRIVATE(issuer_key);
273 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(subject);
274 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(issuer);
275 mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg);
276 char MBEDTLS_PRIVATE(not_before)[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
277 char MBEDTLS_PRIVATE(not_after)[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
278 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(extensions);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200282/**
283 * Item in a verification chain: cert and flags for it
284 */
285typedef struct {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200286 mbedtls_x509_crt *MBEDTLS_PRIVATE(crt);
287 uint32_t MBEDTLS_PRIVATE(flags);
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200288} mbedtls_x509_crt_verify_chain_item;
289
290/**
291 * Max size of verification chain: end-entity + intermediates + trusted root
292 */
293#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
294
295/**
296 * Verification chain as built by \c mbedtls_crt_verify_chain()
297 */
298typedef struct
299{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200300 mbedtls_x509_crt_verify_chain_item MBEDTLS_PRIVATE(items)[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
301 unsigned MBEDTLS_PRIVATE(len);
Hanno Beckerf53893b2019-03-28 13:45:55 +0000302
303#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
304 /* This stores the list of potential trusted signers obtained from
305 * the CA callback used for the CRT verification, if configured.
306 * We must track it somewhere because the callback passes its
307 * ownership to the caller. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200308 mbedtls_x509_crt *MBEDTLS_PRIVATE(trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +0000309#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200310} mbedtls_x509_crt_verify_chain;
311
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200312#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
313
314/**
315 * \brief Context for resuming X.509 verify operations
316 */
317typedef struct
318{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200319 /* for check_signature() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200320 mbedtls_pk_restart_ctx MBEDTLS_PRIVATE(pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200321
322 /* for find_parent_in() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200323 mbedtls_x509_crt *MBEDTLS_PRIVATE(parent); /* non-null iff parent_in in progress */
324 mbedtls_x509_crt *MBEDTLS_PRIVATE(fallback_parent);
325 int MBEDTLS_PRIVATE(fallback_signature_is_good);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200326
327 /* for find_parent() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200328 int MBEDTLS_PRIVATE(parent_is_trusted); /* -1 if find_parent is not in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200329
330 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200331 enum {
332 x509_crt_rs_none,
333 x509_crt_rs_find_parent,
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200334 } MBEDTLS_PRIVATE(in_progress); /* none if no operation is in progress */
335 int MBEDTLS_PRIVATE(self_cnt);
336 mbedtls_x509_crt_verify_chain MBEDTLS_PRIVATE(ver_chain);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200337
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200338} mbedtls_x509_crt_restart_ctx;
339
340#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
341
342/* Now we can declare functions that take a pointer to that */
343typedef void mbedtls_x509_crt_restart_ctx;
344
345#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200349 * Default security profile. Should provide a good balance between security
350 * and compatibility with current deployments.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200351 *
352 * This profile permits:
353 * - SHA2 hashes with at least 256 bits: SHA-256, SHA-384, SHA-512.
Gilles Peskine39957502021-06-17 23:17:52 +0200354 * - Elliptic curves with 255 bits and above except secp256k1.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200355 * - RSA with 2048 bits and above.
356 *
357 * New minor versions of Mbed TLS may extend this profile, for example if
Gilles Peskine39957502021-06-17 23:17:52 +0200358 * new algorithms are added to the library. New minor versions of Mbed TLS will
Gilles Peskineffb92da2021-06-02 00:03:26 +0200359 * not reduce this profile unless serious security concerns require it.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200360 */
361extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
362
363/**
364 * Expected next default profile. Recommended for new deployments.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200365 * Currently targets a 128-bit security level, except for allowing RSA-2048.
366 * This profile may change at any time.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200367 */
368extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
369
370/**
371 * NSA Suite B profile.
372 */
373extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
374
375/**
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200376 * Empty profile that allows nothing. Useful as a basis for constructing
377 * custom profiles.
378 */
379extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none;
380
381/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382 * \brief Parse a single DER formatted certificate and add it
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000383 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000385 * \param chain The pointer to the start of the CRT chain to attach to.
386 * When parsing the first CRT in a chain, this should point
387 * to an instance of ::mbedtls_x509_crt initialized through
388 * mbedtls_x509_crt_init().
389 * \param buf The buffer holding the DER encoded certificate.
390 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000392 * \note This function makes an internal copy of the CRT buffer
393 * \p buf. In particular, \p buf may be destroyed or reused
394 * after this call returns. To avoid duplicating the CRT
395 * buffer (at the cost of stricter lifetime constraints),
396 * use mbedtls_x509_crt_parse_der_nocopy() instead.
397 *
398 * \return \c 0 if successful.
399 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000401int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
402 const unsigned char *buf,
403 size_t buflen );
404
405/**
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200406 * \brief The type of certificate extension callbacks.
407 *
408 * Callbacks of this type are passed to and used by the
Nicola Di Lietofde98f72020-05-28 08:34:33 +0200409 * mbedtls_x509_crt_parse_der_with_ext_cb() routine when
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200410 * it encounters either an unsupported extension or a
411 * "certificate policies" extension containing any
412 * unsupported certificate policies.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200413 * Future versions of the library may invoke the callback
414 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200415 *
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200416 * \param p_ctx An opaque context passed to the callback.
ndilieto6e249802020-05-28 06:17:38 +0000417 * \param crt The certificate being parsed.
418 * \param oid The OID of the extension.
419 * \param critical Whether the extension is critical.
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200420 * \param p Pointer to the start of the extension value
ndilieto6e249802020-05-28 06:17:38 +0000421 * (the content of the OCTET STRING).
ndilieto6e249802020-05-28 06:17:38 +0000422 * \param end End of extension value.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200423 *
424 * \note The callback must fail and return a negative error code
425 * if it can not parse or does not support the extension.
426 * When the callback fails to parse a critical extension
427 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
428 * When the callback fails to parse a non critical extension
429 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
430 * the extension and continues parsing.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200431 *
432 * \return \c 0 on success.
433 * \return A negative error code on failure.
434 */
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200435typedef int (*mbedtls_x509_crt_ext_cb_t)( void *p_ctx,
436 mbedtls_x509_crt const *crt,
ndilieto6e249802020-05-28 06:17:38 +0000437 mbedtls_x509_buf const *oid,
438 int critical,
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200439 const unsigned char *p,
ndilieto6e249802020-05-28 06:17:38 +0000440 const unsigned char *end );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200441
442/**
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200443 * \brief Parse a single DER formatted certificate and add it
444 * to the end of the provided chained list.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200445 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200446 * \param chain The pointer to the start of the CRT chain to attach to.
447 * When parsing the first CRT in a chain, this should point
448 * to an instance of ::mbedtls_x509_crt initialized through
449 * mbedtls_x509_crt_init().
450 * \param buf The buffer holding the DER encoded certificate.
451 * \param buflen The size in Bytes of \p buf.
452 * \param make_copy When not zero this function makes an internal copy of the
453 * CRT buffer \p buf. In particular, \p buf may be destroyed
454 * or reused after this call returns.
455 * When zero this function avoids duplicating the CRT buffer
456 * by taking temporary ownership thereof until the CRT
457 * is destroyed (like mbedtls_x509_crt_parse_der_nocopy())
458 * \param cb A callback invoked for every unsupported certificate
459 * extension.
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200460 * \param p_ctx An opaque context passed to the callback.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200461 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200462 * \note This call is functionally equivalent to
463 * mbedtls_x509_crt_parse_der(), and/or
464 * mbedtls_x509_crt_parse_der_nocopy()
465 * but it calls the callback with every unsupported
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200466 * certificate extension and additionally the
467 * "certificate policies" extension if it contains any
468 * unsupported certificate policies.
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200469 * The callback must return a negative error code if it
470 * does not know how to handle such an extension.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200471 * When the callback fails to parse a critical extension
472 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
473 * When the callback fails to parse a non critical extension
474 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
475 * the extension and continues parsing.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200476 * Future versions of the library may invoke the callback
477 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200478 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200479 * \return \c 0 if successful.
480 * \return A negative error code on failure.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200481 */
Nicola Di Lietofde98f72020-05-28 08:34:33 +0200482int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
483 const unsigned char *buf,
484 size_t buflen,
Nicola Di Lieto5f6ebde2020-05-28 19:00:47 +0200485 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200486 mbedtls_x509_crt_ext_cb_t cb,
487 void *p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200488
489/**
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000490 * \brief Parse a single DER formatted certificate and add it
491 * to the end of the provided chained list. This is a
492 * variant of mbedtls_x509_crt_parse_der() which takes
493 * temporary ownership of the CRT buffer until the CRT
494 * is destroyed.
495 *
496 * \param chain The pointer to the start of the CRT chain to attach to.
497 * When parsing the first CRT in a chain, this should point
498 * to an instance of ::mbedtls_x509_crt initialized through
499 * mbedtls_x509_crt_init().
500 * \param buf The address of the readable buffer holding the DER encoded
501 * certificate to use. On success, this buffer must be
502 * retained and not be changed for the liftetime of the
503 * CRT chain \p chain, that is, until \p chain is destroyed
504 * through a call to mbedtls_x509_crt_free().
505 * \param buflen The size in Bytes of \p buf.
506 *
507 * \note This call is functionally equivalent to
508 * mbedtls_x509_crt_parse_der(), but it avoids creating a
509 * copy of the input buffer at the cost of stronger lifetime
510 * constraints. This is useful in constrained environments
511 * where duplication of the CRT cannot be tolerated.
512 *
513 * \return \c 0 if successful.
514 * \return A negative error code on failure.
515 */
516int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
517 const unsigned char *buf,
518 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519
520/**
Hanno Becker89a91122018-08-23 16:14:00 +0100521 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100522 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100524 * For CRTs in PEM encoding, the function parses permissively:
525 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100526 * returns the number of certificates for which parsing failed
527 * (hence \c 0 if all certificates were parsed successfully).
528 * If no certificate could be parsed, the function returns
529 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100531 * PEM encoded certificates may be interleaved by other data
532 * such as human readable descriptions of their content, as
533 * long as the certificates are enclosed in the PEM specific
534 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
535 *
536 * \param chain The chain to which to add the parsed certificates.
537 * \param buf The buffer holding the certificate data in PEM or DER format.
538 * For certificates in PEM encoding, this may be a concatenation
539 * of multiple certificates; for DER encoding, the buffer must
540 * comprise exactly one certificate.
541 * \param buflen The size of \p buf, including the terminating \c NULL byte
542 * in case of PEM encoded data.
543 *
544 * \return \c 0 if all certificates were parsed successfully.
545 * \return The (positive) number of certificates that couldn't
546 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100547 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100548 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553/**
554 * \brief Load one or more certificates and add them
555 * to the chained list. Parses permissively. If some
556 * certificates can be parsed, the result is the number
557 * of failed certificates it encountered. If none complete
558 * correctly, the first error is returned.
559 *
560 * \param chain points to the start of the chain
561 * \param path filename to read the certificates from
562 *
563 * \return 0 if all certificates parsed successfully, a positive number
564 * if partly successful or a specific X509 or PEM error code
565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
568/**
569 * \brief Load one or more certificate files from a path and add them
570 * to the chained list. Parses permissively. If some
571 * certificates can be parsed, the result is the number
572 * of failed certificates it encountered. If none complete
573 * correctly, the first error is returned.
574 *
575 * \param chain points to the start of the chain
576 * \param path directory / folder to read the certificate files from
577 *
578 * \return 0 if all certificates parsed successfully, a positive number
579 * if partly successful or a specific X509 or PEM error code
580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
Janos Follath11b41eb2019-05-08 15:26:49 +0100582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#endif /* MBEDTLS_FS_IO */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200584/**
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300585 * \brief This function parses an item in the SubjectAlternativeNames
586 * extension.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200587 *
Ron Eldor890819a2019-05-13 19:03:04 +0300588 * \param san_buf The buffer holding the raw data item of the subject
589 * alternative name.
590 * \param san The target structure to populate with the parsed presentation
591 * of the subject alternative name encoded in \p san_raw.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200592 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300593 * \note Only "dnsName" and "otherName" of type hardware_module_name
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200594 * as defined in RFC 4180 is supported.
595 *
Ron Eldor890819a2019-05-13 19:03:04 +0300596 * \note This function should be called on a single raw data of
Ron Eldorcc45cd12019-05-15 10:20:09 +0300597 * subject alternative name. For example, after successful
Ron Eldor890819a2019-05-13 19:03:04 +0300598 * certificate parsing, one must iterate on every item in the
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300599 * \p crt->subject_alt_names sequence, and pass it to
600 * this function.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200601 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300602 * \warning The target structure contains pointers to the raw data of the
Ron Eldorcc45cd12019-05-15 10:20:09 +0300603 * parsed certificate, and its lifetime is restricted by the
604 * lifetime of the certificate.
605 *
Ron Eldor890819a2019-05-13 19:03:04 +0300606 * \return \c 0 on success
607 * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300608 * SAN type.
609 * \return Another negative value for any other failure.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200610 */
Ron Eldor890819a2019-05-13 19:03:04 +0300611int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
612 mbedtls_x509_subject_alternative_name *san );
Peter Kolbus9a969b62018-12-11 13:55:56 -0600613
Hanno Becker612a2f12020-10-09 09:19:39 +0100614#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200615/**
616 * \brief Returns an informational string about the
617 * certificate.
618 *
619 * \param buf Buffer to write to
620 * \param size Maximum size of buffer
621 * \param prefix A line prefix
622 * \param crt The X509 certificate to represent
623 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200624 * \return The length of the string written (not including the
625 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
628 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629
630/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100631 * \brief Returns an informational string about the
632 * verification status of a certificate.
633 *
634 * \param buf Buffer to write to
635 * \param size Maximum size of buffer
636 * \param prefix A line prefix
637 * \param flags Verification flags created by mbedtls_x509_crt_verify()
638 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200639 * \return The length of the string written (not including the
640 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100641 */
642int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200643 uint32_t flags );
Hanno Becker88c2bf32019-06-14 17:21:24 +0100644#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200645
646/**
Hanno Becker902451d2019-03-27 11:48:40 +0000647 * \brief Verify a chain of certificates.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 *
649 * The verify callback is a user-supplied callback that
650 * can clear / modify / add flags for a certificate. If set,
651 * the verification callback is called for each
652 * certificate in the chain (from the trust-ca down to the
653 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 * int *flags). With the flags representing current flags for
656 * that specific certificate and the certificate depth from
657 * the bottom (Peer cert depth = 0).
658 *
659 * All flags left after returning from the callback
660 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200661 * return 0 for anything (including invalid certificates)
662 * other than fatal error, as a non-zero return code
663 * immediately aborts the verification process. For fatal
664 * errors, a specific error code should be used (different
665 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
666 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
667 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100669 * \note In case verification failed, the results can be displayed
670 * using \c mbedtls_x509_crt_verify_info()
671 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200672 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
673 * default security profile.
674 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100675 * \note It is your responsibility to provide up-to-date CRLs for
676 * all trusted CAs. If no CRL is provided for the CA that was
677 * used to sign the certificate, CRL verification is skipped
678 * silently, that is *without* setting any flag.
679 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200680 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200681 * (1) those of trusted root CAs, so that certificates
682 * chaining up to those CAs will be trusted, and (2)
683 * self-signed end-entity certificates to be trusted (for
684 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200685 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200686 *
Hanno Becker902451d2019-03-27 11:48:40 +0000687 * \param crt The certificate chain to be verified.
688 * \param trust_ca The list of trusted CAs.
689 * \param ca_crl The list of CRLs for trusted CAs.
Manuel Pégourié-Gonnardf58e5cc2020-07-24 10:31:37 +0200690 * \param cn The expected Common Name. This will be checked to be
691 * present in the certificate's subjectAltNames extension or,
692 * if this extension is absent, as a CN component in its
693 * Subject name. Currently only DNS names are supported. This
694 * may be \c NULL if the CN need not be verified.
Hanno Becker902451d2019-03-27 11:48:40 +0000695 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100696 * If the verification couldn't be completed, the flag value is
697 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000698 * \param f_vrfy The verification callback to use. See the documentation
699 * of mbedtls_x509_crt_verify() for more information.
700 * \param p_vrfy The context to be passed to \p f_vrfy.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701 *
Hanno Becker902451d2019-03-27 11:48:40 +0000702 * \return \c 0 if the chain is valid with respect to the
703 * passed CN, CAs, CRLs and security profile.
704 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
705 * certificate chain verification failed. In this case,
706 * \c *flags will have one or more
707 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
708 * flags set.
709 * \return Another negative error code in case of a fatal error
710 * encountered during the verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
713 mbedtls_x509_crt *trust_ca,
714 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200715 const char *cn, uint32_t *flags,
716 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200717 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200719/**
Hanno Becker902451d2019-03-27 11:48:40 +0000720 * \brief Verify a chain of certificates with respect to
721 * a configurable security profile.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200722 *
723 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
724 * security profile.
725 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200726 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200727 * for ECDSA) apply to all certificates: trusted root,
728 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200729 *
Hanno Becker902451d2019-03-27 11:48:40 +0000730 * \param crt The certificate chain to be verified.
731 * \param trust_ca The list of trusted CAs.
732 * \param ca_crl The list of CRLs for trusted CAs.
733 * \param profile The security profile to use for the verification.
734 * \param cn The expected Common Name. This may be \c NULL if the
735 * CN need not be verified.
736 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100737 * If the verification couldn't be completed, the flag value is
738 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000739 * \param f_vrfy The verification callback to use. See the documentation
740 * of mbedtls_x509_crt_verify() for more information.
741 * \param p_vrfy The context to be passed to \p f_vrfy.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200742 *
Hanno Becker902451d2019-03-27 11:48:40 +0000743 * \return \c 0 if the chain is valid with respect to the
744 * passed CN, CAs, CRLs and security profile.
745 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
746 * certificate chain verification failed. In this case,
747 * \c *flags will have one or more
748 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
749 * flags set.
750 * \return Another negative error code in case of a fatal error
751 * encountered during the verification process.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200752 */
753int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
754 mbedtls_x509_crt *trust_ca,
755 mbedtls_x509_crl *ca_crl,
756 const mbedtls_x509_crt_profile *profile,
757 const char *cn, uint32_t *flags,
758 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
759 void *p_vrfy );
760
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200761/**
762 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
763 *
764 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
765 * but can return early and restart according to the limit
766 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
767 *
Hanno Becker902451d2019-03-27 11:48:40 +0000768 * \param crt The certificate chain to be verified.
769 * \param trust_ca The list of trusted CAs.
770 * \param ca_crl The list of CRLs for trusted CAs.
771 * \param profile The security profile to use for the verification.
772 * \param cn The expected Common Name. This may be \c NULL if the
773 * CN need not be verified.
774 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100775 * If the verification couldn't be completed, the flag value is
776 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000777 * \param f_vrfy The verification callback to use. See the documentation
778 * of mbedtls_x509_crt_verify() for more information.
779 * \param p_vrfy The context to be passed to \p f_vrfy.
780 * \param rs_ctx The restart context to use. This may be set to \c NULL
781 * to disable restartable ECC.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200782 *
783 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200784 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200785 * operations was reached: see \c mbedtls_ecp_set_max_ops().
786 */
787int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
788 mbedtls_x509_crt *trust_ca,
789 mbedtls_x509_crl *ca_crl,
790 const mbedtls_x509_crt_profile *profile,
791 const char *cn, uint32_t *flags,
792 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
793 void *p_vrfy,
794 mbedtls_x509_crt_restart_ctx *rs_ctx );
795
Hanno Becker5c8df782019-03-27 11:01:17 +0000796/**
797 * \brief The type of trusted certificate callbacks.
798 *
799 * Callbacks of this type are passed to and used by the CRT
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300800 * verification routine mbedtls_x509_crt_verify_with_ca_cb()
Hanno Becker5c8df782019-03-27 11:01:17 +0000801 * when looking for trusted signers of a given certificate.
802 *
803 * On success, the callback returns a list of trusted
804 * certificates to be considered as potential signers
805 * for the input certificate.
806 *
807 * \param p_ctx An opaque context passed to the callback.
808 * \param child The certificate for which to search a potential signer.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300809 * This will point to a readable certificate.
Hanno Becker5c8df782019-03-27 11:01:17 +0000810 * \param candidate_cas The address at which to store the address of the first
811 * entry in the generated linked list of candidate signers.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300812 * This will not be \c NULL.
Hanno Becker5c8df782019-03-27 11:01:17 +0000813 *
814 * \note The callback must only return a non-zero value on a
815 * fatal error. If, in contrast, the search for a potential
816 * signer completes without a single candidate, the
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300817 * callback must return \c 0 and set \c *candidate_cas
Hanno Becker5c8df782019-03-27 11:01:17 +0000818 * to \c NULL.
819 *
820 * \return \c 0 on success. In this case, \c *candidate_cas points
821 * to a heap-allocated linked list of instances of
822 * ::mbedtls_x509_crt, and ownership of this list is passed
823 * to the caller.
824 * \return A negative error code on failure.
825 */
826typedef int (*mbedtls_x509_crt_ca_cb_t)( void *p_ctx,
827 mbedtls_x509_crt const *child,
828 mbedtls_x509_crt **candidate_cas );
Hanno Beckere15dae72019-03-28 13:55:38 +0000829
830#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker5c8df782019-03-27 11:01:17 +0000831/**
832 * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
833 * uses a callback to acquire the list of trusted CA
834 * certificates.
835 *
836 * \param crt The certificate chain to be verified.
837 * \param f_ca_cb The callback to be used to query for potential signers
838 * of a given child certificate. See the documentation of
839 * ::mbedtls_x509_crt_ca_cb_t for more information.
840 * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
841 * \param profile The security profile for the verification.
842 * \param cn The expected Common Name. This may be \c NULL if the
843 * CN need not be verified.
844 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100845 * If the verification couldn't be completed, the flag value is
846 * set to (uint32_t) -1.
Hanno Becker5c8df782019-03-27 11:01:17 +0000847 * \param f_vrfy The verification callback to use. See the documentation
848 * of mbedtls_x509_crt_verify() for more information.
849 * \param p_vrfy The context to be passed to \p f_vrfy.
850 *
851 * \return See \c mbedtls_crt_verify_with_profile().
852 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300853int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker5c8df782019-03-27 11:01:17 +0000854 mbedtls_x509_crt_ca_cb_t f_ca_cb,
855 void *p_ca_cb,
856 const mbedtls_x509_crt_profile *profile,
857 const char *cn, uint32_t *flags,
858 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
859 void *p_vrfy );
860
861#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
862
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200863/**
864 * \brief Check usage of certificate against keyUsage extension.
865 *
866 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200867 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
868 * before using the certificate to perform an RSA key
869 * exchange).
870 *
871 * \note Except for decipherOnly and encipherOnly, a bit set in the
872 * usage argument means this bit MUST be set in the
873 * certificate. For decipherOnly and encipherOnly, it means
874 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200875 *
876 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200878 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200879 *
880 * \note You should only call this function on leaf certificates, on
881 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200883 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200884int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
885 unsigned int usage );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200886
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200887/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000888 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200889 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000890 * \param crt Leaf certificate used.
891 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
892 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200894 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000895 * \return 0 if this use of the certificate is allowed,
896 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200897 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000898 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200899 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000901 const char *usage_oid,
902 size_t usage_len );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200906 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907 *
908 * \param crt a certificate to be verified
909 * \param crl the CRL to verify against
910 *
911 * \return 1 if the certificate is revoked, 0 otherwise
912 *
913 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100914int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916
917/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200918 * \brief Initialize a certificate (chain)
919 *
920 * \param crt Certificate chain to initialize
921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200923
924/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925 * \brief Unallocate all certificate data
926 *
927 * \param crt Certificate chain to free
928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200930
931#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
932/**
933 * \brief Initialize a restart context
934 */
935void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
936
937/**
938 * \brief Free the components of a restart context
939 */
940void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
941#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943
944/* \} name */
945/* \} addtogroup x509_module */
946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948/**
949 * \brief Initialize a CRT writing context
950 *
951 * \param ctx CRT context to initialize
952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
955/**
956 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958 *
959 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
961 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964
965/**
966 * \brief Set the serial number for a Certificate.
967 *
968 * \param ctx CRT context to use
969 * \param serial serial number to set
970 *
971 * \return 0 if successful
972 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974
975/**
976 * \brief Set the validity period for a Certificate
977 * Timestamps should be in string format for UTC timezone
978 * i.e. "YYYYMMDDhhmmss"
979 * e.g. "20131231235959" for December 31st 2013
980 * at 23:59:59
981 *
982 * \param ctx CRT context to use
983 * \param not_before not_before timestamp
984 * \param not_after not_after timestamp
985 *
986 * \return 0 if timestamp was parsed successfully, or
987 * a specific error code
988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100990 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991
992/**
993 * \brief Set the issuer name for a Certificate
994 * Issuer names should contain a comma-separated list
995 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000996 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 *
998 * \param ctx CRT context to use
999 * \param issuer_name issuer name to set
1000 *
1001 * \return 0 if issuer name was parsed successfully, or
1002 * a specific error code
1003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001005 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006
1007/**
1008 * \brief Set the subject name for a Certificate
1009 * Subject names should contain a comma-separated list
1010 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001011 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012 *
1013 * \param ctx CRT context to use
1014 * \param subject_name subject name to set
1015 *
1016 * \return 0 if subject name was parsed successfully, or
1017 * a specific error code
1018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001020 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021
1022/**
1023 * \brief Set the subject public key for the certificate
1024 *
1025 * \param ctx CRT context to use
1026 * \param key public key to include
1027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029
1030/**
1031 * \brief Set the issuer key used for signing the certificate
1032 *
1033 * \param ctx CRT context to use
1034 * \param key private key to sign with
1035 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037
1038/**
1039 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041 *
1042 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001043 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046
1047/**
1048 * \brief Generic function to add to or replace an extension in the
1049 * CRT
1050 *
1051 * \param ctx CRT context to use
1052 * \param oid OID of the extension
1053 * \param oid_len length of the OID
1054 * \param critical if the extension is critical (per the RFC's definition)
1055 * \param val value of the extension OCTET STRING
1056 * \param val_len length of the value data
1057 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001058 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001061 const char *oid, size_t oid_len,
1062 int critical,
1063 const unsigned char *val, size_t val_len );
1064
1065/**
1066 * \brief Set the basicConstraints extension for a CRT
1067 *
1068 * \param ctx CRT context to use
1069 * \param is_ca is this a CA certificate
1070 * \param max_pathlen maximum length of certificate chains below this
1071 * certificate (only for CA certificates, -1 is
1072 * inlimited)
1073 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001074 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077 int is_ca, int max_pathlen );
1078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080/**
1081 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083 * called before
1084 *
1085 * \param ctx CRT context to use
1086 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001087 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090
1091/**
1092 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094 * called before
1095 *
1096 * \param ctx CRT context to use
1097 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001098 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
1101#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102
1103/**
1104 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106 *
1107 * \param ctx CRT context to use
1108 * \param key_usage key usage flags to set
1109 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001110 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +02001112int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
1113 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114
1115/**
1116 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118 *
1119 * \param ctx CRT context to use
1120 * \param ns_cert_type Netscape Cert Type flags to set
1121 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001122 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001125 unsigned char ns_cert_type );
1126
1127/**
1128 * \brief Free the contents of a CRT write context
1129 *
1130 * \param ctx CRT context to free
1131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001133
1134/**
1135 * \brief Write a built up certificate to a X509 DER structure
1136 * Note: data is written at the end of the buffer! Use the
1137 * return value to determine where you should start
1138 * using the buffer
1139 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001140 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001141 * \param buf buffer to write to
1142 * \param size size of the buffer
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001143 * \param f_rng RNG function. This must not be \c NULL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001144 * \param p_rng RNG parameter
1145 *
1146 * \return length of data written if successful, or a specific
1147 * error code
1148 *
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001149 * \note \p f_rng is used for the signature operation.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152 int (*f_rng)(void *, unsigned char *, size_t),
1153 void *p_rng );
1154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156/**
1157 * \brief Write a built up certificate to a X509 PEM string
1158 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001159 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160 * \param buf buffer to write to
1161 * \param size size of the buffer
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001162 * \param f_rng RNG function. This must not be \c NULL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001163 * \param p_rng RNG parameter
1164 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001165 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166 *
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001167 * \note \p f_rng is used for the signature operation.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170 int (*f_rng)(void *, unsigned char *, size_t),
1171 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#endif /* MBEDTLS_PEM_WRITE_C */
1173#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174
1175#ifdef __cplusplus
1176}
1177#endif
1178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179#endif /* mbedtls_x509_crt.h */