blob: 800e64ba8dd30d6f207d15bcd42b7b4a8aee20f5 [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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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é-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_X509_CRT_H
25#define MBEDTLS_X509_CRT_H
Paul Bakker7c6b2c32013-09-16 13:49:26 +020026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032
33#include "x509.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034#include "x509_crl.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
36/**
37 * \addtogroup x509_module
38 * \{
39 */
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/**
46 * \name Structures and functions for parsing and writing X.509 certificates
47 * \{
48 */
49
50/**
51 * Container for an X.509 certificate. The certificate may be chained.
52 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054{
Hanno Becker1a65dcd2019-01-31 08:57:44 +000055 int own_buffer; /**< Indicates if \c raw is owned
56 * by the structure or not. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
58 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020060 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
62 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
65 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
68 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
71 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072
Hanno Becker494dd7a2019-02-06 16:13:41 +000073 mbedtls_x509_buf pk_raw;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
77 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
78 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
79 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080
81 int ext_types; /**< Bit string containing detected and parsed extensions */
82 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
83 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+ */
84
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +010085 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020089 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
92 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
93 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
94 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 +020095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200100/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200101 * Build flag from an algorithm/curve identifier (pk, md, ecp)
102 * Since 0 is always XXX_NONE, ignore it.
103 */
104#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( id - 1 ) )
105
106/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200107 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200108 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200109 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200110 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200111typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200112{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200113 uint32_t allowed_mds; /**< MDs for signatures */
114 uint32_t allowed_pks; /**< PK algs for signatures */
115 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
116 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200117}
118mbedtls_x509_crt_profile;
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#define MBEDTLS_X509_CRT_VERSION_1 0
121#define MBEDTLS_X509_CRT_VERSION_2 1
122#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
125#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200126
Andres AGf9113192016-09-02 14:06:04 +0100127#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
128#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
129#endif
130
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200131/**
132 * Container for writing a certificate (CRT)
133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135{
136 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_mpi serial;
138 mbedtls_pk_context *subject_key;
139 mbedtls_pk_context *issuer_key;
140 mbedtls_asn1_named_data *subject;
141 mbedtls_asn1_named_data *issuer;
142 mbedtls_md_type_t md_alg;
143 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
144 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
145 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200148
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200149/**
150 * Item in a verification chain: cert and flags for it
151 */
152typedef struct {
153 mbedtls_x509_crt *crt;
154 uint32_t flags;
155} mbedtls_x509_crt_verify_chain_item;
156
157/**
158 * Max size of verification chain: end-entity + intermediates + trusted root
159 */
160#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
161
162/**
163 * Verification chain as built by \c mbedtls_crt_verify_chain()
164 */
165typedef struct
166{
167 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200168 unsigned len;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000169
170#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
171 /* This stores the list of potential trusted signers obtained from
172 * the CA callback used for the CRT verification, if configured.
173 * We must track it somewhere because the callback passes its
174 * ownership to the caller. */
175 mbedtls_x509_crt *trust_ca_cb_result;
176#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200177} mbedtls_x509_crt_verify_chain;
178
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200179#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
180
181/**
182 * \brief Context for resuming X.509 verify operations
183 */
184typedef struct
185{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200186 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200187 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200188
189 /* for find_parent_in() */
190 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
191 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200192 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200193
194 /* for find_parent() */
195 int parent_is_trusted; /* -1 if find_parent is not in progress */
196
197 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200198 enum {
199 x509_crt_rs_none,
200 x509_crt_rs_find_parent,
201 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200202 int self_cnt;
203 mbedtls_x509_crt_verify_chain ver_chain;
204
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200205} mbedtls_x509_crt_restart_ctx;
206
207#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
208
209/* Now we can declare functions that take a pointer to that */
210typedef void mbedtls_x509_crt_restart_ctx;
211
212#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200215/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200216 * Default security profile. Should provide a good balance between security
217 * and compatibility with current deployments.
218 */
219extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
220
221/**
222 * Expected next default profile. Recommended for new deployments.
223 * Currently targets a 128-bit security level, except for RSA-2048.
224 */
225extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
226
227/**
228 * NSA Suite B profile.
229 */
230extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
231
232/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200233 * \brief Parse a single DER formatted certificate and add it
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000234 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200235 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000236 * \param chain The pointer to the start of the CRT chain to attach to.
237 * When parsing the first CRT in a chain, this should point
238 * to an instance of ::mbedtls_x509_crt initialized through
239 * mbedtls_x509_crt_init().
240 * \param buf The buffer holding the DER encoded certificate.
241 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000243 * \note This function makes an internal copy of the CRT buffer
244 * \p buf. In particular, \p buf may be destroyed or reused
245 * after this call returns. To avoid duplicating the CRT
246 * buffer (at the cost of stricter lifetime constraints),
247 * use mbedtls_x509_crt_parse_der_nocopy() instead.
248 *
249 * \return \c 0 if successful.
250 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200251 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000252int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
253 const unsigned char *buf,
254 size_t buflen );
255
256/**
257 * \brief Parse a single DER formatted certificate and add it
258 * to the end of the provided chained list. This is a
259 * variant of mbedtls_x509_crt_parse_der() which takes
260 * temporary ownership of the CRT buffer until the CRT
261 * is destroyed.
262 *
263 * \param chain The pointer to the start of the CRT chain to attach to.
264 * When parsing the first CRT in a chain, this should point
265 * to an instance of ::mbedtls_x509_crt initialized through
266 * mbedtls_x509_crt_init().
267 * \param buf The address of the readable buffer holding the DER encoded
268 * certificate to use. On success, this buffer must be
269 * retained and not be changed for the liftetime of the
270 * CRT chain \p chain, that is, until \p chain is destroyed
271 * through a call to mbedtls_x509_crt_free().
272 * \param buflen The size in Bytes of \p buf.
273 *
274 * \note This call is functionally equivalent to
275 * mbedtls_x509_crt_parse_der(), but it avoids creating a
276 * copy of the input buffer at the cost of stronger lifetime
277 * constraints. This is useful in constrained environments
278 * where duplication of the CRT cannot be tolerated.
279 *
280 * \return \c 0 if successful.
281 * \return A negative error code on failure.
282 */
283int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
284 const unsigned char *buf,
285 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286
287/**
Hanno Becker89a91122018-08-23 16:14:00 +0100288 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100289 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100291 * For CRTs in PEM encoding, the function parses permissively:
292 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100293 * returns the number of certificates for which parsing failed
294 * (hence \c 0 if all certificates were parsed successfully).
295 * If no certificate could be parsed, the function returns
296 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200297 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100298 * PEM encoded certificates may be interleaved by other data
299 * such as human readable descriptions of their content, as
300 * long as the certificates are enclosed in the PEM specific
301 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
302 *
303 * \param chain The chain to which to add the parsed certificates.
304 * \param buf The buffer holding the certificate data in PEM or DER format.
305 * For certificates in PEM encoding, this may be a concatenation
306 * of multiple certificates; for DER encoding, the buffer must
307 * comprise exactly one certificate.
308 * \param buflen The size of \p buf, including the terminating \c NULL byte
309 * in case of PEM encoded data.
310 *
311 * \return \c 0 if all certificates were parsed successfully.
312 * \return The (positive) number of certificates that couldn't
313 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100314 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100315 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200320/**
321 * \brief Load one or more certificates and add them
322 * to the chained list. Parses permissively. If some
323 * certificates can be parsed, the result is the number
324 * of failed certificates it encountered. If none complete
325 * correctly, the first error is returned.
326 *
327 * \param chain points to the start of the chain
328 * \param path filename to read the certificates from
329 *
330 * \return 0 if all certificates parsed successfully, a positive number
331 * if partly successful or a specific X509 or PEM error code
332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200334
335/**
336 * \brief Load one or more certificate files from a path and add them
337 * to the chained list. Parses permissively. If some
338 * certificates can be parsed, the result is the number
339 * of failed certificates it encountered. If none complete
340 * correctly, the first error is returned.
341 *
342 * \param chain points to the start of the chain
343 * \param path directory / folder to read the certificate files from
344 *
345 * \return 0 if all certificates parsed successfully, a positive number
346 * if partly successful or a specific X509 or PEM error code
347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
349#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200350
351/**
352 * \brief Returns an informational string about the
353 * certificate.
354 *
355 * \param buf Buffer to write to
356 * \param size Maximum size of buffer
357 * \param prefix A line prefix
358 * \param crt The X509 certificate to represent
359 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200360 * \return The length of the string written (not including the
361 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
364 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365
366/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100367 * \brief Returns an informational string about the
368 * verification status of a certificate.
369 *
370 * \param buf Buffer to write to
371 * \param size Maximum size of buffer
372 * \param prefix A line prefix
373 * \param flags Verification flags created by mbedtls_x509_crt_verify()
374 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200375 * \return The length of the string written (not including the
376 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100377 */
378int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200379 uint32_t flags );
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200380
381/**
Hanno Becker902451d2019-03-27 11:48:40 +0000382 * \brief Verify a chain of certificates.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383 *
384 * The verify callback is a user-supplied callback that
385 * can clear / modify / add flags for a certificate. If set,
386 * the verification callback is called for each
387 * certificate in the chain (from the trust-ca down to the
388 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390 * int *flags). With the flags representing current flags for
391 * that specific certificate and the certificate depth from
392 * the bottom (Peer cert depth = 0).
393 *
394 * All flags left after returning from the callback
395 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200396 * return 0 for anything (including invalid certificates)
397 * other than fatal error, as a non-zero return code
398 * immediately aborts the verification process. For fatal
399 * errors, a specific error code should be used (different
400 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
401 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
402 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100404 * \note In case verification failed, the results can be displayed
405 * using \c mbedtls_x509_crt_verify_info()
406 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200407 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
408 * default security profile.
409 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100410 * \note It is your responsibility to provide up-to-date CRLs for
411 * all trusted CAs. If no CRL is provided for the CA that was
412 * used to sign the certificate, CRL verification is skipped
413 * silently, that is *without* setting any flag.
414 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200415 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200416 * (1) those of trusted root CAs, so that certificates
417 * chaining up to those CAs will be trusted, and (2)
418 * self-signed end-entity certificates to be trusted (for
419 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200420 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200421 *
Hanno Becker902451d2019-03-27 11:48:40 +0000422 * \param crt The certificate chain to be verified.
423 * \param trust_ca The list of trusted CAs.
424 * \param ca_crl The list of CRLs for trusted CAs.
425 * \param cn The expected Common Name. This may be \c NULL if the
426 * CN need not be verified.
427 * \param flags The address at which to store the result of the verification.
428 * \param f_vrfy The verification callback to use. See the documentation
429 * of mbedtls_x509_crt_verify() for more information.
430 * \param p_vrfy The context to be passed to \p f_vrfy.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431 *
Hanno Becker902451d2019-03-27 11:48:40 +0000432 * \return \c 0 if the chain is valid with respect to the
433 * passed CN, CAs, CRLs and security profile.
434 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
435 * certificate chain verification failed. In this case,
436 * \c *flags will have one or more
437 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
438 * flags set.
439 * \return Another negative error code in case of a fatal error
440 * encountered during the verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
443 mbedtls_x509_crt *trust_ca,
444 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200445 const char *cn, uint32_t *flags,
446 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200447 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200449/**
Hanno Becker902451d2019-03-27 11:48:40 +0000450 * \brief Verify a chain of certificates with respect to
451 * a configurable security profile.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200452 *
453 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
454 * security profile.
455 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200456 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200457 * for ECDSA) apply to all certificates: trusted root,
458 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200459 *
Hanno Becker902451d2019-03-27 11:48:40 +0000460 * \param crt The certificate chain to be verified.
461 * \param trust_ca The list of trusted CAs.
462 * \param ca_crl The list of CRLs for trusted CAs.
463 * \param profile The security profile to use for the verification.
464 * \param cn The expected Common Name. This may be \c NULL if the
465 * CN need not be verified.
466 * \param flags The address at which to store the result of the verification.
467 * \param f_vrfy The verification callback to use. See the documentation
468 * of mbedtls_x509_crt_verify() for more information.
469 * \param p_vrfy The context to be passed to \p f_vrfy.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200470 *
Hanno Becker902451d2019-03-27 11:48:40 +0000471 * \return \c 0 if the chain is valid with respect to the
472 * passed CN, CAs, CRLs and security profile.
473 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
474 * certificate chain verification failed. In this case,
475 * \c *flags will have one or more
476 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
477 * flags set.
478 * \return Another negative error code in case of a fatal error
479 * encountered during the verification process.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200480 */
481int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
482 mbedtls_x509_crt *trust_ca,
483 mbedtls_x509_crl *ca_crl,
484 const mbedtls_x509_crt_profile *profile,
485 const char *cn, uint32_t *flags,
486 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
487 void *p_vrfy );
488
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200489/**
490 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
491 *
492 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
493 * but can return early and restart according to the limit
494 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
495 *
Hanno Becker902451d2019-03-27 11:48:40 +0000496 * \param crt The certificate chain to be verified.
497 * \param trust_ca The list of trusted CAs.
498 * \param ca_crl The list of CRLs for trusted CAs.
499 * \param profile The security profile to use for the verification.
500 * \param cn The expected Common Name. This may be \c NULL if the
501 * CN need not be verified.
502 * \param flags The address at which to store the result of the verification.
503 * \param f_vrfy The verification callback to use. See the documentation
504 * of mbedtls_x509_crt_verify() for more information.
505 * \param p_vrfy The context to be passed to \p f_vrfy.
506 * \param rs_ctx The restart context to use. This may be set to \c NULL
507 * to disable restartable ECC.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200508 *
509 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200510 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200511 * operations was reached: see \c mbedtls_ecp_set_max_ops().
512 */
513int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
514 mbedtls_x509_crt *trust_ca,
515 mbedtls_x509_crl *ca_crl,
516 const mbedtls_x509_crt_profile *profile,
517 const char *cn, uint32_t *flags,
518 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
519 void *p_vrfy,
520 mbedtls_x509_crt_restart_ctx *rs_ctx );
521
Hanno Becker5c8df782019-03-27 11:01:17 +0000522/**
523 * \brief The type of trusted certificate callbacks.
524 *
525 * Callbacks of this type are passed to and used by the CRT
526 * verification routine mbedtls_x509_crt_verify_with_cb()
527 * when looking for trusted signers of a given certificate.
528 *
529 * On success, the callback returns a list of trusted
530 * certificates to be considered as potential signers
531 * for the input certificate.
532 *
533 * \param p_ctx An opaque context passed to the callback.
534 * \param child The certificate for which to search a potential signer.
535 * This must point to a readable certificate.
536 * \param candidate_cas The address at which to store the address of the first
537 * entry in the generated linked list of candidate signers.
538 * This must not be \c NULL.
539 *
540 * \note The callback must only return a non-zero value on a
541 * fatal error. If, in contrast, the search for a potential
542 * signer completes without a single candidate, the
543 * callback must return \c 0 and get \c *candidate_cas
544 * to \c NULL.
545 *
546 * \return \c 0 on success. In this case, \c *candidate_cas points
547 * to a heap-allocated linked list of instances of
548 * ::mbedtls_x509_crt, and ownership of this list is passed
549 * to the caller.
550 * \return A negative error code on failure.
551 */
552typedef int (*mbedtls_x509_crt_ca_cb_t)( void *p_ctx,
553 mbedtls_x509_crt const *child,
554 mbedtls_x509_crt **candidate_cas );
Hanno Beckere15dae72019-03-28 13:55:38 +0000555
556#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker5c8df782019-03-27 11:01:17 +0000557/**
558 * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
559 * uses a callback to acquire the list of trusted CA
560 * certificates.
561 *
562 * \param crt The certificate chain to be verified.
563 * \param f_ca_cb The callback to be used to query for potential signers
564 * of a given child certificate. See the documentation of
565 * ::mbedtls_x509_crt_ca_cb_t for more information.
566 * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
567 * \param profile The security profile for the verification.
568 * \param cn The expected Common Name. This may be \c NULL if the
569 * CN need not be verified.
570 * \param flags The address at which to store the result of the verification.
571 * \param f_vrfy The verification callback to use. See the documentation
572 * of mbedtls_x509_crt_verify() for more information.
573 * \param p_vrfy The context to be passed to \p f_vrfy.
574 *
575 * \return See \c mbedtls_crt_verify_with_profile().
576 */
577int mbedtls_x509_crt_verify_with_cb( mbedtls_x509_crt *crt,
578 mbedtls_x509_crt_ca_cb_t f_ca_cb,
579 void *p_ca_cb,
580 const mbedtls_x509_crt_profile *profile,
581 const char *cn, uint32_t *flags,
582 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
583 void *p_vrfy );
584
585#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200588/**
589 * \brief Check usage of certificate against keyUsage extension.
590 *
591 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200592 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
593 * before using the certificate to perform an RSA key
594 * exchange).
595 *
596 * \note Except for decipherOnly and encipherOnly, a bit set in the
597 * usage argument means this bit MUST be set in the
598 * certificate. For decipherOnly and encipherOnly, it means
599 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200600 *
601 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200603 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200604 *
605 * \note You should only call this function on leaf certificates, on
606 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200608 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200609int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
610 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200614/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000615 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200616 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000617 * \param crt Leaf certificate used.
618 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
619 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200621 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000622 * \return 0 if this use of the certificate is allowed,
623 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200624 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000625 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000628 const char *usage_oid,
629 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000630#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200634 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 *
636 * \param crt a certificate to be verified
637 * \param crl the CRL to verify against
638 *
639 * \return 1 if the certificate is revoked, 0 otherwise
640 *
641 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100642int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644
645/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200646 * \brief Initialize a certificate (chain)
647 *
648 * \param crt Certificate chain to initialize
649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200651
652/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653 * \brief Unallocate all certificate data
654 *
655 * \param crt Certificate chain to free
656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200658
659#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
660/**
661 * \brief Initialize a restart context
662 */
663void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
664
665/**
666 * \brief Free the components of a restart context
667 */
668void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
669#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671
672/* \} name */
673/* \} addtogroup x509_module */
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676/**
677 * \brief Initialize a CRT writing context
678 *
679 * \param ctx CRT context to initialize
680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682
683/**
684 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686 *
687 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
689 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692
693/**
694 * \brief Set the serial number for a Certificate.
695 *
696 * \param ctx CRT context to use
697 * \param serial serial number to set
698 *
699 * \return 0 if successful
700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
703/**
704 * \brief Set the validity period for a Certificate
705 * Timestamps should be in string format for UTC timezone
706 * i.e. "YYYYMMDDhhmmss"
707 * e.g. "20131231235959" for December 31st 2013
708 * at 23:59:59
709 *
710 * \param ctx CRT context to use
711 * \param not_before not_before timestamp
712 * \param not_after not_after timestamp
713 *
714 * \return 0 if timestamp was parsed successfully, or
715 * a specific error code
716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100718 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719
720/**
721 * \brief Set the issuer name for a Certificate
722 * Issuer names should contain a comma-separated list
723 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000724 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725 *
726 * \param ctx CRT context to use
727 * \param issuer_name issuer name to set
728 *
729 * \return 0 if issuer name was parsed successfully, or
730 * a specific error code
731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100733 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200734
735/**
736 * \brief Set the subject name for a Certificate
737 * Subject names should contain a comma-separated list
738 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000739 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740 *
741 * \param ctx CRT context to use
742 * \param subject_name subject name to set
743 *
744 * \return 0 if subject name was parsed successfully, or
745 * a specific error code
746 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100748 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200749
750/**
751 * \brief Set the subject public key for the certificate
752 *
753 * \param ctx CRT context to use
754 * \param key public key to include
755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757
758/**
759 * \brief Set the issuer key used for signing the certificate
760 *
761 * \param ctx CRT context to use
762 * \param key private key to sign with
763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765
766/**
767 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 *
770 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100771 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774
775/**
776 * \brief Generic function to add to or replace an extension in the
777 * CRT
778 *
779 * \param ctx CRT context to use
780 * \param oid OID of the extension
781 * \param oid_len length of the OID
782 * \param critical if the extension is critical (per the RFC's definition)
783 * \param val value of the extension OCTET STRING
784 * \param val_len length of the value data
785 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200786 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789 const char *oid, size_t oid_len,
790 int critical,
791 const unsigned char *val, size_t val_len );
792
793/**
794 * \brief Set the basicConstraints extension for a CRT
795 *
796 * \param ctx CRT context to use
797 * \param is_ca is this a CA certificate
798 * \param max_pathlen maximum length of certificate chains below this
799 * certificate (only for CA certificates, -1 is
800 * inlimited)
801 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200802 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200805 int is_ca, int max_pathlen );
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200808/**
809 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811 * called before
812 *
813 * \param ctx CRT context to use
814 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200815 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818
819/**
820 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200822 * called before
823 *
824 * \param ctx CRT context to use
825 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200826 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
829#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830
831/**
832 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200834 *
835 * \param ctx CRT context to use
836 * \param key_usage key usage flags to set
837 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200838 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200840int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
841 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200842
843/**
844 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200846 *
847 * \param ctx CRT context to use
848 * \param ns_cert_type Netscape Cert Type flags to set
849 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200850 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200853 unsigned char ns_cert_type );
854
855/**
856 * \brief Free the contents of a CRT write context
857 *
858 * \param ctx CRT context to free
859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200861
862/**
863 * \brief Write a built up certificate to a X509 DER structure
864 * Note: data is written at the end of the buffer! Use the
865 * return value to determine where you should start
866 * using the buffer
867 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100868 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869 * \param buf buffer to write to
870 * \param size size of the buffer
871 * \param f_rng RNG function (for signature, see note)
872 * \param p_rng RNG parameter
873 *
874 * \return length of data written if successful, or a specific
875 * error code
876 *
877 * \note f_rng may be NULL if RSA is used for signature and the
878 * signature is made offline (otherwise f_rng is desirable
879 * for countermeasures against timing attacks).
880 * ECDSA signatures always require a non-NULL f_rng.
881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883 int (*f_rng)(void *, unsigned char *, size_t),
884 void *p_rng );
885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887/**
888 * \brief Write a built up certificate to a X509 PEM string
889 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100890 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200891 * \param buf buffer to write to
892 * \param size size of the buffer
893 * \param f_rng RNG function (for signature, see note)
894 * \param p_rng RNG parameter
895 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200896 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897 *
898 * \note f_rng may be NULL if RSA is used for signature and the
899 * signature is made offline (otherwise f_rng is desirable
900 * for countermeasures against timing attacks).
901 * ECDSA signatures always require a non-NULL f_rng.
902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 int (*f_rng)(void *, unsigned char *, size_t),
905 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#endif /* MBEDTLS_PEM_WRITE_C */
907#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908
909#ifdef __cplusplus
910}
911#endif
912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#endif /* mbedtls_x509_crt.h */