blob: 72c39019b108365da2019049dffc76185afcb4f5 [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
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. */
78 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079
80 int ext_types; /**< Bit string containing detected and parsed extensions */
81 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
82 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+ */
83
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +010084 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020088 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
91 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
92 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
93 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 +020094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020099/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200100 * Build flag from an algorithm/curve identifier (pk, md, ecp)
101 * Since 0 is always XXX_NONE, ignore it.
102 */
103#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( id - 1 ) )
104
105/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200106 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200107 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200108 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200109 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200110typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200111{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200112 uint32_t allowed_mds; /**< MDs for signatures */
113 uint32_t allowed_pks; /**< PK algs for signatures */
114 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
115 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200116}
117mbedtls_x509_crt_profile;
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#define MBEDTLS_X509_CRT_VERSION_1 0
120#define MBEDTLS_X509_CRT_VERSION_2 1
121#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
124#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200125
Andres AGf9113192016-09-02 14:06:04 +0100126#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
127#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
128#endif
129
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200130/**
131 * Container for writing a certificate (CRT)
132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200134{
135 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_mpi serial;
137 mbedtls_pk_context *subject_key;
138 mbedtls_pk_context *issuer_key;
139 mbedtls_asn1_named_data *subject;
140 mbedtls_asn1_named_data *issuer;
141 mbedtls_md_type_t md_alg;
142 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
143 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
144 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200148/**
149 * Item in a verification chain: cert and flags for it
150 */
151typedef struct {
152 mbedtls_x509_crt *crt;
153 uint32_t flags;
154} mbedtls_x509_crt_verify_chain_item;
155
156/**
157 * Max size of verification chain: end-entity + intermediates + trusted root
158 */
159#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
160
161/**
162 * Verification chain as built by \c mbedtls_crt_verify_chain()
163 */
164typedef struct
165{
166 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200167 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200168} mbedtls_x509_crt_verify_chain;
169
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200170#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
171
172/**
173 * \brief Context for resuming X.509 verify operations
174 */
175typedef struct
176{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200177 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200178 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200179
180 /* for find_parent_in() */
181 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
182 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200183 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200184
185 /* for find_parent() */
186 int parent_is_trusted; /* -1 if find_parent is not in progress */
187
188 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200189 enum {
190 x509_crt_rs_none,
191 x509_crt_rs_find_parent,
192 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200193 int self_cnt;
194 mbedtls_x509_crt_verify_chain ver_chain;
195
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200196} mbedtls_x509_crt_restart_ctx;
197
198#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
199
200/* Now we can declare functions that take a pointer to that */
201typedef void mbedtls_x509_crt_restart_ctx;
202
203#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200207 * Default security profile. Should provide a good balance between security
208 * and compatibility with current deployments.
209 */
210extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
211
212/**
213 * Expected next default profile. Recommended for new deployments.
214 * Currently targets a 128-bit security level, except for RSA-2048.
215 */
216extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
217
218/**
219 * NSA Suite B profile.
220 */
221extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
222
223/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200224 * \brief Parse a single DER formatted certificate and add it
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000225 * to the end of the provided chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200226 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000227 * \param chain The pointer to the start of the CRT chain to attach to.
228 * When parsing the first CRT in a chain, this should point
229 * to an instance of ::mbedtls_x509_crt initialized through
230 * mbedtls_x509_crt_init().
231 * \param buf The buffer holding the DER encoded certificate.
232 * \param buflen The size in Bytes of \p buf.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200233 *
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000234 * \note This function makes an internal copy of the CRT buffer
235 * \p buf. In particular, \p buf may be destroyed or reused
236 * after this call returns. To avoid duplicating the CRT
237 * buffer (at the cost of stricter lifetime constraints),
238 * use mbedtls_x509_crt_parse_der_nocopy() instead.
239 *
240 * \return \c 0 if successful.
241 * \return A negative error code on failure.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000243int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
244 const unsigned char *buf,
245 size_t buflen );
246
247/**
248 * \brief Parse a single DER formatted certificate and add it
249 * to the end of the provided chained list. This is a
250 * variant of mbedtls_x509_crt_parse_der() which takes
251 * temporary ownership of the CRT buffer until the CRT
252 * is destroyed.
253 *
254 * \param chain The pointer to the start of the CRT chain to attach to.
255 * When parsing the first CRT in a chain, this should point
256 * to an instance of ::mbedtls_x509_crt initialized through
257 * mbedtls_x509_crt_init().
258 * \param buf The address of the readable buffer holding the DER encoded
259 * certificate to use. On success, this buffer must be
260 * retained and not be changed for the liftetime of the
261 * CRT chain \p chain, that is, until \p chain is destroyed
262 * through a call to mbedtls_x509_crt_free().
263 * \param buflen The size in Bytes of \p buf.
264 *
265 * \note This call is functionally equivalent to
266 * mbedtls_x509_crt_parse_der(), but it avoids creating a
267 * copy of the input buffer at the cost of stronger lifetime
268 * constraints. This is useful in constrained environments
269 * where duplication of the CRT cannot be tolerated.
270 *
271 * \return \c 0 if successful.
272 * \return A negative error code on failure.
273 */
274int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
275 const unsigned char *buf,
276 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200277
278/**
Hanno Becker89a91122018-08-23 16:14:00 +0100279 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100280 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100282 * For CRTs in PEM encoding, the function parses permissively:
283 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100284 * returns the number of certificates for which parsing failed
285 * (hence \c 0 if all certificates were parsed successfully).
286 * If no certificate could be parsed, the function returns
287 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100289 * PEM encoded certificates may be interleaved by other data
290 * such as human readable descriptions of their content, as
291 * long as the certificates are enclosed in the PEM specific
292 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
293 *
294 * \param chain The chain to which to add the parsed certificates.
295 * \param buf The buffer holding the certificate data in PEM or DER format.
296 * For certificates in PEM encoding, this may be a concatenation
297 * of multiple certificates; for DER encoding, the buffer must
298 * comprise exactly one certificate.
299 * \param buflen The size of \p buf, including the terminating \c NULL byte
300 * in case of PEM encoded data.
301 *
302 * \return \c 0 if all certificates were parsed successfully.
303 * \return The (positive) number of certificates that couldn't
304 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100305 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100306 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311/**
312 * \brief Load one or more certificates and add them
313 * to the chained list. Parses permissively. If some
314 * certificates can be parsed, the result is the number
315 * of failed certificates it encountered. If none complete
316 * correctly, the first error is returned.
317 *
318 * \param chain points to the start of the chain
319 * \param path filename to read the certificates from
320 *
321 * \return 0 if all certificates parsed successfully, a positive number
322 * if partly successful or a specific X509 or PEM error code
323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325
326/**
327 * \brief Load one or more certificate files from a path and add them
328 * to the chained list. Parses permissively. If some
329 * certificates can be parsed, the result is the number
330 * of failed certificates it encountered. If none complete
331 * correctly, the first error is returned.
332 *
333 * \param chain points to the start of the chain
334 * \param path directory / folder to read the certificate files from
335 *
336 * \return 0 if all certificates parsed successfully, a positive number
337 * if partly successful or a specific X509 or PEM error code
338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
340#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341
342/**
343 * \brief Returns an informational string about the
344 * certificate.
345 *
346 * \param buf Buffer to write to
347 * \param size Maximum size of buffer
348 * \param prefix A line prefix
349 * \param crt The X509 certificate to represent
350 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200351 * \return The length of the string written (not including the
352 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
355 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356
357/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100358 * \brief Returns an informational string about the
359 * verification status of a certificate.
360 *
361 * \param buf Buffer to write to
362 * \param size Maximum size of buffer
363 * \param prefix A line prefix
364 * \param flags Verification flags created by mbedtls_x509_crt_verify()
365 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200366 * \return The length of the string written (not including the
367 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100368 */
369int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200370 uint32_t flags );
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200371
372/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200373 * \brief Verify the certificate signature
374 *
375 * The verify callback is a user-supplied callback that
376 * can clear / modify / add flags for a certificate. If set,
377 * the verification callback is called for each
378 * certificate in the chain (from the trust-ca down to the
379 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381 * int *flags). With the flags representing current flags for
382 * that specific certificate and the certificate depth from
383 * the bottom (Peer cert depth = 0).
384 *
385 * All flags left after returning from the callback
386 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200387 * return 0 for anything (including invalid certificates)
388 * other than fatal error, as a non-zero return code
389 * immediately aborts the verification process. For fatal
390 * errors, a specific error code should be used (different
391 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
392 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
393 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200394 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100395 * \note In case verification failed, the results can be displayed
396 * using \c mbedtls_x509_crt_verify_info()
397 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200398 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
399 * default security profile.
400 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100401 * \note It is your responsibility to provide up-to-date CRLs for
402 * all trusted CAs. If no CRL is provided for the CA that was
403 * used to sign the certificate, CRL verification is skipped
404 * silently, that is *without* setting any flag.
405 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200406 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200407 * (1) those of trusted root CAs, so that certificates
408 * chaining up to those CAs will be trusted, and (2)
409 * self-signed end-entity certificates to be trusted (for
410 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200411 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200412 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100413 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200414 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100415 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416 * \param cn expected Common Name (can be set to
417 * NULL if the CN must not be verified)
418 * \param flags result of the verification
419 * \param f_vrfy verification function
420 * \param p_vrfy verification parameter
421 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200422 * \return 0 (and flags set to 0) if the chain was verified and valid,
423 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
424 * but found to be invalid, in which case *flags will have one
425 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
426 * flags set, or another error (and flags set to 0xffffffff)
427 * in case of a fatal error encountered during the
428 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
431 mbedtls_x509_crt *trust_ca,
432 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200433 const char *cn, uint32_t *flags,
434 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200435 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200437/**
438 * \brief Verify the certificate signature according to profile
439 *
440 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
441 * security profile.
442 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200443 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200444 * for ECDSA) apply to all certificates: trusted root,
445 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200446 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100447 * \param crt a certificate (chain) to be verified
448 * \param trust_ca the list of trusted CAs
449 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200450 * \param profile security profile for verification
451 * \param cn expected Common Name (can be set to
452 * NULL if the CN must not be verified)
453 * \param flags result of the verification
454 * \param f_vrfy verification function
455 * \param p_vrfy verification parameter
456 *
457 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
458 * in which case *flags will have one or more
459 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
460 * set,
461 * or another error in case of a fatal error encountered
462 * during the verification process.
463 */
464int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
465 mbedtls_x509_crt *trust_ca,
466 mbedtls_x509_crl *ca_crl,
467 const mbedtls_x509_crt_profile *profile,
468 const char *cn, uint32_t *flags,
469 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
470 void *p_vrfy );
471
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200472/**
473 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
474 *
475 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
476 * but can return early and restart according to the limit
477 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
478 *
479 * \param crt a certificate (chain) to be verified
480 * \param trust_ca the list of trusted CAs
481 * \param ca_crl the list of CRLs for trusted CAs
482 * \param profile security profile for verification
483 * \param cn expected Common Name (can be set to
484 * NULL if the CN must not be verified)
485 * \param flags result of the verification
486 * \param f_vrfy verification function
487 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200488 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200489 *
490 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200491 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200492 * operations was reached: see \c mbedtls_ecp_set_max_ops().
493 */
494int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
495 mbedtls_x509_crt *trust_ca,
496 mbedtls_x509_crl *ca_crl,
497 const mbedtls_x509_crt_profile *profile,
498 const char *cn, uint32_t *flags,
499 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
500 void *p_vrfy,
501 mbedtls_x509_crt_restart_ctx *rs_ctx );
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200504/**
505 * \brief Check usage of certificate against keyUsage extension.
506 *
507 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200508 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
509 * before using the certificate to perform an RSA key
510 * exchange).
511 *
512 * \note Except for decipherOnly and encipherOnly, a bit set in the
513 * usage argument means this bit MUST be set in the
514 * certificate. For decipherOnly and encipherOnly, it means
515 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200516 *
517 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200519 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200520 *
521 * \note You should only call this function on leaf certificates, on
522 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200524 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200525int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
526 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200530/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000531 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200532 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000533 * \param crt Leaf certificate used.
534 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
535 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200537 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000538 * \return 0 if this use of the certificate is allowed,
539 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200540 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000541 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000544 const char *usage_oid,
545 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000546#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200550 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551 *
552 * \param crt a certificate to be verified
553 * \param crl the CRL to verify against
554 *
555 * \return 1 if the certificate is revoked, 0 otherwise
556 *
557 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100558int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560
561/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200562 * \brief Initialize a certificate (chain)
563 *
564 * \param crt Certificate chain to initialize
565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200567
568/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569 * \brief Unallocate all certificate data
570 *
571 * \param crt Certificate chain to free
572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200574
575#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
576/**
577 * \brief Initialize a restart context
578 */
579void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
580
581/**
582 * \brief Free the components of a restart context
583 */
584void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
585#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587
588/* \} name */
589/* \} addtogroup x509_module */
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592/**
593 * \brief Initialize a CRT writing context
594 *
595 * \param ctx CRT context to initialize
596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200598
599/**
600 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602 *
603 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
605 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608
609/**
610 * \brief Set the serial number for a Certificate.
611 *
612 * \param ctx CRT context to use
613 * \param serial serial number to set
614 *
615 * \return 0 if successful
616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618
619/**
620 * \brief Set the validity period for a Certificate
621 * Timestamps should be in string format for UTC timezone
622 * i.e. "YYYYMMDDhhmmss"
623 * e.g. "20131231235959" for December 31st 2013
624 * at 23:59:59
625 *
626 * \param ctx CRT context to use
627 * \param not_before not_before timestamp
628 * \param not_after not_after timestamp
629 *
630 * \return 0 if timestamp was parsed successfully, or
631 * a specific error code
632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100634 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635
636/**
637 * \brief Set the issuer name for a Certificate
638 * Issuer names should contain a comma-separated list
639 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000640 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641 *
642 * \param ctx CRT context to use
643 * \param issuer_name issuer name to set
644 *
645 * \return 0 if issuer name was parsed successfully, or
646 * a specific error code
647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100649 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650
651/**
652 * \brief Set the subject name for a Certificate
653 * Subject names should contain a comma-separated list
654 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000655 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656 *
657 * \param ctx CRT context to use
658 * \param subject_name subject name to set
659 *
660 * \return 0 if subject name was parsed successfully, or
661 * a specific error code
662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100664 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665
666/**
667 * \brief Set the subject public key for the certificate
668 *
669 * \param ctx CRT context to use
670 * \param key public key to include
671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673
674/**
675 * \brief Set the issuer key used for signing the certificate
676 *
677 * \param ctx CRT context to use
678 * \param key private key to sign with
679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
682/**
683 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685 *
686 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100687 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690
691/**
692 * \brief Generic function to add to or replace an extension in the
693 * CRT
694 *
695 * \param ctx CRT context to use
696 * \param oid OID of the extension
697 * \param oid_len length of the OID
698 * \param critical if the extension is critical (per the RFC's definition)
699 * \param val value of the extension OCTET STRING
700 * \param val_len length of the value data
701 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200702 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705 const char *oid, size_t oid_len,
706 int critical,
707 const unsigned char *val, size_t val_len );
708
709/**
710 * \brief Set the basicConstraints extension for a CRT
711 *
712 * \param ctx CRT context to use
713 * \param is_ca is this a CA certificate
714 * \param max_pathlen maximum length of certificate chains below this
715 * certificate (only for CA certificates, -1 is
716 * inlimited)
717 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200718 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721 int is_ca, int max_pathlen );
722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724/**
725 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727 * called before
728 *
729 * \param ctx CRT context to use
730 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200731 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200734
735/**
736 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 * called before
739 *
740 * \param ctx CRT context to use
741 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200742 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
745#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746
747/**
748 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200750 *
751 * \param ctx CRT context to use
752 * \param key_usage key usage flags to set
753 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200754 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200756int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
757 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200758
759/**
760 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762 *
763 * \param ctx CRT context to use
764 * \param ns_cert_type Netscape Cert Type flags to set
765 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200766 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 unsigned char ns_cert_type );
770
771/**
772 * \brief Free the contents of a CRT write context
773 *
774 * \param ctx CRT context to free
775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777
778/**
779 * \brief Write a built up certificate to a X509 DER structure
780 * Note: data is written at the end of the buffer! Use the
781 * return value to determine where you should start
782 * using the buffer
783 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100784 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785 * \param buf buffer to write to
786 * \param size size of the buffer
787 * \param f_rng RNG function (for signature, see note)
788 * \param p_rng RNG parameter
789 *
790 * \return length of data written if successful, or a specific
791 * error code
792 *
793 * \note f_rng may be NULL if RSA is used for signature and the
794 * signature is made offline (otherwise f_rng is desirable
795 * for countermeasures against timing attacks).
796 * ECDSA signatures always require a non-NULL f_rng.
797 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799 int (*f_rng)(void *, unsigned char *, size_t),
800 void *p_rng );
801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803/**
804 * \brief Write a built up certificate to a X509 PEM string
805 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100806 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807 * \param buf buffer to write to
808 * \param size size of the buffer
809 * \param f_rng RNG function (for signature, see note)
810 * \param p_rng RNG parameter
811 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200812 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813 *
814 * \note f_rng may be NULL if RSA is used for signature and the
815 * signature is made offline (otherwise f_rng is desirable
816 * for countermeasures against timing attacks).
817 * ECDSA signatures always require a non-NULL f_rng.
818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820 int (*f_rng)(void *, unsigned char *, size_t),
821 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#endif /* MBEDTLS_PEM_WRITE_C */
823#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824
825#ifdef __cplusplus
826}
827#endif
828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#endif /* mbedtls_x509_crt.h */