blob: 63f2b406c8598af07df2ce2a720c874679d5caa9 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file x509_crt.h
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
4 * \brief X.509 certificate parsing and writing
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#ifndef MBEDTLS_X509_CRT_H
23#define MBEDTLS_X509_CRT_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020024#include "mbedtls/private_access.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027
Jaeden Amero6609aef2019-07-04 20:01:14 +010028#include "mbedtls/x509.h"
29#include "mbedtls/x509_crl.h"
30#include "mbedtls/bignum.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031
32/**
33 * \addtogroup x509_module
34 * \{
35 */
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/**
42 * \name Structures and functions for parsing and writing X.509 certificates
43 * \{
44 */
45
46/**
47 * Container for an X.509 certificate. The certificate may be chained.
48 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020049typedef struct mbedtls_x509_crt {
50 int MBEDTLS_PRIVATE(own_buffer); /**< Indicates if \c raw is owned
51 * by the structure or not. */
52 mbedtls_x509_buf MBEDTLS_PRIVATE(raw); /**< The raw certificate data (DER).
53 */
54 mbedtls_x509_buf MBEDTLS_PRIVATE(tbs); /**< The raw certificate body (DER).
55 The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020057 int MBEDTLS_PRIVATE(version); /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
58 mbedtls_x509_buf MBEDTLS_PRIVATE(serial); /**< Unique id for certificate
59 issued by a specific CA. */
60 mbedtls_x509_buf MBEDTLS_PRIVATE(sig_oid); /**< Signature algorithm, e.g.
61 sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020063 mbedtls_x509_buf MBEDTLS_PRIVATE(issuer_raw); /**< The raw issuer data
64 (DER). Used for quick
65 comparison. */
66 mbedtls_x509_buf MBEDTLS_PRIVATE(subject_raw); /**< The raw subject data
67 (DER). Used for quick
68 comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020070 mbedtls_x509_name MBEDTLS_PRIVATE(issuer); /**< The parsed issuer data
71 (named information object). */
72 mbedtls_x509_name MBEDTLS_PRIVATE(subject); /**< The parsed subject data
73 (named information object).
74 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020076 mbedtls_x509_time MBEDTLS_PRIVATE(valid_from); /**< Start time of
77 certificate validity. */
78 mbedtls_x509_time MBEDTLS_PRIVATE(valid_to); /**< End time of certificate
79 validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080
Mateusz Starzyk846f0212021-05-19 19:44:07 +020081 mbedtls_x509_buf MBEDTLS_PRIVATE(pk_raw);
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020082 mbedtls_pk_context MBEDTLS_PRIVATE(pk); /**< Container for the public key
83 context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020085 mbedtls_x509_buf MBEDTLS_PRIVATE(issuer_id); /**< Optional X.509 v2/v3
86 issuer unique identifier. */
87 mbedtls_x509_buf MBEDTLS_PRIVATE(subject_id); /**< Optional X.509 v2/v3
88 subject unique identifier.
89 */
90 mbedtls_x509_buf MBEDTLS_PRIVATE(v3_ext); /**< Optional X.509 v3 extensions.
91 */
92 mbedtls_x509_sequence MBEDTLS_PRIVATE(subject_alt_names); /**< Optional list
93 of raw entries
94 of Subject
95 Alternative
96 Names extension
97 (currently only
98 dNSName and
99 OtherName are
100 listed). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200102 mbedtls_x509_sequence MBEDTLS_PRIVATE(certificate_policies); /**< Optional
103 list of
104 certificate
105 policies
106 (Only
107 anyPolicy is
108 printed and
109 enforced,
110 however the
111 rest of the
112 policies are
113 still
114 listed). */
Ron Eldor74d9acc2019-03-21 14:00:03 +0200115
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200116 int MBEDTLS_PRIVATE(ext_types); /**< Bit string containing detected and
117 parsed extensions */
118 int MBEDTLS_PRIVATE(ca_istrue); /**< Optional Basic Constraint extension
119 value: 1 if this certificate belongs to a
120 CA, 0 otherwise. */
121 int MBEDTLS_PRIVATE(max_pathlen); /**< Optional Basic Constraint extension
122 value: The maximum path length to the
123 root certificate. Path length is 1
124 higher than RFC 5280 'meaning', so 1+
125 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200126
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200127 unsigned int MBEDTLS_PRIVATE(key_usage); /**< Optional key usage extension
128 value: See the values in x509.h
129 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200130
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200131 mbedtls_x509_sequence MBEDTLS_PRIVATE(ext_key_usage); /**< Optional list of
132 extended key usage
133 OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200134
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200135 unsigned char MBEDTLS_PRIVATE(ns_cert_type); /**< Optional Netscape
136 certificate type extension
137 value: See the values in
138 x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200139
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200140 mbedtls_x509_buf MBEDTLS_PRIVATE(sig); /**< Signature: hash of the tbs part
141 signed with the private key. */
142 mbedtls_md_type_t MBEDTLS_PRIVATE(sig_md); /**< Internal representation of
143 the MD algorithm of the
144 signature algorithm, e.g.
145 MBEDTLS_MD_SHA256 */
146 mbedtls_pk_type_t MBEDTLS_PRIVATE(sig_pk); /**< Internal representation of
147 the Public Key algorithm of
148 the signature algorithm, e.g.
149 MBEDTLS_PK_RSA */
150 void *MBEDTLS_PRIVATE(sig_opts); /**< Signature options to be passed to
151 mbedtls_pk_verify_ext(), e.g. for
152 RSASSA-PSS */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200154 struct mbedtls_x509_crt *MBEDTLS_PRIVATE(next); /**< Next certificate in the
155 CA-chain. */
156} mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200157
Janos Follath5091bec2019-05-08 15:23:08 +0100158/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200159 * From RFC 5280 section 4.2.1.6:
160 * OtherName ::= SEQUENCE {
161 * type-id OBJECT IDENTIFIER,
162 * value [0] EXPLICIT ANY DEFINED BY type-id }
163 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200164typedef struct mbedtls_x509_san_other_name {
Ron Eldor890819a2019-05-13 19:03:04 +0300165 /**
166 * The type_id is an OID as deifned in RFC 5280.
167 * To check the value of the type id, you should use
168 * \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf.
169 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200170 mbedtls_x509_buf MBEDTLS_PRIVATE(type_id); /**< The type id. */
171 union {
Janos Follath5091bec2019-05-08 15:23:08 +0100172 /**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200173 * From RFC 4108 section 5:
174 * HardwareModuleName ::= SEQUENCE {
175 * hwType OBJECT IDENTIFIER,
176 * hwSerialNum OCTET STRING }
177 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200178 struct {
179 mbedtls_x509_buf MBEDTLS_PRIVATE(oid); /**< The object identifier.
180 */
181 mbedtls_x509_buf MBEDTLS_PRIVATE(val); /**< The named value. */
182 } MBEDTLS_PRIVATE(hardware_module_name);
183 } MBEDTLS_PRIVATE(value);
184} mbedtls_x509_san_other_name;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200185
Janos Follath5091bec2019-05-08 15:23:08 +0100186/**
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200187 * A structure for holding the parsed Subject Alternative Name, according to
188 * type
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200189 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200190typedef struct mbedtls_x509_subject_alternative_name {
191 int MBEDTLS_PRIVATE(type); /**< The SAN type, value of MBEDTLS_X509_SAN_XXX.
192 */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200193 union {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200194 mbedtls_x509_san_other_name MBEDTLS_PRIVATE(other_name); /**< The
195 otherName
196 supported
197 type. */
198 mbedtls_x509_buf MBEDTLS_PRIVATE(unstructured_name); /**< The buffer for
199 the un
200 constructed
201 types. Only
202 dnsName
203 currently
204 supported */
205 } MBEDTLS_PRIVATE(san); /**< A union of the supported SAN types */
206} mbedtls_x509_subject_alternative_name;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200207
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200208/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200209 * Build flag from an algorithm/curve identifier (pk, md, ecp)
210 * Since 0 is always XXX_NONE, ignore it.
211 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200212#define MBEDTLS_X509_ID_FLAG(id) (1 << ((id)-1))
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200213
214/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200215 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200216 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200217 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard55a7fb82021-06-17 10:39:39 +0200218 *
219 * The fields of this structure are part of the public API and can be
220 * manipulated directly by applications. Future versions of the library may
221 * add extra fields or reorder existing fields.
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200222 *
223 * You can create custom profiles by starting from a copy of
224 * an existing profile, such as mbedtls_x509_crt_profile_default or
225 * mbedtls_x509_ctr_profile_none and then tune it to your needs.
226 *
227 * For example to allow SHA-224 in addition to the default:
228 *
229 * mbedtls_x509_crt_profile my_profile = mbedtls_x509_crt_profile_default;
230 * my_profile.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 );
231 *
232 * Or to allow only RSA-3072+ with SHA-256:
233 *
234 * mbedtls_x509_crt_profile my_profile = mbedtls_x509_crt_profile_none;
235 * my_profile.allowed_mds = MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 );
236 * my_profile.allowed_pks = MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA );
237 * my_profile.rsa_min_bitlen = 3072;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200238 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200239typedef struct mbedtls_x509_crt_profile {
240 uint32_t allowed_mds; /**< MDs for signatures */
241 uint32_t allowed_pks; /**< PK algs for signatures */
242 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
243 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
244} mbedtls_x509_crt_profile;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200245
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200246#define MBEDTLS_X509_CRT_VERSION_1 0
247#define MBEDTLS_X509_CRT_VERSION_2 1
248#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
251#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200252
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200253#if !defined(MBEDTLS_X509_MAX_FILE_PATH_LEN)
254# define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
Andres AGf9113192016-09-02 14:06:04 +0100255#endif
256
Hanno Becker7ac83f92020-10-09 10:48:22 +0100257/* This macro unfolds to the concatenation of macro invocations
258 * X509_CRT_ERROR_INFO( error code,
259 * error code as string,
260 * human readable description )
261 * where X509_CRT_ERROR_INFO is defined by the user.
262 * See x509_crt.c for an example of how to use this. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200263#define MBEDTLS_X509_CRT_ERROR_INFO_LIST \
264 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_EXPIRED, \
265 "MBEDTLS_X509_BADCERT_EXPIRED", \
266 "The certificate validity has expired") \
267 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_REVOKED, \
268 "MBEDTLS_X509_BADCERT_REVOKED", \
269 "The certificate has been revoked (is on a CRL)") \
270 X509_CRT_ERROR_INFO( \
271 MBEDTLS_X509_BADCERT_CN_MISMATCH, "MBEDTLS_X509_BADCERT_CN_MISMATCH", \
272 "The certificate Common Name (CN) does not match with the expected CN") \
273 X509_CRT_ERROR_INFO( \
274 MBEDTLS_X509_BADCERT_NOT_TRUSTED, "MBEDTLS_X509_BADCERT_NOT_TRUSTED", \
275 "The certificate is not correctly signed by the trusted CA") \
276 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_NOT_TRUSTED, \
277 "MBEDTLS_X509_BADCRL_NOT_TRUSTED", \
278 "The CRL is not correctly signed by the trusted CA") \
279 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_EXPIRED, \
280 "MBEDTLS_X509_BADCRL_EXPIRED", "The CRL is expired") \
281 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_MISSING, \
282 "MBEDTLS_X509_BADCERT_MISSING", \
283 "Certificate was missing") \
284 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_SKIP_VERIFY, \
285 "MBEDTLS_X509_BADCERT_SKIP_VERIFY", \
286 "Certificate verification was skipped") \
287 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_OTHER, \
288 "MBEDTLS_X509_BADCERT_OTHER", \
289 "Other reason (can be used by verify callback)") \
290 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_FUTURE, \
291 "MBEDTLS_X509_BADCERT_FUTURE", \
292 "The certificate validity starts in the future") \
293 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_FUTURE, \
294 "MBEDTLS_X509_BADCRL_FUTURE", \
295 "The CRL is from the future") \
296 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_KEY_USAGE, \
297 "MBEDTLS_X509_BADCERT_KEY_USAGE", \
298 "Usage does not match the keyUsage extension") \
299 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, \
300 "MBEDTLS_X509_BADCERT_EXT_KEY_USAGE", \
301 "Usage does not match the extendedKeyUsage extension") \
302 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_NS_CERT_TYPE, \
303 "MBEDTLS_X509_BADCERT_NS_CERT_TYPE", \
304 "Usage does not match the nsCertType extension") \
305 X509_CRT_ERROR_INFO( \
306 MBEDTLS_X509_BADCERT_BAD_MD, "MBEDTLS_X509_BADCERT_BAD_MD", \
307 "The certificate is signed with an unacceptable hash.") \
308 X509_CRT_ERROR_INFO( \
309 MBEDTLS_X509_BADCERT_BAD_PK, "MBEDTLS_X509_BADCERT_BAD_PK", \
310 "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA).") \
311 X509_CRT_ERROR_INFO( \
312 MBEDTLS_X509_BADCERT_BAD_KEY, "MBEDTLS_X509_BADCERT_BAD_KEY", \
313 "The certificate is signed with an unacceptable key (eg bad curve, RSA too short).") \
314 X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_BAD_MD, \
315 "MBEDTLS_X509_BADCRL_BAD_MD", \
316 "The CRL is signed with an unacceptable hash.") \
317 X509_CRT_ERROR_INFO( \
318 MBEDTLS_X509_BADCRL_BAD_PK, "MBEDTLS_X509_BADCRL_BAD_PK", \
319 "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA).") \
320 X509_CRT_ERROR_INFO( \
321 MBEDTLS_X509_BADCRL_BAD_KEY, "MBEDTLS_X509_BADCRL_BAD_KEY", \
322 "The CRL is signed with an unacceptable key (eg bad curve, RSA too short).")
Hanno Becker7ac83f92020-10-09 10:48:22 +0100323
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200324/**
325 * Container for writing a certificate (CRT)
326 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200327typedef struct mbedtls_x509write_cert {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200328 int MBEDTLS_PRIVATE(version);
329 mbedtls_mpi MBEDTLS_PRIVATE(serial);
330 mbedtls_pk_context *MBEDTLS_PRIVATE(subject_key);
331 mbedtls_pk_context *MBEDTLS_PRIVATE(issuer_key);
332 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(subject);
333 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(issuer);
334 mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg);
335 char MBEDTLS_PRIVATE(not_before)[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
336 char MBEDTLS_PRIVATE(not_after)[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
337 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(extensions);
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200338} mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200340/**
341 * Item in a verification chain: cert and flags for it
342 */
343typedef struct {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200344 mbedtls_x509_crt *MBEDTLS_PRIVATE(crt);
345 uint32_t MBEDTLS_PRIVATE(flags);
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200346} mbedtls_x509_crt_verify_chain_item;
347
348/**
349 * Max size of verification chain: end-entity + intermediates + trusted root
350 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200351#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE \
352 (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200353
354/**
355 * Verification chain as built by \c mbedtls_crt_verify_chain()
356 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200357typedef struct {
358 mbedtls_x509_crt_verify_chain_item MBEDTLS_PRIVATE(items)
359 [MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200360 unsigned MBEDTLS_PRIVATE(len);
Hanno Beckerf53893b2019-03-28 13:45:55 +0000361
362#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
363 /* This stores the list of potential trusted signers obtained from
364 * the CA callback used for the CRT verification, if configured.
365 * We must track it somewhere because the callback passes its
366 * ownership to the caller. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200367 mbedtls_x509_crt *MBEDTLS_PRIVATE(trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +0000368#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200369} mbedtls_x509_crt_verify_chain;
370
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200371#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
372
373/**
374 * \brief Context for resuming X.509 verify operations
375 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200376typedef struct {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200377 /* for check_signature() */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200378 mbedtls_pk_restart_ctx MBEDTLS_PRIVATE(pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200379
380 /* for find_parent_in() */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200381 mbedtls_x509_crt *MBEDTLS_PRIVATE(parent); /* non-null iff parent_in in
382 progress */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200383 mbedtls_x509_crt *MBEDTLS_PRIVATE(fallback_parent);
384 int MBEDTLS_PRIVATE(fallback_signature_is_good);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200385
386 /* for find_parent() */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200387 int MBEDTLS_PRIVATE(parent_is_trusted); /* -1 if find_parent is not in
388 progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200389
390 /* for verify_chain() */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200391 enum
392 {
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200393 x509_crt_rs_none,
394 x509_crt_rs_find_parent,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200395 } MBEDTLS_PRIVATE(in_progress); /* none if no operation is in progress */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200396 int MBEDTLS_PRIVATE(self_cnt);
397 mbedtls_x509_crt_verify_chain MBEDTLS_PRIVATE(ver_chain);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200398
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200399} mbedtls_x509_crt_restart_ctx;
400
401#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
402
403/* Now we can declare functions that take a pointer to that */
404typedef void mbedtls_x509_crt_restart_ctx;
405
406#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200410 * Default security profile. Should provide a good balance between security
411 * and compatibility with current deployments.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200412 *
413 * This profile permits:
414 * - SHA2 hashes with at least 256 bits: SHA-256, SHA-384, SHA-512.
Gilles Peskine39957502021-06-17 23:17:52 +0200415 * - Elliptic curves with 255 bits and above except secp256k1.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200416 * - RSA with 2048 bits and above.
417 *
418 * New minor versions of Mbed TLS may extend this profile, for example if
Gilles Peskine39957502021-06-17 23:17:52 +0200419 * new algorithms are added to the library. New minor versions of Mbed TLS will
Gilles Peskineffb92da2021-06-02 00:03:26 +0200420 * not reduce this profile unless serious security concerns require it.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200421 */
422extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
423
424/**
425 * Expected next default profile. Recommended for new deployments.
Gilles Peskineffb92da2021-06-02 00:03:26 +0200426 * Currently targets a 128-bit security level, except for allowing RSA-2048.
427 * This profile may change at any time.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200428 */
429extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
430
431/**
432 * NSA Suite B profile.
433 */
434extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
435
436/**
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200437 * Empty profile that allows nothing. Useful as a basis for constructing
438 * custom profiles.
439 */
440extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none;
441
442/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443 * \brief Parse a single DER formatted certificate and add it
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000444 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000446 * \param chain The pointer to the start of the CRT chain to attach to.
447 * When parsing the first CRT in a chain, this should point
448 * to an instance of ::mbedtls_x509_crt initialized through
449 * mbedtls_x509_crt_init().
450 * \param buf The buffer holding the DER encoded certificate.
451 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000453 * \note This function makes an internal copy of the CRT buffer
454 * \p buf. In particular, \p buf may be destroyed or reused
455 * after this call returns. To avoid duplicating the CRT
456 * buffer (at the cost of stricter lifetime constraints),
457 * use mbedtls_x509_crt_parse_der_nocopy() instead.
458 *
459 * \return \c 0 if successful.
460 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200462int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
463 const unsigned char *buf,
464 size_t buflen);
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000465
466/**
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200467 * \brief The type of certificate extension callbacks.
468 *
469 * Callbacks of this type are passed to and used by the
Nicola Di Lietofde98f72020-05-28 08:34:33 +0200470 * mbedtls_x509_crt_parse_der_with_ext_cb() routine when
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200471 * it encounters either an unsupported extension or a
472 * "certificate policies" extension containing any
473 * unsupported certificate policies.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200474 * Future versions of the library may invoke the callback
475 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200476 *
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200477 * \param p_ctx An opaque context passed to the callback.
ndilieto6e249802020-05-28 06:17:38 +0000478 * \param crt The certificate being parsed.
479 * \param oid The OID of the extension.
480 * \param critical Whether the extension is critical.
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200481 * \param p Pointer to the start of the extension value
ndilieto6e249802020-05-28 06:17:38 +0000482 * (the content of the OCTET STRING).
ndilieto6e249802020-05-28 06:17:38 +0000483 * \param end End of extension value.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200484 *
485 * \note The callback must fail and return a negative error code
486 * if it can not parse or does not support the extension.
487 * When the callback fails to parse a critical extension
488 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
489 * When the callback fails to parse a non critical extension
490 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
491 * the extension and continues parsing.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200492 *
493 * \return \c 0 on success.
494 * \return A negative error code on failure.
495 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200496typedef int (*mbedtls_x509_crt_ext_cb_t)(void *p_ctx,
497 mbedtls_x509_crt const *crt,
498 mbedtls_x509_buf const *oid,
499 int critical,
500 const unsigned char *p,
501 const unsigned char *end);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200502
503/**
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200504 * \brief Parse a single DER formatted certificate and add it
505 * to the end of the provided chained list.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200506 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200507 * \param chain The pointer to the start of the CRT chain to attach to.
508 * When parsing the first CRT in a chain, this should point
509 * to an instance of ::mbedtls_x509_crt initialized through
510 * mbedtls_x509_crt_init().
511 * \param buf The buffer holding the DER encoded certificate.
512 * \param buflen The size in Bytes of \p buf.
513 * \param make_copy When not zero this function makes an internal copy of the
514 * CRT buffer \p buf. In particular, \p buf may be destroyed
515 * or reused after this call returns.
516 * When zero this function avoids duplicating the CRT buffer
517 * by taking temporary ownership thereof until the CRT
518 * is destroyed (like mbedtls_x509_crt_parse_der_nocopy())
519 * \param cb A callback invoked for every unsupported certificate
520 * extension.
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200521 * \param p_ctx An opaque context passed to the callback.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200522 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200523 * \note This call is functionally equivalent to
524 * mbedtls_x509_crt_parse_der(), and/or
525 * mbedtls_x509_crt_parse_der_nocopy()
526 * but it calls the callback with every unsupported
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200527 * certificate extension and additionally the
528 * "certificate policies" extension if it contains any
529 * unsupported certificate policies.
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200530 * The callback must return a negative error code if it
531 * does not know how to handle such an extension.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200532 * When the callback fails to parse a critical extension
533 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
534 * When the callback fails to parse a non critical extension
535 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
536 * the extension and continues parsing.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200537 * Future versions of the library may invoke the callback
538 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200539 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200540 * \return \c 0 if successful.
541 * \return A negative error code on failure.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200542 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200543int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
544 const unsigned char *buf,
545 size_t buflen,
546 int make_copy,
547 mbedtls_x509_crt_ext_cb_t cb,
548 void *p_ctx);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200549
550/**
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000551 * \brief Parse a single DER formatted certificate and add it
552 * to the end of the provided chained list. This is a
553 * variant of mbedtls_x509_crt_parse_der() which takes
554 * temporary ownership of the CRT buffer until the CRT
555 * is destroyed.
556 *
557 * \param chain The pointer to the start of the CRT chain to attach to.
558 * When parsing the first CRT in a chain, this should point
559 * to an instance of ::mbedtls_x509_crt initialized through
560 * mbedtls_x509_crt_init().
561 * \param buf The address of the readable buffer holding the DER encoded
562 * certificate to use. On success, this buffer must be
563 * retained and not be changed for the liftetime of the
564 * CRT chain \p chain, that is, until \p chain is destroyed
565 * through a call to mbedtls_x509_crt_free().
566 * \param buflen The size in Bytes of \p buf.
567 *
568 * \note This call is functionally equivalent to
569 * mbedtls_x509_crt_parse_der(), but it avoids creating a
570 * copy of the input buffer at the cost of stronger lifetime
571 * constraints. This is useful in constrained environments
572 * where duplication of the CRT cannot be tolerated.
573 *
574 * \return \c 0 if successful.
575 * \return A negative error code on failure.
576 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200577int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
578 const unsigned char *buf,
579 size_t buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580
581/**
Hanno Becker89a91122018-08-23 16:14:00 +0100582 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100583 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100585 * For CRTs in PEM encoding, the function parses permissively:
586 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100587 * returns the number of certificates for which parsing failed
588 * (hence \c 0 if all certificates were parsed successfully).
589 * If no certificate could be parsed, the function returns
590 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100592 * PEM encoded certificates may be interleaved by other data
593 * such as human readable descriptions of their content, as
594 * long as the certificates are enclosed in the PEM specific
595 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
596 *
597 * \param chain The chain to which to add the parsed certificates.
598 * \param buf The buffer holding the certificate data in PEM or DER format.
599 * For certificates in PEM encoding, this may be a concatenation
600 * of multiple certificates; for DER encoding, the buffer must
601 * comprise exactly one certificate.
602 * \param buflen The size of \p buf, including the terminating \c NULL byte
603 * in case of PEM encoded data.
604 *
605 * \return \c 0 if all certificates were parsed successfully.
606 * \return The (positive) number of certificates that couldn't
607 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100608 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100609 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200611int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
612 const unsigned char *buf,
613 size_t buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200615# if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200616/**
617 * \brief Load one or more certificates and add them
618 * to the chained list. Parses permissively. If some
619 * certificates can be parsed, the result is the number
620 * of failed certificates it encountered. If none complete
621 * correctly, the first error is returned.
622 *
623 * \param chain points to the start of the chain
624 * \param path filename to read the certificates from
625 *
626 * \return 0 if all certificates parsed successfully, a positive number
627 * if partly successful or a specific X509 or PEM error code
628 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200629int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630
631/**
632 * \brief Load one or more certificate files from a path and add them
633 * to the chained list. Parses permissively. If some
634 * certificates can be parsed, the result is the number
635 * of failed certificates it encountered. If none complete
636 * correctly, the first error is returned.
637 *
638 * \param chain points to the start of the chain
639 * \param path directory / folder to read the certificate files from
640 *
641 * \return 0 if all certificates parsed successfully, a positive number
642 * if partly successful or a specific X509 or PEM error code
643 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200644int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path);
Janos Follath11b41eb2019-05-08 15:26:49 +0100645
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200646# endif /* MBEDTLS_FS_IO */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200647/**
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300648 * \brief This function parses an item in the SubjectAlternativeNames
649 * extension.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200650 *
Ron Eldor890819a2019-05-13 19:03:04 +0300651 * \param san_buf The buffer holding the raw data item of the subject
652 * alternative name.
653 * \param san The target structure to populate with the parsed presentation
654 * of the subject alternative name encoded in \p san_raw.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200655 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300656 * \note Only "dnsName" and "otherName" of type hardware_module_name
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200657 * as defined in RFC 4180 is supported.
658 *
Ron Eldor890819a2019-05-13 19:03:04 +0300659 * \note This function should be called on a single raw data of
Ron Eldorcc45cd12019-05-15 10:20:09 +0300660 * subject alternative name. For example, after successful
Ron Eldor890819a2019-05-13 19:03:04 +0300661 * certificate parsing, one must iterate on every item in the
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300662 * \p crt->subject_alt_names sequence, and pass it to
663 * this function.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200664 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300665 * \warning The target structure contains pointers to the raw data of the
Ron Eldorcc45cd12019-05-15 10:20:09 +0300666 * parsed certificate, and its lifetime is restricted by the
667 * lifetime of the certificate.
668 *
Ron Eldor890819a2019-05-13 19:03:04 +0300669 * \return \c 0 on success
670 * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300671 * SAN type.
672 * \return Another negative value for any other failure.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200673 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200674int mbedtls_x509_parse_subject_alt_name(
675 const mbedtls_x509_buf *san_buf,
676 mbedtls_x509_subject_alternative_name *san);
Peter Kolbus9a969b62018-12-11 13:55:56 -0600677
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200678# if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200679/**
680 * \brief Returns an informational string about the
681 * certificate.
682 *
683 * \param buf Buffer to write to
684 * \param size Maximum size of buffer
685 * \param prefix A line prefix
686 * \param crt The X509 certificate to represent
687 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200688 * \return The length of the string written (not including the
689 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200691int mbedtls_x509_crt_info(char *buf,
692 size_t size,
693 const char *prefix,
694 const mbedtls_x509_crt *crt);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695
696/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100697 * \brief Returns an informational string about the
698 * verification status of a certificate.
699 *
700 * \param buf Buffer to write to
701 * \param size Maximum size of buffer
702 * \param prefix A line prefix
703 * \param flags Verification flags created by mbedtls_x509_crt_verify()
704 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200705 * \return The length of the string written (not including the
706 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100707 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200708int mbedtls_x509_crt_verify_info(char *buf,
709 size_t size,
710 const char *prefix,
711 uint32_t flags);
712# endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200713
714/**
Hanno Becker902451d2019-03-27 11:48:40 +0000715 * \brief Verify a chain of certificates.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716 *
717 * The verify callback is a user-supplied callback that
718 * can clear / modify / add flags for a certificate. If set,
719 * the verification callback is called for each
720 * certificate in the chain (from the trust-ca down to the
721 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200723 * int *flags). With the flags representing current flags for
724 * that specific certificate and the certificate depth from
725 * the bottom (Peer cert depth = 0).
726 *
727 * All flags left after returning from the callback
728 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200729 * return 0 for anything (including invalid certificates)
730 * other than fatal error, as a non-zero return code
731 * immediately aborts the verification process. For fatal
732 * errors, a specific error code should be used (different
733 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
734 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
735 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100737 * \note In case verification failed, the results can be displayed
738 * using \c mbedtls_x509_crt_verify_info()
739 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200740 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
741 * default security profile.
742 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100743 * \note It is your responsibility to provide up-to-date CRLs for
744 * all trusted CAs. If no CRL is provided for the CA that was
745 * used to sign the certificate, CRL verification is skipped
746 * silently, that is *without* setting any flag.
747 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200748 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200749 * (1) those of trusted root CAs, so that certificates
750 * chaining up to those CAs will be trusted, and (2)
751 * self-signed end-entity certificates to be trusted (for
752 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200753 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200754 *
Hanno Becker902451d2019-03-27 11:48:40 +0000755 * \param crt The certificate chain to be verified.
756 * \param trust_ca The list of trusted CAs.
757 * \param ca_crl The list of CRLs for trusted CAs.
Manuel Pégourié-Gonnardf58e5cc2020-07-24 10:31:37 +0200758 * \param cn The expected Common Name. This will be checked to be
759 * present in the certificate's subjectAltNames extension or,
760 * if this extension is absent, as a CN component in its
761 * Subject name. Currently only DNS names are supported. This
762 * may be \c NULL if the CN need not be verified.
Hanno Becker902451d2019-03-27 11:48:40 +0000763 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100764 * If the verification couldn't be completed, the flag value is
765 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000766 * \param f_vrfy The verification callback to use. See the documentation
767 * of mbedtls_x509_crt_verify() for more information.
768 * \param p_vrfy The context to be passed to \p f_vrfy.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 *
Hanno Becker902451d2019-03-27 11:48:40 +0000770 * \return \c 0 if the chain is valid with respect to the
771 * passed CN, CAs, CRLs and security profile.
772 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
773 * certificate chain verification failed. In this case,
774 * \c *flags will have one or more
775 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
776 * flags set.
777 * \return Another negative error code in case of a fatal error
778 * encountered during the verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200780int mbedtls_x509_crt_verify(
781 mbedtls_x509_crt *crt,
782 mbedtls_x509_crt *trust_ca,
783 mbedtls_x509_crl *ca_crl,
784 const char *cn,
785 uint32_t *flags,
786 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
787 void *p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200788
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200789/**
Hanno Becker902451d2019-03-27 11:48:40 +0000790 * \brief Verify a chain of certificates with respect to
791 * a configurable security profile.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200792 *
793 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
794 * security profile.
795 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200796 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200797 * for ECDSA) apply to all certificates: trusted root,
798 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200799 *
Hanno Becker902451d2019-03-27 11:48:40 +0000800 * \param crt The certificate chain to be verified.
801 * \param trust_ca The list of trusted CAs.
802 * \param ca_crl The list of CRLs for trusted CAs.
803 * \param profile The security profile to use for the verification.
804 * \param cn The expected Common Name. This may be \c NULL if the
805 * CN need not be verified.
806 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100807 * If the verification couldn't be completed, the flag value is
808 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000809 * \param f_vrfy The verification callback to use. See the documentation
810 * of mbedtls_x509_crt_verify() for more information.
811 * \param p_vrfy The context to be passed to \p f_vrfy.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200812 *
Hanno Becker902451d2019-03-27 11:48:40 +0000813 * \return \c 0 if the chain is valid with respect to the
814 * passed CN, CAs, CRLs and security profile.
815 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
816 * certificate chain verification failed. In this case,
817 * \c *flags will have one or more
818 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
819 * flags set.
820 * \return Another negative error code in case of a fatal error
821 * encountered during the verification process.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200822 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200823int mbedtls_x509_crt_verify_with_profile(
824 mbedtls_x509_crt *crt,
825 mbedtls_x509_crt *trust_ca,
826 mbedtls_x509_crl *ca_crl,
827 const mbedtls_x509_crt_profile *profile,
828 const char *cn,
829 uint32_t *flags,
830 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
831 void *p_vrfy);
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200832
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200833/**
834 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
835 *
836 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
837 * but can return early and restart according to the limit
838 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
839 *
Hanno Becker902451d2019-03-27 11:48:40 +0000840 * \param crt The certificate chain to be verified.
841 * \param trust_ca The list of trusted CAs.
842 * \param ca_crl The list of CRLs for trusted CAs.
843 * \param profile The security profile to use for the verification.
844 * \param cn The expected Common Name. This may be \c NULL if the
845 * CN need not be verified.
846 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100847 * If the verification couldn't be completed, the flag value is
848 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000849 * \param f_vrfy The verification callback to use. See the documentation
850 * of mbedtls_x509_crt_verify() for more information.
851 * \param p_vrfy The context to be passed to \p f_vrfy.
852 * \param rs_ctx The restart context to use. This may be set to \c NULL
853 * to disable restartable ECC.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200854 *
855 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200856 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200857 * operations was reached: see \c mbedtls_ecp_set_max_ops().
858 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200859int mbedtls_x509_crt_verify_restartable(
860 mbedtls_x509_crt *crt,
861 mbedtls_x509_crt *trust_ca,
862 mbedtls_x509_crl *ca_crl,
863 const mbedtls_x509_crt_profile *profile,
864 const char *cn,
865 uint32_t *flags,
866 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
867 void *p_vrfy,
868 mbedtls_x509_crt_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200869
Hanno Becker5c8df782019-03-27 11:01:17 +0000870/**
871 * \brief The type of trusted certificate callbacks.
872 *
873 * Callbacks of this type are passed to and used by the CRT
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300874 * verification routine mbedtls_x509_crt_verify_with_ca_cb()
Hanno Becker5c8df782019-03-27 11:01:17 +0000875 * when looking for trusted signers of a given certificate.
876 *
877 * On success, the callback returns a list of trusted
878 * certificates to be considered as potential signers
879 * for the input certificate.
880 *
881 * \param p_ctx An opaque context passed to the callback.
882 * \param child The certificate for which to search a potential signer.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300883 * This will point to a readable certificate.
Hanno Becker5c8df782019-03-27 11:01:17 +0000884 * \param candidate_cas The address at which to store the address of the first
885 * entry in the generated linked list of candidate signers.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300886 * This will not be \c NULL.
Hanno Becker5c8df782019-03-27 11:01:17 +0000887 *
888 * \note The callback must only return a non-zero value on a
889 * fatal error. If, in contrast, the search for a potential
890 * signer completes without a single candidate, the
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300891 * callback must return \c 0 and set \c *candidate_cas
Hanno Becker5c8df782019-03-27 11:01:17 +0000892 * to \c NULL.
893 *
894 * \return \c 0 on success. In this case, \c *candidate_cas points
895 * to a heap-allocated linked list of instances of
896 * ::mbedtls_x509_crt, and ownership of this list is passed
897 * to the caller.
898 * \return A negative error code on failure.
899 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200900typedef int (*mbedtls_x509_crt_ca_cb_t)(void *p_ctx,
901 mbedtls_x509_crt const *child,
902 mbedtls_x509_crt **candidate_cas);
Hanno Beckere15dae72019-03-28 13:55:38 +0000903
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200904# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker5c8df782019-03-27 11:01:17 +0000905/**
906 * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
907 * uses a callback to acquire the list of trusted CA
908 * certificates.
909 *
910 * \param crt The certificate chain to be verified.
911 * \param f_ca_cb The callback to be used to query for potential signers
912 * of a given child certificate. See the documentation of
913 * ::mbedtls_x509_crt_ca_cb_t for more information.
914 * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
915 * \param profile The security profile for the verification.
916 * \param cn The expected Common Name. This may be \c NULL if the
917 * CN need not be verified.
918 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100919 * If the verification couldn't be completed, the flag value is
920 * set to (uint32_t) -1.
Hanno Becker5c8df782019-03-27 11:01:17 +0000921 * \param f_vrfy The verification callback to use. See the documentation
922 * of mbedtls_x509_crt_verify() for more information.
923 * \param p_vrfy The context to be passed to \p f_vrfy.
924 *
925 * \return See \c mbedtls_crt_verify_with_profile().
926 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200927int mbedtls_x509_crt_verify_with_ca_cb(
928 mbedtls_x509_crt *crt,
929 mbedtls_x509_crt_ca_cb_t f_ca_cb,
930 void *p_ca_cb,
931 const mbedtls_x509_crt_profile *profile,
932 const char *cn,
933 uint32_t *flags,
934 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
935 void *p_vrfy);
Hanno Becker5c8df782019-03-27 11:01:17 +0000936
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200937# endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Hanno Becker5c8df782019-03-27 11:01:17 +0000938
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200939/**
940 * \brief Check usage of certificate against keyUsage extension.
941 *
942 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200943 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
944 * before using the certificate to perform an RSA key
945 * exchange).
946 *
947 * \note Except for decipherOnly and encipherOnly, a bit set in the
948 * usage argument means this bit MUST be set in the
949 * certificate. For decipherOnly and encipherOnly, it means
950 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200951 *
952 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200954 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200955 *
956 * \note You should only call this function on leaf certificates, on
957 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200959 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200960int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
961 unsigned int usage);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200962
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200963/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000964 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200965 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000966 * \param crt Leaf certificate used.
967 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
968 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200970 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000971 * \return 0 if this use of the certificate is allowed,
972 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200973 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000974 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200975 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200976int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
977 const char *usage_oid,
978 size_t usage_len);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200979
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200980# if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200982 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983 *
984 * \param crt a certificate to be verified
985 * \param crl the CRL to verify against
986 *
987 * \return 1 if the certificate is revoked, 0 otherwise
988 *
989 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200990int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt,
991 const mbedtls_x509_crl *crl);
992# endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993
994/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200995 * \brief Initialize a certificate (chain)
996 *
997 * \param crt Certificate chain to initialize
998 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200999void mbedtls_x509_crt_init(mbedtls_x509_crt *crt);
Paul Bakker369d2eb2013-09-18 11:58:25 +02001000
1001/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 * \brief Unallocate all certificate data
1003 *
1004 * \param crt Certificate chain to free
1005 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001006void mbedtls_x509_crt_free(mbedtls_x509_crt *crt);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02001007
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001008# if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02001009/**
1010 * \brief Initialize a restart context
1011 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001012void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02001013
1014/**
1015 * \brief Free the components of a restart context
1016 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001017void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx);
1018# endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020
1021/* \} name */
1022/* \} addtogroup x509_module */
1023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025/**
1026 * \brief Initialize a CRT writing context
1027 *
1028 * \param ctx CRT context to initialize
1029 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001030void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031
1032/**
1033 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035 *
1036 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
1038 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001040void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx,
1041 int version);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042
1043/**
1044 * \brief Set the serial number for a Certificate.
1045 *
1046 * \param ctx CRT context to use
1047 * \param serial serial number to set
1048 *
1049 * \return 0 if successful
1050 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001051int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert *ctx,
1052 const mbedtls_mpi *serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053
1054/**
1055 * \brief Set the validity period for a Certificate
1056 * Timestamps should be in string format for UTC timezone
1057 * i.e. "YYYYMMDDhhmmss"
1058 * e.g. "20131231235959" for December 31st 2013
1059 * at 23:59:59
1060 *
1061 * \param ctx CRT context to use
1062 * \param not_before not_before timestamp
1063 * \param not_after not_after timestamp
1064 *
1065 * \return 0 if timestamp was parsed successfully, or
1066 * a specific error code
1067 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001068int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx,
1069 const char *not_before,
1070 const char *not_after);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071
1072/**
1073 * \brief Set the issuer name for a Certificate
1074 * Issuer names should contain a comma-separated list
1075 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001076 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077 *
1078 * \param ctx CRT context to use
1079 * \param issuer_name issuer name to set
1080 *
1081 * \return 0 if issuer name was parsed successfully, or
1082 * a specific error code
1083 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001084int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx,
1085 const char *issuer_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086
1087/**
1088 * \brief Set the subject name for a Certificate
1089 * Subject names should contain a comma-separated list
1090 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00001091 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092 *
1093 * \param ctx CRT context to use
1094 * \param subject_name subject name to set
1095 *
1096 * \return 0 if subject name was parsed successfully, or
1097 * a specific error code
1098 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001099int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx,
1100 const char *subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101
1102/**
1103 * \brief Set the subject public key for the certificate
1104 *
1105 * \param ctx CRT context to use
1106 * \param key public key to include
1107 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001108void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx,
1109 mbedtls_pk_context *key);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110
1111/**
1112 * \brief Set the issuer key used for signing the certificate
1113 *
1114 * \param ctx CRT context to use
1115 * \param key private key to sign with
1116 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001117void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx,
1118 mbedtls_pk_context *key);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119
1120/**
1121 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123 *
1124 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001125 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001126 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001127void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx,
1128 mbedtls_md_type_t md_alg);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129
1130/**
1131 * \brief Generic function to add to or replace an extension in the
1132 * CRT
1133 *
1134 * \param ctx CRT context to use
1135 * \param oid OID of the extension
1136 * \param oid_len length of the OID
1137 * \param critical if the extension is critical (per the RFC's definition)
1138 * \param val value of the extension OCTET STRING
1139 * \param val_len length of the value data
1140 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001141 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001143int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
1144 const char *oid,
1145 size_t oid_len,
1146 int critical,
1147 const unsigned char *val,
1148 size_t val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149
1150/**
1151 * \brief Set the basicConstraints extension for a CRT
1152 *
1153 * \param ctx CRT context to use
1154 * \param is_ca is this a CA certificate
1155 * \param max_pathlen maximum length of certificate chains below this
1156 * certificate (only for CA certificates, -1 is
1157 * inlimited)
1158 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001159 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001161int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
1162 int is_ca,
1163 int max_pathlen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001165# if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166/**
1167 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169 * called before
1170 *
1171 * \param ctx CRT context to use
1172 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001173 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001175int mbedtls_x509write_crt_set_subject_key_identifier(
1176 mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001177
1178/**
1179 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181 * called before
1182 *
1183 * \param ctx CRT context to use
1184 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001185 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001187int mbedtls_x509write_crt_set_authority_key_identifier(
1188 mbedtls_x509write_cert *ctx);
1189# endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001190
1191/**
1192 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 *
1195 * \param ctx CRT context to use
1196 * \param key_usage key usage flags to set
1197 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001198 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001199 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001200int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
1201 unsigned int key_usage);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001202
1203/**
1204 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206 *
1207 * \param ctx CRT context to use
1208 * \param ns_cert_type Netscape Cert Type flags to set
1209 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001210 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001211 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001212int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
1213 unsigned char ns_cert_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001214
1215/**
1216 * \brief Free the contents of a CRT write context
1217 *
1218 * \param ctx CRT context to free
1219 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001220void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001221
1222/**
1223 * \brief Write a built up certificate to a X509 DER structure
1224 * Note: data is written at the end of the buffer! Use the
1225 * return value to determine where you should start
1226 * using the buffer
1227 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001228 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229 * \param buf buffer to write to
1230 * \param size size of the buffer
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001231 * \param f_rng RNG function. This must not be \c NULL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001232 * \param p_rng RNG parameter
1233 *
1234 * \return length of data written if successful, or a specific
1235 * error code
1236 *
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001237 * \note \p f_rng is used for the signature operation.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001238 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001239int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
1240 unsigned char *buf,
1241 size_t size,
1242 int (*f_rng)(void *, unsigned char *, size_t),
1243 void *p_rng);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001244
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001245# if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001246/**
1247 * \brief Write a built up certificate to a X509 PEM string
1248 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001249 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001250 * \param buf buffer to write to
1251 * \param size size of the buffer
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001252 * \param f_rng RNG function. This must not be \c NULL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001253 * \param p_rng RNG parameter
1254 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001255 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001256 *
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +02001257 * \note \p f_rng is used for the signature operation.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001258 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001259int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *ctx,
1260 unsigned char *buf,
1261 size_t size,
1262 int (*f_rng)(void *, unsigned char *, size_t),
1263 void *p_rng);
1264# endif /* MBEDTLS_PEM_WRITE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001266
1267#ifdef __cplusplus
1268}
1269#endif
1270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#endif /* mbedtls_x509_crt.h */