blob: ac23cffe84af48a47838a74987f9dc0e57851c9b [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 */
108typedef struct
109{
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200148 * Default security profile. Should provide a good balance between security
149 * and compatibility with current deployments.
150 */
151extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
152
153/**
154 * Expected next default profile. Recommended for new deployments.
155 * Currently targets a 128-bit security level, except for RSA-2048.
156 */
157extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
158
159/**
160 * NSA Suite B profile.
161 */
162extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
163
164/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165 * \brief Parse a single DER formatted certificate and add it
166 * to the chained list.
167 *
168 * \param chain points to the start of the chain
169 * \param buf buffer holding the certificate DER data
170 * \param buflen size of the buffer
171 *
172 * \return 0 if successful, or a specific X509 or PEM error code
173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200175 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200176
177/**
178 * \brief Parse one or more certificates and add them
179 * to the chained list. Parses permissively. If some
180 * certificates can be parsed, the result is the number
181 * of failed certificates it encountered. If none complete
182 * correctly, the first error is returned.
183 *
184 * \param chain points to the start of the chain
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200185 * \param buf buffer holding the certificate data in PEM or DER format
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186 * \param buflen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200187 * (including the terminating null byte for PEM data)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200188 *
189 * \return 0 if all certificates parsed successfully, a positive number
190 * if partly successful or a specific X509 or PEM error code
191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200195/**
196 * \brief Load one or more certificates and add them
197 * to the chained list. Parses permissively. If some
198 * certificates can be parsed, the result is the number
199 * of failed certificates it encountered. If none complete
200 * correctly, the first error is returned.
201 *
202 * \param chain points to the start of the chain
203 * \param path filename to read the certificates from
204 *
205 * \return 0 if all certificates parsed successfully, a positive number
206 * if partly successful or a specific X509 or PEM error code
207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200209
210/**
211 * \brief Load one or more certificate files from a path and add them
212 * to the chained list. Parses permissively. If some
213 * certificates can be parsed, the result is the number
214 * of failed certificates it encountered. If none complete
215 * correctly, the first error is returned.
216 *
217 * \param chain points to the start of the chain
218 * \param path directory / folder to read the certificate files from
219 *
220 * \return 0 if all certificates parsed successfully, a positive number
221 * if partly successful or a specific X509 or PEM error code
222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
224#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200225
226/**
227 * \brief Returns an informational string about the
228 * certificate.
229 *
230 * \param buf Buffer to write to
231 * \param size Maximum size of buffer
232 * \param prefix A line prefix
233 * \param crt The X509 certificate to represent
234 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200235 * \return The length of the string written (not including the
236 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
239 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200240
241/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100242 * \brief Returns an informational string about the
243 * verification status of a certificate.
244 *
245 * \param buf Buffer to write to
246 * \param size Maximum size of buffer
247 * \param prefix A line prefix
248 * \param flags Verification flags created by mbedtls_x509_crt_verify()
249 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200250 * \return The length of the string written (not including the
251 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100252 */
253int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200254 uint32_t flags );
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200255
256/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200257 * \brief Verify the certificate signature
258 *
259 * The verify callback is a user-supplied callback that
260 * can clear / modify / add flags for a certificate. If set,
261 * the verification callback is called for each
262 * certificate in the chain (from the trust-ca down to the
263 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265 * int *flags). With the flags representing current flags for
266 * that specific certificate and the certificate depth from
267 * the bottom (Peer cert depth = 0).
268 *
269 * All flags left after returning from the callback
270 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200271 * return 0 for anything (including invalid certificates)
272 * other than fatal error, as a non-zero return code
273 * immediately aborts the verification process. For fatal
274 * errors, a specific error code should be used (different
275 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
276 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
277 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100279 * \note In case verification failed, the results can be displayed
280 * using \c mbedtls_x509_crt_verify_info()
281 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200282 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
283 * default security profile.
284 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100285 * \note It is your responsibility to provide up-to-date CRLs for
286 * all trusted CAs. If no CRL is provided for the CA that was
287 * used to sign the certificate, CRL verification is skipped
288 * silently, that is *without* setting any flag.
289 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200290 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200291 * (1) those of trusted root CAs, so that certificates
292 * chaining up to those CAs will be trusted, and (2)
293 * self-signed end-entity certificates to be trusted (for
294 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200295 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200296 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100297 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200298 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100299 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 * \param cn expected Common Name (can be set to
301 * NULL if the CN must not be verified)
302 * \param flags result of the verification
303 * \param f_vrfy verification function
304 * \param p_vrfy verification parameter
305 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200306 * \return 0 (and flags set to 0) if the chain was verified and valid,
307 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
308 * but found to be invalid, in which case *flags will have one
309 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
310 * flags set, or another error (and flags set to 0xffffffff)
311 * in case of a fatal error encountered during the
312 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
315 mbedtls_x509_crt *trust_ca,
316 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200317 const char *cn, uint32_t *flags,
318 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200319 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200320
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200321/**
322 * \brief Verify the certificate signature according to profile
323 *
324 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
325 * security profile.
326 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200327 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200328 * for ECDSA) apply to all certificates: trusted root,
329 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200330 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100331 * \param crt a certificate (chain) to be verified
332 * \param trust_ca the list of trusted CAs
333 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200334 * \param profile security profile for verification
335 * \param cn expected Common Name (can be set to
336 * NULL if the CN must not be verified)
337 * \param flags result of the verification
338 * \param f_vrfy verification function
339 * \param p_vrfy verification parameter
340 *
341 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
342 * in which case *flags will have one or more
343 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
344 * set,
345 * or another error in case of a fatal error encountered
346 * during the verification process.
347 */
348int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
349 mbedtls_x509_crt *trust_ca,
350 mbedtls_x509_crl *ca_crl,
351 const mbedtls_x509_crt_profile *profile,
352 const char *cn, uint32_t *flags,
353 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
354 void *p_vrfy );
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200357/**
358 * \brief Check usage of certificate against keyUsage extension.
359 *
360 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200361 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
362 * before using the certificate to perform an RSA key
363 * exchange).
364 *
365 * \note Except for decipherOnly and encipherOnly, a bit set in the
366 * usage argument means this bit MUST be set in the
367 * certificate. For decipherOnly and encipherOnly, it means
368 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200369 *
370 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200372 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200373 *
374 * \note You should only call this function on leaf certificates, on
375 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200377 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200378int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
379 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200383/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000384 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200385 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000386 * \param crt Leaf certificate used.
387 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
388 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200390 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000391 * \return 0 if this use of the certificate is allowed,
392 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200393 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000394 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000397 const char *usage_oid,
398 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000399#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200403 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404 *
405 * \param crt a certificate to be verified
406 * \param crl the CRL to verify against
407 *
408 * \return 1 if the certificate is revoked, 0 otherwise
409 *
410 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100411int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413
414/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200415 * \brief Initialize a certificate (chain)
416 *
417 * \param crt Certificate chain to initialize
418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200420
421/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422 * \brief Unallocate all certificate data
423 *
424 * \param crt Certificate chain to free
425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
427#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
429/* \} name */
430/* \} addtogroup x509_module */
431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433/**
434 * \brief Initialize a CRT writing context
435 *
436 * \param ctx CRT context to initialize
437 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439
440/**
441 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443 *
444 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
446 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449
450/**
451 * \brief Set the serial number for a Certificate.
452 *
453 * \param ctx CRT context to use
454 * \param serial serial number to set
455 *
456 * \return 0 if successful
457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459
460/**
461 * \brief Set the validity period for a Certificate
462 * Timestamps should be in string format for UTC timezone
463 * i.e. "YYYYMMDDhhmmss"
464 * e.g. "20131231235959" for December 31st 2013
465 * at 23:59:59
466 *
467 * \param ctx CRT context to use
468 * \param not_before not_before timestamp
469 * \param not_after not_after timestamp
470 *
471 * \return 0 if timestamp was parsed successfully, or
472 * a specific error code
473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100475 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476
477/**
478 * \brief Set the issuer name for a Certificate
479 * Issuer names should contain a comma-separated list
480 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000481 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482 *
483 * \param ctx CRT context to use
484 * \param issuer_name issuer name to set
485 *
486 * \return 0 if issuer name was parsed successfully, or
487 * a specific error code
488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100490 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491
492/**
493 * \brief Set the subject name for a Certificate
494 * Subject names should contain a comma-separated list
495 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000496 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 *
498 * \param ctx CRT context to use
499 * \param subject_name subject name to set
500 *
501 * \return 0 if subject name was parsed successfully, or
502 * a specific error code
503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100505 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
507/**
508 * \brief Set the subject public key for the certificate
509 *
510 * \param ctx CRT context to use
511 * \param key public key to include
512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514
515/**
516 * \brief Set the issuer key used for signing the certificate
517 *
518 * \param ctx CRT context to use
519 * \param key private key to sign with
520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522
523/**
524 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526 *
527 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100528 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531
532/**
533 * \brief Generic function to add to or replace an extension in the
534 * CRT
535 *
536 * \param ctx CRT context to use
537 * \param oid OID of the extension
538 * \param oid_len length of the OID
539 * \param critical if the extension is critical (per the RFC's definition)
540 * \param val value of the extension OCTET STRING
541 * \param val_len length of the value data
542 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200543 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546 const char *oid, size_t oid_len,
547 int critical,
548 const unsigned char *val, size_t val_len );
549
550/**
551 * \brief Set the basicConstraints extension for a CRT
552 *
553 * \param ctx CRT context to use
554 * \param is_ca is this a CA certificate
555 * \param max_pathlen maximum length of certificate chains below this
556 * certificate (only for CA certificates, -1 is
557 * inlimited)
558 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200559 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562 int is_ca, int max_pathlen );
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565/**
566 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568 * called before
569 *
570 * \param ctx CRT context to use
571 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200572 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
576/**
577 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579 * called before
580 *
581 * \param ctx CRT context to use
582 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200583 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
586#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587
588/**
589 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591 *
592 * \param ctx CRT context to use
593 * \param key_usage key usage flags to set
594 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200595 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200596 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200597int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
598 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599
600/**
601 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200603 *
604 * \param ctx CRT context to use
605 * \param ns_cert_type Netscape Cert Type flags to set
606 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200607 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610 unsigned char ns_cert_type );
611
612/**
613 * \brief Free the contents of a CRT write context
614 *
615 * \param ctx CRT context to free
616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618
619/**
620 * \brief Write a built up certificate to a X509 DER structure
621 * Note: data is written at the end of the buffer! Use the
622 * return value to determine where you should start
623 * using the buffer
624 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100625 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 * \param buf buffer to write to
627 * \param size size of the buffer
628 * \param f_rng RNG function (for signature, see note)
629 * \param p_rng RNG parameter
630 *
631 * \return length of data written if successful, or a specific
632 * error code
633 *
634 * \note f_rng may be NULL if RSA is used for signature and the
635 * signature is made offline (otherwise f_rng is desirable
636 * for countermeasures against timing attacks).
637 * ECDSA signatures always require a non-NULL f_rng.
638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 int (*f_rng)(void *, unsigned char *, size_t),
641 void *p_rng );
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644/**
645 * \brief Write a built up certificate to a X509 PEM string
646 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100647 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 * \param buf buffer to write to
649 * \param size size of the buffer
650 * \param f_rng RNG function (for signature, see note)
651 * \param p_rng RNG parameter
652 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200653 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654 *
655 * \note f_rng may be NULL if RSA is used for signature and the
656 * signature is made offline (otherwise f_rng is desirable
657 * for countermeasures against timing attacks).
658 * ECDSA signatures always require a non-NULL f_rng.
659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661 int (*f_rng)(void *, unsigned char *, size_t),
662 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#endif /* MBEDTLS_PEM_WRITE_C */
664#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665
666#ifdef __cplusplus
667}
668#endif
669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#endif /* mbedtls_x509_crt.h */