blob: 9817d35a7d4071b44a7506dbbbf5e87e6a0fd3e7 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker7c6b2c32013-09-16 13:49:26 +02009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_X509_CRT_H
11#define MBEDTLS_X509_CRT_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020012#include "mbedtls/private_access.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020013
Bence Szépkútic662b362021-05-27 11:25:03 +020014#include "mbedtls/build_info.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020015
Jaeden Amero6609aef2019-07-04 20:01:14 +010016#include "mbedtls/x509.h"
17#include "mbedtls/x509_crl.h"
18#include "mbedtls/bignum.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020019
20/**
21 * \addtogroup x509_module
22 * \{
23 */
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29/**
30 * \name Structures and functions for parsing and writing X.509 certificates
31 * \{
32 */
33
34/**
35 * Container for an X.509 certificate. The certificate may be chained.
Gilles Peskine842edf42021-08-04 21:56:10 +020036 *
37 * Some fields of this structure are publicly readable. Do not modify
38 * them except via Mbed TLS library functions: the effect of modifying
39 * those fields or the data that those fields points to is unspecified.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020040 */
Gilles Peskine449bd832023-01-11 14:50:10 +010041typedef struct mbedtls_x509_crt {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020042 int MBEDTLS_PRIVATE(own_buffer); /**< Indicates if \c raw is owned
Gilles Peskine449bd832023-01-11 14:50:10 +010043 * by the structure or not. */
Gilles Peskine842edf42021-08-04 21:56:10 +020044 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
45 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020046
Gilles Peskine842edf42021-08-04 21:56:10 +020047 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
48 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
49 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050
Gilles Peskine842edf42021-08-04 21:56:10 +020051 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
52 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053
Gilles Peskine842edf42021-08-04 21:56:10 +020054 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
55 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056
Gilles Peskine842edf42021-08-04 21:56:10 +020057 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
58 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059
Gilles Peskine842edf42021-08-04 21:56:10 +020060 mbedtls_x509_buf pk_raw;
61 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062
Gilles Peskine842edf42021-08-04 21:56:10 +020063 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
64 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
65 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
Andrzej Kurek17473042023-04-30 14:11:23 -040066 mbedtls_x509_sequence subject_alt_names; /**< Optional list of raw entries of Subject Alternative Names extension. These can be later parsed by mbedtls_x509_parse_subject_alt_name. */
toth92ga41954d2021-02-12 16:11:17 +010067 mbedtls_x509_buf subject_key_id; /**< Optional X.509 v3 extension subject key identifier. */
68 mbedtls_x509_authority authority_key_id; /**< Optional X.509 v3 extension authority key identifier. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069
Gilles Peskine842edf42021-08-04 21:56:10 +020070 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 +020071
Mateusz Starzyk846f0212021-05-19 19:44:07 +020072 int MBEDTLS_PRIVATE(ext_types); /**< Bit string containing detected and parsed extensions */
73 int MBEDTLS_PRIVATE(ca_istrue); /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
74 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 +020075
Mateusz Starzyk846f0212021-05-19 19:44:07 +020076 unsigned int MBEDTLS_PRIVATE(key_usage); /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077
Gilles Peskine842edf42021-08-04 21:56:10 +020078 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079
Mateusz Starzyk846f0212021-05-19 19:44:07 +020080 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 +020081
Mateusz Starzyk846f0212021-05-19 19:44:07 +020082 mbedtls_x509_buf MBEDTLS_PRIVATE(sig); /**< Signature: hash of the tbs part signed with the private key. */
83 mbedtls_md_type_t MBEDTLS_PRIVATE(sig_md); /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
84 mbedtls_pk_type_t MBEDTLS_PRIVATE(sig_pk); /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
85 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 +020086
Gilles Peskineca939952021-08-31 23:18:07 +020087 /** Next certificate in the linked list that constitutes the CA chain.
88 * \p NULL indicates the end of the list.
89 * Do not modify this field directly. */
90 struct mbedtls_x509_crt *next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093
Janos Follath5091bec2019-05-08 15:23:08 +010094/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020095 * Build flag from an algorithm/curve identifier (pk, md, ecp)
96 * Since 0 is always XXX_NONE, ignore it.
97 */
Gilles Peskine449bd832023-01-11 14:50:10 +010098#define MBEDTLS_X509_ID_FLAG(id) (1 << ((id) - 1))
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020099
100/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200101 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200102 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200103 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard55a7fb82021-06-17 10:39:39 +0200104 *
105 * The fields of this structure are part of the public API and can be
106 * manipulated directly by applications. Future versions of the library may
107 * add extra fields or reorder existing fields.
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200108 *
109 * You can create custom profiles by starting from a copy of
110 * an existing profile, such as mbedtls_x509_crt_profile_default or
111 * mbedtls_x509_ctr_profile_none and then tune it to your needs.
112 *
113 * For example to allow SHA-224 in addition to the default:
114 *
115 * mbedtls_x509_crt_profile my_profile = mbedtls_x509_crt_profile_default;
116 * my_profile.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 );
117 *
118 * Or to allow only RSA-3072+ with SHA-256:
119 *
120 * mbedtls_x509_crt_profile my_profile = mbedtls_x509_crt_profile_none;
121 * my_profile.allowed_mds = MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 );
122 * my_profile.allowed_pks = MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA );
123 * my_profile.rsa_min_bitlen = 3072;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200124 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125typedef struct mbedtls_x509_crt_profile {
Manuel Pégourié-Gonnard5314e082021-06-17 10:32:01 +0200126 uint32_t allowed_mds; /**< MDs for signatures */
Hanno Becker2b9fb882018-10-11 11:36:29 +0100127 uint32_t allowed_pks; /**< PK algs for public keys;
Manuel Pégourié-Gonnard8ba99e72022-04-25 10:12:01 +0200128 * this applies to all certificates
Hanno Becker2b9fb882018-10-11 11:36:29 +0100129 * in the provided chain. */
Manuel Pégourié-Gonnard5314e082021-06-17 10:32:01 +0200130 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
131 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200132}
133mbedtls_x509_crt_profile;
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#define MBEDTLS_X509_CRT_VERSION_1 0
136#define MBEDTLS_X509_CRT_VERSION_2 1
137#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138
Valerio Settida0afcc2023-01-05 16:46:59 +0100139#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 20
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142#if !defined(MBEDTLS_X509_MAX_FILE_PATH_LEN)
Andres AGf9113192016-09-02 14:06:04 +0100143#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
144#endif
145
Hanno Becker7ac83f92020-10-09 10:48:22 +0100146/* This macro unfolds to the concatenation of macro invocations
147 * X509_CRT_ERROR_INFO( error code,
148 * error code as string,
149 * human readable description )
150 * where X509_CRT_ERROR_INFO is defined by the user.
151 * See x509_crt.c for an example of how to use this. */
152#define MBEDTLS_X509_CRT_ERROR_INFO_LIST \
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_EXPIRED, \
154 "MBEDTLS_X509_BADCERT_EXPIRED", \
155 "The certificate validity has expired") \
156 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_REVOKED, \
157 "MBEDTLS_X509_BADCERT_REVOKED", \
158 "The certificate has been revoked (is on a CRL)") \
159 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_CN_MISMATCH, \
160 "MBEDTLS_X509_BADCERT_CN_MISMATCH", \
161 "The certificate Common Name (CN) does not match with the expected CN") \
162 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_NOT_TRUSTED, \
163 "MBEDTLS_X509_BADCERT_NOT_TRUSTED", \
164 "The certificate is not correctly signed by the trusted CA") \
165 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_NOT_TRUSTED, \
166 "MBEDTLS_X509_BADCRL_NOT_TRUSTED", \
167 "The CRL is not correctly signed by the trusted CA") \
168 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_EXPIRED, \
169 "MBEDTLS_X509_BADCRL_EXPIRED", \
170 "The CRL is expired") \
171 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_MISSING, \
172 "MBEDTLS_X509_BADCERT_MISSING", \
173 "Certificate was missing") \
174 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_SKIP_VERIFY, \
175 "MBEDTLS_X509_BADCERT_SKIP_VERIFY", \
176 "Certificate verification was skipped") \
177 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_OTHER, \
178 "MBEDTLS_X509_BADCERT_OTHER", \
179 "Other reason (can be used by verify callback)") \
180 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_FUTURE, \
181 "MBEDTLS_X509_BADCERT_FUTURE", \
182 "The certificate validity starts in the future") \
183 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_FUTURE, \
184 "MBEDTLS_X509_BADCRL_FUTURE", \
185 "The CRL is from the future") \
186 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_KEY_USAGE, \
187 "MBEDTLS_X509_BADCERT_KEY_USAGE", \
188 "Usage does not match the keyUsage extension") \
189 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, \
190 "MBEDTLS_X509_BADCERT_EXT_KEY_USAGE", \
191 "Usage does not match the extendedKeyUsage extension") \
192 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_NS_CERT_TYPE, \
193 "MBEDTLS_X509_BADCERT_NS_CERT_TYPE", \
194 "Usage does not match the nsCertType extension") \
195 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_BAD_MD, \
196 "MBEDTLS_X509_BADCERT_BAD_MD", \
197 "The certificate is signed with an unacceptable hash.") \
198 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_BAD_PK, \
199 "MBEDTLS_X509_BADCERT_BAD_PK", \
200 "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA).") \
201 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_BAD_KEY, \
202 "MBEDTLS_X509_BADCERT_BAD_KEY", \
203 "The certificate is signed with an unacceptable key (eg bad curve, RSA too short).") \
204 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_BAD_MD, \
205 "MBEDTLS_X509_BADCRL_BAD_MD", \
206 "The CRL is signed with an unacceptable hash.") \
207 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_BAD_PK, \
208 "MBEDTLS_X509_BADCRL_BAD_PK", \
209 "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA).") \
210 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_BAD_KEY, \
211 "MBEDTLS_X509_BADCRL_BAD_KEY", \
212 "The CRL is signed with an unacceptable key (eg bad curve, RSA too short).")
Hanno Becker7ac83f92020-10-09 10:48:22 +0100213
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200214/**
215 * Container for writing a certificate (CRT)
216 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100217typedef struct mbedtls_x509write_cert {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200218 int MBEDTLS_PRIVATE(version);
Valerio Settida0afcc2023-01-05 16:46:59 +0100219 unsigned char MBEDTLS_PRIVATE(serial)[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN];
220 size_t MBEDTLS_PRIVATE(serial_len);
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200221 mbedtls_pk_context *MBEDTLS_PRIVATE(subject_key);
222 mbedtls_pk_context *MBEDTLS_PRIVATE(issuer_key);
223 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(subject);
224 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(issuer);
225 mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg);
226 char MBEDTLS_PRIVATE(not_before)[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
227 char MBEDTLS_PRIVATE(not_after)[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
228 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(extensions);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200231
Andrzej Kurek1bc7df22023-04-04 07:09:04 -0400232/**
233 * \brief Set Subject Alternative Name
234 *
235 * \param ctx Certificate context to use
236 * \param san_list List of SAN values
237 *
238 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
239 *
240 * \note "dnsName", "uniformResourceIdentifier", "IP address",
241 * "otherName", and "DirectoryName", as defined in RFC 5280,
242 * are supported.
243 */
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400244int mbedtls_x509write_crt_set_subject_alternative_name(mbedtls_x509write_cert *ctx,
245 const mbedtls_x509_san_list *san_list);
246
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200247/**
248 * Item in a verification chain: cert and flags for it
249 */
250typedef struct {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200251 mbedtls_x509_crt *MBEDTLS_PRIVATE(crt);
252 uint32_t MBEDTLS_PRIVATE(flags);
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200253} mbedtls_x509_crt_verify_chain_item;
254
255/**
256 * Max size of verification chain: end-entity + intermediates + trusted root
257 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100258#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200259
260/**
261 * Verification chain as built by \c mbedtls_crt_verify_chain()
262 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263typedef struct {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200264 mbedtls_x509_crt_verify_chain_item MBEDTLS_PRIVATE(items)[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
265 unsigned MBEDTLS_PRIVATE(len);
Hanno Beckerf53893b2019-03-28 13:45:55 +0000266
267#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
268 /* This stores the list of potential trusted signers obtained from
269 * the CA callback used for the CRT verification, if configured.
270 * We must track it somewhere because the callback passes its
271 * ownership to the caller. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200272 mbedtls_x509_crt *MBEDTLS_PRIVATE(trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +0000273#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200274} mbedtls_x509_crt_verify_chain;
275
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200276#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
277
278/**
279 * \brief Context for resuming X.509 verify operations
280 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281typedef struct {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200282 /* for check_signature() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200283 mbedtls_pk_restart_ctx MBEDTLS_PRIVATE(pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200284
285 /* for find_parent_in() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200286 mbedtls_x509_crt *MBEDTLS_PRIVATE(parent); /* non-null iff parent_in in progress */
287 mbedtls_x509_crt *MBEDTLS_PRIVATE(fallback_parent);
288 int MBEDTLS_PRIVATE(fallback_signature_is_good);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200289
290 /* for find_parent() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200291 int MBEDTLS_PRIVATE(parent_is_trusted); /* -1 if find_parent is not in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200292
293 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200294 enum {
295 x509_crt_rs_none,
296 x509_crt_rs_find_parent,
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200297 } MBEDTLS_PRIVATE(in_progress); /* none if no operation is in progress */
298 int MBEDTLS_PRIVATE(self_cnt);
299 mbedtls_x509_crt_verify_chain MBEDTLS_PRIVATE(ver_chain);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200300
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200301} mbedtls_x509_crt_restart_ctx;
302
303#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
304
305/* Now we can declare functions that take a pointer to that */
306typedef void mbedtls_x509_crt_restart_ctx;
307
308#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200312 * Default security profile. Should provide a good balance between security
313 * and compatibility with current deployments.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200314 *
315 * This profile permits:
316 * - SHA2 hashes with at least 256 bits: SHA-256, SHA-384, SHA-512.
Gilles Peskine39957502021-06-17 23:17:52 +0200317 * - Elliptic curves with 255 bits and above except secp256k1.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200318 * - RSA with 2048 bits and above.
319 *
320 * New minor versions of Mbed TLS may extend this profile, for example if
Gilles Peskine39957502021-06-17 23:17:52 +0200321 * new algorithms are added to the library. New minor versions of Mbed TLS will
Gilles Peskineffb92da2021-06-02 00:03:26 +0200322 * not reduce this profile unless serious security concerns require it.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200323 */
324extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
325
326/**
327 * Expected next default profile. Recommended for new deployments.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200328 * Currently targets a 128-bit security level, except for allowing RSA-2048.
329 * This profile may change at any time.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200330 */
331extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
332
333/**
334 * NSA Suite B profile.
335 */
336extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
337
338/**
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200339 * Empty profile that allows nothing. Useful as a basis for constructing
340 * custom profiles.
341 */
342extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none;
343
344/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345 * \brief Parse a single DER formatted certificate and add it
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000346 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347 *
Janos Follath582ecd02024-11-19 16:17:07 +0000348 * \note The PSA crypto subsystem must have been initialized by
349 * calling psa_crypto_init() before calling this function.
Gilles Peskine5b7e1642022-08-04 23:44:59 +0200350 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000351 * \param chain The pointer to the start of the CRT chain to attach to.
352 * When parsing the first CRT in a chain, this should point
353 * to an instance of ::mbedtls_x509_crt initialized through
354 * mbedtls_x509_crt_init().
355 * \param buf The buffer holding the DER encoded certificate.
356 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000358 * \note This function makes an internal copy of the CRT buffer
359 * \p buf. In particular, \p buf may be destroyed or reused
360 * after this call returns. To avoid duplicating the CRT
361 * buffer (at the cost of stricter lifetime constraints),
362 * use mbedtls_x509_crt_parse_der_nocopy() instead.
363 *
364 * \return \c 0 if successful.
365 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
368 const unsigned char *buf,
369 size_t buflen);
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000370
371/**
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200372 * \brief The type of certificate extension callbacks.
373 *
374 * Callbacks of this type are passed to and used by the
Nicola Di Lietofde98f72020-05-28 08:34:33 +0200375 * mbedtls_x509_crt_parse_der_with_ext_cb() routine when
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200376 * it encounters either an unsupported extension or a
377 * "certificate policies" extension containing any
378 * unsupported certificate policies.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200379 * Future versions of the library may invoke the callback
380 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200381 *
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200382 * \param p_ctx An opaque context passed to the callback.
ndilieto6e249802020-05-28 06:17:38 +0000383 * \param crt The certificate being parsed.
384 * \param oid The OID of the extension.
385 * \param critical Whether the extension is critical.
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200386 * \param p Pointer to the start of the extension value
ndilieto6e249802020-05-28 06:17:38 +0000387 * (the content of the OCTET STRING).
ndilieto6e249802020-05-28 06:17:38 +0000388 * \param end End of extension value.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200389 *
390 * \note The callback must fail and return a negative error code
391 * if it can not parse or does not support the extension.
392 * When the callback fails to parse a critical extension
393 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
394 * When the callback fails to parse a non critical extension
395 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
396 * the extension and continues parsing.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200397 *
398 * \return \c 0 on success.
399 * \return A negative error code on failure.
400 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100401typedef int (*mbedtls_x509_crt_ext_cb_t)(void *p_ctx,
402 mbedtls_x509_crt const *crt,
403 mbedtls_x509_buf const *oid,
404 int critical,
405 const unsigned char *p,
406 const unsigned char *end);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200407
408/**
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200409 * \brief Parse a single DER formatted certificate and add it
410 * to the end of the provided chained list.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200411 *
Janos Follath582ecd02024-11-19 16:17:07 +0000412 * \note The PSA crypto subsystem must have been initialized by
413 * calling psa_crypto_init() before calling this function.
Gilles Peskine5b7e1642022-08-04 23:44:59 +0200414 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200415 * \param chain The pointer to the start of the CRT chain to attach to.
416 * When parsing the first CRT in a chain, this should point
417 * to an instance of ::mbedtls_x509_crt initialized through
418 * mbedtls_x509_crt_init().
419 * \param buf The buffer holding the DER encoded certificate.
420 * \param buflen The size in Bytes of \p buf.
421 * \param make_copy When not zero this function makes an internal copy of the
422 * CRT buffer \p buf. In particular, \p buf may be destroyed
423 * or reused after this call returns.
424 * When zero this function avoids duplicating the CRT buffer
425 * by taking temporary ownership thereof until the CRT
426 * is destroyed (like mbedtls_x509_crt_parse_der_nocopy())
427 * \param cb A callback invoked for every unsupported certificate
428 * extension.
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200429 * \param p_ctx An opaque context passed to the callback.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200430 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200431 * \note This call is functionally equivalent to
432 * mbedtls_x509_crt_parse_der(), and/or
433 * mbedtls_x509_crt_parse_der_nocopy()
434 * but it calls the callback with every unsupported
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200435 * certificate extension and additionally the
436 * "certificate policies" extension if it contains any
437 * unsupported certificate policies.
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200438 * The callback must return a negative error code if it
439 * does not know how to handle such an extension.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200440 * When the callback fails to parse a critical extension
441 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
442 * When the callback fails to parse a non critical extension
443 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
444 * the extension and continues parsing.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200445 * Future versions of the library may invoke the callback
446 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200447 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200448 * \return \c 0 if successful.
449 * \return A negative error code on failure.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200450 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100451int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
452 const unsigned char *buf,
453 size_t buflen,
454 int make_copy,
455 mbedtls_x509_crt_ext_cb_t cb,
456 void *p_ctx);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200457
458/**
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000459 * \brief Parse a single DER formatted certificate and add it
460 * to the end of the provided chained list. This is a
461 * variant of mbedtls_x509_crt_parse_der() which takes
462 * temporary ownership of the CRT buffer until the CRT
463 * is destroyed.
464 *
Janos Follath582ecd02024-11-19 16:17:07 +0000465 * \note The PSA crypto subsystem must have been initialized by
466 * calling psa_crypto_init() before calling this function.
Gilles Peskine5b7e1642022-08-04 23:44:59 +0200467 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000468 * \param chain The pointer to the start of the CRT chain to attach to.
469 * When parsing the first CRT in a chain, this should point
470 * to an instance of ::mbedtls_x509_crt initialized through
471 * mbedtls_x509_crt_init().
472 * \param buf The address of the readable buffer holding the DER encoded
473 * certificate to use. On success, this buffer must be
Jan Brucknerb33f6e52022-10-06 11:23:49 +0200474 * retained and not be changed for the lifetime of the
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000475 * CRT chain \p chain, that is, until \p chain is destroyed
476 * through a call to mbedtls_x509_crt_free().
477 * \param buflen The size in Bytes of \p buf.
478 *
479 * \note This call is functionally equivalent to
480 * mbedtls_x509_crt_parse_der(), but it avoids creating a
481 * copy of the input buffer at the cost of stronger lifetime
482 * constraints. This is useful in constrained environments
483 * where duplication of the CRT cannot be tolerated.
484 *
485 * \return \c 0 if successful.
486 * \return A negative error code on failure.
487 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100488int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
489 const unsigned char *buf,
490 size_t buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491
492/**
Hanno Becker89a91122018-08-23 16:14:00 +0100493 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100494 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100496 * For CRTs in PEM encoding, the function parses permissively:
497 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100498 * returns the number of certificates for which parsing failed
499 * (hence \c 0 if all certificates were parsed successfully).
500 * If no certificate could be parsed, the function returns
501 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100503 * PEM encoded certificates may be interleaved by other data
504 * such as human readable descriptions of their content, as
505 * long as the certificates are enclosed in the PEM specific
506 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
507 *
Janos Follath582ecd02024-11-19 16:17:07 +0000508 * \note The PSA crypto subsystem must have been initialized by
509 * calling psa_crypto_init() before calling this function.
Gilles Peskine5b7e1642022-08-04 23:44:59 +0200510 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100511 * \param chain The chain to which to add the parsed certificates.
512 * \param buf The buffer holding the certificate data in PEM or DER format.
513 * For certificates in PEM encoding, this may be a concatenation
514 * of multiple certificates; for DER encoding, the buffer must
515 * comprise exactly one certificate.
516 * \param buflen The size of \p buf, including the terminating \c NULL byte
517 * in case of PEM encoded data.
518 *
519 * \return \c 0 if all certificates were parsed successfully.
520 * \return The (positive) number of certificates that couldn't
521 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100522 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100523 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100525int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528/**
529 * \brief Load one or more certificates and add them
530 * to the chained list. Parses permissively. If some
531 * certificates can be parsed, the result is the number
532 * of failed certificates it encountered. If none complete
533 * correctly, the first error is returned.
534 *
Janos Follath582ecd02024-11-19 16:17:07 +0000535 * \note The PSA crypto subsystem must have been initialized by
536 * calling psa_crypto_init() before calling this function.
Gilles Peskine5b7e1642022-08-04 23:44:59 +0200537 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538 * \param chain points to the start of the chain
539 * \param path filename to read the certificates from
540 *
541 * \return 0 if all certificates parsed successfully, a positive number
542 * if partly successful or a specific X509 or PEM error code
543 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545
546/**
547 * \brief Load one or more certificate files from a path and add them
548 * to the chained list. Parses permissively. If some
549 * certificates can be parsed, the result is the number
550 * of failed certificates it encountered. If none complete
551 * correctly, the first error is returned.
552 *
553 * \param chain points to the start of the chain
554 * \param path directory / folder to read the certificate files from
555 *
556 * \return 0 if all certificates parsed successfully, a positive number
557 * if partly successful or a specific X509 or PEM error code
558 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100559int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path);
Janos Follath11b41eb2019-05-08 15:26:49 +0100560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#endif /* MBEDTLS_FS_IO */
toth92g8d435a02021-05-10 15:16:33 +0200562
Hanno Becker612a2f12020-10-09 09:19:39 +0100563#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564/**
565 * \brief Returns an informational string about the
566 * certificate.
567 *
568 * \param buf Buffer to write to
569 * \param size Maximum size of buffer
570 * \param prefix A line prefix
571 * \param crt The X509 certificate to represent
572 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200573 * \return The length of the string written (not including the
574 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100576int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
577 const mbedtls_x509_crt *crt);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578
579/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100580 * \brief Returns an informational string about the
581 * verification status of a certificate.
582 *
583 * \param buf Buffer to write to
584 * \param size Maximum size of buffer
585 * \param prefix A line prefix
586 * \param flags Verification flags created by mbedtls_x509_crt_verify()
587 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200588 * \return The length of the string written (not including the
589 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100590 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100591int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
592 uint32_t flags);
Hanno Becker88c2bf32019-06-14 17:21:24 +0100593#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200594
595/**
Hanno Becker902451d2019-03-27 11:48:40 +0000596 * \brief Verify a chain of certificates.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597 *
598 * The verify callback is a user-supplied callback that
599 * can clear / modify / add flags for a certificate. If set,
600 * the verification callback is called for each
601 * certificate in the chain (from the trust-ca down to the
602 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200604 * int *flags). With the flags representing current flags for
605 * that specific certificate and the certificate depth from
606 * the bottom (Peer cert depth = 0).
607 *
608 * All flags left after returning from the callback
609 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200610 * return 0 for anything (including invalid certificates)
611 * other than fatal error, as a non-zero return code
612 * immediately aborts the verification process. For fatal
613 * errors, a specific error code should be used (different
614 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
615 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
616 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100618 * \note In case verification failed, the results can be displayed
619 * using \c mbedtls_x509_crt_verify_info()
620 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200621 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
622 * default security profile.
623 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100624 * \note It is your responsibility to provide up-to-date CRLs for
625 * all trusted CAs. If no CRL is provided for the CA that was
626 * used to sign the certificate, CRL verification is skipped
627 * silently, that is *without* setting any flag.
628 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200629 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200630 * (1) those of trusted root CAs, so that certificates
631 * chaining up to those CAs will be trusted, and (2)
632 * self-signed end-entity certificates to be trusted (for
633 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200634 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200635 *
Hanno Becker902451d2019-03-27 11:48:40 +0000636 * \param crt The certificate chain to be verified.
637 * \param trust_ca The list of trusted CAs.
638 * \param ca_crl The list of CRLs for trusted CAs.
Manuel Pégourié-Gonnardf58e5cc2020-07-24 10:31:37 +0200639 * \param cn The expected Common Name. This will be checked to be
640 * present in the certificate's subjectAltNames extension or,
641 * if this extension is absent, as a CN component in its
Andrzej Kurek199eab92023-05-10 09:57:19 -0400642 * Subject name. DNS names and IP addresses are fully
643 * supported, while the URI subtype is partially supported:
644 * only exact matching, without any normalization procedures
645 * described in 7.4 of RFC5280, will result in a positive
646 * URI verification.
647 * This may be \c NULL if the CN need not be verified.
Hanno Becker902451d2019-03-27 11:48:40 +0000648 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100649 * If the verification couldn't be completed, the flag value is
650 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000651 * \param f_vrfy The verification callback to use. See the documentation
652 * of mbedtls_x509_crt_verify() for more information.
653 * \param p_vrfy The context to be passed to \p f_vrfy.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654 *
Hanno Becker902451d2019-03-27 11:48:40 +0000655 * \return \c 0 if the chain is valid with respect to the
656 * passed CN, CAs, CRLs and security profile.
657 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
658 * certificate chain verification failed. In this case,
659 * \c *flags will have one or more
660 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
661 * flags set.
662 * \return Another negative error code in case of a fatal error
663 * encountered during the verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
666 mbedtls_x509_crt *trust_ca,
667 mbedtls_x509_crl *ca_crl,
668 const char *cn, uint32_t *flags,
669 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
670 void *p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200672/**
Hanno Becker902451d2019-03-27 11:48:40 +0000673 * \brief Verify a chain of certificates with respect to
674 * a configurable security profile.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200675 *
676 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
677 * security profile.
678 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200679 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200680 * for ECDSA) apply to all certificates: trusted root,
681 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200682 *
Hanno Becker902451d2019-03-27 11:48:40 +0000683 * \param crt The certificate chain to be verified.
684 * \param trust_ca The list of trusted CAs.
685 * \param ca_crl The list of CRLs for trusted CAs.
686 * \param profile The security profile to use for the verification.
687 * \param cn The expected Common Name. This may be \c NULL if the
688 * CN need not be verified.
689 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100690 * If the verification couldn't be completed, the flag value is
691 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000692 * \param f_vrfy The verification callback to use. See the documentation
693 * of mbedtls_x509_crt_verify() for more information.
694 * \param p_vrfy The context to be passed to \p f_vrfy.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200695 *
Hanno Becker902451d2019-03-27 11:48:40 +0000696 * \return \c 0 if the chain is valid with respect to the
697 * passed CN, CAs, CRLs and security profile.
698 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
699 * certificate chain verification failed. In this case,
700 * \c *flags will have one or more
701 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
702 * flags set.
703 * \return Another negative error code in case of a fatal error
704 * encountered during the verification process.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200705 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
707 mbedtls_x509_crt *trust_ca,
708 mbedtls_x509_crl *ca_crl,
709 const mbedtls_x509_crt_profile *profile,
710 const char *cn, uint32_t *flags,
711 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
712 void *p_vrfy);
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200713
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200714/**
715 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
716 *
717 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
718 * but can return early and restart according to the limit
719 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
720 *
Hanno Becker902451d2019-03-27 11:48:40 +0000721 * \param crt The certificate chain to be verified.
722 * \param trust_ca The list of trusted CAs.
723 * \param ca_crl The list of CRLs for trusted CAs.
724 * \param profile The security profile to use for the verification.
725 * \param cn The expected Common Name. This may be \c NULL if the
726 * CN need not be verified.
727 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100728 * If the verification couldn't be completed, the flag value is
729 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000730 * \param f_vrfy The verification callback to use. See the documentation
731 * of mbedtls_x509_crt_verify() for more information.
732 * \param p_vrfy The context to be passed to \p f_vrfy.
733 * \param rs_ctx The restart context to use. This may be set to \c NULL
734 * to disable restartable ECC.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200735 *
736 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200737 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200738 * operations was reached: see \c mbedtls_ecp_set_max_ops().
739 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100740int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
741 mbedtls_x509_crt *trust_ca,
742 mbedtls_x509_crl *ca_crl,
743 const mbedtls_x509_crt_profile *profile,
744 const char *cn, uint32_t *flags,
745 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
746 void *p_vrfy,
747 mbedtls_x509_crt_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200748
Hanno Becker5c8df782019-03-27 11:01:17 +0000749/**
750 * \brief The type of trusted certificate callbacks.
751 *
752 * Callbacks of this type are passed to and used by the CRT
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300753 * verification routine mbedtls_x509_crt_verify_with_ca_cb()
Hanno Becker5c8df782019-03-27 11:01:17 +0000754 * when looking for trusted signers of a given certificate.
755 *
756 * On success, the callback returns a list of trusted
757 * certificates to be considered as potential signers
758 * for the input certificate.
759 *
760 * \param p_ctx An opaque context passed to the callback.
761 * \param child The certificate for which to search a potential signer.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300762 * This will point to a readable certificate.
Hanno Becker5c8df782019-03-27 11:01:17 +0000763 * \param candidate_cas The address at which to store the address of the first
764 * entry in the generated linked list of candidate signers.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300765 * This will not be \c NULL.
Hanno Becker5c8df782019-03-27 11:01:17 +0000766 *
767 * \note The callback must only return a non-zero value on a
768 * fatal error. If, in contrast, the search for a potential
769 * signer completes without a single candidate, the
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300770 * callback must return \c 0 and set \c *candidate_cas
Hanno Becker5c8df782019-03-27 11:01:17 +0000771 * to \c NULL.
772 *
773 * \return \c 0 on success. In this case, \c *candidate_cas points
774 * to a heap-allocated linked list of instances of
775 * ::mbedtls_x509_crt, and ownership of this list is passed
776 * to the caller.
777 * \return A negative error code on failure.
778 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100779typedef int (*mbedtls_x509_crt_ca_cb_t)(void *p_ctx,
780 mbedtls_x509_crt const *child,
781 mbedtls_x509_crt **candidate_cas);
Hanno Beckere15dae72019-03-28 13:55:38 +0000782
783#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker5c8df782019-03-27 11:01:17 +0000784/**
785 * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
786 * uses a callback to acquire the list of trusted CA
787 * certificates.
788 *
789 * \param crt The certificate chain to be verified.
790 * \param f_ca_cb The callback to be used to query for potential signers
791 * of a given child certificate. See the documentation of
792 * ::mbedtls_x509_crt_ca_cb_t for more information.
793 * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
794 * \param profile The security profile for the verification.
795 * \param cn The expected Common Name. This may be \c NULL if the
796 * CN need not be verified.
797 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100798 * If the verification couldn't be completed, the flag value is
799 * set to (uint32_t) -1.
Hanno Becker5c8df782019-03-27 11:01:17 +0000800 * \param f_vrfy The verification callback to use. See the documentation
801 * of mbedtls_x509_crt_verify() for more information.
802 * \param p_vrfy The context to be passed to \p f_vrfy.
803 *
804 * \return See \c mbedtls_crt_verify_with_profile().
805 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
807 mbedtls_x509_crt_ca_cb_t f_ca_cb,
808 void *p_ca_cb,
809 const mbedtls_x509_crt_profile *profile,
810 const char *cn, uint32_t *flags,
811 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
812 void *p_vrfy);
Hanno Becker5c8df782019-03-27 11:01:17 +0000813
814#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
815
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200816/**
817 * \brief Check usage of certificate against keyUsage extension.
818 *
819 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200820 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
821 * before using the certificate to perform an RSA key
822 * exchange).
823 *
824 * \note Except for decipherOnly and encipherOnly, a bit set in the
825 * usage argument means this bit MUST be set in the
826 * certificate. For decipherOnly and encipherOnly, it means
827 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200828 *
829 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200831 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200832 *
833 * \note You should only call this function on leaf certificates, on
834 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200836 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
838 unsigned int usage);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200839
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200840/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000841 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200842 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000843 * \param crt Leaf certificate used.
844 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
845 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200847 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000848 * \return 0 if this use of the certificate is allowed,
849 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200850 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000851 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200852 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
854 const char *usage_oid,
855 size_t usage_len);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200858/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200859 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200860 *
861 * \param crt a certificate to be verified
862 * \param crl the CRL to verify against
863 *
864 * \return 1 if the certificate is revoked, 0 otherwise
865 *
866 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100867int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869
870/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200871 * \brief Initialize a certificate (chain)
872 *
873 * \param crt Certificate chain to initialize
874 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875void mbedtls_x509_crt_init(mbedtls_x509_crt *crt);
Paul Bakker369d2eb2013-09-18 11:58:25 +0200876
877/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878 * \brief Unallocate all certificate data
879 *
880 * \param crt Certificate chain to free
881 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100882void mbedtls_x509_crt_free(mbedtls_x509_crt *crt);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200883
884#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
885/**
886 * \brief Initialize a restart context
887 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100888void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200889
890/**
891 * \brief Free the components of a restart context
892 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200894#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896
Thomas Daubneya5f39e02022-06-06 15:42:32 +0100897/**
898 * \brief Query certificate for given extension type
899 *
900 * \param[in] ctx Certificate context to be queried, must not be \c NULL
901 * \param ext_type Extension type being queried for, must be a valid
902 * extension type. Must be one of the MBEDTLS_X509_EXT_XXX
903 * values
904 *
905 * \return 0 if the given extension type is not present,
906 * non-zero otherwise
907 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908static inline int mbedtls_x509_crt_has_ext_type(const mbedtls_x509_crt *ctx,
909 int ext_type)
Thomas Daubneya5f39e02022-06-06 15:42:32 +0100910{
911 return ctx->MBEDTLS_PRIVATE(ext_types) & ext_type;
912}
913
Minos Galanakis2abbac72024-01-18 17:05:21 +0000914/**
915 * \brief Access the ca_istrue field
916 *
917 * \param[in] crt Certificate to be queried, must not be \c NULL
918 *
919 * \return \c 1 if this a CA certificate \c 0 otherwise.
Minos Galanakis87b4f6d2024-03-05 11:05:51 +0000920 * \return MBEDTLS_ERR_X509_INVALID_EXTENSIONS if the certificate does not contain
Minos Galanakis2abbac72024-01-18 17:05:21 +0000921 * the Optional Basic Constraint extension.
922 *
923 */
924int mbedtls_x509_crt_get_ca_istrue(const mbedtls_x509_crt *crt);
925
Andrzej Kurek38d4fdd2021-12-28 16:22:52 +0100926/** \} name Structures and functions for parsing and writing X.509 certificates */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929/**
930 * \brief Initialize a CRT writing context
931 *
932 * \param ctx CRT context to initialize
933 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100934void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935
936/**
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800937 * \brief Set the version for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939 *
940 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
942 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx, int version);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945
Valerio Settida0afcc2023-01-05 16:46:59 +0100946/**
947 * \brief Set the serial number for a Certificate.
948 *
Valerio Setti746def52023-01-10 11:46:34 +0100949 * \param ctx CRT context to use
950 * \param serial A raw array of bytes containing the serial number in big
951 * endian format
Valerio Setti9b5e1da2023-01-26 17:49:10 +0100952 * \param serial_len Length of valid bytes (expressed in bytes) in \p serial
953 * input buffer
Valerio Settida0afcc2023-01-05 16:46:59 +0100954 *
955 * \return 0 if successful, or
Valerio Settiacf12fb2023-01-09 17:19:26 +0100956 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the provided input buffer
957 * is too big (longer than MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN)
Valerio Settida0afcc2023-01-05 16:46:59 +0100958 */
Valerio Settiaf4815c2023-01-26 17:43:09 +0100959int mbedtls_x509write_crt_set_serial_raw(mbedtls_x509write_cert *ctx,
Valerio Setti746def52023-01-10 11:46:34 +0100960 unsigned char *serial, size_t serial_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961
962/**
963 * \brief Set the validity period for a Certificate
964 * Timestamps should be in string format for UTC timezone
965 * i.e. "YYYYMMDDhhmmss"
966 * e.g. "20131231235959" for December 31st 2013
967 * at 23:59:59
968 *
969 * \param ctx CRT context to use
970 * \param not_before not_before timestamp
971 * \param not_after not_after timestamp
972 *
973 * \return 0 if timestamp was parsed successfully, or
974 * a specific error code
975 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx, const char *not_before,
977 const char *not_after);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978
979/**
980 * \brief Set the issuer name for a Certificate
981 * Issuer names should contain a comma-separated list
982 * of OID types and values:
Gilles Peskinee820c0a2023-08-03 17:45:20 +0200983 * e.g. "C=UK,O=ARM,CN=Mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984 *
985 * \param ctx CRT context to use
986 * \param issuer_name issuer name to set
987 *
988 * \return 0 if issuer name was parsed successfully, or
989 * a specific error code
990 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx,
992 const char *issuer_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993
994/**
995 * \brief Set the subject name for a Certificate
996 * Subject names should contain a comma-separated list
997 * of OID types and values:
Gilles Peskinee820c0a2023-08-03 17:45:20 +0200998 * e.g. "C=UK,O=ARM,CN=Mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999 *
1000 * \param ctx CRT context to use
1001 * \param subject_name subject name to set
1002 *
1003 * \return 0 if subject name was parsed successfully, or
1004 * a specific error code
1005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001006int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx,
1007 const char *subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008
1009/**
1010 * \brief Set the subject public key for the certificate
1011 *
1012 * \param ctx CRT context to use
1013 * \param key public key to include
1014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx, mbedtls_pk_context *key);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016
1017/**
1018 * \brief Set the issuer key used for signing the certificate
1019 *
1020 * \param ctx CRT context to use
1021 * \param key private key to sign with
1022 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001023void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx, mbedtls_pk_context *key);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024
1025/**
1026 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 *
1029 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001030 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001032void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033
1034/**
1035 * \brief Generic function to add to or replace an extension in the
1036 * CRT
1037 *
1038 * \param ctx CRT context to use
1039 * \param oid OID of the extension
1040 * \param oid_len length of the OID
1041 * \param critical if the extension is critical (per the RFC's definition)
1042 * \param val value of the extension OCTET STRING
1043 * \param val_len length of the value data
1044 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001045 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
1048 const char *oid, size_t oid_len,
1049 int critical,
1050 const unsigned char *val, size_t val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051
1052/**
1053 * \brief Set the basicConstraints extension for a CRT
1054 *
1055 * \param ctx CRT context to use
1056 * \param is_ca is this a CA certificate
1057 * \param max_pathlen maximum length of certificate chains below this
1058 * certificate (only for CA certificates, -1 is
Tom Cosgrove1e211442022-05-26 11:51:00 +01001059 * unlimited)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001061 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
1064 int is_ca, int max_pathlen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065
Elena Uziunaite9fc5be02024-09-04 18:12:59 +01001066#if defined(PSA_WANT_ALG_SHA_1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067/**
1068 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070 * called before
1071 *
1072 * \param ctx CRT context to use
1073 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001074 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001076int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077
1078/**
1079 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081 * called before
1082 *
1083 * \param ctx CRT context to use
1084 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001085 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001087int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx);
Elena Uziunaite9fc5be02024-09-04 18:12:59 +01001088#endif /* PSA_WANT_ALG_SHA_1 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089
1090/**
1091 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093 *
1094 * \param ctx CRT context to use
1095 * \param key_usage key usage flags to set
1096 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001097 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
1100 unsigned int key_usage);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101
1102/**
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +01001103 * \brief Set the Extended Key Usage Extension
1104 * (e.g. MBEDTLS_OID_SERVER_AUTH)
1105 *
1106 * \param ctx CRT context to use
1107 * \param exts extended key usage extensions to set, a sequence of
1108 * MBEDTLS_ASN1_OID objects
1109 *
1110 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
1111 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001112int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx,
1113 const mbedtls_asn1_sequence *exts);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +01001114
1115/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001116 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118 *
1119 * \param ctx CRT context to use
1120 * \param ns_cert_type Netscape Cert Type flags to set
1121 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001122 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
1125 unsigned char ns_cert_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001126
1127/**
1128 * \brief Free the contents of a CRT write context
1129 *
1130 * \param ctx CRT context to free
1131 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001132void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001133
1134/**
1135 * \brief Write a built up certificate to a X509 DER structure
1136 * Note: data is written at the end of the buffer! Use the
1137 * return value to determine where you should start
1138 * using the buffer
1139 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001140 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001141 * \param buf buffer to write to
1142 * \param size size of the buffer
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001143 *
1144 * \return length of data written if successful, or a specific
1145 * error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001146 */
Ben Taylor440cb2a2025-03-05 09:40:08 +00001147int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150/**
1151 * \brief Write a built up certificate to a X509 PEM string
1152 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001153 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154 * \param buf buffer to write to
1155 * \param size size of the buffer
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001157 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001159 */
Ben Taylor440cb2a2025-03-05 09:40:08 +00001160int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#endif /* MBEDTLS_PEM_WRITE_C */
1162#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001163
Andrzej Kureka0defed2021-12-30 12:33:31 +01001164/** \} addtogroup x509_module */
1165
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166#ifdef __cplusplus
1167}
1168#endif
1169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#endif /* mbedtls_x509_crt.h */