blob: 661f8aa7b81f8d0239699f2b44a16f36fdf28adc [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 */
Gilles Peskine449bd832023-01-11 14:50:10 +010054typedef struct mbedtls_x509_crt {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020055 int MBEDTLS_PRIVATE(own_buffer); /**< Indicates if \c raw is owned
Gilles Peskine449bd832023-01-11 14:50:10 +010056 * by the structure or not. */
Gilles Peskine842edf42021-08-04 21:56:10 +020057 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
58 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059
Gilles Peskine842edf42021-08-04 21:56:10 +020060 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
61 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
62 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063
Gilles Peskine842edf42021-08-04 21:56:10 +020064 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
65 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Gilles Peskine842edf42021-08-04 21:56:10 +020067 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
68 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069
Gilles Peskine842edf42021-08-04 21:56:10 +020070 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
71 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072
Gilles Peskine842edf42021-08-04 21:56:10 +020073 mbedtls_x509_buf pk_raw;
74 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075
Gilles Peskine842edf42021-08-04 21:56:10 +020076 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
77 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
78 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
79 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 +020080
Gilles Peskine842edf42021-08-04 21:56:10 +020081 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 +020082
Mateusz Starzyk846f0212021-05-19 19:44:07 +020083 int MBEDTLS_PRIVATE(ext_types); /**< Bit string containing detected and parsed extensions */
84 int MBEDTLS_PRIVATE(ca_istrue); /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
85 int MBEDTLS_PRIVATE(max_pathlen); /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. Path length is 1 higher than RFC 5280 'meaning', so 1+ */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086
Mateusz Starzyk846f0212021-05-19 19:44:07 +020087 unsigned int MBEDTLS_PRIVATE(key_usage); /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088
Gilles Peskine842edf42021-08-04 21:56:10 +020089 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090
Mateusz Starzyk846f0212021-05-19 19:44:07 +020091 unsigned char MBEDTLS_PRIVATE(ns_cert_type); /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092
Mateusz Starzyk846f0212021-05-19 19:44:07 +020093 mbedtls_x509_buf MBEDTLS_PRIVATE(sig); /**< Signature: hash of the tbs part signed with the private key. */
94 mbedtls_md_type_t MBEDTLS_PRIVATE(sig_md); /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
95 mbedtls_pk_type_t MBEDTLS_PRIVATE(sig_pk); /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
96 void *MBEDTLS_PRIVATE(sig_opts); /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097
Gilles Peskineca939952021-08-31 23:18:07 +020098 /** Next certificate in the linked list that constitutes the CA chain.
99 * \p NULL indicates the end of the list.
100 * Do not modify this field directly. */
101 struct mbedtls_x509_crt *next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104
Janos Follath5091bec2019-05-08 15:23:08 +0100105/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200106 * From RFC 5280 section 4.2.1.6:
107 * OtherName ::= SEQUENCE {
108 * type-id OBJECT IDENTIFIER,
109 * value [0] EXPLICIT ANY DEFINED BY type-id }
Gilles Peskine842edf42021-08-04 21:56:10 +0200110 *
111 * Future versions of the library may add new fields to this structure or
112 * to its embedded union and structure.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114typedef struct mbedtls_x509_san_other_name {
Ron Eldor890819a2019-05-13 19:03:04 +0300115 /**
Tom Cosgrove1e211442022-05-26 11:51:00 +0100116 * The type_id is an OID as defined in RFC 5280.
Ron Eldor890819a2019-05-13 19:03:04 +0300117 * To check the value of the type id, you should use
118 * \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf.
119 */
Gilles Peskine842edf42021-08-04 21:56:10 +0200120 mbedtls_x509_buf type_id; /**< The type id. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 union {
Janos Follath5091bec2019-05-08 15:23:08 +0100122 /**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200123 * From RFC 4108 section 5:
124 * HardwareModuleName ::= SEQUENCE {
125 * hwType OBJECT IDENTIFIER,
126 * hwSerialNum OCTET STRING }
127 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 struct {
Gilles Peskine842edf42021-08-04 21:56:10 +0200129 mbedtls_x509_buf oid; /**< The object identifier. */
130 mbedtls_x509_buf val; /**< The named value. */
Ron Eldorf05f5942019-05-13 19:11:31 +0300131 }
Gilles Peskine842edf42021-08-04 21:56:10 +0200132 hardware_module_name;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200133 }
Gilles Peskine842edf42021-08-04 21:56:10 +0200134 value;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200135}
136mbedtls_x509_san_other_name;
137
Janos Follath5091bec2019-05-08 15:23:08 +0100138/**
Gilles Peskine842edf42021-08-04 21:56:10 +0200139 * A structure for holding the parsed Subject Alternative Name,
140 * according to type.
141 *
142 * Future versions of the library may add new fields to this structure or
143 * to its embedded union and structure.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200144 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145typedef struct mbedtls_x509_subject_alternative_name {
Gilles Peskine842edf42021-08-04 21:56:10 +0200146 int type; /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200147 union {
Gilles Peskine842edf42021-08-04 21:56:10 +0200148 mbedtls_x509_san_other_name other_name; /**< The otherName supported type. */
149 mbedtls_x509_buf unstructured_name; /**< The buffer for the un constructed types. Only dnsName currently supported */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200150 }
Gilles Peskine842edf42021-08-04 21:56:10 +0200151 san; /**< A union of the supported SAN types */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200152}
153mbedtls_x509_subject_alternative_name;
154
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200155/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200156 * Build flag from an algorithm/curve identifier (pk, md, ecp)
157 * Since 0 is always XXX_NONE, ignore it.
158 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159#define MBEDTLS_X509_ID_FLAG(id) (1 << ((id) - 1))
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200160
161/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200162 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200163 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200164 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard55a7fb82021-06-17 10:39:39 +0200165 *
166 * The fields of this structure are part of the public API and can be
167 * manipulated directly by applications. Future versions of the library may
168 * add extra fields or reorder existing fields.
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200169 *
170 * You can create custom profiles by starting from a copy of
171 * an existing profile, such as mbedtls_x509_crt_profile_default or
172 * mbedtls_x509_ctr_profile_none and then tune it to your needs.
173 *
174 * For example to allow SHA-224 in addition to the default:
175 *
176 * mbedtls_x509_crt_profile my_profile = mbedtls_x509_crt_profile_default;
177 * my_profile.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 );
178 *
179 * Or to allow only RSA-3072+ with SHA-256:
180 *
181 * mbedtls_x509_crt_profile my_profile = mbedtls_x509_crt_profile_none;
182 * my_profile.allowed_mds = MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 );
183 * my_profile.allowed_pks = MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA );
184 * my_profile.rsa_min_bitlen = 3072;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200185 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186typedef struct mbedtls_x509_crt_profile {
Manuel Pégourié-Gonnard5314e082021-06-17 10:32:01 +0200187 uint32_t allowed_mds; /**< MDs for signatures */
Hanno Becker2b9fb882018-10-11 11:36:29 +0100188 uint32_t allowed_pks; /**< PK algs for public keys;
Manuel Pégourié-Gonnard8ba99e72022-04-25 10:12:01 +0200189 * this applies to all certificates
Hanno Becker2b9fb882018-10-11 11:36:29 +0100190 * in the provided chain. */
Manuel Pégourié-Gonnard5314e082021-06-17 10:32:01 +0200191 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
192 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200193}
194mbedtls_x509_crt_profile;
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#define MBEDTLS_X509_CRT_VERSION_1 0
197#define MBEDTLS_X509_CRT_VERSION_2 1
198#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
201#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203#if !defined(MBEDTLS_X509_MAX_FILE_PATH_LEN)
Andres AGf9113192016-09-02 14:06:04 +0100204#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
205#endif
206
Hanno Becker7ac83f92020-10-09 10:48:22 +0100207/* This macro unfolds to the concatenation of macro invocations
208 * X509_CRT_ERROR_INFO( error code,
209 * error code as string,
210 * human readable description )
211 * where X509_CRT_ERROR_INFO is defined by the user.
212 * See x509_crt.c for an example of how to use this. */
213#define MBEDTLS_X509_CRT_ERROR_INFO_LIST \
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_EXPIRED, \
215 "MBEDTLS_X509_BADCERT_EXPIRED", \
216 "The certificate validity has expired") \
217 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_REVOKED, \
218 "MBEDTLS_X509_BADCERT_REVOKED", \
219 "The certificate has been revoked (is on a CRL)") \
220 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_CN_MISMATCH, \
221 "MBEDTLS_X509_BADCERT_CN_MISMATCH", \
222 "The certificate Common Name (CN) does not match with the expected CN") \
223 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_NOT_TRUSTED, \
224 "MBEDTLS_X509_BADCERT_NOT_TRUSTED", \
225 "The certificate is not correctly signed by the trusted CA") \
226 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_NOT_TRUSTED, \
227 "MBEDTLS_X509_BADCRL_NOT_TRUSTED", \
228 "The CRL is not correctly signed by the trusted CA") \
229 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_EXPIRED, \
230 "MBEDTLS_X509_BADCRL_EXPIRED", \
231 "The CRL is expired") \
232 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_MISSING, \
233 "MBEDTLS_X509_BADCERT_MISSING", \
234 "Certificate was missing") \
235 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_SKIP_VERIFY, \
236 "MBEDTLS_X509_BADCERT_SKIP_VERIFY", \
237 "Certificate verification was skipped") \
238 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_OTHER, \
239 "MBEDTLS_X509_BADCERT_OTHER", \
240 "Other reason (can be used by verify callback)") \
241 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_FUTURE, \
242 "MBEDTLS_X509_BADCERT_FUTURE", \
243 "The certificate validity starts in the future") \
244 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_FUTURE, \
245 "MBEDTLS_X509_BADCRL_FUTURE", \
246 "The CRL is from the future") \
247 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_KEY_USAGE, \
248 "MBEDTLS_X509_BADCERT_KEY_USAGE", \
249 "Usage does not match the keyUsage extension") \
250 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, \
251 "MBEDTLS_X509_BADCERT_EXT_KEY_USAGE", \
252 "Usage does not match the extendedKeyUsage extension") \
253 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_NS_CERT_TYPE, \
254 "MBEDTLS_X509_BADCERT_NS_CERT_TYPE", \
255 "Usage does not match the nsCertType extension") \
256 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_BAD_MD, \
257 "MBEDTLS_X509_BADCERT_BAD_MD", \
258 "The certificate is signed with an unacceptable hash.") \
259 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_BAD_PK, \
260 "MBEDTLS_X509_BADCERT_BAD_PK", \
261 "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA).") \
262 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_BAD_KEY, \
263 "MBEDTLS_X509_BADCERT_BAD_KEY", \
264 "The certificate is signed with an unacceptable key (eg bad curve, RSA too short).") \
265 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_BAD_MD, \
266 "MBEDTLS_X509_BADCRL_BAD_MD", \
267 "The CRL is signed with an unacceptable hash.") \
268 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_BAD_PK, \
269 "MBEDTLS_X509_BADCRL_BAD_PK", \
270 "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA).") \
271 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_BAD_KEY, \
272 "MBEDTLS_X509_BADCRL_BAD_KEY", \
273 "The CRL is signed with an unacceptable key (eg bad curve, RSA too short).")
Hanno Becker7ac83f92020-10-09 10:48:22 +0100274
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275/**
276 * Container for writing a certificate (CRT)
277 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100278typedef struct mbedtls_x509write_cert {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200279 int MBEDTLS_PRIVATE(version);
280 mbedtls_mpi MBEDTLS_PRIVATE(serial);
281 mbedtls_pk_context *MBEDTLS_PRIVATE(subject_key);
282 mbedtls_pk_context *MBEDTLS_PRIVATE(issuer_key);
283 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(subject);
284 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(issuer);
285 mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg);
286 char MBEDTLS_PRIVATE(not_before)[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
287 char MBEDTLS_PRIVATE(not_after)[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
288 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(extensions);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200292/**
293 * Item in a verification chain: cert and flags for it
294 */
295typedef struct {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200296 mbedtls_x509_crt *MBEDTLS_PRIVATE(crt);
297 uint32_t MBEDTLS_PRIVATE(flags);
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200298} mbedtls_x509_crt_verify_chain_item;
299
300/**
301 * Max size of verification chain: end-entity + intermediates + trusted root
302 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100303#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200304
305/**
306 * Verification chain as built by \c mbedtls_crt_verify_chain()
307 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308typedef struct {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200309 mbedtls_x509_crt_verify_chain_item MBEDTLS_PRIVATE(items)[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
310 unsigned MBEDTLS_PRIVATE(len);
Hanno Beckerf53893b2019-03-28 13:45:55 +0000311
312#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
313 /* This stores the list of potential trusted signers obtained from
314 * the CA callback used for the CRT verification, if configured.
315 * We must track it somewhere because the callback passes its
316 * ownership to the caller. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200317 mbedtls_x509_crt *MBEDTLS_PRIVATE(trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +0000318#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200319} mbedtls_x509_crt_verify_chain;
320
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200321#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
322
323/**
324 * \brief Context for resuming X.509 verify operations
325 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326typedef struct {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200327 /* for check_signature() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200328 mbedtls_pk_restart_ctx MBEDTLS_PRIVATE(pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200329
330 /* for find_parent_in() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200331 mbedtls_x509_crt *MBEDTLS_PRIVATE(parent); /* non-null iff parent_in in progress */
332 mbedtls_x509_crt *MBEDTLS_PRIVATE(fallback_parent);
333 int MBEDTLS_PRIVATE(fallback_signature_is_good);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200334
335 /* for find_parent() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200336 int MBEDTLS_PRIVATE(parent_is_trusted); /* -1 if find_parent is not in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200337
338 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200339 enum {
340 x509_crt_rs_none,
341 x509_crt_rs_find_parent,
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200342 } MBEDTLS_PRIVATE(in_progress); /* none if no operation is in progress */
343 int MBEDTLS_PRIVATE(self_cnt);
344 mbedtls_x509_crt_verify_chain MBEDTLS_PRIVATE(ver_chain);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200345
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200346} mbedtls_x509_crt_restart_ctx;
347
348#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
349
350/* Now we can declare functions that take a pointer to that */
351typedef void mbedtls_x509_crt_restart_ctx;
352
353#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200357 * Default security profile. Should provide a good balance between security
358 * and compatibility with current deployments.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200359 *
360 * This profile permits:
361 * - SHA2 hashes with at least 256 bits: SHA-256, SHA-384, SHA-512.
Gilles Peskine39957502021-06-17 23:17:52 +0200362 * - Elliptic curves with 255 bits and above except secp256k1.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200363 * - RSA with 2048 bits and above.
364 *
365 * New minor versions of Mbed TLS may extend this profile, for example if
Gilles Peskine39957502021-06-17 23:17:52 +0200366 * new algorithms are added to the library. New minor versions of Mbed TLS will
Gilles Peskineffb92da2021-06-02 00:03:26 +0200367 * not reduce this profile unless serious security concerns require it.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200368 */
369extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
370
371/**
372 * Expected next default profile. Recommended for new deployments.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200373 * Currently targets a 128-bit security level, except for allowing RSA-2048.
374 * This profile may change at any time.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200375 */
376extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
377
378/**
379 * NSA Suite B profile.
380 */
381extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
382
383/**
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200384 * Empty profile that allows nothing. Useful as a basis for constructing
385 * custom profiles.
386 */
387extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none;
388
389/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390 * \brief Parse a single DER formatted certificate and add it
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000391 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000393 * \param chain The pointer to the start of the CRT chain to attach to.
394 * When parsing the first CRT in a chain, this should point
395 * to an instance of ::mbedtls_x509_crt initialized through
396 * mbedtls_x509_crt_init().
397 * \param buf The buffer holding the DER encoded certificate.
398 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000400 * \note This function makes an internal copy of the CRT buffer
401 * \p buf. In particular, \p buf may be destroyed or reused
402 * after this call returns. To avoid duplicating the CRT
403 * buffer (at the cost of stricter lifetime constraints),
404 * use mbedtls_x509_crt_parse_der_nocopy() instead.
405 *
406 * \return \c 0 if successful.
407 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
410 const unsigned char *buf,
411 size_t buflen);
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000412
413/**
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200414 * \brief The type of certificate extension callbacks.
415 *
416 * Callbacks of this type are passed to and used by the
Nicola Di Lietofde98f72020-05-28 08:34:33 +0200417 * mbedtls_x509_crt_parse_der_with_ext_cb() routine when
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200418 * it encounters either an unsupported extension or a
419 * "certificate policies" extension containing any
420 * unsupported certificate policies.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200421 * Future versions of the library may invoke the callback
422 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200423 *
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200424 * \param p_ctx An opaque context passed to the callback.
ndilieto6e249802020-05-28 06:17:38 +0000425 * \param crt The certificate being parsed.
426 * \param oid The OID of the extension.
427 * \param critical Whether the extension is critical.
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200428 * \param p Pointer to the start of the extension value
ndilieto6e249802020-05-28 06:17:38 +0000429 * (the content of the OCTET STRING).
ndilieto6e249802020-05-28 06:17:38 +0000430 * \param end End of extension value.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200431 *
432 * \note The callback must fail and return a negative error code
433 * if it can not parse or does not support the extension.
434 * When the callback fails to parse a critical extension
435 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
436 * When the callback fails to parse a non critical extension
437 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
438 * the extension and continues parsing.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200439 *
440 * \return \c 0 on success.
441 * \return A negative error code on failure.
442 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443typedef int (*mbedtls_x509_crt_ext_cb_t)(void *p_ctx,
444 mbedtls_x509_crt const *crt,
445 mbedtls_x509_buf const *oid,
446 int critical,
447 const unsigned char *p,
448 const unsigned char *end);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200449
450/**
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200451 * \brief Parse a single DER formatted certificate and add it
452 * to the end of the provided chained list.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200453 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200454 * \param chain The pointer to the start of the CRT chain to attach to.
455 * When parsing the first CRT in a chain, this should point
456 * to an instance of ::mbedtls_x509_crt initialized through
457 * mbedtls_x509_crt_init().
458 * \param buf The buffer holding the DER encoded certificate.
459 * \param buflen The size in Bytes of \p buf.
460 * \param make_copy When not zero this function makes an internal copy of the
461 * CRT buffer \p buf. In particular, \p buf may be destroyed
462 * or reused after this call returns.
463 * When zero this function avoids duplicating the CRT buffer
464 * by taking temporary ownership thereof until the CRT
465 * is destroyed (like mbedtls_x509_crt_parse_der_nocopy())
466 * \param cb A callback invoked for every unsupported certificate
467 * extension.
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200468 * \param p_ctx An opaque context passed to the callback.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200469 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200470 * \note This call is functionally equivalent to
471 * mbedtls_x509_crt_parse_der(), and/or
472 * mbedtls_x509_crt_parse_der_nocopy()
473 * but it calls the callback with every unsupported
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200474 * certificate extension and additionally the
475 * "certificate policies" extension if it contains any
476 * unsupported certificate policies.
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200477 * The callback must return a negative error code if it
478 * does not know how to handle such an extension.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200479 * When the callback fails to parse a critical extension
480 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
481 * When the callback fails to parse a non critical extension
482 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
483 * the extension and continues parsing.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200484 * Future versions of the library may invoke the callback
485 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200486 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200487 * \return \c 0 if successful.
488 * \return A negative error code on failure.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200489 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
491 const unsigned char *buf,
492 size_t buflen,
493 int make_copy,
494 mbedtls_x509_crt_ext_cb_t cb,
495 void *p_ctx);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200496
497/**
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000498 * \brief Parse a single DER formatted certificate and add it
499 * to the end of the provided chained list. This is a
500 * variant of mbedtls_x509_crt_parse_der() which takes
501 * temporary ownership of the CRT buffer until the CRT
502 * is destroyed.
503 *
504 * \param chain The pointer to the start of the CRT chain to attach to.
505 * When parsing the first CRT in a chain, this should point
506 * to an instance of ::mbedtls_x509_crt initialized through
507 * mbedtls_x509_crt_init().
508 * \param buf The address of the readable buffer holding the DER encoded
509 * certificate to use. On success, this buffer must be
Jan Brucknerb33f6e52022-10-06 11:23:49 +0200510 * retained and not be changed for the lifetime of the
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000511 * CRT chain \p chain, that is, until \p chain is destroyed
512 * through a call to mbedtls_x509_crt_free().
513 * \param buflen The size in Bytes of \p buf.
514 *
515 * \note This call is functionally equivalent to
516 * mbedtls_x509_crt_parse_der(), but it avoids creating a
517 * copy of the input buffer at the cost of stronger lifetime
518 * constraints. This is useful in constrained environments
519 * where duplication of the CRT cannot be tolerated.
520 *
521 * \return \c 0 if successful.
522 * \return A negative error code on failure.
523 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
525 const unsigned char *buf,
526 size_t buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527
528/**
Hanno Becker89a91122018-08-23 16:14:00 +0100529 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100530 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100532 * For CRTs in PEM encoding, the function parses permissively:
533 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100534 * returns the number of certificates for which parsing failed
535 * (hence \c 0 if all certificates were parsed successfully).
536 * If no certificate could be parsed, the function returns
537 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100539 * PEM encoded certificates may be interleaved by other data
540 * such as human readable descriptions of their content, as
541 * long as the certificates are enclosed in the PEM specific
542 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
543 *
544 * \param chain The chain to which to add the parsed certificates.
545 * \param buf The buffer holding the certificate data in PEM or DER format.
546 * For certificates in PEM encoding, this may be a concatenation
547 * of multiple certificates; for DER encoding, the buffer must
548 * comprise exactly one certificate.
549 * \param buflen The size of \p buf, including the terminating \c NULL byte
550 * in case of PEM encoded data.
551 *
552 * \return \c 0 if all certificates were parsed successfully.
553 * \return The (positive) number of certificates that couldn't
554 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100555 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100556 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100558int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561/**
562 * \brief Load one or more certificates and add them
563 * to the chained list. Parses permissively. If some
564 * certificates can be parsed, the result is the number
565 * of failed certificates it encountered. If none complete
566 * correctly, the first error is returned.
567 *
568 * \param chain points to the start of the chain
569 * \param path filename to read the certificates from
570 *
571 * \return 0 if all certificates parsed successfully, a positive number
572 * if partly successful or a specific X509 or PEM error code
573 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100574int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
576/**
577 * \brief Load one or more certificate files from a path and add them
578 * to the chained list. Parses permissively. If some
579 * certificates can be parsed, the result is the number
580 * of failed certificates it encountered. If none complete
581 * correctly, the first error is returned.
582 *
583 * \param chain points to the start of the chain
584 * \param path directory / folder to read the certificate files from
585 *
586 * \return 0 if all certificates parsed successfully, a positive number
587 * if partly successful or a specific X509 or PEM error code
588 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100589int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path);
Janos Follath11b41eb2019-05-08 15:26:49 +0100590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#endif /* MBEDTLS_FS_IO */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200592/**
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300593 * \brief This function parses an item in the SubjectAlternativeNames
594 * extension.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200595 *
Ron Eldor890819a2019-05-13 19:03:04 +0300596 * \param san_buf The buffer holding the raw data item of the subject
597 * alternative name.
598 * \param san The target structure to populate with the parsed presentation
599 * of the subject alternative name encoded in \p san_raw.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200600 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300601 * \note Only "dnsName" and "otherName" of type hardware_module_name
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200602 * as defined in RFC 4180 is supported.
603 *
Ron Eldor890819a2019-05-13 19:03:04 +0300604 * \note This function should be called on a single raw data of
Ron Eldorcc45cd12019-05-15 10:20:09 +0300605 * subject alternative name. For example, after successful
Ron Eldor890819a2019-05-13 19:03:04 +0300606 * certificate parsing, one must iterate on every item in the
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300607 * \p crt->subject_alt_names sequence, and pass it to
608 * this function.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200609 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300610 * \warning The target structure contains pointers to the raw data of the
Ron Eldorcc45cd12019-05-15 10:20:09 +0300611 * parsed certificate, and its lifetime is restricted by the
612 * lifetime of the certificate.
613 *
Ron Eldor890819a2019-05-13 19:03:04 +0300614 * \return \c 0 on success
615 * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300616 * SAN type.
617 * \return Another negative value for any other failure.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200618 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100619int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
620 mbedtls_x509_subject_alternative_name *san);
Peter Kolbus9a969b62018-12-11 13:55:56 -0600621
Hanno Becker612a2f12020-10-09 09:19:39 +0100622#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623/**
624 * \brief Returns an informational string about the
625 * certificate.
626 *
627 * \param buf Buffer to write to
628 * \param size Maximum size of buffer
629 * \param prefix A line prefix
630 * \param crt The X509 certificate to represent
631 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200632 * \return The length of the string written (not including the
633 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100635int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
636 const mbedtls_x509_crt *crt);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637
638/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100639 * \brief Returns an informational string about the
640 * verification status of a certificate.
641 *
642 * \param buf Buffer to write to
643 * \param size Maximum size of buffer
644 * \param prefix A line prefix
645 * \param flags Verification flags created by mbedtls_x509_crt_verify()
646 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200647 * \return The length of the string written (not including the
648 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100649 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100650int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
651 uint32_t flags);
Hanno Becker88c2bf32019-06-14 17:21:24 +0100652#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200653
654/**
Hanno Becker902451d2019-03-27 11:48:40 +0000655 * \brief Verify a chain of certificates.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656 *
657 * The verify callback is a user-supplied callback that
658 * can clear / modify / add flags for a certificate. If set,
659 * the verification callback is called for each
660 * certificate in the chain (from the trust-ca down to the
661 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663 * int *flags). With the flags representing current flags for
664 * that specific certificate and the certificate depth from
665 * the bottom (Peer cert depth = 0).
666 *
667 * All flags left after returning from the callback
668 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200669 * return 0 for anything (including invalid certificates)
670 * other than fatal error, as a non-zero return code
671 * immediately aborts the verification process. For fatal
672 * errors, a specific error code should be used (different
673 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
674 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
675 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100677 * \note In case verification failed, the results can be displayed
678 * using \c mbedtls_x509_crt_verify_info()
679 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200680 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
681 * default security profile.
682 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100683 * \note It is your responsibility to provide up-to-date CRLs for
684 * all trusted CAs. If no CRL is provided for the CA that was
685 * used to sign the certificate, CRL verification is skipped
686 * silently, that is *without* setting any flag.
687 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200688 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200689 * (1) those of trusted root CAs, so that certificates
690 * chaining up to those CAs will be trusted, and (2)
691 * self-signed end-entity certificates to be trusted (for
692 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200693 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200694 *
Hanno Becker902451d2019-03-27 11:48:40 +0000695 * \param crt The certificate chain to be verified.
696 * \param trust_ca The list of trusted CAs.
697 * \param ca_crl The list of CRLs for trusted CAs.
Manuel Pégourié-Gonnardf58e5cc2020-07-24 10:31:37 +0200698 * \param cn The expected Common Name. This will be checked to be
699 * present in the certificate's subjectAltNames extension or,
700 * if this extension is absent, as a CN component in its
701 * Subject name. Currently only DNS names are supported. This
702 * may be \c NULL if the CN need not be verified.
Hanno Becker902451d2019-03-27 11:48:40 +0000703 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100704 * If the verification couldn't be completed, the flag value is
705 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000706 * \param f_vrfy The verification callback to use. See the documentation
707 * of mbedtls_x509_crt_verify() for more information.
708 * \param p_vrfy The context to be passed to \p f_vrfy.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709 *
Hanno Becker902451d2019-03-27 11:48:40 +0000710 * \return \c 0 if the chain is valid with respect to the
711 * passed CN, CAs, CRLs and security profile.
712 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
713 * certificate chain verification failed. In this case,
714 * \c *flags will have one or more
715 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
716 * flags set.
717 * \return Another negative error code in case of a fatal error
718 * encountered during the verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
721 mbedtls_x509_crt *trust_ca,
722 mbedtls_x509_crl *ca_crl,
723 const char *cn, uint32_t *flags,
724 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
725 void *p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200727/**
Hanno Becker902451d2019-03-27 11:48:40 +0000728 * \brief Verify a chain of certificates with respect to
729 * a configurable security profile.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200730 *
731 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
732 * security profile.
733 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200734 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200735 * for ECDSA) apply to all certificates: trusted root,
736 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200737 *
Hanno Becker902451d2019-03-27 11:48:40 +0000738 * \param crt The certificate chain to be verified.
739 * \param trust_ca The list of trusted CAs.
740 * \param ca_crl The list of CRLs for trusted CAs.
741 * \param profile The security profile to use for the verification.
742 * \param cn The expected Common Name. This may be \c NULL if the
743 * CN need not be verified.
744 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100745 * If the verification couldn't be completed, the flag value is
746 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000747 * \param f_vrfy The verification callback to use. See the documentation
748 * of mbedtls_x509_crt_verify() for more information.
749 * \param p_vrfy The context to be passed to \p f_vrfy.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200750 *
Hanno Becker902451d2019-03-27 11:48:40 +0000751 * \return \c 0 if the chain is valid with respect to the
752 * passed CN, CAs, CRLs and security profile.
753 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
754 * certificate chain verification failed. In this case,
755 * \c *flags will have one or more
756 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
757 * flags set.
758 * \return Another negative error code in case of a fatal error
759 * encountered during the verification process.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200760 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
762 mbedtls_x509_crt *trust_ca,
763 mbedtls_x509_crl *ca_crl,
764 const mbedtls_x509_crt_profile *profile,
765 const char *cn, uint32_t *flags,
766 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
767 void *p_vrfy);
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200768
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200769/**
770 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
771 *
772 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
773 * but can return early and restart according to the limit
774 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
775 *
Hanno Becker902451d2019-03-27 11:48:40 +0000776 * \param crt The certificate chain to be verified.
777 * \param trust_ca The list of trusted CAs.
778 * \param ca_crl The list of CRLs for trusted CAs.
779 * \param profile The security profile to use for the verification.
780 * \param cn The expected Common Name. This may be \c NULL if the
781 * CN need not be verified.
782 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100783 * If the verification couldn't be completed, the flag value is
784 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000785 * \param f_vrfy The verification callback to use. See the documentation
786 * of mbedtls_x509_crt_verify() for more information.
787 * \param p_vrfy The context to be passed to \p f_vrfy.
788 * \param rs_ctx The restart context to use. This may be set to \c NULL
789 * to disable restartable ECC.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200790 *
791 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200792 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200793 * operations was reached: see \c mbedtls_ecp_set_max_ops().
794 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
796 mbedtls_x509_crt *trust_ca,
797 mbedtls_x509_crl *ca_crl,
798 const mbedtls_x509_crt_profile *profile,
799 const char *cn, uint32_t *flags,
800 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
801 void *p_vrfy,
802 mbedtls_x509_crt_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200803
Hanno Becker5c8df782019-03-27 11:01:17 +0000804/**
805 * \brief The type of trusted certificate callbacks.
806 *
807 * Callbacks of this type are passed to and used by the CRT
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300808 * verification routine mbedtls_x509_crt_verify_with_ca_cb()
Hanno Becker5c8df782019-03-27 11:01:17 +0000809 * when looking for trusted signers of a given certificate.
810 *
811 * On success, the callback returns a list of trusted
812 * certificates to be considered as potential signers
813 * for the input certificate.
814 *
815 * \param p_ctx An opaque context passed to the callback.
816 * \param child The certificate for which to search a potential signer.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300817 * This will point to a readable certificate.
Hanno Becker5c8df782019-03-27 11:01:17 +0000818 * \param candidate_cas The address at which to store the address of the first
819 * entry in the generated linked list of candidate signers.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300820 * This will not be \c NULL.
Hanno Becker5c8df782019-03-27 11:01:17 +0000821 *
822 * \note The callback must only return a non-zero value on a
823 * fatal error. If, in contrast, the search for a potential
824 * signer completes without a single candidate, the
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300825 * callback must return \c 0 and set \c *candidate_cas
Hanno Becker5c8df782019-03-27 11:01:17 +0000826 * to \c NULL.
827 *
828 * \return \c 0 on success. In this case, \c *candidate_cas points
829 * to a heap-allocated linked list of instances of
830 * ::mbedtls_x509_crt, and ownership of this list is passed
831 * to the caller.
832 * \return A negative error code on failure.
833 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834typedef int (*mbedtls_x509_crt_ca_cb_t)(void *p_ctx,
835 mbedtls_x509_crt const *child,
836 mbedtls_x509_crt **candidate_cas);
Hanno Beckere15dae72019-03-28 13:55:38 +0000837
838#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker5c8df782019-03-27 11:01:17 +0000839/**
840 * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
841 * uses a callback to acquire the list of trusted CA
842 * certificates.
843 *
844 * \param crt The certificate chain to be verified.
845 * \param f_ca_cb The callback to be used to query for potential signers
846 * of a given child certificate. See the documentation of
847 * ::mbedtls_x509_crt_ca_cb_t for more information.
848 * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
849 * \param profile The security profile for the verification.
850 * \param cn The expected Common Name. This may be \c NULL if the
851 * CN need not be verified.
852 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100853 * If the verification couldn't be completed, the flag value is
854 * set to (uint32_t) -1.
Hanno Becker5c8df782019-03-27 11:01:17 +0000855 * \param f_vrfy The verification callback to use. See the documentation
856 * of mbedtls_x509_crt_verify() for more information.
857 * \param p_vrfy The context to be passed to \p f_vrfy.
858 *
859 * \return See \c mbedtls_crt_verify_with_profile().
860 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
862 mbedtls_x509_crt_ca_cb_t f_ca_cb,
863 void *p_ca_cb,
864 const mbedtls_x509_crt_profile *profile,
865 const char *cn, uint32_t *flags,
866 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
867 void *p_vrfy);
Hanno Becker5c8df782019-03-27 11:01:17 +0000868
869#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
870
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200871/**
872 * \brief Check usage of certificate against keyUsage extension.
873 *
874 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200875 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
876 * before using the certificate to perform an RSA key
877 * exchange).
878 *
879 * \note Except for decipherOnly and encipherOnly, a bit set in the
880 * usage argument means this bit MUST be set in the
881 * certificate. For decipherOnly and encipherOnly, it means
882 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200883 *
884 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200886 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200887 *
888 * \note You should only call this function on leaf certificates, on
889 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200891 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100892int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
893 unsigned int usage);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200894
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200895/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000896 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200897 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000898 * \param crt Leaf certificate used.
899 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
900 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200902 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000903 * \return 0 if this use of the certificate is allowed,
904 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200905 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000906 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200907 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
909 const char *usage_oid,
910 size_t usage_len);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200914 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915 *
916 * \param crt a certificate to be verified
917 * \param crl the CRL to verify against
918 *
919 * \return 1 if the certificate is revoked, 0 otherwise
920 *
921 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100922int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924
925/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200926 * \brief Initialize a certificate (chain)
927 *
928 * \param crt Certificate chain to initialize
929 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930void mbedtls_x509_crt_init(mbedtls_x509_crt *crt);
Paul Bakker369d2eb2013-09-18 11:58:25 +0200931
932/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 * \brief Unallocate all certificate data
934 *
935 * \param crt Certificate chain to free
936 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937void mbedtls_x509_crt_free(mbedtls_x509_crt *crt);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200938
939#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
940/**
941 * \brief Initialize a restart context
942 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100943void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200944
945/**
946 * \brief Free the components of a restart context
947 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100948void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200949#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951
Thomas Daubneya5f39e02022-06-06 15:42:32 +0100952/**
953 * \brief Query certificate for given extension type
954 *
955 * \param[in] ctx Certificate context to be queried, must not be \c NULL
956 * \param ext_type Extension type being queried for, must be a valid
957 * extension type. Must be one of the MBEDTLS_X509_EXT_XXX
958 * values
959 *
960 * \return 0 if the given extension type is not present,
961 * non-zero otherwise
962 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963static inline int mbedtls_x509_crt_has_ext_type(const mbedtls_x509_crt *ctx,
964 int ext_type)
Thomas Daubneya5f39e02022-06-06 15:42:32 +0100965{
966 return ctx->MBEDTLS_PRIVATE(ext_types) & ext_type;
967}
968
Andrzej Kurek38d4fdd2021-12-28 16:22:52 +0100969/** \} name Structures and functions for parsing and writing X.509 certificates */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972/**
973 * \brief Initialize a CRT writing context
974 *
975 * \param ctx CRT context to initialize
976 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978
979/**
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800980 * \brief Set the version for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 *
983 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
985 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100987void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx, int version);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988
989/**
990 * \brief Set the serial number for a Certificate.
991 *
992 * \param ctx CRT context to use
993 * \param serial serial number to set
994 *
995 * \return 0 if successful
996 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100997int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998
999/**
1000 * \brief Set the validity period for a Certificate
1001 * Timestamps should be in string format for UTC timezone
1002 * i.e. "YYYYMMDDhhmmss"
1003 * e.g. "20131231235959" for December 31st 2013
1004 * at 23:59:59
1005 *
1006 * \param ctx CRT context to use
1007 * \param not_before not_before timestamp
1008 * \param not_after not_after timestamp
1009 *
1010 * \return 0 if timestamp was parsed successfully, or
1011 * a specific error code
1012 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001013int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx, const char *not_before,
1014 const char *not_after);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015
1016/**
1017 * \brief Set the issuer name for a Certificate
1018 * Issuer names should contain a comma-separated list
1019 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001020 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 *
1022 * \param ctx CRT context to use
1023 * \param issuer_name issuer name to set
1024 *
1025 * \return 0 if issuer name was parsed successfully, or
1026 * a specific error code
1027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx,
1029 const char *issuer_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030
1031/**
1032 * \brief Set the subject name for a Certificate
1033 * Subject names should contain a comma-separated list
1034 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001035 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036 *
1037 * \param ctx CRT context to use
1038 * \param subject_name subject name to set
1039 *
1040 * \return 0 if subject name was parsed successfully, or
1041 * a specific error code
1042 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx,
1044 const char *subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045
1046/**
1047 * \brief Set the subject public key for the certificate
1048 *
1049 * \param ctx CRT context to use
1050 * \param key public key to include
1051 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx, mbedtls_pk_context *key);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053
1054/**
1055 * \brief Set the issuer key used for signing the certificate
1056 *
1057 * \param ctx CRT context to use
1058 * \param key private key to sign with
1059 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001060void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx, mbedtls_pk_context *key);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001061
1062/**
1063 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065 *
1066 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001067 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070
1071/**
1072 * \brief Generic function to add to or replace an extension in the
1073 * CRT
1074 *
1075 * \param ctx CRT context to use
1076 * \param oid OID of the extension
1077 * \param oid_len length of the OID
1078 * \param critical if the extension is critical (per the RFC's definition)
1079 * \param val value of the extension OCTET STRING
1080 * \param val_len length of the value data
1081 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001082 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001084int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
1085 const char *oid, size_t oid_len,
1086 int critical,
1087 const unsigned char *val, size_t val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088
1089/**
1090 * \brief Set the basicConstraints extension for a CRT
1091 *
1092 * \param ctx CRT context to use
1093 * \param is_ca is this a CA certificate
1094 * \param max_pathlen maximum length of certificate chains below this
1095 * certificate (only for CA certificates, -1 is
Tom Cosgrove1e211442022-05-26 11:51:00 +01001096 * unlimited)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097 *
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 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001100int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
1101 int is_ca, int max_pathlen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102
Manuel Pégourié-Gonnarde8967052022-09-15 11:41:16 +02001103#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104/**
1105 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001107 * called before
1108 *
1109 * \param ctx CRT context to use
1110 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001111 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114
1115/**
1116 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118 * called before
1119 *
1120 * \param ctx CRT context to use
1121 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001122 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx);
Manuel Pégourié-Gonnarde8967052022-09-15 11:41:16 +02001125#endif /* MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001126
1127/**
1128 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001130 *
1131 * \param ctx CRT context to use
1132 * \param key_usage key usage flags to set
1133 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001134 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001135 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001136int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
1137 unsigned int key_usage);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001138
1139/**
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +01001140 * \brief Set the Extended Key Usage Extension
1141 * (e.g. MBEDTLS_OID_SERVER_AUTH)
1142 *
1143 * \param ctx CRT context to use
1144 * \param exts extended key usage extensions to set, a sequence of
1145 * MBEDTLS_ASN1_OID objects
1146 *
1147 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
1148 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001149int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx,
1150 const mbedtls_asn1_sequence *exts);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +01001151
1152/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001153 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155 *
1156 * \param ctx CRT context to use
1157 * \param ns_cert_type Netscape Cert Type flags to set
1158 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001159 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001161int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
1162 unsigned char ns_cert_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001163
1164/**
1165 * \brief Free the contents of a CRT write context
1166 *
1167 * \param ctx CRT context to free
1168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170
1171/**
1172 * \brief Write a built up certificate to a X509 DER structure
1173 * Note: data is written at the end of the buffer! Use the
1174 * return value to determine where you should start
1175 * using the buffer
1176 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001177 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001178 * \param buf buffer to write to
1179 * \param size size of the buffer
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001180 * \param f_rng RNG function. This must not be \c NULL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181 * \param p_rng RNG parameter
1182 *
1183 * \return length of data written if successful, or a specific
1184 * error code
1185 *
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001186 * \note \p f_rng is used for the signature operation.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001187 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001188int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
1189 int (*f_rng)(void *, unsigned char *, size_t),
1190 void *p_rng);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001193/**
1194 * \brief Write a built up certificate to a X509 PEM string
1195 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001196 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001197 * \param buf buffer to write to
1198 * \param size size of the buffer
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001199 * \param f_rng RNG function. This must not be \c NULL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001200 * \param p_rng RNG parameter
1201 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001202 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203 *
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001204 * \note \p f_rng is used for the signature operation.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001205 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001206int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
1207 int (*f_rng)(void *, unsigned char *, size_t),
1208 void *p_rng);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#endif /* MBEDTLS_PEM_WRITE_C */
1210#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001211
Andrzej Kureka0defed2021-12-30 12:33:31 +01001212/** \} addtogroup x509_module */
1213
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001214#ifdef __cplusplus
1215}
1216#endif
1217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#endif /* mbedtls_x509_crt.h */