blob: 961db9844fd93ac7dfc0771c9a5a38f9857b9b0a [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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
56 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020058 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
60 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
63 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
66 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
69 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
74 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
75 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
76 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077
78 int ext_types; /**< Bit string containing detected and parsed extensions */
79 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
80 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+ */
81
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +010082 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020086 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
89 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
90 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
91 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 +020092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020097/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020098 * Build flag from an algorithm/curve identifier (pk, md, ecp)
99 * Since 0 is always XXX_NONE, ignore it.
100 */
101#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( id - 1 ) )
102
103/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200104 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200105 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200106 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200107 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200108typedef struct mbedtls_x509_crt_profile
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200109{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200110 uint32_t allowed_mds; /**< MDs for signatures */
111 uint32_t allowed_pks; /**< PK algs for signatures */
112 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
113 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200114}
115mbedtls_x509_crt_profile;
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#define MBEDTLS_X509_CRT_VERSION_1 0
118#define MBEDTLS_X509_CRT_VERSION_2 1
119#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
122#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200123
Andres AGf9113192016-09-02 14:06:04 +0100124#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
125#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
126#endif
127
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200128/**
129 * Container for writing a certificate (CRT)
130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200132{
133 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_mpi serial;
135 mbedtls_pk_context *subject_key;
136 mbedtls_pk_context *issuer_key;
137 mbedtls_asn1_named_data *subject;
138 mbedtls_asn1_named_data *issuer;
139 mbedtls_md_type_t md_alg;
140 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
141 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
142 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200143}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200146/**
147 * Item in a verification chain: cert and flags for it
148 */
149typedef struct {
150 mbedtls_x509_crt *crt;
151 uint32_t flags;
152} mbedtls_x509_crt_verify_chain_item;
153
154/**
155 * Max size of verification chain: end-entity + intermediates + trusted root
156 */
157#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
158
159/**
160 * Verification chain as built by \c mbedtls_crt_verify_chain()
161 */
162typedef struct
163{
164 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +0200165 unsigned len;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200166} mbedtls_x509_crt_verify_chain;
167
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200168#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
169
170/**
171 * \brief Context for resuming X.509 verify operations
172 */
173typedef struct
174{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200175 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200176 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200177
178 /* for find_parent_in() */
179 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
180 mbedtls_x509_crt *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +0200181 int fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200182
183 /* for find_parent() */
184 int parent_is_trusted; /* -1 if find_parent is not in progress */
185
186 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200187 enum {
188 x509_crt_rs_none,
189 x509_crt_rs_find_parent,
190 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200191 int self_cnt;
192 mbedtls_x509_crt_verify_chain ver_chain;
193
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200194} mbedtls_x509_crt_restart_ctx;
195
196#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
197
198/* Now we can declare functions that take a pointer to that */
199typedef void mbedtls_x509_crt_restart_ctx;
200
201#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200204/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200205 * Default security profile. Should provide a good balance between security
206 * and compatibility with current deployments.
207 */
208extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
209
210/**
211 * Expected next default profile. Recommended for new deployments.
212 * Currently targets a 128-bit security level, except for RSA-2048.
213 */
214extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
215
216/**
217 * NSA Suite B profile.
218 */
219extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
220
221/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200222 * \brief Parse a single DER formatted certificate and add it
223 * to the chained list.
224 *
225 * \param chain points to the start of the chain
226 * \param buf buffer holding the certificate DER data
227 * \param buflen size of the buffer
228 *
229 * \return 0 if successful, or a specific X509 or PEM error code
230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200232 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200233
234/**
235 * \brief Parse one or more certificates and add them
236 * to the chained list. Parses permissively. If some
237 * certificates can be parsed, the result is the number
238 * of failed certificates it encountered. If none complete
239 * correctly, the first error is returned.
240 *
241 * \param chain points to the start of the chain
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200242 * \param buf buffer holding the certificate data in PEM or DER format
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200243 * \param buflen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200244 * (including the terminating null byte for PEM data)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200245 *
246 * \return 0 if all certificates parsed successfully, a positive number
247 * if partly successful or a specific X509 or PEM error code
248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200252/**
253 * \brief Load one or more certificates and add them
254 * to the chained list. Parses permissively. If some
255 * certificates can be parsed, the result is the number
256 * of failed certificates it encountered. If none complete
257 * correctly, the first error is returned.
258 *
259 * \param chain points to the start of the chain
260 * \param path filename to read the certificates from
261 *
262 * \return 0 if all certificates parsed successfully, a positive number
263 * if partly successful or a specific X509 or PEM error code
264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200266
267/**
268 * \brief Load one or more certificate files from a path and add them
269 * to the chained list. Parses permissively. If some
270 * certificates can be parsed, the result is the number
271 * of failed certificates it encountered. If none complete
272 * correctly, the first error is returned.
273 *
274 * \param chain points to the start of the chain
275 * \param path directory / folder to read the certificate files from
276 *
277 * \return 0 if all certificates parsed successfully, a positive number
278 * if partly successful or a specific X509 or PEM error code
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
281#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200282
283/**
284 * \brief Returns an informational string about the
285 * certificate.
286 *
287 * \param buf Buffer to write to
288 * \param size Maximum size of buffer
289 * \param prefix A line prefix
290 * \param crt The X509 certificate to represent
291 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200292 * \return The length of the string written (not including the
293 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
296 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200297
298/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100299 * \brief Returns an informational string about the
300 * verification status of a certificate.
301 *
302 * \param buf Buffer to write to
303 * \param size Maximum size of buffer
304 * \param prefix A line prefix
305 * \param flags Verification flags created by mbedtls_x509_crt_verify()
306 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200307 * \return The length of the string written (not including the
308 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100309 */
310int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200311 uint32_t flags );
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200312
313/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200314 * \brief Verify the certificate signature
315 *
316 * The verify callback is a user-supplied callback that
317 * can clear / modify / add flags for a certificate. If set,
318 * the verification callback is called for each
319 * certificate in the chain (from the trust-ca down to the
320 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200322 * int *flags). With the flags representing current flags for
323 * that specific certificate and the certificate depth from
324 * the bottom (Peer cert depth = 0).
325 *
326 * All flags left after returning from the callback
327 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200328 * return 0 for anything (including invalid certificates)
329 * other than fatal error, as a non-zero return code
330 * immediately aborts the verification process. For fatal
331 * errors, a specific error code should be used (different
332 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
333 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
334 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100336 * \note In case verification failed, the results can be displayed
337 * using \c mbedtls_x509_crt_verify_info()
338 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200339 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
340 * default security profile.
341 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100342 * \note It is your responsibility to provide up-to-date CRLs for
343 * all trusted CAs. If no CRL is provided for the CA that was
344 * used to sign the certificate, CRL verification is skipped
345 * silently, that is *without* setting any flag.
346 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200347 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200348 * (1) those of trusted root CAs, so that certificates
349 * chaining up to those CAs will be trusted, and (2)
350 * self-signed end-entity certificates to be trusted (for
351 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200352 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200353 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100354 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200355 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100356 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357 * \param cn expected Common Name (can be set to
358 * NULL if the CN must not be verified)
359 * \param flags result of the verification
360 * \param f_vrfy verification function
361 * \param p_vrfy verification parameter
362 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200363 * \return 0 (and flags set to 0) if the chain was verified and valid,
364 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
365 * but found to be invalid, in which case *flags will have one
366 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
367 * flags set, or another error (and flags set to 0xffffffff)
368 * in case of a fatal error encountered during the
369 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
372 mbedtls_x509_crt *trust_ca,
373 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200374 const char *cn, uint32_t *flags,
375 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200376 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200378/**
379 * \brief Verify the certificate signature according to profile
380 *
381 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
382 * security profile.
383 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200384 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200385 * for ECDSA) apply to all certificates: trusted root,
386 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200387 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100388 * \param crt a certificate (chain) to be verified
389 * \param trust_ca the list of trusted CAs
390 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200391 * \param profile security profile for verification
392 * \param cn expected Common Name (can be set to
393 * NULL if the CN must not be verified)
394 * \param flags result of the verification
395 * \param f_vrfy verification function
396 * \param p_vrfy verification parameter
397 *
398 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
399 * in which case *flags will have one or more
400 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
401 * set,
402 * or another error in case of a fatal error encountered
403 * during the verification process.
404 */
405int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
406 mbedtls_x509_crt *trust_ca,
407 mbedtls_x509_crl *ca_crl,
408 const mbedtls_x509_crt_profile *profile,
409 const char *cn, uint32_t *flags,
410 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
411 void *p_vrfy );
412
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200413/**
414 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
415 *
416 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
417 * but can return early and restart according to the limit
418 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
419 *
420 * \param crt a certificate (chain) to be verified
421 * \param trust_ca the list of trusted CAs
422 * \param ca_crl the list of CRLs for trusted CAs
423 * \param profile security profile for verification
424 * \param cn expected Common Name (can be set to
425 * NULL if the CN must not be verified)
426 * \param flags result of the verification
427 * \param f_vrfy verification function
428 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +0200429 * \param rs_ctx restart context
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200430 *
431 * \return See \c mbedtls_crt_verify_with_profile(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200432 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200433 * operations was reached: see \c mbedtls_ecp_set_max_ops().
434 */
435int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
436 mbedtls_x509_crt *trust_ca,
437 mbedtls_x509_crl *ca_crl,
438 const mbedtls_x509_crt_profile *profile,
439 const char *cn, uint32_t *flags,
440 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
441 void *p_vrfy,
442 mbedtls_x509_crt_restart_ctx *rs_ctx );
443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200445/**
446 * \brief Check usage of certificate against keyUsage extension.
447 *
448 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200449 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
450 * before using the certificate to perform an RSA key
451 * exchange).
452 *
453 * \note Except for decipherOnly and encipherOnly, a bit set in the
454 * usage argument means this bit MUST be set in the
455 * certificate. For decipherOnly and encipherOnly, it means
456 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200457 *
458 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200460 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200461 *
462 * \note You should only call this function on leaf certificates, on
463 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200465 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200466int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
467 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200471/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000472 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200473 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000474 * \param crt Leaf certificate used.
475 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
476 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200478 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000479 * \return 0 if this use of the certificate is allowed,
480 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200481 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000482 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000485 const char *usage_oid,
486 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000487#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200491 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492 *
493 * \param crt a certificate to be verified
494 * \param crl the CRL to verify against
495 *
496 * \return 1 if the certificate is revoked, 0 otherwise
497 *
498 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100499int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501
502/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200503 * \brief Initialize a certificate (chain)
504 *
505 * \param crt Certificate chain to initialize
506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200508
509/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510 * \brief Unallocate all certificate data
511 *
512 * \param crt Certificate chain to free
513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200515
516#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
517/**
518 * \brief Initialize a restart context
519 */
520void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
521
522/**
523 * \brief Free the components of a restart context
524 */
525void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
526#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528
529/* \} name */
530/* \} addtogroup x509_module */
531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533/**
534 * \brief Initialize a CRT writing context
535 *
536 * \param ctx CRT context to initialize
537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539
540/**
541 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543 *
544 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
546 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549
550/**
551 * \brief Set the serial number for a Certificate.
552 *
553 * \param ctx CRT context to use
554 * \param serial serial number to set
555 *
556 * \return 0 if successful
557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559
560/**
561 * \brief Set the validity period for a Certificate
562 * Timestamps should be in string format for UTC timezone
563 * i.e. "YYYYMMDDhhmmss"
564 * e.g. "20131231235959" for December 31st 2013
565 * at 23:59:59
566 *
567 * \param ctx CRT context to use
568 * \param not_before not_before timestamp
569 * \param not_after not_after timestamp
570 *
571 * \return 0 if timestamp was parsed successfully, or
572 * a specific error code
573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100575 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576
577/**
578 * \brief Set the issuer name for a Certificate
579 * Issuer names should contain a comma-separated list
580 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000581 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582 *
583 * \param ctx CRT context to use
584 * \param issuer_name issuer name to set
585 *
586 * \return 0 if issuer name was parsed successfully, or
587 * a specific error code
588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100590 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591
592/**
593 * \brief Set the subject name for a Certificate
594 * Subject names should contain a comma-separated list
595 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000596 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597 *
598 * \param ctx CRT context to use
599 * \param subject_name subject name to set
600 *
601 * \return 0 if subject name was parsed successfully, or
602 * a specific error code
603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100605 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200606
607/**
608 * \brief Set the subject public key for the certificate
609 *
610 * \param ctx CRT context to use
611 * \param key public key to include
612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614
615/**
616 * \brief Set the issuer key used for signing the certificate
617 *
618 * \param ctx CRT context to use
619 * \param key private key to sign with
620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622
623/**
624 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 *
627 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100628 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631
632/**
633 * \brief Generic function to add to or replace an extension in the
634 * CRT
635 *
636 * \param ctx CRT context to use
637 * \param oid OID of the extension
638 * \param oid_len length of the OID
639 * \param critical if the extension is critical (per the RFC's definition)
640 * \param val value of the extension OCTET STRING
641 * \param val_len length of the value data
642 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200643 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646 const char *oid, size_t oid_len,
647 int critical,
648 const unsigned char *val, size_t val_len );
649
650/**
651 * \brief Set the basicConstraints extension for a CRT
652 *
653 * \param ctx CRT context to use
654 * \param is_ca is this a CA certificate
655 * \param max_pathlen maximum length of certificate chains below this
656 * certificate (only for CA certificates, -1 is
657 * inlimited)
658 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200659 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662 int is_ca, int max_pathlen );
663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665/**
666 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668 * called before
669 *
670 * \param ctx CRT context to use
671 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200672 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675
676/**
677 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200679 * called before
680 *
681 * \param ctx CRT context to use
682 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200683 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
686#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687
688/**
689 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 *
692 * \param ctx CRT context to use
693 * \param key_usage key usage flags to set
694 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200695 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200697int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
698 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699
700/**
701 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703 *
704 * \param ctx CRT context to use
705 * \param ns_cert_type Netscape Cert Type flags to set
706 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200707 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710 unsigned char ns_cert_type );
711
712/**
713 * \brief Free the contents of a CRT write context
714 *
715 * \param ctx CRT context to free
716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718
719/**
720 * \brief Write a built up certificate to a X509 DER structure
721 * Note: data is written at the end of the buffer! Use the
722 * return value to determine where you should start
723 * using the buffer
724 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100725 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726 * \param buf buffer to write to
727 * \param size size of the buffer
728 * \param f_rng RNG function (for signature, see note)
729 * \param p_rng RNG parameter
730 *
731 * \return length of data written if successful, or a specific
732 * error code
733 *
734 * \note f_rng may be NULL if RSA is used for signature and the
735 * signature is made offline (otherwise f_rng is desirable
736 * for countermeasures against timing attacks).
737 * ECDSA signatures always require a non-NULL f_rng.
738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740 int (*f_rng)(void *, unsigned char *, size_t),
741 void *p_rng );
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200744/**
745 * \brief Write a built up certificate to a X509 PEM string
746 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100747 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748 * \param buf buffer to write to
749 * \param size size of the buffer
750 * \param f_rng RNG function (for signature, see note)
751 * \param p_rng RNG parameter
752 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200753 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200754 *
755 * \note f_rng may be NULL if RSA is used for signature and the
756 * signature is made offline (otherwise f_rng is desirable
757 * for countermeasures against timing attacks).
758 * ECDSA signatures always require a non-NULL f_rng.
759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761 int (*f_rng)(void *, unsigned char *, size_t),
762 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#endif /* MBEDTLS_PEM_WRITE_C */
764#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765
766#ifdef __cplusplus
767}
768#endif
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#endif /* mbedtls_x509_crt.h */