blob: 4aae923ea0282408d796aa7bc4764548a6e650f6 [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
Bence Szépkútif744bd72020-06-05 13:02:18 +02008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 *
10 * This file is provided under the Apache License 2.0, or the
11 * GNU General Public License v2.0 or later.
12 *
13 * **********
14 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020028 * **********
29 *
30 * **********
31 * GNU General Public License v2.0 or later:
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 *
47 * **********
48 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000049 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_X509_CRT_H
52#define MBEDTLS_X509_CRT_H
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059
60#include "x509.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#include "x509_crl.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062
63/**
64 * \addtogroup x509_module
65 * \{
66 */
67
68#ifdef __cplusplus
69extern "C" {
70#endif
71
72/**
73 * \name Structures and functions for parsing and writing X.509 certificates
74 * \{
75 */
76
77/**
78 * Container for an X.509 certificate. The certificate may be chained.
79 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
83 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020085 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
87 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
90 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
93 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
96 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
101 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
102 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
103 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104
105 int ext_types; /**< Bit string containing detected and parsed extensions */
106 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
107 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+ */
108
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100109 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +0200113 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
116 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
117 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
118 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 +0200119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200121}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200123
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200124/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200125 * Build flag from an algorithm/curve identifier (pk, md, ecp)
126 * Since 0 is always XXX_NONE, ignore it.
127 */
Hanno Beckerd6028a12018-10-15 12:01:35 +0100128#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200129
130/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200131 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200132 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200133 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200134 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200135typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200136{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200137 uint32_t allowed_mds; /**< MDs for signatures */
138 uint32_t allowed_pks; /**< PK algs for signatures */
139 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
140 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200141}
142mbedtls_x509_crt_profile;
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#define MBEDTLS_X509_CRT_VERSION_1 0
145#define MBEDTLS_X509_CRT_VERSION_2 1
146#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
149#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200150
Andres AGf9113192016-09-02 14:06:04 +0100151#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
152#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
153#endif
154
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155/**
156 * Container for writing a certificate (CRT)
157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200159{
160 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_mpi serial;
162 mbedtls_pk_context *subject_key;
163 mbedtls_pk_context *issuer_key;
164 mbedtls_asn1_named_data *subject;
165 mbedtls_asn1_named_data *issuer;
166 mbedtls_md_type_t md_alg;
167 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
168 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
169 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200173/**
174 * Item in a verification chain: cert and flags for it
175 */
176typedef struct {
177 mbedtls_x509_crt *crt;
178 uint32_t flags;
179} mbedtls_x509_crt_verify_chain_item;
180
181/**
182 * Max size of verification chain: end-entity + intermediates + trusted root
183 */
184#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
185
186/**
187 * Verification chain as built by \c mbedtls_crt_verify_chain()
188 */
189typedef struct
190{
191 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200192 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200193} mbedtls_x509_crt_verify_chain;
194
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200195#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
196
197/**
198 * \brief Context for resuming X.509 verify operations
199 */
200typedef struct
201{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200202 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200203 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200204
205 /* for find_parent_in() */
206 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
207 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200208 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200209
210 /* for find_parent() */
211 int parent_is_trusted; /* -1 if find_parent is not in progress */
212
213 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200214 enum {
215 x509_crt_rs_none,
216 x509_crt_rs_find_parent,
217 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200218 int self_cnt;
219 mbedtls_x509_crt_verify_chain ver_chain;
220
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200221} mbedtls_x509_crt_restart_ctx;
222
223#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
224
225/* Now we can declare functions that take a pointer to that */
226typedef void mbedtls_x509_crt_restart_ctx;
227
228#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200231/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200232 * Default security profile. Should provide a good balance between security
233 * and compatibility with current deployments.
234 */
235extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
236
237/**
238 * Expected next default profile. Recommended for new deployments.
239 * Currently targets a 128-bit security level, except for RSA-2048.
240 */
241extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
242
243/**
244 * NSA Suite B profile.
245 */
246extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
247
248/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249 * \brief Parse a single DER formatted certificate and add it
250 * to the chained list.
251 *
252 * \param chain points to the start of the chain
253 * \param buf buffer holding the certificate DER data
254 * \param buflen size of the buffer
255 *
256 * \return 0 if successful, or a specific X509 or PEM error code
257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200259 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260
261/**
Hanno Becker89a91122018-08-23 16:14:00 +0100262 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100263 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264 *
Hanno Beckere8658e22018-08-24 10:01:17 +0100265 * For CRTs in PEM encoding, the function parses permissively:
266 * if at least one certificate can be parsed, the function
Hanno Becker65e619a2018-08-23 15:46:33 +0100267 * returns the number of certificates for which parsing failed
268 * (hence \c 0 if all certificates were parsed successfully).
269 * If no certificate could be parsed, the function returns
270 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100272 * PEM encoded certificates may be interleaved by other data
273 * such as human readable descriptions of their content, as
274 * long as the certificates are enclosed in the PEM specific
275 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
276 *
277 * \param chain The chain to which to add the parsed certificates.
278 * \param buf The buffer holding the certificate data in PEM or DER format.
279 * For certificates in PEM encoding, this may be a concatenation
280 * of multiple certificates; for DER encoding, the buffer must
281 * comprise exactly one certificate.
282 * \param buflen The size of \p buf, including the terminating \c NULL byte
283 * in case of PEM encoded data.
284 *
285 * \return \c 0 if all certificates were parsed successfully.
286 * \return The (positive) number of certificates that couldn't
287 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100288 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100289 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200294/**
295 * \brief Load one or more certificates and add them
296 * to the chained list. Parses permissively. If some
297 * certificates can be parsed, the result is the number
298 * of failed certificates it encountered. If none complete
299 * correctly, the first error is returned.
300 *
301 * \param chain points to the start of the chain
302 * \param path filename to read the certificates from
303 *
304 * \return 0 if all certificates parsed successfully, a positive number
305 * if partly successful or a specific X509 or PEM error code
306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200308
309/**
310 * \brief Load one or more certificate files from a path and add them
311 * to the chained list. Parses permissively. If some
312 * certificates can be parsed, the result is the number
313 * of failed certificates it encountered. If none complete
314 * correctly, the first error is returned.
315 *
316 * \param chain points to the start of the chain
317 * \param path directory / folder to read the certificate files from
318 *
319 * \return 0 if all certificates parsed successfully, a positive number
320 * if partly successful or a specific X509 or PEM error code
321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
323#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200324
325/**
326 * \brief Returns an informational string about the
327 * certificate.
328 *
329 * \param buf Buffer to write to
330 * \param size Maximum size of buffer
331 * \param prefix A line prefix
332 * \param crt The X509 certificate to represent
333 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200334 * \return The length of the string written (not including the
335 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
338 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339
340/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100341 * \brief Returns an informational string about the
342 * verification status of a certificate.
343 *
344 * \param buf Buffer to write to
345 * \param size Maximum size of buffer
346 * \param prefix A line prefix
347 * \param flags Verification flags created by mbedtls_x509_crt_verify()
348 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200349 * \return The length of the string written (not including the
350 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100351 */
352int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200353 uint32_t flags );
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200354
355/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356 * \brief Verify the certificate signature
357 *
358 * The verify callback is a user-supplied callback that
359 * can clear / modify / add flags for a certificate. If set,
360 * the verification callback is called for each
361 * certificate in the chain (from the trust-ca down to the
362 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 * int *flags). With the flags representing current flags for
365 * that specific certificate and the certificate depth from
366 * the bottom (Peer cert depth = 0).
367 *
368 * All flags left after returning from the callback
369 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200370 * return 0 for anything (including invalid certificates)
371 * other than fatal error, as a non-zero return code
372 * immediately aborts the verification process. For fatal
373 * errors, a specific error code should be used (different
374 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
375 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
376 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100378 * \note In case verification failed, the results can be displayed
379 * using \c mbedtls_x509_crt_verify_info()
380 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200381 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
382 * default security profile.
383 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100384 * \note It is your responsibility to provide up-to-date CRLs for
385 * all trusted CAs. If no CRL is provided for the CA that was
386 * used to sign the certificate, CRL verification is skipped
387 * silently, that is *without* setting any flag.
388 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200389 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200390 * (1) those of trusted root CAs, so that certificates
391 * chaining up to those CAs will be trusted, and (2)
392 * self-signed end-entity certificates to be trusted (for
393 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200394 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200395 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100396 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200397 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100398 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399 * \param cn expected Common Name (can be set to
400 * NULL if the CN must not be verified)
401 * \param flags result of the verification
402 * \param f_vrfy verification function
403 * \param p_vrfy verification parameter
404 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200405 * \return 0 (and flags set to 0) if the chain was verified and valid,
406 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
407 * but found to be invalid, in which case *flags will have one
408 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
409 * flags set, or another error (and flags set to 0xffffffff)
410 * in case of a fatal error encountered during the
411 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
414 mbedtls_x509_crt *trust_ca,
415 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200416 const char *cn, uint32_t *flags,
417 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200418 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200420/**
421 * \brief Verify the certificate signature according to profile
422 *
423 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
424 * security profile.
425 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200426 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200427 * for ECDSA) apply to all certificates: trusted root,
428 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200429 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100430 * \param crt a certificate (chain) to be verified
431 * \param trust_ca the list of trusted CAs
432 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200433 * \param profile security profile for verification
434 * \param cn expected Common Name (can be set to
435 * NULL if the CN must not be verified)
436 * \param flags result of the verification
437 * \param f_vrfy verification function
438 * \param p_vrfy verification parameter
439 *
440 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
441 * in which case *flags will have one or more
442 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
443 * set,
444 * or another error in case of a fatal error encountered
445 * during the verification process.
446 */
447int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
448 mbedtls_x509_crt *trust_ca,
449 mbedtls_x509_crl *ca_crl,
450 const mbedtls_x509_crt_profile *profile,
451 const char *cn, uint32_t *flags,
452 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
453 void *p_vrfy );
454
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200455/**
456 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
457 *
458 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
459 * but can return early and restart according to the limit
460 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
461 *
462 * \param crt a certificate (chain) to be verified
463 * \param trust_ca the list of trusted CAs
464 * \param ca_crl the list of CRLs for trusted CAs
465 * \param profile security profile for verification
466 * \param cn expected Common Name (can be set to
467 * NULL if the CN must not be verified)
468 * \param flags result of the verification
469 * \param f_vrfy verification function
470 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200471 * \param rs_ctx restart context (NULL to disable restart)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200472 *
473 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200474 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200475 * operations was reached: see \c mbedtls_ecp_set_max_ops().
476 */
477int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
478 mbedtls_x509_crt *trust_ca,
479 mbedtls_x509_crl *ca_crl,
480 const mbedtls_x509_crt_profile *profile,
481 const char *cn, uint32_t *flags,
482 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
483 void *p_vrfy,
484 mbedtls_x509_crt_restart_ctx *rs_ctx );
485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200487/**
488 * \brief Check usage of certificate against keyUsage extension.
489 *
490 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200491 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
492 * before using the certificate to perform an RSA key
493 * exchange).
494 *
495 * \note Except for decipherOnly and encipherOnly, a bit set in the
496 * usage argument means this bit MUST be set in the
497 * certificate. For decipherOnly and encipherOnly, it means
498 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200499 *
500 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200502 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200503 *
504 * \note You should only call this function on leaf certificates, on
505 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200507 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200508int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
509 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200513/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000514 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200515 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000516 * \param crt Leaf certificate used.
517 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
518 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200520 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000521 * \return 0 if this use of the certificate is allowed,
522 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200523 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000524 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000527 const char *usage_oid,
528 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000529#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200533 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534 *
535 * \param crt a certificate to be verified
536 * \param crl the CRL to verify against
537 *
538 * \return 1 if the certificate is revoked, 0 otherwise
539 *
540 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100541int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543
544/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200545 * \brief Initialize a certificate (chain)
546 *
547 * \param crt Certificate chain to initialize
548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200550
551/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552 * \brief Unallocate all certificate data
553 *
554 * \param crt Certificate chain to free
555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200557
558#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
559/**
560 * \brief Initialize a restart context
561 */
562void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
563
564/**
565 * \brief Free the components of a restart context
566 */
567void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
568#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200570
571/* \} name */
572/* \} addtogroup x509_module */
573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575/**
576 * \brief Initialize a CRT writing context
577 *
578 * \param ctx CRT context to initialize
579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581
582/**
583 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585 *
586 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
588 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591
592/**
593 * \brief Set the serial number for a Certificate.
594 *
595 * \param ctx CRT context to use
596 * \param serial serial number to set
597 *
598 * \return 0 if successful
599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200601
602/**
603 * \brief Set the validity period for a Certificate
604 * Timestamps should be in string format for UTC timezone
605 * i.e. "YYYYMMDDhhmmss"
606 * e.g. "20131231235959" for December 31st 2013
607 * at 23:59:59
608 *
609 * \param ctx CRT context to use
610 * \param not_before not_before timestamp
611 * \param not_after not_after timestamp
612 *
613 * \return 0 if timestamp was parsed successfully, or
614 * a specific error code
615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100617 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618
619/**
620 * \brief Set the issuer name for a Certificate
621 * Issuer names should contain a comma-separated list
622 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000623 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624 *
625 * \param ctx CRT context to use
626 * \param issuer_name issuer name to set
627 *
628 * \return 0 if issuer name was parsed successfully, or
629 * a specific error code
630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100632 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633
634/**
635 * \brief Set the subject name for a Certificate
636 * Subject names should contain a comma-separated list
637 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000638 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639 *
640 * \param ctx CRT context to use
641 * \param subject_name subject name to set
642 *
643 * \return 0 if subject name was parsed successfully, or
644 * a specific error code
645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100647 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648
649/**
650 * \brief Set the subject public key for the certificate
651 *
652 * \param ctx CRT context to use
653 * \param key public key to include
654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656
657/**
658 * \brief Set the issuer key used for signing the certificate
659 *
660 * \param ctx CRT context to use
661 * \param key private key to sign with
662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664
665/**
666 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668 *
669 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100670 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673
674/**
675 * \brief Generic function to add to or replace an extension in the
676 * CRT
677 *
678 * \param ctx CRT context to use
679 * \param oid OID of the extension
680 * \param oid_len length of the OID
681 * \param critical if the extension is critical (per the RFC's definition)
682 * \param val value of the extension OCTET STRING
683 * \param val_len length of the value data
684 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200685 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688 const char *oid, size_t oid_len,
689 int critical,
690 const unsigned char *val, size_t val_len );
691
692/**
693 * \brief Set the basicConstraints extension for a CRT
694 *
695 * \param ctx CRT context to use
696 * \param is_ca is this a CA certificate
697 * \param max_pathlen maximum length of certificate chains below this
698 * certificate (only for CA certificates, -1 is
699 * inlimited)
700 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200701 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704 int is_ca, int max_pathlen );
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707/**
708 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710 * called before
711 *
712 * \param ctx CRT context to use
713 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200714 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717
718/**
719 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721 * called before
722 *
723 * \param ctx CRT context to use
724 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200725 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
728#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200729
730/**
731 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733 *
734 * \param ctx CRT context to use
735 * \param key_usage key usage flags to set
736 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200737 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200739int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
740 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741
742/**
743 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745 *
746 * \param ctx CRT context to use
747 * \param ns_cert_type Netscape Cert Type flags to set
748 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200749 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752 unsigned char ns_cert_type );
753
754/**
755 * \brief Free the contents of a CRT write context
756 *
757 * \param ctx CRT context to free
758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200760
761/**
762 * \brief Write a built up certificate to a X509 DER structure
763 * Note: data is written at the end of the buffer! Use the
764 * return value to determine where you should start
765 * using the buffer
766 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100767 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 * \param buf buffer to write to
769 * \param size size of the buffer
770 * \param f_rng RNG function (for signature, see note)
771 * \param p_rng RNG parameter
772 *
773 * \return length of data written if successful, or a specific
774 * error code
775 *
776 * \note f_rng may be NULL if RSA is used for signature and the
777 * signature is made offline (otherwise f_rng is desirable
778 * for countermeasures against timing attacks).
779 * ECDSA signatures always require a non-NULL f_rng.
780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200782 int (*f_rng)(void *, unsigned char *, size_t),
783 void *p_rng );
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786/**
787 * \brief Write a built up certificate to a X509 PEM string
788 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100789 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200790 * \param buf buffer to write to
791 * \param size size of the buffer
792 * \param f_rng RNG function (for signature, see note)
793 * \param p_rng RNG parameter
794 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200795 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796 *
797 * \note f_rng may be NULL if RSA is used for signature and the
798 * signature is made offline (otherwise f_rng is desirable
799 * for countermeasures against timing attacks).
800 * ECDSA signatures always require a non-NULL f_rng.
801 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803 int (*f_rng)(void *, unsigned char *, size_t),
804 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805#endif /* MBEDTLS_PEM_WRITE_C */
806#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807
808#ifdef __cplusplus
809}
810#endif
811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#endif /* mbedtls_x509_crt.h */