blob: 6906585c007992e83b5771b29591bdb63ad73ad0 [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
Paul Bakker7c6b2c32013-09-16 13:49:26 +020024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amero6609aef2019-07-04 20:01:14 +010026#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030
Jaeden Amero6609aef2019-07-04 20:01:14 +010031#include "mbedtls/x509.h"
32#include "mbedtls/x509_crl.h"
33#include "mbedtls/bignum.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034
35/**
36 * \addtogroup x509_module
37 * \{
38 */
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/**
45 * \name Structures and functions for parsing and writing X.509 certificates
46 * \{
47 */
48
49/**
50 * Container for an X.509 certificate. The certificate may be chained.
51 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053{
Hanno Becker1a65dcd2019-01-31 08:57:44 +000054 int own_buffer; /**< Indicates if \c raw is owned
55 * by the structure or not. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
57 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020059 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
61 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
64 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
67 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
70 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071
Hanno Becker494dd7a2019-02-06 16:13:41 +000072 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
76 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
77 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
Ron Eldor78c30402019-05-13 15:49:53 +030078 mbedtls_x509_sequence subject_alt_names; /**< Optional list of raw entries of Subject Alternative Names extension (currently only dNSName and OtherName are listed). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079
Ron Eldorc8b5f3f2019-05-15 15:15:55 +030080 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 +020081
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082 int ext_types; /**< Bit string containing detected and parsed extensions */
83 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
84 int 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+ */
85
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +010086 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020090 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
93 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
94 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
95 void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100
Janos Follath5091bec2019-05-08 15:23:08 +0100101/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200102 * From RFC 5280 section 4.2.1.6:
103 * OtherName ::= SEQUENCE {
104 * type-id OBJECT IDENTIFIER,
105 * value [0] EXPLICIT ANY DEFINED BY type-id }
106 */
107typedef struct mbedtls_x509_san_other_name
108{
Ron Eldor890819a2019-05-13 19:03:04 +0300109 /**
110 * The type_id is an OID as deifned in RFC 5280.
111 * To check the value of the type id, you should use
112 * \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf.
113 */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200114 mbedtls_x509_buf type_id; /**< The type id. */
115 union
116 {
Janos Follath5091bec2019-05-08 15:23:08 +0100117 /**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200118 * From RFC 4108 section 5:
119 * HardwareModuleName ::= SEQUENCE {
120 * hwType OBJECT IDENTIFIER,
121 * hwSerialNum OCTET STRING }
122 */
Ron Eldorf05f5942019-05-13 19:11:31 +0300123 struct
124 {
125 mbedtls_x509_buf oid; /**< The object identifier. */
126 mbedtls_x509_buf val; /**< The named value. */
127 }
128 hardware_module_name;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200129 }
130 value;
131}
132mbedtls_x509_san_other_name;
133
Janos Follath5091bec2019-05-08 15:23:08 +0100134/**
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200135 * A structure for holding the parsed Subject Alternative Name, according to type
136 */
137typedef struct mbedtls_x509_subject_alternative_name
138{
139 int type; /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */
140 union {
141 mbedtls_x509_san_other_name other_name; /**< The otherName supported type. */
142 mbedtls_x509_buf unstructured_name; /**< The buffer for the un constructed types. Only dnsName currently supported */
143 }
144 san; /**< A union of the supported SAN types */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200145}
146mbedtls_x509_subject_alternative_name;
147
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200148/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200149 * Build flag from an algorithm/curve identifier (pk, md, ecp)
150 * Since 0 is always XXX_NONE, ignore it.
151 */
Hanno Becker1eeca412018-10-15 12:01:35 +0100152#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200153
154/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200155 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200156 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200157 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200158 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200159typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200160{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200161 uint32_t allowed_mds; /**< MDs for signatures */
162 uint32_t allowed_pks; /**< PK algs for signatures */
163 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
164 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200165}
166mbedtls_x509_crt_profile;
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#define MBEDTLS_X509_CRT_VERSION_1 0
169#define MBEDTLS_X509_CRT_VERSION_2 1
170#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
173#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200174
Andres AGf9113192016-09-02 14:06:04 +0100175#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
176#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
177#endif
178
Hanno Becker7ac83f92020-10-09 10:48:22 +0100179/* This macro unfolds to the concatenation of macro invocations
180 * X509_CRT_ERROR_INFO( error code,
181 * error code as string,
182 * human readable description )
183 * where X509_CRT_ERROR_INFO is defined by the user.
184 * See x509_crt.c for an example of how to use this. */
185#define MBEDTLS_X509_CRT_ERROR_INFO_LIST \
186 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_EXPIRED, \
187 "MBEDTLS_X509_BADCERT_EXPIRED", \
188 "The certificate validity has expired" ) \
189 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_REVOKED, \
190 "MBEDTLS_X509_BADCERT_REVOKED", \
191 "The certificate has been revoked (is on a CRL)" ) \
192 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_CN_MISMATCH, \
193 "MBEDTLS_X509_BADCERT_CN_MISMATCH", \
194 "The certificate Common Name (CN) does not match with the expected CN" ) \
195 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_NOT_TRUSTED, \
196 "MBEDTLS_X509_BADCERT_NOT_TRUSTED", \
197 "The certificate is not correctly signed by the trusted CA" ) \
198 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_NOT_TRUSTED, \
199 "MBEDTLS_X509_BADCRL_NOT_TRUSTED", \
200 "The CRL is not correctly signed by the trusted CA" ) \
201 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_EXPIRED, \
202 "MBEDTLS_X509_BADCRL_EXPIRED", \
203 "The CRL is expired" ) \
204 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_MISSING, \
205 "MBEDTLS_X509_BADCERT_MISSING", \
206 "Certificate was missing" ) \
207 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_SKIP_VERIFY, \
208 "MBEDTLS_X509_BADCERT_SKIP_VERIFY", \
209 "Certificate verification was skipped" ) \
210 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_OTHER, \
211 "MBEDTLS_X509_BADCERT_OTHER", \
212 "Other reason (can be used by verify callback)" ) \
213 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_FUTURE, \
214 "MBEDTLS_X509_BADCERT_FUTURE", \
215 "The certificate validity starts in the future" ) \
216 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_FUTURE, \
217 "MBEDTLS_X509_BADCRL_FUTURE", \
218 "The CRL is from the future" ) \
219 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_KEY_USAGE, \
220 "MBEDTLS_X509_BADCERT_KEY_USAGE", \
221 "Usage does not match the keyUsage extension" ) \
222 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, \
223 "MBEDTLS_X509_BADCERT_EXT_KEY_USAGE", \
224 "Usage does not match the extendedKeyUsage extension" ) \
225 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_NS_CERT_TYPE, \
226 "MBEDTLS_X509_BADCERT_NS_CERT_TYPE", \
227 "Usage does not match the nsCertType extension" ) \
228 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_BAD_MD, \
229 "MBEDTLS_X509_BADCERT_BAD_MD", \
230 "The certificate is signed with an unacceptable hash." ) \
231 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_BAD_PK, \
232 "MBEDTLS_X509_BADCERT_BAD_PK", \
233 "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." ) \
234 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCERT_BAD_KEY, \
235 "MBEDTLS_X509_BADCERT_BAD_KEY", \
236 "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." ) \
237 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_BAD_MD, \
238 "MBEDTLS_X509_BADCRL_BAD_MD", \
239 "The CRL is signed with an unacceptable hash." ) \
240 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_BAD_PK, \
241 "MBEDTLS_X509_BADCRL_BAD_PK", \
242 "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." ) \
243 X509_CRT_ERROR_INFO( MBEDTLS_X509_BADCRL_BAD_KEY, \
244 "MBEDTLS_X509_BADCRL_BAD_KEY", \
245 "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." )
246
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247/**
248 * Container for writing a certificate (CRT)
249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200251{
252 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_mpi serial;
254 mbedtls_pk_context *subject_key;
255 mbedtls_pk_context *issuer_key;
256 mbedtls_asn1_named_data *subject;
257 mbedtls_asn1_named_data *issuer;
258 mbedtls_md_type_t md_alg;
259 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
260 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
261 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200262}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200265/**
266 * Item in a verification chain: cert and flags for it
267 */
268typedef struct {
269 mbedtls_x509_crt *crt;
270 uint32_t flags;
271} mbedtls_x509_crt_verify_chain_item;
272
273/**
274 * Max size of verification chain: end-entity + intermediates + trusted root
275 */
276#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
277
278/**
279 * Verification chain as built by \c mbedtls_crt_verify_chain()
280 */
281typedef struct
282{
283 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200284 unsigned len;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000285
286#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
287 /* This stores the list of potential trusted signers obtained from
288 * the CA callback used for the CRT verification, if configured.
289 * We must track it somewhere because the callback passes its
290 * ownership to the caller. */
291 mbedtls_x509_crt *trust_ca_cb_result;
292#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200293} mbedtls_x509_crt_verify_chain;
294
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200295#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
296
297/**
298 * \brief Context for resuming X.509 verify operations
299 */
300typedef struct
301{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200302 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200303 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200304
305 /* for find_parent_in() */
306 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
307 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200308 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200309
310 /* for find_parent() */
311 int parent_is_trusted; /* -1 if find_parent is not in progress */
312
313 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200314 enum {
315 x509_crt_rs_none,
316 x509_crt_rs_find_parent,
317 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200318 int self_cnt;
319 mbedtls_x509_crt_verify_chain ver_chain;
320
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200321} mbedtls_x509_crt_restart_ctx;
322
323#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
324
325/* Now we can declare functions that take a pointer to that */
326typedef void mbedtls_x509_crt_restart_ctx;
327
328#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200331/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200332 * Default security profile. Should provide a good balance between security
333 * and compatibility with current deployments.
334 */
335extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
336
337/**
338 * Expected next default profile. Recommended for new deployments.
339 * Currently targets a 128-bit security level, except for RSA-2048.
340 */
341extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
342
343/**
344 * NSA Suite B profile.
345 */
346extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
347
348/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349 * \brief Parse a single DER formatted certificate and add it
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000350 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000352 * \param chain The pointer to the start of the CRT chain to attach to.
353 * When parsing the first CRT in a chain, this should point
354 * to an instance of ::mbedtls_x509_crt initialized through
355 * mbedtls_x509_crt_init().
356 * \param buf The buffer holding the DER encoded certificate.
357 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000359 * \note This function makes an internal copy of the CRT buffer
360 * \p buf. In particular, \p buf may be destroyed or reused
361 * after this call returns. To avoid duplicating the CRT
362 * buffer (at the cost of stricter lifetime constraints),
363 * use mbedtls_x509_crt_parse_der_nocopy() instead.
364 *
365 * \return \c 0 if successful.
366 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000368int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
369 const unsigned char *buf,
370 size_t buflen );
371
372/**
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200373 * \brief The type of certificate extension callbacks.
374 *
375 * Callbacks of this type are passed to and used by the
Nicola Di Lietofde98f72020-05-28 08:34:33 +0200376 * mbedtls_x509_crt_parse_der_with_ext_cb() routine when
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200377 * it encounters either an unsupported extension or a
378 * "certificate policies" extension containing any
379 * unsupported certificate policies.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200380 * Future versions of the library may invoke the callback
381 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200382 *
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200383 * \param p_ctx An opaque context passed to the callback.
ndilieto6e249802020-05-28 06:17:38 +0000384 * \param crt The certificate being parsed.
385 * \param oid The OID of the extension.
386 * \param critical Whether the extension is critical.
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200387 * \param p Pointer to the start of the extension value
ndilieto6e249802020-05-28 06:17:38 +0000388 * (the content of the OCTET STRING).
ndilieto6e249802020-05-28 06:17:38 +0000389 * \param end End of extension value.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200390 *
391 * \note The callback must fail and return a negative error code
392 * if it can not parse or does not support the extension.
393 * When the callback fails to parse a critical extension
394 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
395 * When the callback fails to parse a non critical extension
396 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
397 * the extension and continues parsing.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200398 *
399 * \return \c 0 on success.
400 * \return A negative error code on failure.
401 */
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200402typedef int (*mbedtls_x509_crt_ext_cb_t)( void *p_ctx,
403 mbedtls_x509_crt const *crt,
ndilieto6e249802020-05-28 06:17:38 +0000404 mbedtls_x509_buf const *oid,
405 int critical,
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200406 const unsigned char *p,
ndilieto6e249802020-05-28 06:17:38 +0000407 const unsigned char *end );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200408
409/**
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200410 * \brief Parse a single DER formatted certificate and add it
411 * to the end of the provided chained list.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200412 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200413 * \param chain The pointer to the start of the CRT chain to attach to.
414 * When parsing the first CRT in a chain, this should point
415 * to an instance of ::mbedtls_x509_crt initialized through
416 * mbedtls_x509_crt_init().
417 * \param buf The buffer holding the DER encoded certificate.
418 * \param buflen The size in Bytes of \p buf.
419 * \param make_copy When not zero this function makes an internal copy of the
420 * CRT buffer \p buf. In particular, \p buf may be destroyed
421 * or reused after this call returns.
422 * When zero this function avoids duplicating the CRT buffer
423 * by taking temporary ownership thereof until the CRT
424 * is destroyed (like mbedtls_x509_crt_parse_der_nocopy())
425 * \param cb A callback invoked for every unsupported certificate
426 * extension.
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200427 * \param p_ctx An opaque context passed to the callback.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200428 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200429 * \note This call is functionally equivalent to
430 * mbedtls_x509_crt_parse_der(), and/or
431 * mbedtls_x509_crt_parse_der_nocopy()
432 * but it calls the callback with every unsupported
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200433 * certificate extension and additionally the
434 * "certificate policies" extension if it contains any
435 * unsupported certificate policies.
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200436 * The callback must return a negative error code if it
437 * does not know how to handle such an extension.
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200438 * When the callback fails to parse a critical extension
439 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
440 * When the callback fails to parse a non critical extension
441 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
442 * the extension and continues parsing.
Nicola Di Lieto511bc8c2020-06-23 00:15:28 +0200443 * Future versions of the library may invoke the callback
444 * in other cases, if and when the need arises.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200445 *
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +0200446 * \return \c 0 if successful.
447 * \return A negative error code on failure.
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200448 */
Nicola Di Lietofde98f72020-05-28 08:34:33 +0200449int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
450 const unsigned char *buf,
451 size_t buflen,
Nicola Di Lieto5f6ebde2020-05-28 19:00:47 +0200452 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200453 mbedtls_x509_crt_ext_cb_t cb,
454 void *p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200455
456/**
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000457 * \brief Parse a single DER formatted certificate and add it
458 * to the end of the provided chained list. This is a
459 * variant of mbedtls_x509_crt_parse_der() which takes
460 * temporary ownership of the CRT buffer until the CRT
461 * is destroyed.
462 *
463 * \param chain The pointer to the start of the CRT chain to attach to.
464 * When parsing the first CRT in a chain, this should point
465 * to an instance of ::mbedtls_x509_crt initialized through
466 * mbedtls_x509_crt_init().
467 * \param buf The address of the readable buffer holding the DER encoded
468 * certificate to use. On success, this buffer must be
469 * retained and not be changed for the liftetime of the
470 * CRT chain \p chain, that is, until \p chain is destroyed
471 * through a call to mbedtls_x509_crt_free().
472 * \param buflen The size in Bytes of \p buf.
473 *
474 * \note This call is functionally equivalent to
475 * mbedtls_x509_crt_parse_der(), but it avoids creating a
476 * copy of the input buffer at the cost of stronger lifetime
477 * constraints. This is useful in constrained environments
478 * where duplication of the CRT cannot be tolerated.
479 *
480 * \return \c 0 if successful.
481 * \return A negative error code on failure.
482 */
483int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
484 const unsigned char *buf,
485 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486
487/**
Hanno Becker89a91122018-08-23 16:14:00 +0100488 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100489 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100491 * For CRTs in PEM encoding, the function parses permissively:
492 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100493 * returns the number of certificates for which parsing failed
494 * (hence \c 0 if all certificates were parsed successfully).
495 * If no certificate could be parsed, the function returns
496 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100498 * PEM encoded certificates may be interleaved by other data
499 * such as human readable descriptions of their content, as
500 * long as the certificates are enclosed in the PEM specific
501 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
502 *
503 * \param chain The chain to which to add the parsed certificates.
504 * \param buf The buffer holding the certificate data in PEM or DER format.
505 * For certificates in PEM encoding, this may be a concatenation
506 * of multiple certificates; for DER encoding, the buffer must
507 * comprise exactly one certificate.
508 * \param buflen The size of \p buf, including the terminating \c NULL byte
509 * in case of PEM encoded data.
510 *
511 * \return \c 0 if all certificates were parsed successfully.
512 * \return The (positive) number of certificates that couldn't
513 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100514 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100515 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520/**
521 * \brief Load one or more certificates and add them
522 * to the chained list. Parses permissively. If some
523 * certificates can be parsed, the result is the number
524 * of failed certificates it encountered. If none complete
525 * correctly, the first error is returned.
526 *
527 * \param chain points to the start of the chain
528 * \param path filename to read the certificates from
529 *
530 * \return 0 if all certificates parsed successfully, a positive number
531 * if partly successful or a specific X509 or PEM error code
532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534
535/**
536 * \brief Load one or more certificate files from a path and add them
537 * to the chained list. Parses permissively. If some
538 * certificates can be parsed, the result is the number
539 * of failed certificates it encountered. If none complete
540 * correctly, the first error is returned.
541 *
542 * \param chain points to the start of the chain
543 * \param path directory / folder to read the certificate files from
544 *
545 * \return 0 if all certificates parsed successfully, a positive number
546 * if partly successful or a specific X509 or PEM error code
547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
Janos Follath11b41eb2019-05-08 15:26:49 +0100549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550#endif /* MBEDTLS_FS_IO */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200551/**
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300552 * \brief This function parses an item in the SubjectAlternativeNames
553 * extension.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200554 *
Ron Eldor890819a2019-05-13 19:03:04 +0300555 * \param san_buf The buffer holding the raw data item of the subject
556 * alternative name.
557 * \param san The target structure to populate with the parsed presentation
558 * of the subject alternative name encoded in \p san_raw.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200559 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300560 * \note Only "dnsName" and "otherName" of type hardware_module_name
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200561 * as defined in RFC 4180 is supported.
562 *
Ron Eldor890819a2019-05-13 19:03:04 +0300563 * \note This function should be called on a single raw data of
Ron Eldorcc45cd12019-05-15 10:20:09 +0300564 * subject alternative name. For example, after successful
Ron Eldor890819a2019-05-13 19:03:04 +0300565 * certificate parsing, one must iterate on every item in the
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300566 * \p crt->subject_alt_names sequence, and pass it to
567 * this function.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200568 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300569 * \warning The target structure contains pointers to the raw data of the
Ron Eldorcc45cd12019-05-15 10:20:09 +0300570 * parsed certificate, and its lifetime is restricted by the
571 * lifetime of the certificate.
572 *
Ron Eldor890819a2019-05-13 19:03:04 +0300573 * \return \c 0 on success
574 * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300575 * SAN type.
576 * \return Another negative value for any other failure.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200577 */
Ron Eldor890819a2019-05-13 19:03:04 +0300578int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
579 mbedtls_x509_subject_alternative_name *san );
Peter Kolbus9a969b62018-12-11 13:55:56 -0600580
Hanno Becker612a2f12020-10-09 09:19:39 +0100581#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582/**
583 * \brief Returns an informational string about the
584 * certificate.
585 *
586 * \param buf Buffer to write to
587 * \param size Maximum size of buffer
588 * \param prefix A line prefix
589 * \param crt The X509 certificate to represent
590 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200591 * \return The length of the string written (not including the
592 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
595 const mbedtls_x509_crt *crt );
Peter Kolbus9a969b62018-12-11 13:55:56 -0600596#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597
Hanno Becker612a2f12020-10-09 09:19:39 +0100598#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100600 * \brief Returns an informational string about the
601 * verification status of a certificate.
602 *
603 * \param buf Buffer to write to
604 * \param size Maximum size of buffer
605 * \param prefix A line prefix
606 * \param flags Verification flags created by mbedtls_x509_crt_verify()
607 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200608 * \return The length of the string written (not including the
609 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100610 */
611int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200612 uint32_t flags );
Peter Kolbus9a969b62018-12-11 13:55:56 -0600613#endif
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200614
615/**
Hanno Becker902451d2019-03-27 11:48:40 +0000616 * \brief Verify a chain of certificates.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617 *
618 * The verify callback is a user-supplied callback that
619 * can clear / modify / add flags for a certificate. If set,
620 * the verification callback is called for each
621 * certificate in the chain (from the trust-ca down to the
622 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624 * int *flags). With the flags representing current flags for
625 * that specific certificate and the certificate depth from
626 * the bottom (Peer cert depth = 0).
627 *
628 * All flags left after returning from the callback
629 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200630 * return 0 for anything (including invalid certificates)
631 * other than fatal error, as a non-zero return code
632 * immediately aborts the verification process. For fatal
633 * errors, a specific error code should be used (different
634 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
635 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
636 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100638 * \note In case verification failed, the results can be displayed
639 * using \c mbedtls_x509_crt_verify_info()
640 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200641 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
642 * default security profile.
643 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100644 * \note It is your responsibility to provide up-to-date CRLs for
645 * all trusted CAs. If no CRL is provided for the CA that was
646 * used to sign the certificate, CRL verification is skipped
647 * silently, that is *without* setting any flag.
648 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200649 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200650 * (1) those of trusted root CAs, so that certificates
651 * chaining up to those CAs will be trusted, and (2)
652 * self-signed end-entity certificates to be trusted (for
653 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200654 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200655 *
Hanno Becker902451d2019-03-27 11:48:40 +0000656 * \param crt The certificate chain to be verified.
657 * \param trust_ca The list of trusted CAs.
658 * \param ca_crl The list of CRLs for trusted CAs.
Manuel Pégourié-Gonnardf58e5cc2020-07-24 10:31:37 +0200659 * \param cn The expected Common Name. This will be checked to be
660 * present in the certificate's subjectAltNames extension or,
661 * if this extension is absent, as a CN component in its
662 * Subject name. Currently only DNS names are supported. This
663 * may be \c NULL if the CN need not be verified.
Hanno Becker902451d2019-03-27 11:48:40 +0000664 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100665 * If the verification couldn't be completed, the flag value is
666 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000667 * \param f_vrfy The verification callback to use. See the documentation
668 * of mbedtls_x509_crt_verify() for more information.
669 * \param p_vrfy The context to be passed to \p f_vrfy.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670 *
Hanno Becker902451d2019-03-27 11:48:40 +0000671 * \return \c 0 if the chain is valid with respect to the
672 * passed CN, CAs, CRLs and security profile.
673 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
674 * certificate chain verification failed. In this case,
675 * \c *flags will have one or more
676 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
677 * flags set.
678 * \return Another negative error code in case of a fatal error
679 * encountered during the verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
682 mbedtls_x509_crt *trust_ca,
683 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200684 const char *cn, uint32_t *flags,
685 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200686 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200688/**
Hanno Becker902451d2019-03-27 11:48:40 +0000689 * \brief Verify a chain of certificates with respect to
690 * a configurable security profile.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200691 *
692 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
693 * security profile.
694 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200695 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200696 * for ECDSA) apply to all certificates: trusted root,
697 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200698 *
Hanno Becker902451d2019-03-27 11:48:40 +0000699 * \param crt The certificate chain to be verified.
700 * \param trust_ca The list of trusted CAs.
701 * \param ca_crl The list of CRLs for trusted CAs.
702 * \param profile The security profile to use for the verification.
703 * \param cn The expected Common Name. This may be \c NULL if the
704 * CN need not be verified.
705 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100706 * If the verification couldn't be completed, the flag value is
707 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000708 * \param f_vrfy The verification callback to use. See the documentation
709 * of mbedtls_x509_crt_verify() for more information.
710 * \param p_vrfy The context to be passed to \p f_vrfy.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200711 *
Hanno Becker902451d2019-03-27 11:48:40 +0000712 * \return \c 0 if the chain is valid with respect to the
713 * passed CN, CAs, CRLs and security profile.
714 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
715 * certificate chain verification failed. In this case,
716 * \c *flags will have one or more
717 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
718 * flags set.
719 * \return Another negative error code in case of a fatal error
720 * encountered during the verification process.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200721 */
722int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
723 mbedtls_x509_crt *trust_ca,
724 mbedtls_x509_crl *ca_crl,
725 const mbedtls_x509_crt_profile *profile,
726 const char *cn, uint32_t *flags,
727 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
728 void *p_vrfy );
729
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200730/**
731 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
732 *
733 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
734 * but can return early and restart according to the limit
735 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
736 *
Hanno Becker902451d2019-03-27 11:48:40 +0000737 * \param crt The certificate chain to be verified.
738 * \param trust_ca The list of trusted CAs.
739 * \param ca_crl The list of CRLs for trusted CAs.
740 * \param profile The security profile to use for the verification.
741 * \param cn The expected Common Name. This may be \c NULL if the
742 * CN need not be verified.
743 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100744 * If the verification couldn't be completed, the flag value is
745 * set to (uint32_t) -1.
Hanno Becker902451d2019-03-27 11:48:40 +0000746 * \param f_vrfy The verification callback to use. See the documentation
747 * of mbedtls_x509_crt_verify() for more information.
748 * \param p_vrfy The context to be passed to \p f_vrfy.
749 * \param rs_ctx The restart context to use. This may be set to \c NULL
750 * to disable restartable ECC.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200751 *
752 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200753 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200754 * operations was reached: see \c mbedtls_ecp_set_max_ops().
755 */
756int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
757 mbedtls_x509_crt *trust_ca,
758 mbedtls_x509_crl *ca_crl,
759 const mbedtls_x509_crt_profile *profile,
760 const char *cn, uint32_t *flags,
761 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
762 void *p_vrfy,
763 mbedtls_x509_crt_restart_ctx *rs_ctx );
764
Hanno Becker5c8df782019-03-27 11:01:17 +0000765/**
766 * \brief The type of trusted certificate callbacks.
767 *
768 * Callbacks of this type are passed to and used by the CRT
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300769 * verification routine mbedtls_x509_crt_verify_with_ca_cb()
Hanno Becker5c8df782019-03-27 11:01:17 +0000770 * when looking for trusted signers of a given certificate.
771 *
772 * On success, the callback returns a list of trusted
773 * certificates to be considered as potential signers
774 * for the input certificate.
775 *
776 * \param p_ctx An opaque context passed to the callback.
777 * \param child The certificate for which to search a potential signer.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300778 * This will point to a readable certificate.
Hanno Becker5c8df782019-03-27 11:01:17 +0000779 * \param candidate_cas The address at which to store the address of the first
780 * entry in the generated linked list of candidate signers.
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300781 * This will not be \c NULL.
Hanno Becker5c8df782019-03-27 11:01:17 +0000782 *
783 * \note The callback must only return a non-zero value on a
784 * fatal error. If, in contrast, the search for a potential
785 * signer completes without a single candidate, the
Jarno Lamsaf49fedc2019-04-01 14:58:30 +0300786 * callback must return \c 0 and set \c *candidate_cas
Hanno Becker5c8df782019-03-27 11:01:17 +0000787 * to \c NULL.
788 *
789 * \return \c 0 on success. In this case, \c *candidate_cas points
790 * to a heap-allocated linked list of instances of
791 * ::mbedtls_x509_crt, and ownership of this list is passed
792 * to the caller.
793 * \return A negative error code on failure.
794 */
795typedef int (*mbedtls_x509_crt_ca_cb_t)( void *p_ctx,
796 mbedtls_x509_crt const *child,
797 mbedtls_x509_crt **candidate_cas );
Hanno Beckere15dae72019-03-28 13:55:38 +0000798
799#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker5c8df782019-03-27 11:01:17 +0000800/**
801 * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
802 * uses a callback to acquire the list of trusted CA
803 * certificates.
804 *
805 * \param crt The certificate chain to be verified.
806 * \param f_ca_cb The callback to be used to query for potential signers
807 * of a given child certificate. See the documentation of
808 * ::mbedtls_x509_crt_ca_cb_t for more information.
809 * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
810 * \param profile The security profile for the verification.
811 * \param cn The expected Common Name. This may be \c NULL if the
812 * CN need not be verified.
813 * \param flags The address at which to store the result of the verification.
Janos Follath846ae7a2019-04-05 16:45:01 +0100814 * If the verification couldn't be completed, the flag value is
815 * set to (uint32_t) -1.
Hanno Becker5c8df782019-03-27 11:01:17 +0000816 * \param f_vrfy The verification callback to use. See the documentation
817 * of mbedtls_x509_crt_verify() for more information.
818 * \param p_vrfy The context to be passed to \p f_vrfy.
819 *
820 * \return See \c mbedtls_crt_verify_with_profile().
821 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300822int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker5c8df782019-03-27 11:01:17 +0000823 mbedtls_x509_crt_ca_cb_t f_ca_cb,
824 void *p_ca_cb,
825 const mbedtls_x509_crt_profile *profile,
826 const char *cn, uint32_t *flags,
827 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
828 void *p_vrfy );
829
830#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200833/**
834 * \brief Check usage of certificate against keyUsage extension.
835 *
836 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200837 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
838 * before using the certificate to perform an RSA key
839 * exchange).
840 *
841 * \note Except for decipherOnly and encipherOnly, a bit set in the
842 * usage argument means this bit MUST be set in the
843 * certificate. For decipherOnly and encipherOnly, it means
844 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200845 *
846 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200848 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200849 *
850 * \note You should only call this function on leaf certificates, on
851 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200853 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200854int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
855 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200859/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000860 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200861 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000862 * \param crt Leaf certificate used.
863 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
864 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200866 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000867 * \return 0 if this use of the certificate is allowed,
868 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200869 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000870 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000873 const char *usage_oid,
874 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000875#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200879 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 *
881 * \param crt a certificate to be verified
882 * \param crl the CRL to verify against
883 *
884 * \return 1 if the certificate is revoked, 0 otherwise
885 *
886 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100887int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889
890/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200891 * \brief Initialize a certificate (chain)
892 *
893 * \param crt Certificate chain to initialize
894 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200896
897/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 * \brief Unallocate all certificate data
899 *
900 * \param crt Certificate chain to free
901 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200903
904#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
905/**
906 * \brief Initialize a restart context
907 */
908void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
909
910/**
911 * \brief Free the components of a restart context
912 */
913void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
914#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916
917/* \} name */
918/* \} addtogroup x509_module */
919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921/**
922 * \brief Initialize a CRT writing context
923 *
924 * \param ctx CRT context to initialize
925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927
928/**
929 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931 *
932 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
934 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937
938/**
939 * \brief Set the serial number for a Certificate.
940 *
941 * \param ctx CRT context to use
942 * \param serial serial number to set
943 *
944 * \return 0 if successful
945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947
948/**
949 * \brief Set the validity period for a Certificate
950 * Timestamps should be in string format for UTC timezone
951 * i.e. "YYYYMMDDhhmmss"
952 * e.g. "20131231235959" for December 31st 2013
953 * at 23:59:59
954 *
955 * \param ctx CRT context to use
956 * \param not_before not_before timestamp
957 * \param not_after not_after timestamp
958 *
959 * \return 0 if timestamp was parsed successfully, or
960 * a specific error code
961 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100963 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964
965/**
966 * \brief Set the issuer name for a Certificate
967 * Issuer names should contain a comma-separated list
968 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000969 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 *
971 * \param ctx CRT context to use
972 * \param issuer_name issuer name to set
973 *
974 * \return 0 if issuer name was parsed successfully, or
975 * a specific error code
976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100978 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979
980/**
981 * \brief Set the subject name for a Certificate
982 * Subject names should contain a comma-separated list
983 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000984 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 *
986 * \param ctx CRT context to use
987 * \param subject_name subject name to set
988 *
989 * \return 0 if subject name was parsed successfully, or
990 * a specific error code
991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100993 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994
995/**
996 * \brief Set the subject public key for the certificate
997 *
998 * \param ctx CRT context to use
999 * \param key public key to include
1000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002
1003/**
1004 * \brief Set the issuer key used for signing the certificate
1005 *
1006 * \param ctx CRT context to use
1007 * \param key private key to sign with
1008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010
1011/**
1012 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014 *
1015 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +01001016 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019
1020/**
1021 * \brief Generic function to add to or replace an extension in the
1022 * CRT
1023 *
1024 * \param ctx CRT context to use
1025 * \param oid OID of the extension
1026 * \param oid_len length of the OID
1027 * \param critical if the extension is critical (per the RFC's definition)
1028 * \param val value of the extension OCTET STRING
1029 * \param val_len length of the value data
1030 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001031 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 const char *oid, size_t oid_len,
1035 int critical,
1036 const unsigned char *val, size_t val_len );
1037
1038/**
1039 * \brief Set the basicConstraints extension for a CRT
1040 *
1041 * \param ctx CRT context to use
1042 * \param is_ca is this a CA certificate
1043 * \param max_pathlen maximum length of certificate chains below this
1044 * certificate (only for CA certificates, -1 is
1045 * inlimited)
1046 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001047 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050 int is_ca, int max_pathlen );
1051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053/**
1054 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056 * called before
1057 *
1058 * \param ctx CRT context to use
1059 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001060 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063
1064/**
1065 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067 * called before
1068 *
1069 * \param ctx CRT context to use
1070 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001071 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
1074#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075
1076/**
1077 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079 *
1080 * \param ctx CRT context to use
1081 * \param key_usage key usage flags to set
1082 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001083 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001084 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +02001085int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
1086 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001087
1088/**
1089 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091 *
1092 * \param ctx CRT context to use
1093 * \param ns_cert_type Netscape Cert Type flags to set
1094 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001095 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 unsigned char ns_cert_type );
1099
1100/**
1101 * \brief Free the contents of a CRT write context
1102 *
1103 * \param ctx CRT context to free
1104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106
1107/**
1108 * \brief Write a built up certificate to a X509 DER structure
1109 * Note: data is written at the end of the buffer! Use the
1110 * return value to determine where you should start
1111 * using the buffer
1112 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001113 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114 * \param buf buffer to write to
1115 * \param size size of the buffer
1116 * \param f_rng RNG function (for signature, see note)
1117 * \param p_rng RNG parameter
1118 *
1119 * \return length of data written if successful, or a specific
1120 * error code
1121 *
1122 * \note f_rng may be NULL if RSA is used for signature and the
1123 * signature is made offline (otherwise f_rng is desirable
1124 * for countermeasures against timing attacks).
1125 * ECDSA signatures always require a non-NULL f_rng.
1126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128 int (*f_rng)(void *, unsigned char *, size_t),
1129 void *p_rng );
1130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132/**
1133 * \brief Write a built up certificate to a X509 PEM string
1134 *
Paul Bakkera36d23e2013-12-30 17:57:27 +01001135 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001136 * \param buf buffer to write to
1137 * \param size size of the buffer
1138 * \param f_rng RNG function (for signature, see note)
1139 * \param p_rng RNG parameter
1140 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001141 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142 *
1143 * \note f_rng may be NULL if RSA is used for signature and the
1144 * signature is made offline (otherwise f_rng is desirable
1145 * for countermeasures against timing attacks).
1146 * ECDSA signatures always require a non-NULL f_rng.
1147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149 int (*f_rng)(void *, unsigned char *, size_t),
1150 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_PEM_WRITE_C */
1152#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001153
1154#ifdef __cplusplus
1155}
1156#endif
1157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#endif /* mbedtls_x509_crt.h */