blob: 25d4a7ba3c71fad0ec4712b189ebfe704175bd71 [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"
Manuel Pégourié-Gonnarde8967052022-09-15 11:41:16 +020027#include "mbedtls/legacy_or_psa.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028
Jaeden Amero6609aef2019-07-04 20:01:14 +010029#include "mbedtls/x509.h"
30#include "mbedtls/x509_crl.h"
31#include "mbedtls/bignum.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032
33/**
34 * \addtogroup x509_module
35 * \{
36 */
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/**
43 * \name Structures and functions for parsing and writing X.509 certificates
44 * \{
45 */
46
47/**
48 * Container for an X.509 certificate. The certificate may be chained.
Gilles Peskine842edf42021-08-04 21:56:10 +020049 *
50 * Some fields of this structure are publicly readable. Do not modify
51 * them except via Mbed TLS library functions: the effect of modifying
52 * those fields or the data that those fields points to is unspecified.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020056 int MBEDTLS_PRIVATE(own_buffer); /**< Indicates if \c raw is owned
Hanno Becker1a65dcd2019-01-31 08:57:44 +000057 * by the structure or not. */
Gilles Peskine842edf42021-08-04 21:56:10 +020058 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
59 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060
Gilles Peskine842edf42021-08-04 21:56:10 +020061 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
62 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
63 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064
Gilles Peskine842edf42021-08-04 21:56:10 +020065 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
66 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067
Gilles Peskine842edf42021-08-04 21:56:10 +020068 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
69 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070
Gilles Peskine842edf42021-08-04 21:56:10 +020071 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
72 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073
Gilles Peskine842edf42021-08-04 21:56:10 +020074 mbedtls_x509_buf pk_raw;
75 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076
Gilles Peskine842edf42021-08-04 21:56:10 +020077 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
78 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
79 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
80 mbedtls_x509_sequence 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 +020081
Gilles Peskine842edf42021-08-04 21:56:10 +020082 mbedtls_x509_sequence 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 +020083
Mateusz Starzyk846f0212021-05-19 19:44:07 +020084 int MBEDTLS_PRIVATE(ext_types); /**< Bit string containing detected and parsed extensions */
85 int MBEDTLS_PRIVATE(ca_istrue); /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
86 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 +020087
Mateusz Starzyk846f0212021-05-19 19:44:07 +020088 unsigned int MBEDTLS_PRIVATE(key_usage); /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089
Gilles Peskine842edf42021-08-04 21:56:10 +020090 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091
Mateusz Starzyk846f0212021-05-19 19:44:07 +020092 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 +020093
Mateusz Starzyk846f0212021-05-19 19:44:07 +020094 mbedtls_x509_buf MBEDTLS_PRIVATE(sig); /**< Signature: hash of the tbs part signed with the private key. */
95 mbedtls_md_type_t MBEDTLS_PRIVATE(sig_md); /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
96 mbedtls_pk_type_t MBEDTLS_PRIVATE(sig_pk); /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
97 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 +020098
Gilles Peskineca939952021-08-31 23:18:07 +020099 /** Next certificate in the linked list that constitutes the CA chain.
100 * \p NULL indicates the end of the list.
101 * Do not modify this field directly. */
102 struct mbedtls_x509_crt *next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105
Janos Follath5091bec2019-05-08 15:23:08 +0100106/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200107 * From RFC 5280 section 4.2.1.6:
108 * OtherName ::= SEQUENCE {
109 * type-id OBJECT IDENTIFIER,
110 * value [0] EXPLICIT ANY DEFINED BY type-id }
Gilles Peskine842edf42021-08-04 21:56:10 +0200111 *
112 * Future versions of the library may add new fields to this structure or
113 * to its embedded union and structure.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200114 */
115typedef struct mbedtls_x509_san_other_name
116{
Ron Eldor890819a2019-05-13 19:03:04 +0300117 /**
Tom Cosgrove1e211442022-05-26 11:51:00 +0100118 * The type_id is an OID as defined in RFC 5280.
Ron Eldor890819a2019-05-13 19:03:04 +0300119 * To check the value of the type id, you should use
120 * \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf.
121 */
Gilles Peskine842edf42021-08-04 21:56:10 +0200122 mbedtls_x509_buf type_id; /**< The type id. */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200123 union
124 {
Janos Follath5091bec2019-05-08 15:23:08 +0100125 /**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200126 * From RFC 4108 section 5:
127 * HardwareModuleName ::= SEQUENCE {
128 * hwType OBJECT IDENTIFIER,
129 * hwSerialNum OCTET STRING }
130 */
Ron Eldorf05f5942019-05-13 19:11:31 +0300131 struct
132 {
Gilles Peskine842edf42021-08-04 21:56:10 +0200133 mbedtls_x509_buf oid; /**< The object identifier. */
134 mbedtls_x509_buf val; /**< The named value. */
Ron Eldorf05f5942019-05-13 19:11:31 +0300135 }
Gilles Peskine842edf42021-08-04 21:56:10 +0200136 hardware_module_name;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200137 }
Gilles Peskine842edf42021-08-04 21:56:10 +0200138 value;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200139}
140mbedtls_x509_san_other_name;
141
Janos Follath5091bec2019-05-08 15:23:08 +0100142/**
Gilles Peskine842edf42021-08-04 21:56:10 +0200143 * A structure for holding the parsed Subject Alternative Name,
144 * according to type.
145 *
146 * Future versions of the library may add new fields to this structure or
147 * to its embedded union and structure.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200148 */
149typedef struct mbedtls_x509_subject_alternative_name
150{
Gilles Peskine842edf42021-08-04 21:56:10 +0200151 int type; /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200152 union {
Gilles Peskine842edf42021-08-04 21:56:10 +0200153 mbedtls_x509_san_other_name other_name; /**< The otherName supported type. */
154 mbedtls_x509_buf unstructured_name; /**< The buffer for the un constructed types. Only dnsName currently supported */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200155 }
Gilles Peskine842edf42021-08-04 21:56:10 +0200156 san; /**< A union of the supported SAN types */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200157}
158mbedtls_x509_subject_alternative_name;
159
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200160/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200161 * Build flag from an algorithm/curve identifier (pk, md, ecp)
162 * Since 0 is always XXX_NONE, ignore it.
163 */
Hanno Becker1eeca412018-10-15 12:01:35 +0100164#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200165
166/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200167 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200168 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200169 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard55a7fb82021-06-17 10:39:39 +0200170 *
171 * The fields of this structure are part of the public API and can be
172 * manipulated directly by applications. Future versions of the library may
173 * add extra fields or reorder existing fields.
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200174 *
175 * You can create custom profiles by starting from a copy of
176 * an existing profile, such as mbedtls_x509_crt_profile_default or
177 * mbedtls_x509_ctr_profile_none and then tune it to your needs.
178 *
179 * For example to allow SHA-224 in addition to the default:
180 *
181 * mbedtls_x509_crt_profile my_profile = mbedtls_x509_crt_profile_default;
182 * my_profile.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 );
183 *
184 * Or to allow only RSA-3072+ with SHA-256:
185 *
186 * mbedtls_x509_crt_profile my_profile = mbedtls_x509_crt_profile_none;
187 * my_profile.allowed_mds = MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 );
188 * my_profile.allowed_pks = MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA );
189 * my_profile.rsa_min_bitlen = 3072;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200190 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200191typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200192{
Manuel Pégourié-Gonnard5314e082021-06-17 10:32:01 +0200193 uint32_t allowed_mds; /**< MDs for signatures */
Hanno Becker2b9fb882018-10-11 11:36:29 +0100194 uint32_t allowed_pks; /**< PK algs for public keys;
Manuel Pégourié-Gonnard8ba99e72022-04-25 10:12:01 +0200195 * this applies to all certificates
Hanno Becker2b9fb882018-10-11 11:36:29 +0100196 * in the provided chain. */
Manuel Pégourié-Gonnard5314e082021-06-17 10:32:01 +0200197 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
198 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200199}
200mbedtls_x509_crt_profile;
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#define MBEDTLS_X509_CRT_VERSION_1 0
203#define MBEDTLS_X509_CRT_VERSION_2 1
204#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200205
Valerio Settie888c172023-01-05 16:46:59 +0100206#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 20
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200208
Andres AGf9113192016-09-02 14:06:04 +0100209#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
210#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
211#endif
212
Hanno Becker7ac83f92020-10-09 10:48:22 +0100213/* This macro unfolds to the concatenation of macro invocations
214 * X509_CRT_ERROR_INFO( error code,
215 * error code as string,
216 * human readable description )
217 * where X509_CRT_ERROR_INFO is defined by the user.
218 * See x509_crt.c for an example of how to use this. */
219#define MBEDTLS_X509_CRT_ERROR_INFO_LIST \
220 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_EXPIRED, \
221 "MBEDTLS_X509_BADCERT_EXPIRED", \
222 "The certificate validity has expired" ) \
223 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_REVOKED, \
224 "MBEDTLS_X509_BADCERT_REVOKED", \
225 "The certificate has been revoked (is on a CRL)" ) \
226 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_CN_MISMATCH, \
227 "MBEDTLS_X509_BADCERT_CN_MISMATCH", \
228 "The certificate Common Name (CN) does not match with the expected CN" ) \
229 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_NOT_TRUSTED, \
230 "MBEDTLS_X509_BADCERT_NOT_TRUSTED", \
231 "The certificate is not correctly signed by the trusted CA" ) \
232 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_NOT_TRUSTED, \
233 "MBEDTLS_X509_BADCRL_NOT_TRUSTED", \
234 "The CRL is not correctly signed by the trusted CA" ) \
235 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_EXPIRED, \
236 "MBEDTLS_X509_BADCRL_EXPIRED", \
237 "The CRL is expired" ) \
238 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_MISSING, \
239 "MBEDTLS_X509_BADCERT_MISSING", \
240 "Certificate was missing" ) \
241 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_SKIP_VERIFY, \
242 "MBEDTLS_X509_BADCERT_SKIP_VERIFY", \
243 "Certificate verification was skipped" ) \
244 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_OTHER, \
245 "MBEDTLS_X509_BADCERT_OTHER", \
246 "Other reason (can be used by verify callback)" ) \
247 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_FUTURE, \
248 "MBEDTLS_X509_BADCERT_FUTURE", \
249 "The certificate validity starts in the future" ) \
250 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_FUTURE, \
251 "MBEDTLS_X509_BADCRL_FUTURE", \
252 "The CRL is from the future" ) \
253 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_KEY_USAGE, \
254 "MBEDTLS_X509_BADCERT_KEY_USAGE", \
255 "Usage does not match the keyUsage extension" ) \
256 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, \
257 "MBEDTLS_X509_BADCERT_EXT_KEY_USAGE", \
258 "Usage does not match the extendedKeyUsage extension" ) \
259 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_NS_CERT_TYPE, \
260 "MBEDTLS_X509_BADCERT_NS_CERT_TYPE", \
261 "Usage does not match the nsCertType extension" ) \
262 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_BAD_MD, \
263 "MBEDTLS_X509_BADCERT_BAD_MD", \
264 "The certificate is signed with an unacceptable hash." ) \
265 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_BAD_PK, \
266 "MBEDTLS_X509_BADCERT_BAD_PK", \
267 "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." ) \
268 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_BAD_KEY, \
269 "MBEDTLS_X509_BADCERT_BAD_KEY", \
270 "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." ) \
271 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_BAD_MD, \
272 "MBEDTLS_X509_BADCRL_BAD_MD", \
273 "The CRL is signed with an unacceptable hash." ) \
274 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_BAD_PK, \
275 "MBEDTLS_X509_BADCRL_BAD_PK", \
276 "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." ) \
277 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_BAD_KEY, \
278 "MBEDTLS_X509_BADCRL_BAD_KEY", \
279 "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." )
280
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281/**
282 * Container for writing a certificate (CRT)
283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200286 int MBEDTLS_PRIVATE(version);
Valerio Settie888c172023-01-05 16:46:59 +0100287 unsigned char MBEDTLS_PRIVATE(serial)[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN];
288 size_t MBEDTLS_PRIVATE(serial_len);
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200289 mbedtls_pk_context *MBEDTLS_PRIVATE(subject_key);
290 mbedtls_pk_context *MBEDTLS_PRIVATE(issuer_key);
291 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(subject);
292 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(issuer);
293 mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg);
294 char MBEDTLS_PRIVATE(not_before)[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
295 char MBEDTLS_PRIVATE(not_after)[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
296 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(extensions);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200299
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200300/**
301 * Item in a verification chain: cert and flags for it
302 */
303typedef struct {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200304 mbedtls_x509_crt *MBEDTLS_PRIVATE(crt);
305 uint32_t MBEDTLS_PRIVATE(flags);
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200306} mbedtls_x509_crt_verify_chain_item;
307
308/**
309 * Max size of verification chain: end-entity + intermediates + trusted root
310 */
311#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
312
313/**
314 * Verification chain as built by \c mbedtls_crt_verify_chain()
315 */
316typedef struct
317{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200318 mbedtls_x509_crt_verify_chain_item MBEDTLS_PRIVATE(items)[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
319 unsigned MBEDTLS_PRIVATE(len);
Hanno Beckerf53893b2019-03-28 13:45:55 +0000320
321#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
322 /* This stores the list of potential trusted signers obtained from
323 * the CA callback used for the CRT verification, if configured.
324 * We must track it somewhere because the callback passes its
325 * ownership to the caller. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200326 mbedtls_x509_crt *MBEDTLS_PRIVATE(trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +0000327#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200328} mbedtls_x509_crt_verify_chain;
329
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200330#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
331
332/**
333 * \brief Context for resuming X.509 verify operations
334 */
335typedef struct
336{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200337 /* for check_signature() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200338 mbedtls_pk_restart_ctx MBEDTLS_PRIVATE(pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200339
340 /* for find_parent_in() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200341 mbedtls_x509_crt *MBEDTLS_PRIVATE(parent); /* non-null iff parent_in in progress */
342 mbedtls_x509_crt *MBEDTLS_PRIVATE(fallback_parent);
343 int MBEDTLS_PRIVATE(fallback_signature_is_good);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200344
345 /* for find_parent() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200346 int MBEDTLS_PRIVATE(parent_is_trusted); /* -1 if find_parent is not in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200347
348 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200349 enum {
350 x509_crt_rs_none,
351 x509_crt_rs_find_parent,
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200352 } MBEDTLS_PRIVATE(in_progress); /* none if no operation is in progress */
353 int MBEDTLS_PRIVATE(self_cnt);
354 mbedtls_x509_crt_verify_chain MBEDTLS_PRIVATE(ver_chain);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200355
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200356} mbedtls_x509_crt_restart_ctx;
357
358#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
359
360/* Now we can declare functions that take a pointer to that */
361typedef void mbedtls_x509_crt_restart_ctx;
362
363#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200367 * Default security profile. Should provide a good balance between security
368 * and compatibility with current deployments.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200369 *
370 * This profile permits:
371 * - SHA2 hashes with at least 256 bits: SHA-256, SHA-384, SHA-512.
Gilles Peskine39957502021-06-17 23:17:52 +0200372 * - Elliptic curves with 255 bits and above except secp256k1.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200373 * - RSA with 2048 bits and above.
374 *
375 * New minor versions of Mbed TLS may extend this profile, for example if
Gilles Peskine39957502021-06-17 23:17:52 +0200376 * new algorithms are added to the library. New minor versions of Mbed TLS will
Gilles Peskineffb92da2021-06-02 00:03:26 +0200377 * not reduce this profile unless serious security concerns require it.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200378 */
379extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
380
381/**
382 * Expected next default profile. Recommended for new deployments.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200383 * Currently targets a 128-bit security level, except for allowing RSA-2048.
384 * This profile may change at any time.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200385 */
386extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
387
388/**
389 * NSA Suite B profile.
390 */
391extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
392
393/**
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200394 * Empty profile that allows nothing. Useful as a basis for constructing
395 * custom profiles.
396 */
397extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none;
398
399/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400 * \brief Parse a single DER formatted certificate and add it
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000401 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000403 * \param chain The pointer to the start of the CRT chain to attach to.
404 * When parsing the first CRT in a chain, this should point
405 * to an instance of ::mbedtls_x509_crt initialized through
406 * mbedtls_x509_crt_init().
407 * \param buf The buffer holding the DER encoded certificate.
408 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000410 * \note This function makes an internal copy of the CRT buffer
411 * \p buf. In particular, \p buf may be destroyed or reused
412 * after this call returns. To avoid duplicating the CRT
413 * buffer (at the cost of stricter lifetime constraints),
414 * use mbedtls_x509_crt_parse_der_nocopy() instead.
415 *
416 * \return \c 0 if successful.
417 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000419int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
420 const unsigned char *buf,
421 size_t buflen );
422
423/**
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200424 * \brief The type of certificate extension callbacks.
425 *
426 * Callbacks of this type are passed to and used by the
Nicola Di Lietofde98f72020-05-28 08:34:33 +0200427 * mbedtls_x509_crt_parse_der_with_ext_cb() routine when
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200428 * it encounters either an unsupported extension or a
429 * "certificate policies" extension containing any
430 * unsupported certificate policies.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200431 * Future versions of the library may invoke the callback
432 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200433 *
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200434 * \param p_ctx An opaque context passed to the callback.
ndilieto6e249802020-05-28 06:17:38 +0000435 * \param crt The certificate being parsed.
436 * \param oid The OID of the extension.
437 * \param critical Whether the extension is critical.
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200438 * \param p Pointer to the start of the extension value
ndilieto6e249802020-05-28 06:17:38 +0000439 * (the content of the OCTET STRING).
ndilieto6e249802020-05-28 06:17:38 +0000440 * \param end End of extension value.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200441 *
442 * \note The callback must fail and return a negative error code
443 * if it can not parse or does not support the extension.
444 * When the callback fails to parse a critical extension
445 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
446 * When the callback fails to parse a non critical extension
447 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
448 * the extension and continues parsing.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200449 *
450 * \return \c 0 on success.
451 * \return A negative error code on failure.
452 */
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200453typedef int (*mbedtls_x509_crt_ext_cb_t)( void *p_ctx,
454 mbedtls_x509_crt const *crt,
ndilieto6e249802020-05-28 06:17:38 +0000455 mbedtls_x509_buf const *oid,
456 int critical,
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200457 const unsigned char *p,
ndilieto6e249802020-05-28 06:17:38 +0000458 const unsigned char *end );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200459
460/**
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200461 * \brief Parse a single DER formatted certificate and add it
462 * to the end of the provided chained list.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200463 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200464 * \param chain The pointer to the start of the CRT chain to attach to.
465 * When parsing the first CRT in a chain, this should point
466 * to an instance of ::mbedtls_x509_crt initialized through
467 * mbedtls_x509_crt_init().
468 * \param buf The buffer holding the DER encoded certificate.
469 * \param buflen The size in Bytes of \p buf.
470 * \param make_copy When not zero this function makes an internal copy of the
471 * CRT buffer \p buf. In particular, \p buf may be destroyed
472 * or reused after this call returns.
473 * When zero this function avoids duplicating the CRT buffer
474 * by taking temporary ownership thereof until the CRT
475 * is destroyed (like mbedtls_x509_crt_parse_der_nocopy())
476 * \param cb A callback invoked for every unsupported certificate
477 * extension.
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200478 * \param p_ctx An opaque context passed to the callback.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200479 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200480 * \note This call is functionally equivalent to
481 * mbedtls_x509_crt_parse_der(), and/or
482 * mbedtls_x509_crt_parse_der_nocopy()
483 * but it calls the callback with every unsupported
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200484 * certificate extension and additionally the
485 * "certificate policies" extension if it contains any
486 * unsupported certificate policies.
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200487 * The callback must return a negative error code if it
488 * does not know how to handle such an extension.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200489 * When the callback fails to parse a critical extension
490 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
491 * When the callback fails to parse a non critical extension
492 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
493 * the extension and continues parsing.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200494 * Future versions of the library may invoke the callback
495 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200496 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200497 * \return \c 0 if successful.
498 * \return A negative error code on failure.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200499 */
Nicola Di Lietofde98f72020-05-28 08:34:33 +0200500int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
501 const unsigned char *buf,
502 size_t buflen,
Nicola Di Lieto5f6ebde2020-05-28 19:00:47 +0200503 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200504 mbedtls_x509_crt_ext_cb_t cb,
505 void *p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200506
507/**
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000508 * \brief Parse a single DER formatted certificate and add it
509 * to the end of the provided chained list. This is a
510 * variant of mbedtls_x509_crt_parse_der() which takes
511 * temporary ownership of the CRT buffer until the CRT
512 * is destroyed.
513 *
514 * \param chain The pointer to the start of the CRT chain to attach to.
515 * When parsing the first CRT in a chain, this should point
516 * to an instance of ::mbedtls_x509_crt initialized through
517 * mbedtls_x509_crt_init().
518 * \param buf The address of the readable buffer holding the DER encoded
519 * certificate to use. On success, this buffer must be
Jan Brucknerb33f6e52022-10-06 11:23:49 +0200520 * retained and not be changed for the lifetime of the
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000521 * CRT chain \p chain, that is, until \p chain is destroyed
522 * through a call to mbedtls_x509_crt_free().
523 * \param buflen The size in Bytes of \p buf.
524 *
525 * \note This call is functionally equivalent to
526 * mbedtls_x509_crt_parse_der(), but it avoids creating a
527 * copy of the input buffer at the cost of stronger lifetime
528 * constraints. This is useful in constrained environments
529 * where duplication of the CRT cannot be tolerated.
530 *
531 * \return \c 0 if successful.
532 * \return A negative error code on failure.
533 */
534int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
535 const unsigned char *buf,
536 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
538/**
Hanno Becker89a91122018-08-23 16:14:00 +0100539 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100540 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100542 * For CRTs in PEM encoding, the function parses permissively:
543 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100544 * returns the number of certificates for which parsing failed
545 * (hence \c 0 if all certificates were parsed successfully).
546 * If no certificate could be parsed, the function returns
547 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100549 * PEM encoded certificates may be interleaved by other data
550 * such as human readable descriptions of their content, as
551 * long as the certificates are enclosed in the PEM specific
552 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
553 *
554 * \param chain The chain to which to add the parsed certificates.
555 * \param buf The buffer holding the certificate data in PEM or DER format.
556 * For certificates in PEM encoding, this may be a concatenation
557 * of multiple certificates; for DER encoding, the buffer must
558 * comprise exactly one certificate.
559 * \param buflen The size of \p buf, including the terminating \c NULL byte
560 * in case of PEM encoded data.
561 *
562 * \return \c 0 if all certificates were parsed successfully.
563 * \return The (positive) number of certificates that couldn't
564 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100565 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100566 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571/**
572 * \brief Load one or more certificates and add them
573 * to the chained list. Parses permissively. If some
574 * certificates can be parsed, the result is the number
575 * of failed certificates it encountered. If none complete
576 * correctly, the first error is returned.
577 *
578 * \param chain points to the start of the chain
579 * \param path filename to read the certificates from
580 *
581 * \return 0 if all certificates parsed successfully, a positive number
582 * if partly successful or a specific X509 or PEM error code
583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585
586/**
587 * \brief Load one or more certificate files from a path and add them
588 * to the chained list. Parses permissively. If some
589 * certificates can be parsed, the result is the number
590 * of failed certificates it encountered. If none complete
591 * correctly, the first error is returned.
592 *
593 * \param chain points to the start of the chain
594 * \param path directory / folder to read the certificate files from
595 *
596 * \return 0 if all certificates parsed successfully, a positive number
597 * if partly successful or a specific X509 or PEM error code
598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
Janos Follath11b41eb2019-05-08 15:26:49 +0100600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#endif /* MBEDTLS_FS_IO */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200602/**
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300603 * \brief This function parses an item in the SubjectAlternativeNames
604 * extension.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200605 *
Ron Eldor890819a2019-05-13 19:03:04 +0300606 * \param san_buf The buffer holding the raw data item of the subject
607 * alternative name.
608 * \param san The target structure to populate with the parsed presentation
609 * of the subject alternative name encoded in \p san_raw.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200610 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300611 * \note Only "dnsName" and "otherName" of type hardware_module_name
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200612 * as defined in RFC 4180 is supported.
613 *
Ron Eldor890819a2019-05-13 19:03:04 +0300614 * \note This function should be called on a single raw data of
Ron Eldorcc45cd12019-05-15 10:20:09 +0300615 * subject alternative name. For example, after successful
Ron Eldor890819a2019-05-13 19:03:04 +0300616 * certificate parsing, one must iterate on every item in the
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300617 * \p crt->subject_alt_names sequence, and pass it to
618 * this function.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200619 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300620 * \warning The target structure contains pointers to the raw data of the
Ron Eldorcc45cd12019-05-15 10:20:09 +0300621 * parsed certificate, and its lifetime is restricted by the
622 * lifetime of the certificate.
623 *
Ron Eldor890819a2019-05-13 19:03:04 +0300624 * \return \c 0 on success
625 * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300626 * SAN type.
627 * \return Another negative value for any other failure.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200628 */
Ron Eldor890819a2019-05-13 19:03:04 +0300629int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
630 mbedtls_x509_subject_alternative_name *san );
Peter Kolbus9a969b62018-12-11 13:55:56 -0600631
Hanno Becker612a2f12020-10-09 09:19:39 +0100632#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633/**
634 * \brief Returns an informational string about the
635 * certificate.
636 *
637 * \param buf Buffer to write to
638 * \param size Maximum size of buffer
639 * \param prefix A line prefix
640 * \param crt The X509 certificate to represent
641 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200642 * \return The length of the string written (not including the
643 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
646 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647
648/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100649 * \brief Returns an informational string about the
650 * verification status of a certificate.
651 *
652 * \param buf Buffer to write to
653 * \param size Maximum size of buffer
654 * \param prefix A line prefix
655 * \param flags Verification flags created by mbedtls_x509_crt_verify()
656 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200657 * \return The length of the string written (not including the
658 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100659 */
660int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200661 uint32_t flags );
Hanno Becker88c2bf32019-06-14 17:21:24 +0100662#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200663
664/**
Hanno Becker902451d2019-03-27 11:48:40 +0000665 * \brief Verify a chain of certificates.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666 *
667 * The verify callback is a user-supplied callback that
668 * can clear / modify / add flags for a certificate. If set,
669 * the verification callback is called for each
670 * certificate in the chain (from the trust-ca down to the
671 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673 * int *flags). With the flags representing current flags for
674 * that specific certificate and the certificate depth from
675 * the bottom (Peer cert depth = 0).
676 *
677 * All flags left after returning from the callback
678 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200679 * return 0 for anything (including invalid certificates)
680 * other than fatal error, as a non-zero return code
681 * immediately aborts the verification process. For fatal
682 * errors, a specific error code should be used (different
683 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
684 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
685 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100687 * \note In case verification failed, the results can be displayed
688 * using \c mbedtls_x509_crt_verify_info()
689 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200690 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
691 * default security profile.
692 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100693 * \note It is your responsibility to provide up-to-date CRLs for
694 * all trusted CAs. If no CRL is provided for the CA that was
695 * used to sign the certificate, CRL verification is skipped
696 * silently, that is *without* setting any flag.
697 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200698 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200699 * (1) those of trusted root CAs, so that certificates
700 * chaining up to those CAs will be trusted, and (2)
701 * self-signed end-entity certificates to be trusted (for
702 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200703 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200704 *
Hanno Becker902451d2019-03-27 11:48:40 +0000705 * \param crt The certificate chain to be verified.
706 * \param trust_ca The list of trusted CAs.
707 * \param ca_crl The list of CRLs for trusted CAs.
Manuel Pégourié-Gonnardf58e5cc2020-07-24 10:31:37 +0200708 * \param cn The expected Common Name. This will be checked to be
709 * present in the certificate's subjectAltNames extension or,
710 * if this extension is absent, as a CN component in its
711 * Subject name. Currently only DNS names are supported. This
712 * may be \c NULL if the CN need not be verified.
Hanno Becker902451d2019-03-27 11:48:40 +0000713 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100714 * If the verification couldn't be completed, the flag value is
715 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000716 * \param f_vrfy The verification callback to use. See the documentation
717 * of mbedtls_x509_crt_verify() for more information.
718 * \param p_vrfy The context to be passed to \p f_vrfy.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719 *
Hanno Becker902451d2019-03-27 11:48:40 +0000720 * \return \c 0 if the chain is valid with respect to the
721 * passed CN, CAs, CRLs and security profile.
722 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
723 * certificate chain verification failed. In this case,
724 * \c *flags will have one or more
725 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
726 * flags set.
727 * \return Another negative error code in case of a fatal error
728 * encountered during the verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
731 mbedtls_x509_crt *trust_ca,
732 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200733 const char *cn, uint32_t *flags,
734 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200735 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200737/**
Hanno Becker902451d2019-03-27 11:48:40 +0000738 * \brief Verify a chain of certificates with respect to
739 * a configurable security profile.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200740 *
741 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
742 * security profile.
743 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200744 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200745 * for ECDSA) apply to all certificates: trusted root,
746 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200747 *
Hanno Becker902451d2019-03-27 11:48:40 +0000748 * \param crt The certificate chain to be verified.
749 * \param trust_ca The list of trusted CAs.
750 * \param ca_crl The list of CRLs for trusted CAs.
751 * \param profile The security profile to use for the verification.
752 * \param cn The expected Common Name. This may be \c NULL if the
753 * CN need not be verified.
754 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100755 * If the verification couldn't be completed, the flag value is
756 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000757 * \param f_vrfy The verification callback to use. See the documentation
758 * of mbedtls_x509_crt_verify() for more information.
759 * \param p_vrfy The context to be passed to \p f_vrfy.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200760 *
Hanno Becker902451d2019-03-27 11:48:40 +0000761 * \return \c 0 if the chain is valid with respect to the
762 * passed CN, CAs, CRLs and security profile.
763 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
764 * certificate chain verification failed. In this case,
765 * \c *flags will have one or more
766 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
767 * flags set.
768 * \return Another negative error code in case of a fatal error
769 * encountered during the verification process.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200770 */
771int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
772 mbedtls_x509_crt *trust_ca,
773 mbedtls_x509_crl *ca_crl,
774 const mbedtls_x509_crt_profile *profile,
775 const char *cn, uint32_t *flags,
776 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
777 void *p_vrfy );
778
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200779/**
780 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
781 *
782 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
783 * but can return early and restart according to the limit
784 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
785 *
Hanno Becker902451d2019-03-27 11:48:40 +0000786 * \param crt The certificate chain to be verified.
787 * \param trust_ca The list of trusted CAs.
788 * \param ca_crl The list of CRLs for trusted CAs.
789 * \param profile The security profile to use for the verification.
790 * \param cn The expected Common Name. This may be \c NULL if the
791 * CN need not be verified.
792 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100793 * If the verification couldn't be completed, the flag value is
794 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000795 * \param f_vrfy The verification callback to use. See the documentation
796 * of mbedtls_x509_crt_verify() for more information.
797 * \param p_vrfy The context to be passed to \p f_vrfy.
798 * \param rs_ctx The restart context to use. This may be set to \c NULL
799 * to disable restartable ECC.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200800 *
801 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200802 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200803 * operations was reached: see \c mbedtls_ecp_set_max_ops().
804 */
805int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
806 mbedtls_x509_crt *trust_ca,
807 mbedtls_x509_crl *ca_crl,
808 const mbedtls_x509_crt_profile *profile,
809 const char *cn, uint32_t *flags,
810 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
811 void *p_vrfy,
812 mbedtls_x509_crt_restart_ctx *rs_ctx );
813
Hanno Becker5c8df782019-03-27 11:01:17 +0000814/**
815 * \brief The type of trusted certificate callbacks.
816 *
817 * Callbacks of this type are passed to and used by the CRT
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300818 * verification routine mbedtls_x509_crt_verify_with_ca_cb()
Hanno Becker5c8df782019-03-27 11:01:17 +0000819 * when looking for trusted signers of a given certificate.
820 *
821 * On success, the callback returns a list of trusted
822 * certificates to be considered as potential signers
823 * for the input certificate.
824 *
825 * \param p_ctx An opaque context passed to the callback.
826 * \param child The certificate for which to search a potential signer.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300827 * This will point to a readable certificate.
Hanno Becker5c8df782019-03-27 11:01:17 +0000828 * \param candidate_cas The address at which to store the address of the first
829 * entry in the generated linked list of candidate signers.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300830 * This will not be \c NULL.
Hanno Becker5c8df782019-03-27 11:01:17 +0000831 *
832 * \note The callback must only return a non-zero value on a
833 * fatal error. If, in contrast, the search for a potential
834 * signer completes without a single candidate, the
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300835 * callback must return \c 0 and set \c *candidate_cas
Hanno Becker5c8df782019-03-27 11:01:17 +0000836 * to \c NULL.
837 *
838 * \return \c 0 on success. In this case, \c *candidate_cas points
839 * to a heap-allocated linked list of instances of
840 * ::mbedtls_x509_crt, and ownership of this list is passed
841 * to the caller.
842 * \return A negative error code on failure.
843 */
844typedef int (*mbedtls_x509_crt_ca_cb_t)( void *p_ctx,
845 mbedtls_x509_crt const *child,
846 mbedtls_x509_crt **candidate_cas );
Hanno Beckere15dae72019-03-28 13:55:38 +0000847
848#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker5c8df782019-03-27 11:01:17 +0000849/**
850 * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
851 * uses a callback to acquire the list of trusted CA
852 * certificates.
853 *
854 * \param crt The certificate chain to be verified.
855 * \param f_ca_cb The callback to be used to query for potential signers
856 * of a given child certificate. See the documentation of
857 * ::mbedtls_x509_crt_ca_cb_t for more information.
858 * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
859 * \param profile The security profile for the verification.
860 * \param cn The expected Common Name. This may be \c NULL if the
861 * CN need not be verified.
862 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100863 * If the verification couldn't be completed, the flag value is
864 * set to (uint32_t) -1.
Hanno Becker5c8df782019-03-27 11:01:17 +0000865 * \param f_vrfy The verification callback to use. See the documentation
866 * of mbedtls_x509_crt_verify() for more information.
867 * \param p_vrfy The context to be passed to \p f_vrfy.
868 *
869 * \return See \c mbedtls_crt_verify_with_profile().
870 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300871int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker5c8df782019-03-27 11:01:17 +0000872 mbedtls_x509_crt_ca_cb_t f_ca_cb,
873 void *p_ca_cb,
874 const mbedtls_x509_crt_profile *profile,
875 const char *cn, uint32_t *flags,
876 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
877 void *p_vrfy );
878
879#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
880
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200881/**
882 * \brief Check usage of certificate against keyUsage extension.
883 *
884 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200885 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
886 * before using the certificate to perform an RSA key
887 * exchange).
888 *
889 * \note Except for decipherOnly and encipherOnly, a bit set in the
890 * usage argument means this bit MUST be set in the
891 * certificate. For decipherOnly and encipherOnly, it means
892 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200893 *
894 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200896 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200897 *
898 * \note You should only call this function on leaf certificates, on
899 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200901 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200902int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
903 unsigned int usage );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200904
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200905/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000906 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200907 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000908 * \param crt Leaf certificate used.
909 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
910 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200912 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000913 * \return 0 if this use of the certificate is allowed,
914 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200915 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000916 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200917 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000919 const char *usage_oid,
920 size_t usage_len );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200924 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925 *
926 * \param crt a certificate to be verified
927 * \param crl the CRL to verify against
928 *
929 * \return 1 if the certificate is revoked, 0 otherwise
930 *
931 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100932int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934
935/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200936 * \brief Initialize a certificate (chain)
937 *
938 * \param crt Certificate chain to initialize
939 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200941
942/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943 * \brief Unallocate all certificate data
944 *
945 * \param crt Certificate chain to free
946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200948
949#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
950/**
951 * \brief Initialize a restart context
952 */
953void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
954
955/**
956 * \brief Free the components of a restart context
957 */
958void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
959#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961
Thomas Daubneya5f39e02022-06-06 15:42:32 +0100962/**
963 * \brief Query certificate for given extension type
964 *
965 * \param[in] ctx Certificate context to be queried, must not be \c NULL
966 * \param ext_type Extension type being queried for, must be a valid
967 * extension type. Must be one of the MBEDTLS_X509_EXT_XXX
968 * values
969 *
970 * \return 0 if the given extension type is not present,
971 * non-zero otherwise
972 */
973static inline int mbedtls_x509_crt_has_ext_type( const mbedtls_x509_crt *ctx,
974 int ext_type )
975{
976 return ctx->MBEDTLS_PRIVATE(ext_types) & ext_type;
977}
978
Andrzej Kurek38d4fdd2021-12-28 16:22:52 +0100979/** \} name Structures and functions for parsing and writing X.509 certificates */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982/**
983 * \brief Initialize a CRT writing context
984 *
985 * \param ctx CRT context to initialize
986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988
989/**
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800990 * \brief Set the version for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 *
993 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
995 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998
Valerio Setti0e88e812023-01-09 12:19:40 +0100999#if defined(MBEDTLS_BIGNUM_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000/**
1001 * \brief Set the serial number for a Certificate.
1002 *
Valerio Settie888c172023-01-05 16:46:59 +01001003 * \deprecated This function is deprecated and will be removed in a
1004 * future version of the library. Please use
1005 * mbedtls_x509write_crt_set_serial_new() instead.
1006 *
1007 * \note Even though the MBEDTLS_BIGNUM_C guard looks redundant since
1008 * X509 depends on PK and PK depends on BIGNUM, this emphasizes
1009 * a direct dependency between X509 and BIGNUM which is going
1010 * to be deprecated in the future.
1011 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012 * \param ctx CRT context to use
1013 * \param serial serial number to set
1014 *
1015 * \return 0 if successful
1016 */
Valerio Setti0e88e812023-01-09 12:19:40 +01001017int MBEDTLS_DEPRECATED mbedtls_x509write_crt_set_serial(
1018 mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
1019#endif // MBEDTLS_BIGNUM_C && !MBEDTLS_DEPRECATED_REMOVED
Valerio Settie888c172023-01-05 16:46:59 +01001020
1021/**
1022 * \brief Set the serial number for a Certificate.
1023 *
1024 * \param ctx CRT context to use
1025 * \param serial_buff Input buffer containing the serial number in big
1026 * endian format
1027 * \param serial_buff_len Length of the previous input buffer buffer
1028 *
1029 * \return 0 if successful, or
1030 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the provided input buffer:
1031 * - is too big (longer than MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN)
1032 * - contains invalid chars
1033 */
1034int mbedtls_x509write_crt_set_serial_new( mbedtls_x509write_cert *ctx,
1035 char* serial_buff, size_t serial_buff_len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036
1037/**
1038 * \brief Set the validity period for a Certificate
1039 * Timestamps should be in string format for UTC timezone
1040 * i.e. "YYYYMMDDhhmmss"
1041 * e.g. "20131231235959" for December 31st 2013
1042 * at 23:59:59
1043 *
1044 * \param ctx CRT context to use
1045 * \param not_before not_before timestamp
1046 * \param not_after not_after timestamp
1047 *
1048 * \return 0 if timestamp was parsed successfully, or
1049 * a specific error code
1050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +01001052 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053
1054/**
1055 * \brief Set the issuer name for a Certificate
1056 * Issuer names should contain a comma-separated list
1057 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001058 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059 *
1060 * \param ctx CRT context to use
1061 * \param issuer_name issuer name to set
1062 *
1063 * \return 0 if issuer name was parsed successfully, or
1064 * a specific error code
1065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001067 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068
1069/**
1070 * \brief Set the subject name for a Certificate
1071 * Subject names should contain a comma-separated list
1072 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001073 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074 *
1075 * \param ctx CRT context to use
1076 * \param subject_name subject name to set
1077 *
1078 * \return 0 if subject name was parsed successfully, or
1079 * a specific error code
1080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +01001082 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083
1084/**
1085 * \brief Set the subject public key for the certificate
1086 *
1087 * \param ctx CRT context to use
1088 * \param key public key to include
1089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091
1092/**
1093 * \brief Set the issuer key used for signing the certificate
1094 *
1095 * \param ctx CRT context to use
1096 * \param key private key to sign with
1097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099
1100/**
1101 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103 *
1104 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001105 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108
1109/**
1110 * \brief Generic function to add to or replace an extension in the
1111 * CRT
1112 *
1113 * \param ctx CRT context to use
1114 * \param oid OID of the extension
1115 * \param oid_len length of the OID
1116 * \param critical if the extension is critical (per the RFC's definition)
1117 * \param val value of the extension OCTET STRING
1118 * \param val_len length of the value data
1119 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001120 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123 const char *oid, size_t oid_len,
1124 int critical,
1125 const unsigned char *val, size_t val_len );
1126
1127/**
1128 * \brief Set the basicConstraints extension for a CRT
1129 *
1130 * \param ctx CRT context to use
1131 * \param is_ca is this a CA certificate
1132 * \param max_pathlen maximum length of certificate chains below this
1133 * certificate (only for CA certificates, -1 is
Tom Cosgrove1e211442022-05-26 11:51:00 +01001134 * unlimited)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001135 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001136 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139 int is_ca, int max_pathlen );
1140
Manuel Pégourié-Gonnarde8967052022-09-15 11:41:16 +02001141#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142/**
1143 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001145 * called before
1146 *
1147 * \param ctx CRT context to use
1148 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001149 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152
1153/**
1154 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 * called before
1157 *
1158 * \param ctx CRT context to use
1159 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001160 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
Manuel Pégourié-Gonnarde8967052022-09-15 11:41:16 +02001163#endif /* MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164
1165/**
1166 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168 *
1169 * \param ctx CRT context to use
1170 * \param key_usage key usage flags to set
1171 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001172 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +02001174int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
1175 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176
1177/**
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +01001178 * \brief Set the Extended Key Usage Extension
1179 * (e.g. MBEDTLS_OID_SERVER_AUTH)
1180 *
1181 * \param ctx CRT context to use
1182 * \param exts extended key usage extensions to set, a sequence of
1183 * MBEDTLS_ASN1_OID objects
1184 *
1185 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
1186 */
1187int mbedtls_x509write_crt_set_ext_key_usage( mbedtls_x509write_cert *ctx,
1188 const mbedtls_asn1_sequence *exts );
1189
1190/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001193 *
1194 * \param ctx CRT context to use
1195 * \param ns_cert_type Netscape Cert Type flags to set
1196 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001197 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001200 unsigned char ns_cert_type );
1201
1202/**
1203 * \brief Free the contents of a CRT write context
1204 *
1205 * \param ctx CRT context to free
1206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001208
1209/**
1210 * \brief Write a built up certificate to a X509 DER structure
1211 * Note: data is written at the end of the buffer! Use the
1212 * return value to determine where you should start
1213 * using the buffer
1214 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001215 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001216 * \param buf buffer to write to
1217 * \param size size of the buffer
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001218 * \param f_rng RNG function. This must not be \c NULL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219 * \param p_rng RNG parameter
1220 *
1221 * \return length of data written if successful, or a specific
1222 * error code
1223 *
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001224 * \note \p f_rng is used for the signature operation.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227 int (*f_rng)(void *, unsigned char *, size_t),
1228 void *p_rng );
1229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001231/**
1232 * \brief Write a built up certificate to a X509 PEM string
1233 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001234 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235 * \param buf buffer to write to
1236 * \param size size of the buffer
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001237 * \param f_rng RNG function. This must not be \c NULL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001238 * \param p_rng RNG parameter
1239 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001240 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001241 *
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001242 * \note \p f_rng is used for the signature operation.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245 int (*f_rng)(void *, unsigned char *, size_t),
1246 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247#endif /* MBEDTLS_PEM_WRITE_C */
1248#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001249
Andrzej Kureka0defed2021-12-30 12:33:31 +01001250/** \} addtogroup x509_module */
1251
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001252#ifdef __cplusplus
1253}
1254#endif
1255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#endif /* mbedtls_x509_crt.h */