blob: fb86d4c25a047fc9e3261521df0c7b6cd67a40f8 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002 * \file mbedtls_x509_crt.h
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
4 * \brief X.509 certificate parsing and writing
5 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02009 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +020010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
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
124/**
125 * Container for writing a certificate (CRT)
126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200128{
129 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_mpi serial;
131 mbedtls_pk_context *subject_key;
132 mbedtls_pk_context *issuer_key;
133 mbedtls_asn1_named_data *subject;
134 mbedtls_asn1_named_data *issuer;
135 mbedtls_md_type_t md_alg;
136 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
137 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
138 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200139}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200143/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200144 * Default security profile. Should provide a good balance between security
145 * and compatibility with current deployments.
146 */
147extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
148
149/**
150 * Expected next default profile. Recommended for new deployments.
151 * Currently targets a 128-bit security level, except for RSA-2048.
152 */
153extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
154
155/**
156 * NSA Suite B profile.
157 */
158extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
159
160/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200161 * \brief Parse a single DER formatted certificate and add it
162 * to the chained list.
163 *
164 * \param chain points to the start of the chain
165 * \param buf buffer holding the certificate DER data
166 * \param buflen size of the buffer
167 *
168 * \return 0 if successful, or a specific X509 or PEM error code
169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200171 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172
173/**
174 * \brief Parse one or more certificates and add them
175 * to the chained list. Parses permissively. If some
176 * certificates can be parsed, the result is the number
177 * of failed certificates it encountered. If none complete
178 * correctly, the first error is returned.
179 *
180 * \param chain points to the start of the chain
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200181 * \param buf buffer holding the certificate data in PEM or DER format
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200182 * \param buflen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200183 * (including the terminating null byte for PEM data)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200184 *
185 * \return 0 if all certificates parsed successfully, a positive number
186 * if partly successful or a specific X509 or PEM error code
187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200191/**
192 * \brief Load one or more certificates and add them
193 * to the chained list. Parses permissively. If some
194 * certificates can be parsed, the result is the number
195 * of failed certificates it encountered. If none complete
196 * correctly, the first error is returned.
197 *
198 * \param chain points to the start of the chain
199 * \param path filename to read the certificates from
200 *
201 * \return 0 if all certificates parsed successfully, a positive number
202 * if partly successful or a specific X509 or PEM error code
203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200205
206/**
207 * \brief Load one or more certificate files from a path and add them
208 * to the chained list. Parses permissively. If some
209 * certificates can be parsed, the result is the number
210 * of failed certificates it encountered. If none complete
211 * correctly, the first error is returned.
212 *
213 * \param chain points to the start of the chain
214 * \param path directory / folder to read the certificate files from
215 *
216 * \return 0 if all certificates parsed successfully, a positive number
217 * if partly successful or a specific X509 or PEM error code
218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
220#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221
222/**
223 * \brief Returns an informational string about the
224 * certificate.
225 *
226 * \param buf Buffer to write to
227 * \param size Maximum size of buffer
228 * \param prefix A line prefix
229 * \param crt The X509 certificate to represent
230 *
231 * \return The amount of data written to the buffer, or -1 in
232 * case of an error.
233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
235 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236
237/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100238 * \brief Returns an informational string about the
239 * verification status of a certificate.
240 *
241 * \param buf Buffer to write to
242 * \param size Maximum size of buffer
243 * \param prefix A line prefix
244 * \param flags Verification flags created by mbedtls_x509_crt_verify()
245 *
246 * \return The amount of data written to the buffer, or -1 in
247 * case of an error.
248 */
249int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200250 uint32_t flags );
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200251
252/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200253 * \brief Verify the certificate signature
254 *
255 * The verify callback is a user-supplied callback that
256 * can clear / modify / add flags for a certificate. If set,
257 * the verification callback is called for each
258 * certificate in the chain (from the trust-ca down to the
259 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261 * int *flags). With the flags representing current flags for
262 * that specific certificate and the certificate depth from
263 * the bottom (Peer cert depth = 0).
264 *
265 * All flags left after returning from the callback
266 * are also returned to the application. The function should
267 * return 0 for anything but a fatal error.
268 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100269 * \note In case verification failed, the results can be displayed
270 * using \c mbedtls_x509_crt_verify_info()
271 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200272 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
273 * default security profile.
274 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275 * \param crt a certificate to be verified
276 * \param trust_ca the trusted CA chain
277 * \param ca_crl the CRL chain for trusted CA's
278 * \param cn expected Common Name (can be set to
279 * NULL if the CN must not be verified)
280 * \param flags result of the verification
281 * \param f_vrfy verification function
282 * \param p_vrfy verification parameter
283 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100285 * in which case *flags will have one or more
286 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
287 * set,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 * or another error in case of a fatal error encountered
289 * during the verification process.
290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
292 mbedtls_x509_crt *trust_ca,
293 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200294 const char *cn, uint32_t *flags,
295 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200296 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200297
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200298/**
299 * \brief Verify the certificate signature according to profile
300 *
301 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
302 * security profile.
303 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200304 * \note The restrictions on keys (RSA minimum size, allowed curves
305 * for ECDSA) only applys to (intermediate) CAs, not to the
306 * end-entity certificate.
307 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200308 * \param crt a certificate to be verified
309 * \param trust_ca the trusted CA chain
310 * \param ca_crl the CRL chain for trusted CA's
311 * \param profile security profile for verification
312 * \param cn expected Common Name (can be set to
313 * NULL if the CN must not be verified)
314 * \param flags result of the verification
315 * \param f_vrfy verification function
316 * \param p_vrfy verification parameter
317 *
318 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
319 * in which case *flags will have one or more
320 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
321 * set,
322 * or another error in case of a fatal error encountered
323 * during the verification process.
324 */
325int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
326 mbedtls_x509_crt *trust_ca,
327 mbedtls_x509_crl *ca_crl,
328 const mbedtls_x509_crt_profile *profile,
329 const char *cn, uint32_t *flags,
330 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
331 void *p_vrfy );
332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200334/**
335 * \brief Check usage of certificate against keyUsage extension.
336 *
337 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200338 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
339 * before using the certificate to perform an RSA key
340 * exchange).
341 *
342 * \note Except for decipherOnly and encipherOnly, a bit set in the
343 * usage argument means this bit MUST be set in the
344 * certificate. For decipherOnly and encipherOnly, it means
345 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200346 *
347 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200349 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200350 *
351 * \note You should only call this function on leaf certificates, on
352 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200354 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200355int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
356 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200360/**
361 * \brief Check usage of certificate against extentedJeyUsage.
362 *
363 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or MBEDTLS_OID_CLIENT_AUTH).
365 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200366 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200367 * \return 0 if this use of the certificate is allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200369 *
370 * \note Usually only makes sense on leaf certificates.
371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200373 const char *usage_oid,
374 size_t usage_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200379 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380 *
381 * \param crt a certificate to be verified
382 * \param crl the CRL to verify against
383 *
384 * \return 1 if the certificate is revoked, 0 otherwise
385 *
386 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100387int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389
390/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200391 * \brief Initialize a certificate (chain)
392 *
393 * \param crt Certificate chain to initialize
394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200396
397/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398 * \brief Unallocate all certificate data
399 *
400 * \param crt Certificate chain to free
401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
403#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404
405/* \} name */
406/* \} addtogroup x509_module */
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409/**
410 * \brief Initialize a CRT writing context
411 *
412 * \param ctx CRT context to initialize
413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415
416/**
417 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419 *
420 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
422 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
426/**
427 * \brief Set the serial number for a Certificate.
428 *
429 * \param ctx CRT context to use
430 * \param serial serial number to set
431 *
432 * \return 0 if successful
433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
436/**
437 * \brief Set the validity period for a Certificate
438 * Timestamps should be in string format for UTC timezone
439 * i.e. "YYYYMMDDhhmmss"
440 * e.g. "20131231235959" for December 31st 2013
441 * at 23:59:59
442 *
443 * \param ctx CRT context to use
444 * \param not_before not_before timestamp
445 * \param not_after not_after timestamp
446 *
447 * \return 0 if timestamp was parsed successfully, or
448 * a specific error code
449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100451 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452
453/**
454 * \brief Set the issuer name for a Certificate
455 * Issuer names should contain a comma-separated list
456 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000457 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458 *
459 * \param ctx CRT context to use
460 * \param issuer_name issuer name to set
461 *
462 * \return 0 if issuer name was parsed successfully, or
463 * a specific error code
464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100466 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467
468/**
469 * \brief Set the subject name for a Certificate
470 * Subject names should contain a comma-separated list
471 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000472 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473 *
474 * \param ctx CRT context to use
475 * \param subject_name subject name to set
476 *
477 * \return 0 if subject name was parsed successfully, or
478 * a specific error code
479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100481 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
483/**
484 * \brief Set the subject public key for the certificate
485 *
486 * \param ctx CRT context to use
487 * \param key public key to include
488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490
491/**
492 * \brief Set the issuer key used for signing the certificate
493 *
494 * \param ctx CRT context to use
495 * \param key private key to sign with
496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498
499/**
500 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502 *
503 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100504 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507
508/**
509 * \brief Generic function to add to or replace an extension in the
510 * CRT
511 *
512 * \param ctx CRT context to use
513 * \param oid OID of the extension
514 * \param oid_len length of the OID
515 * \param critical if the extension is critical (per the RFC's definition)
516 * \param val value of the extension OCTET STRING
517 * \param val_len length of the value data
518 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200519 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522 const char *oid, size_t oid_len,
523 int critical,
524 const unsigned char *val, size_t val_len );
525
526/**
527 * \brief Set the basicConstraints extension for a CRT
528 *
529 * \param ctx CRT context to use
530 * \param is_ca is this a CA certificate
531 * \param max_pathlen maximum length of certificate chains below this
532 * certificate (only for CA certificates, -1 is
533 * inlimited)
534 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200535 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538 int is_ca, int max_pathlen );
539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541/**
542 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544 * called before
545 *
546 * \param ctx CRT context to use
547 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200548 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551
552/**
553 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555 * called before
556 *
557 * \param ctx CRT context to use
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_authority_key_identifier( mbedtls_x509write_cert *ctx );
562#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563
564/**
565 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567 *
568 * \param ctx CRT context to use
569 * \param key_usage key usage flags to set
570 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200571 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200573int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
574 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
576/**
577 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579 *
580 * \param ctx CRT context to use
581 * \param ns_cert_type Netscape Cert Type flags to set
582 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200583 * \return 0 if successful, or 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_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586 unsigned char ns_cert_type );
587
588/**
589 * \brief Free the contents of a CRT write context
590 *
591 * \param ctx CRT context to free
592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594
595/**
596 * \brief Write a built up certificate to a X509 DER structure
597 * Note: data is written at the end of the buffer! Use the
598 * return value to determine where you should start
599 * using the buffer
600 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100601 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602 * \param buf buffer to write to
603 * \param size size of the buffer
604 * \param f_rng RNG function (for signature, see note)
605 * \param p_rng RNG parameter
606 *
607 * \return length of data written if successful, or a specific
608 * error code
609 *
610 * \note f_rng may be NULL if RSA is used for signature and the
611 * signature is made offline (otherwise f_rng is desirable
612 * for countermeasures against timing attacks).
613 * ECDSA signatures always require a non-NULL f_rng.
614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200616 int (*f_rng)(void *, unsigned char *, size_t),
617 void *p_rng );
618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620/**
621 * \brief Write a built up certificate to a X509 PEM string
622 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100623 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624 * \param buf buffer to write to
625 * \param size size of the buffer
626 * \param f_rng RNG function (for signature, see note)
627 * \param p_rng RNG parameter
628 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200629 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 *
631 * \note f_rng may be NULL if RSA is used for signature and the
632 * signature is made offline (otherwise f_rng is desirable
633 * for countermeasures against timing attacks).
634 * ECDSA signatures always require a non-NULL f_rng.
635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637 int (*f_rng)(void *, unsigned char *, size_t),
638 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#endif /* MBEDTLS_PEM_WRITE_C */
640#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641
642#ifdef __cplusplus
643}
644#endif
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#endif /* mbedtls_x509_crt.h */