blob: 68318127027ef29a262b5f8314c92ed6ef1b5bdd [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
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_X509_CRT_H
24#define MBEDTLS_X509_CRT_H
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031
32#include "x509.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033#include "x509_crl.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034
35/**
36 * \addtogroup x509_module
37 * \{
38 */
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/**
45 * \name Structures and functions for parsing and writing X.509 certificates
46 * \{
47 */
48
49/**
50 * Container for an X.509 certificate. The certificate may be chained.
51 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
55 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020057 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
59 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
62 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
65 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
68 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
73 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
74 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
75 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076
77 int ext_types; /**< Bit string containing detected and parsed extensions */
78 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
79 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+ */
80
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +010081 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020085 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
88 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
89 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
90 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 +020091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020096/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020097 * Build flag from an algorithm/curve identifier (pk, md, ecp)
98 * Since 0 is always XXX_NONE, ignore it.
99 */
100#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( id - 1 ) )
101
102/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200103 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200104 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200105 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200106 */
107typedef struct
108{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200109 uint32_t allowed_mds; /**< MDs for signatures */
110 uint32_t allowed_pks; /**< PK algs for signatures */
111 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
112 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200113}
114mbedtls_x509_crt_profile;
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#define MBEDTLS_X509_CRT_VERSION_1 0
117#define MBEDTLS_X509_CRT_VERSION_2 1
118#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
121#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122
Andres AGf9113192016-09-02 14:06:04 +0100123#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
124#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
125#endif
126
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127/**
128 * Container for writing a certificate (CRT)
129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200131{
132 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_mpi serial;
134 mbedtls_pk_context *subject_key;
135 mbedtls_pk_context *issuer_key;
136 mbedtls_asn1_named_data *subject;
137 mbedtls_asn1_named_data *issuer;
138 mbedtls_md_type_t md_alg;
139 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
140 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
141 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200144
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +0200145/**
146 * Item in a verification chain: cert and flags for it
147 */
148typedef struct {
149 mbedtls_x509_crt *crt;
150 uint32_t flags;
151} mbedtls_x509_crt_verify_chain_item;
152
153/**
154 * Max size of verification chain: end-entity + intermediates + trusted root
155 */
156#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
157
158/**
159 * Verification chain as built by \c mbedtls_crt_verify_chain()
160 */
161typedef struct
162{
163 mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
164 size_t len;
165} mbedtls_x509_crt_verify_chain;
166
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200167#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
168
169/**
170 * \brief Context for resuming X.509 verify operations
171 */
172typedef struct
173{
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200174 /* for check_signature() */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200175 mbedtls_pk_restart_ctx pk;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200176
177 /* for find_parent_in() */
178 mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
179 mbedtls_x509_crt *fallback_parent;
180 int fallback_sign_good;
181
182 /* for find_parent() */
183 int parent_is_trusted; /* -1 if find_parent is not in progress */
184
185 /* for verify_chain() */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +0200186 enum {
187 x509_crt_rs_none,
188 x509_crt_rs_find_parent,
189 } in_progress; /* none if no operation is in progress */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200190 int self_cnt;
191 mbedtls_x509_crt_verify_chain ver_chain;
192
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200193} mbedtls_x509_crt_restart_ctx;
194
195#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
196
197/* Now we can declare functions that take a pointer to that */
198typedef void mbedtls_x509_crt_restart_ctx;
199
200#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200203/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200204 * Default security profile. Should provide a good balance between security
205 * and compatibility with current deployments.
206 */
207extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
208
209/**
210 * Expected next default profile. Recommended for new deployments.
211 * Currently targets a 128-bit security level, except for RSA-2048.
212 */
213extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
214
215/**
216 * NSA Suite B profile.
217 */
218extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
219
220/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221 * \brief Parse a single DER formatted certificate and add it
222 * to the chained list.
223 *
224 * \param chain points to the start of the chain
225 * \param buf buffer holding the certificate DER data
226 * \param buflen size of the buffer
227 *
228 * \return 0 if successful, or a specific X509 or PEM error code
229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200231 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200232
233/**
234 * \brief Parse one or more certificates and add them
235 * to the chained list. Parses permissively. If some
236 * certificates can be parsed, the result is the number
237 * of failed certificates it encountered. If none complete
238 * correctly, the first error is returned.
239 *
240 * \param chain points to the start of the chain
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200241 * \param buf buffer holding the certificate data in PEM or DER format
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242 * \param buflen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200243 * (including the terminating null byte for PEM data)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200244 *
245 * \return 0 if all certificates parsed successfully, a positive number
246 * if partly successful or a specific X509 or PEM error code
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200251/**
252 * \brief Load one or more certificates and add them
253 * to the chained list. Parses permissively. If some
254 * certificates can be parsed, the result is the number
255 * of failed certificates it encountered. If none complete
256 * correctly, the first error is returned.
257 *
258 * \param chain points to the start of the chain
259 * \param path filename to read the certificates from
260 *
261 * \return 0 if all certificates parsed successfully, a positive number
262 * if partly successful or a specific X509 or PEM error code
263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265
266/**
267 * \brief Load one or more certificate files from a path and add them
268 * to the chained list. Parses permissively. If some
269 * certificates can be parsed, the result is the number
270 * of failed certificates it encountered. If none complete
271 * correctly, the first error is returned.
272 *
273 * \param chain points to the start of the chain
274 * \param path directory / folder to read the certificate files from
275 *
276 * \return 0 if all certificates parsed successfully, a positive number
277 * if partly successful or a specific X509 or PEM error code
278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
280#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281
282/**
283 * \brief Returns an informational string about the
284 * certificate.
285 *
286 * \param buf Buffer to write to
287 * \param size Maximum size of buffer
288 * \param prefix A line prefix
289 * \param crt The X509 certificate to represent
290 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200291 * \return The length of the string written (not including the
292 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
295 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296
297/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100298 * \brief Returns an informational string about the
299 * verification status of a certificate.
300 *
301 * \param buf Buffer to write to
302 * \param size Maximum size of buffer
303 * \param prefix A line prefix
304 * \param flags Verification flags created by mbedtls_x509_crt_verify()
305 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200306 * \return The length of the string written (not including the
307 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100308 */
309int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200310 uint32_t flags );
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200311
312/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313 * \brief Verify the certificate signature
314 *
315 * The verify callback is a user-supplied callback that
316 * can clear / modify / add flags for a certificate. If set,
317 * the verification callback is called for each
318 * certificate in the chain (from the trust-ca down to the
319 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200321 * int *flags). With the flags representing current flags for
322 * that specific certificate and the certificate depth from
323 * the bottom (Peer cert depth = 0).
324 *
325 * All flags left after returning from the callback
326 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200327 * return 0 for anything (including invalid certificates)
328 * other than fatal error, as a non-zero return code
329 * immediately aborts the verification process. For fatal
330 * errors, a specific error code should be used (different
331 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
332 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
333 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200334 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100335 * \note In case verification failed, the results can be displayed
336 * using \c mbedtls_x509_crt_verify_info()
337 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200338 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
339 * default security profile.
340 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100341 * \note It is your responsibility to provide up-to-date CRLs for
342 * all trusted CAs. If no CRL is provided for the CA that was
343 * used to sign the certificate, CRL verification is skipped
344 * silently, that is *without* setting any flag.
345 *
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200346 * \note The \c trust_ca list can contain two type of certificates:
347 * (1) those of trusted root CAs, so that certificates
348 * chaining up to those CAs will be trusted, and (2)
349 * self-signed end-entity certificates to be trusted (for
350 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200351 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200352 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100353 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200354 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100355 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356 * \param cn expected Common Name (can be set to
357 * NULL if the CN must not be verified)
358 * \param flags result of the verification
359 * \param f_vrfy verification function
360 * \param p_vrfy verification parameter
361 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200362 * \return 0 (and flags set to 0) if the chain was verified and valid,
363 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
364 * but found to be invalid, in which case *flags will have one
365 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
366 * flags set, or another error (and flags set to 0xffffffff)
367 * in case of a fatal error encountered during the
368 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
371 mbedtls_x509_crt *trust_ca,
372 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200373 const char *cn, uint32_t *flags,
374 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200375 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200377/**
378 * \brief Verify the certificate signature according to profile
379 *
380 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
381 * security profile.
382 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200383 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200384 * for ECDSA) apply to all certificates: trusted root,
385 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200386 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100387 * \param crt a certificate (chain) to be verified
388 * \param trust_ca the list of trusted CAs
389 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200390 * \param profile security profile for verification
391 * \param cn expected Common Name (can be set to
392 * NULL if the CN must not be verified)
393 * \param flags result of the verification
394 * \param f_vrfy verification function
395 * \param p_vrfy verification parameter
396 *
397 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
398 * in which case *flags will have one or more
399 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
400 * set,
401 * or another error in case of a fatal error encountered
402 * during the verification process.
403 */
404int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
405 mbedtls_x509_crt *trust_ca,
406 mbedtls_x509_crl *ca_crl,
407 const mbedtls_x509_crt_profile *profile,
408 const char *cn, uint32_t *flags,
409 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
410 void *p_vrfy );
411
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200412/**
413 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
414 *
415 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
416 * but can return early and restart according to the limit
417 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
418 *
419 * \param crt a certificate (chain) to be verified
420 * \param trust_ca the list of trusted CAs
421 * \param ca_crl the list of CRLs for trusted CAs
422 * \param profile security profile for verification
423 * \param cn expected Common Name (can be set to
424 * NULL if the CN must not be verified)
425 * \param flags result of the verification
426 * \param f_vrfy verification function
427 * \param p_vrfy verification parameter
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +0200428 * \param rs_ctx restart context
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200429 *
430 * \return See \c mbedtls_crt_verify_with_profile(), or
431 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
432 * operations was reached: see \c mbedtls_ecp_set_max_ops().
433 */
434int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
435 mbedtls_x509_crt *trust_ca,
436 mbedtls_x509_crl *ca_crl,
437 const mbedtls_x509_crt_profile *profile,
438 const char *cn, uint32_t *flags,
439 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
440 void *p_vrfy,
441 mbedtls_x509_crt_restart_ctx *rs_ctx );
442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200444/**
445 * \brief Check usage of certificate against keyUsage extension.
446 *
447 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200448 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
449 * before using the certificate to perform an RSA key
450 * exchange).
451 *
452 * \note Except for decipherOnly and encipherOnly, a bit set in the
453 * usage argument means this bit MUST be set in the
454 * certificate. For decipherOnly and encipherOnly, it means
455 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200456 *
457 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200459 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200460 *
461 * \note You should only call this function on leaf certificates, on
462 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200464 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200465int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
466 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200470/**
471 * \brief Check usage of certificate against extentedJeyUsage.
472 *
473 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or MBEDTLS_OID_CLIENT_AUTH).
475 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200476 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200477 * \return 0 if this use of the certificate is allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200479 *
480 * \note Usually only makes sense on leaf certificates.
481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200483 const char *usage_oid,
484 size_t usage_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200489 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 *
491 * \param crt a certificate to be verified
492 * \param crl the CRL to verify against
493 *
494 * \return 1 if the certificate is revoked, 0 otherwise
495 *
496 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100497int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499
500/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200501 * \brief Initialize a certificate (chain)
502 *
503 * \param crt Certificate chain to initialize
504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200506
507/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508 * \brief Unallocate all certificate data
509 *
510 * \param crt Certificate chain to free
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +0200513
514#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
515/**
516 * \brief Initialize a restart context
517 */
518void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
519
520/**
521 * \brief Free the components of a restart context
522 */
523void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
524#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526
527/* \} name */
528/* \} addtogroup x509_module */
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531/**
532 * \brief Initialize a CRT writing context
533 *
534 * \param ctx CRT context to initialize
535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
538/**
539 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541 *
542 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
544 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547
548/**
549 * \brief Set the serial number for a Certificate.
550 *
551 * \param ctx CRT context to use
552 * \param serial serial number to set
553 *
554 * \return 0 if successful
555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557
558/**
559 * \brief Set the validity period for a Certificate
560 * Timestamps should be in string format for UTC timezone
561 * i.e. "YYYYMMDDhhmmss"
562 * e.g. "20131231235959" for December 31st 2013
563 * at 23:59:59
564 *
565 * \param ctx CRT context to use
566 * \param not_before not_before timestamp
567 * \param not_after not_after timestamp
568 *
569 * \return 0 if timestamp was parsed successfully, or
570 * a specific error code
571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100573 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574
575/**
576 * \brief Set the issuer name for a Certificate
577 * Issuer names should contain a comma-separated list
578 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000579 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580 *
581 * \param ctx CRT context to use
582 * \param issuer_name issuer name to set
583 *
584 * \return 0 if issuer name was parsed successfully, or
585 * a specific error code
586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100588 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589
590/**
591 * \brief Set the subject name for a Certificate
592 * Subject names should contain a comma-separated list
593 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000594 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595 *
596 * \param ctx CRT context to use
597 * \param subject_name subject name to set
598 *
599 * \return 0 if subject name was parsed successfully, or
600 * a specific error code
601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100603 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200604
605/**
606 * \brief Set the subject public key for the certificate
607 *
608 * \param ctx CRT context to use
609 * \param key public key to include
610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612
613/**
614 * \brief Set the issuer key used for signing the certificate
615 *
616 * \param ctx CRT context to use
617 * \param key private key to sign with
618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620
621/**
622 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624 *
625 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100626 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629
630/**
631 * \brief Generic function to add to or replace an extension in the
632 * CRT
633 *
634 * \param ctx CRT context to use
635 * \param oid OID of the extension
636 * \param oid_len length of the OID
637 * \param critical if the extension is critical (per the RFC's definition)
638 * \param val value of the extension OCTET STRING
639 * \param val_len length of the value data
640 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200641 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644 const char *oid, size_t oid_len,
645 int critical,
646 const unsigned char *val, size_t val_len );
647
648/**
649 * \brief Set the basicConstraints extension for a CRT
650 *
651 * \param ctx CRT context to use
652 * \param is_ca is this a CA certificate
653 * \param max_pathlen maximum length of certificate chains below this
654 * certificate (only for CA certificates, -1 is
655 * inlimited)
656 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200657 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660 int is_ca, int max_pathlen );
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663/**
664 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666 * called before
667 *
668 * \param ctx CRT context to use
669 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200670 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673
674/**
675 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 * called before
678 *
679 * \param ctx CRT context to use
680 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200681 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
684#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685
686/**
687 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689 *
690 * \param ctx CRT context to use
691 * \param key_usage key usage flags to set
692 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200693 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200695int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
696 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697
698/**
699 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701 *
702 * \param ctx CRT context to use
703 * \param ns_cert_type Netscape Cert Type flags to set
704 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200705 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 unsigned char ns_cert_type );
709
710/**
711 * \brief Free the contents of a CRT write context
712 *
713 * \param ctx CRT context to free
714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716
717/**
718 * \brief Write a built up certificate to a X509 DER structure
719 * Note: data is written at the end of the buffer! Use the
720 * return value to determine where you should start
721 * using the buffer
722 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100723 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724 * \param buf buffer to write to
725 * \param size size of the buffer
726 * \param f_rng RNG function (for signature, see note)
727 * \param p_rng RNG parameter
728 *
729 * \return length of data written if successful, or a specific
730 * error code
731 *
732 * \note f_rng may be NULL if RSA is used for signature and the
733 * signature is made offline (otherwise f_rng is desirable
734 * for countermeasures against timing attacks).
735 * ECDSA signatures always require a non-NULL f_rng.
736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 int (*f_rng)(void *, unsigned char *, size_t),
739 void *p_rng );
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200742/**
743 * \brief Write a built up certificate to a X509 PEM string
744 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100745 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746 * \param buf buffer to write to
747 * \param size size of the buffer
748 * \param f_rng RNG function (for signature, see note)
749 * \param p_rng RNG parameter
750 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200751 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752 *
753 * \note f_rng may be NULL if RSA is used for signature and the
754 * signature is made offline (otherwise f_rng is desirable
755 * for countermeasures against timing attacks).
756 * ECDSA signatures always require a non-NULL f_rng.
757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200759 int (*f_rng)(void *, unsigned char *, size_t),
760 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761#endif /* MBEDTLS_PEM_WRITE_C */
762#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763
764#ifdef __cplusplus
765}
766#endif
767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#endif /* mbedtls_x509_crt.h */