blob: 9227701d316087a531385adf7e3053334021b178 [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é-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/**
Hanno Becker89a91122018-08-23 16:14:00 +0100178 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
Hanno Becker65e619a2018-08-23 15:46:33 +0100179 * certificates and add them to the chained list.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200180 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100181 * For PEM-encoded CRTs, the function parses permissively:
182 * If at least one certificate can be parsed, the function
183 * returns the number of certificates for which parsing failed
184 * (hence \c 0 if all certificates were parsed successfully).
185 * If no certificate could be parsed, the function returns
186 * the first (negative) error encountered during parsing.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200187 *
Hanno Becker65e619a2018-08-23 15:46:33 +0100188 * PEM encoded certificates may be interleaved by other data
189 * such as human readable descriptions of their content, as
190 * long as the certificates are enclosed in the PEM specific
191 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
192 *
193 * \param chain The chain to which to add the parsed certificates.
194 * \param buf The buffer holding the certificate data in PEM or DER format.
195 * For certificates in PEM encoding, this may be a concatenation
196 * of multiple certificates; for DER encoding, the buffer must
197 * comprise exactly one certificate.
198 * \param buflen The size of \p buf, including the terminating \c NULL byte
199 * in case of PEM encoded data.
200 *
201 * \return \c 0 if all certificates were parsed successfully.
202 * \return The (positive) number of certificates that couldn't
203 * be parsed if parsing was partly successful (see above).
Hanno Becker89a91122018-08-23 16:14:00 +0100204 * \return A negative X509 or PEM error code otherwise.
Hanno Becker65e619a2018-08-23 15:46:33 +0100205 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200210/**
211 * \brief Load one or more certificates 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 filename to read the certificates 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_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200224
225/**
226 * \brief Load one or more certificate files from a path and add them
227 * to the chained list. Parses permissively. If some
228 * certificates can be parsed, the result is the number
229 * of failed certificates it encountered. If none complete
230 * correctly, the first error is returned.
231 *
232 * \param chain points to the start of the chain
233 * \param path directory / folder to read the certificate files from
234 *
235 * \return 0 if all certificates parsed successfully, a positive number
236 * if partly successful or a specific X509 or PEM error code
237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
239#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200240
241/**
242 * \brief Returns an informational string about the
243 * certificate.
244 *
245 * \param buf Buffer to write to
246 * \param size Maximum size of buffer
247 * \param prefix A line prefix
248 * \param crt The X509 certificate to represent
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.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
254 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200255
256/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100257 * \brief Returns an informational string about the
258 * verification status of a certificate.
259 *
260 * \param buf Buffer to write to
261 * \param size Maximum size of buffer
262 * \param prefix A line prefix
263 * \param flags Verification flags created by mbedtls_x509_crt_verify()
264 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200265 * \return The length of the string written (not including the
266 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100267 */
268int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200269 uint32_t flags );
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200270
271/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200272 * \brief Verify the certificate signature
273 *
274 * The verify callback is a user-supplied callback that
275 * can clear / modify / add flags for a certificate. If set,
276 * the verification callback is called for each
277 * certificate in the chain (from the trust-ca down to the
278 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280 * int *flags). With the flags representing current flags for
281 * that specific certificate and the certificate depth from
282 * the bottom (Peer cert depth = 0).
283 *
284 * All flags left after returning from the callback
285 * are also returned to the application. The function should
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +0200286 * return 0 for anything (including invalid certificates)
287 * other than fatal error, as a non-zero return code
288 * immediately aborts the verification process. For fatal
289 * errors, a specific error code should be used (different
290 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
291 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
292 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100294 * \note In case verification failed, the results can be displayed
295 * using \c mbedtls_x509_crt_verify_info()
296 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200297 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
298 * default security profile.
299 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100300 * \note It is your responsibility to provide up-to-date CRLs for
301 * all trusted CAs. If no CRL is provided for the CA that was
302 * used to sign the certificate, CRL verification is skipped
303 * silently, that is *without* setting any flag.
304 *
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +0200305 * \note The \c trust_ca list can contain two types of certificates:
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200306 * (1) those of trusted root CAs, so that certificates
307 * chaining up to those CAs will be trusted, and (2)
308 * self-signed end-entity certificates to be trusted (for
309 * specific peers you know) - in that case, the self-signed
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +0200310 * certificate doesn't need to have the CA bit set.
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200311 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100312 * \param crt a certificate (chain) to be verified
Manuel Pégourié-Gonnarda4a206e2017-06-21 09:35:44 +0200313 * \param trust_ca the list of trusted CAs (see note above)
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100314 * \param ca_crl the list of CRLs for trusted CAs (see note above)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200315 * \param cn expected Common Name (can be set to
316 * NULL if the CN must not be verified)
317 * \param flags result of the verification
318 * \param f_vrfy verification function
319 * \param p_vrfy verification parameter
320 *
Manuel Pégourié-Gonnard760c9b92017-07-06 15:00:32 +0200321 * \return 0 (and flags set to 0) if the chain was verified and valid,
322 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
323 * but found to be invalid, in which case *flags will have one
324 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
325 * flags set, or another error (and flags set to 0xffffffff)
326 * in case of a fatal error encountered during the
327 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
330 mbedtls_x509_crt *trust_ca,
331 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200332 const char *cn, uint32_t *flags,
333 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200334 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200336/**
337 * \brief Verify the certificate signature according to profile
338 *
339 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
340 * security profile.
341 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200342 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200343 * for ECDSA) apply to all certificates: trusted root,
344 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200345 *
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +0100346 * \param crt a certificate (chain) to be verified
347 * \param trust_ca the list of trusted CAs
348 * \param ca_crl the list of CRLs for trusted CAs
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200349 * \param profile security profile for verification
350 * \param cn expected Common Name (can be set to
351 * NULL if the CN must not be verified)
352 * \param flags result of the verification
353 * \param f_vrfy verification function
354 * \param p_vrfy verification parameter
355 *
356 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
357 * in which case *flags will have one or more
358 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
359 * set,
360 * or another error in case of a fatal error encountered
361 * during the verification process.
362 */
363int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
364 mbedtls_x509_crt *trust_ca,
365 mbedtls_x509_crl *ca_crl,
366 const mbedtls_x509_crt_profile *profile,
367 const char *cn, uint32_t *flags,
368 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
369 void *p_vrfy );
370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200372/**
373 * \brief Check usage of certificate against keyUsage extension.
374 *
375 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200376 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
377 * before using the certificate to perform an RSA key
378 * exchange).
379 *
380 * \note Except for decipherOnly and encipherOnly, a bit set in the
381 * usage argument means this bit MUST be set in the
382 * certificate. For decipherOnly and encipherOnly, it means
383 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200384 *
385 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200387 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200388 *
389 * \note You should only call this function on leaf certificates, on
390 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200392 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200393int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
394 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200398/**
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000399 * \brief Check usage of certificate against extendedKeyUsage.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200400 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000401 * \param crt Leaf certificate used.
402 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
403 * MBEDTLS_OID_CLIENT_AUTH).
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200405 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000406 * \return 0 if this use of the certificate is allowed,
407 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200408 *
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000409 * \note Usually only makes sense on leaf certificates.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Andres Amaya Garcia5a6da632017-11-14 21:40:51 +0000412 const char *usage_oid,
413 size_t usage_len );
Andres Amaya Garciac81fcb92017-11-14 21:40:02 +0000414#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200418 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419 *
420 * \param crt a certificate to be verified
421 * \param crl the CRL to verify against
422 *
423 * \return 1 if the certificate is revoked, 0 otherwise
424 *
425 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100426int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
429/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200430 * \brief Initialize a certificate (chain)
431 *
432 * \param crt Certificate chain to initialize
433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200435
436/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437 * \brief Unallocate all certificate data
438 *
439 * \param crt Certificate chain to free
440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
442#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
444/* \} name */
445/* \} addtogroup x509_module */
446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448/**
449 * \brief Initialize a CRT writing context
450 *
451 * \param ctx CRT context to initialize
452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454
455/**
456 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458 *
459 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
461 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
465/**
466 * \brief Set the serial number for a Certificate.
467 *
468 * \param ctx CRT context to use
469 * \param serial serial number to set
470 *
471 * \return 0 if successful
472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
475/**
476 * \brief Set the validity period for a Certificate
477 * Timestamps should be in string format for UTC timezone
478 * i.e. "YYYYMMDDhhmmss"
479 * e.g. "20131231235959" for December 31st 2013
480 * at 23:59:59
481 *
482 * \param ctx CRT context to use
483 * \param not_before not_before timestamp
484 * \param not_after not_after timestamp
485 *
486 * \return 0 if timestamp was parsed successfully, or
487 * a specific error code
488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100490 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491
492/**
493 * \brief Set the issuer name for a Certificate
494 * Issuer 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 CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 *
498 * \param ctx CRT context to use
499 * \param issuer_name issuer name to set
500 *
501 * \return 0 if issuer 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_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100505 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
507/**
508 * \brief Set the subject name for a Certificate
509 * Subject names should contain a comma-separated list
510 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000511 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512 *
513 * \param ctx CRT context to use
514 * \param subject_name subject name to set
515 *
516 * \return 0 if subject name was parsed successfully, or
517 * a specific error code
518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100520 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521
522/**
523 * \brief Set the subject public key for the certificate
524 *
525 * \param ctx CRT context to use
526 * \param key public key to include
527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529
530/**
531 * \brief Set the issuer key used for signing the certificate
532 *
533 * \param ctx CRT context to use
534 * \param key private key to sign with
535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
538/**
539 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541 *
542 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100543 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546
547/**
548 * \brief Generic function to add to or replace an extension in the
549 * CRT
550 *
551 * \param ctx CRT context to use
552 * \param oid OID of the extension
553 * \param oid_len length of the OID
554 * \param critical if the extension is critical (per the RFC's definition)
555 * \param val value of the extension OCTET STRING
556 * \param val_len length of the value data
557 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200558 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561 const char *oid, size_t oid_len,
562 int critical,
563 const unsigned char *val, size_t val_len );
564
565/**
566 * \brief Set the basicConstraints extension for a CRT
567 *
568 * \param ctx CRT context to use
569 * \param is_ca is this a CA certificate
570 * \param max_pathlen maximum length of certificate chains below this
571 * certificate (only for CA certificates, -1 is
572 * inlimited)
573 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200574 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577 int is_ca, int max_pathlen );
578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580/**
581 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583 * called before
584 *
585 * \param ctx CRT context to use
586 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200587 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590
591/**
592 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594 * called before
595 *
596 * \param ctx CRT context to use
597 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200598 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
601#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602
603/**
604 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200606 *
607 * \param ctx CRT context to use
608 * \param key_usage key usage flags to set
609 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200610 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200612int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
613 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614
615/**
616 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618 *
619 * \param ctx CRT context to use
620 * \param ns_cert_type Netscape Cert Type flags to set
621 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200622 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625 unsigned char ns_cert_type );
626
627/**
628 * \brief Free the contents of a CRT write context
629 *
630 * \param ctx CRT context to free
631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633
634/**
635 * \brief Write a built up certificate to a X509 DER structure
636 * Note: data is written at the end of the buffer! Use the
637 * return value to determine where you should start
638 * using the buffer
639 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100640 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641 * \param buf buffer to write to
642 * \param size size of the buffer
643 * \param f_rng RNG function (for signature, see note)
644 * \param p_rng RNG parameter
645 *
646 * \return length of data written if successful, or a specific
647 * error code
648 *
649 * \note f_rng may be NULL if RSA is used for signature and the
650 * signature is made offline (otherwise f_rng is desirable
651 * for countermeasures against timing attacks).
652 * ECDSA signatures always require a non-NULL f_rng.
653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 int (*f_rng)(void *, unsigned char *, size_t),
656 void *p_rng );
657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659/**
660 * \brief Write a built up certificate to a X509 PEM string
661 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100662 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663 * \param buf buffer to write to
664 * \param size size of the buffer
665 * \param f_rng RNG function (for signature, see note)
666 * \param p_rng RNG parameter
667 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200668 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200669 *
670 * \note f_rng may be NULL if RSA is used for signature and the
671 * signature is made offline (otherwise f_rng is desirable
672 * for countermeasures against timing attacks).
673 * ECDSA signatures always require a non-NULL f_rng.
674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676 int (*f_rng)(void *, unsigned char *, size_t),
677 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#endif /* MBEDTLS_PEM_WRITE_C */
679#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200680
681#ifdef __cplusplus
682}
683#endif
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#endif /* mbedtls_x509_crt.h */