blob: aced7eb0f08ed35024c074a3d52401821da2cfaa [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#define MBEDTLS_X509_CRT_VERSION_1 0
98#define MBEDTLS_X509_CRT_VERSION_2 1
99#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
102#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103
104/**
105 * Container for writing a certificate (CRT)
106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200108{
109 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_mpi serial;
111 mbedtls_pk_context *subject_key;
112 mbedtls_pk_context *issuer_key;
113 mbedtls_asn1_named_data *subject;
114 mbedtls_asn1_named_data *issuer;
115 mbedtls_md_type_t md_alg;
116 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
117 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
118 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200123/**
124 * \brief Parse a single DER formatted certificate and add it
125 * to the chained list.
126 *
127 * \param chain points to the start of the chain
128 * \param buf buffer holding the certificate DER data
129 * \param buflen size of the buffer
130 *
131 * \return 0 if successful, or a specific X509 or PEM error code
132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200134 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135
136/**
137 * \brief Parse one or more certificates and add them
138 * to the chained list. Parses permissively. If some
139 * certificates can be parsed, the result is the number
140 * of failed certificates it encountered. If none complete
141 * correctly, the first error is returned.
142 *
143 * \param chain points to the start of the chain
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200144 * \param buf buffer holding the certificate data in PEM or DER format
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 * \param buflen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200146 * (including the terminating null byte for PEM data)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147 *
148 * \return 0 if all certificates parsed successfully, a positive number
149 * if partly successful or a specific X509 or PEM error code
150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200154/**
155 * \brief Load one or more certificates and add them
156 * to the chained list. Parses permissively. If some
157 * certificates can be parsed, the result is the number
158 * of failed certificates it encountered. If none complete
159 * correctly, the first error is returned.
160 *
161 * \param chain points to the start of the chain
162 * \param path filename to read the certificates from
163 *
164 * \return 0 if all certificates parsed successfully, a positive number
165 * if partly successful or a specific X509 or PEM error code
166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200168
169/**
170 * \brief Load one or more certificate files from a path and add them
171 * to the chained list. Parses permissively. If some
172 * certificates can be parsed, the result is the number
173 * of failed certificates it encountered. If none complete
174 * correctly, the first error is returned.
175 *
Manuel Pégourié-Gonnarde3339ce2013-11-28 17:16:41 +0100176 * \warning This function is NOT thread-safe unless
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 * MBEDTLS_THREADING_PTHREAD is defined. If you're using an
Manuel Pégourié-Gonnarde3339ce2013-11-28 17:16:41 +0100178 * alternative threading implementation, you should either use
179 * this function only in the main thread, or mutex it.
180 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200181 * \param chain points to the start of the chain
182 * \param path directory / folder to read the certificate files from
183 *
184 * \return 0 if all certificates parsed successfully, a positive number
185 * if partly successful or a specific X509 or PEM error code
186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
188#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200189
190/**
191 * \brief Returns an informational string about the
192 * certificate.
193 *
194 * \param buf Buffer to write to
195 * \param size Maximum size of buffer
196 * \param prefix A line prefix
197 * \param crt The X509 certificate to represent
198 *
199 * \return The amount of data written to the buffer, or -1 in
200 * case of an error.
201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
203 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200204
205/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100206 * \brief Returns an informational string about the
207 * verification status of a certificate.
208 *
209 * \param buf Buffer to write to
210 * \param size Maximum size of buffer
211 * \param prefix A line prefix
212 * \param flags Verification flags created by mbedtls_x509_crt_verify()
213 *
214 * \return The amount of data written to the buffer, or -1 in
215 * case of an error.
216 */
217int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200218 uint32_t flags );
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200219
220/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221 * \brief Verify the certificate signature
222 *
223 * The verify callback is a user-supplied callback that
224 * can clear / modify / add flags for a certificate. If set,
225 * the verification callback is called for each
226 * certificate in the chain (from the trust-ca down to the
227 * presented crt). The parameters for the callback are:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229 * int *flags). With the flags representing current flags for
230 * that specific certificate and the certificate depth from
231 * the bottom (Peer cert depth = 0).
232 *
233 * All flags left after returning from the callback
234 * are also returned to the application. The function should
235 * return 0 for anything but a fatal error.
236 *
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100237 * \note In case verification failed, the results can be displayed
238 * using \c mbedtls_x509_crt_verify_info()
239 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200240 * \param crt a certificate to be verified
241 * \param trust_ca the trusted CA chain
242 * \param ca_crl the CRL chain for trusted CA's
243 * \param cn expected Common Name (can be set to
244 * NULL if the CN must not be verified)
245 * \param flags result of the verification
246 * \param f_vrfy verification function
247 * \param p_vrfy verification parameter
248 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100250 * in which case *flags will have one or more
251 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
252 * set,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200253 * or another error in case of a fatal error encountered
254 * during the verification process.
255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
257 mbedtls_x509_crt *trust_ca,
258 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200259 const char *cn, uint32_t *flags,
260 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200261 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200264/**
265 * \brief Check usage of certificate against keyUsage extension.
266 *
267 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100268 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT before using the
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200269 * certificate to perform an RSA key exchange).
270 *
271 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200273 * is present but does not contain all the bits set in the
274 * usage argument.
275 *
276 * \note You should only call this function on leaf certificates, on
277 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt, unsigned int usage );
281#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200284/**
285 * \brief Check usage of certificate against extentedJeyUsage.
286 *
287 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or MBEDTLS_OID_CLIENT_AUTH).
289 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200290 *
291 * \return 0 is this use of the certificate is allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200293 *
294 * \note Usually only makes sense on leaf certificates.
295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200297 const char *usage_oid,
298 size_t usage_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200302/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200303 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200304 *
305 * \param crt a certificate to be verified
306 * \param crl the CRL to verify against
307 *
308 * \return 1 if the certificate is revoked, 0 otherwise
309 *
310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311int mbedtls_x509_crt_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
312#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313
314/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200315 * \brief Initialize a certificate (chain)
316 *
317 * \param crt Certificate chain to initialize
318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200320
321/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200322 * \brief Unallocate all certificate data
323 *
324 * \param crt Certificate chain to free
325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
327#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328
329/* \} name */
330/* \} addtogroup x509_module */
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333/**
334 * \brief Initialize a CRT writing context
335 *
336 * \param ctx CRT context to initialize
337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339
340/**
341 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343 *
344 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
346 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349
350/**
351 * \brief Set the serial number for a Certificate.
352 *
353 * \param ctx CRT context to use
354 * \param serial serial number to set
355 *
356 * \return 0 if successful
357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359
360/**
361 * \brief Set the validity period for a Certificate
362 * Timestamps should be in string format for UTC timezone
363 * i.e. "YYYYMMDDhhmmss"
364 * e.g. "20131231235959" for December 31st 2013
365 * at 23:59:59
366 *
367 * \param ctx CRT context to use
368 * \param not_before not_before timestamp
369 * \param not_after not_after timestamp
370 *
371 * \return 0 if timestamp was parsed successfully, or
372 * a specific error code
373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100375 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376
377/**
378 * \brief Set the issuer name for a Certificate
379 * Issuer names should contain a comma-separated list
380 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000381 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382 *
383 * \param ctx CRT context to use
384 * \param issuer_name issuer name to set
385 *
386 * \return 0 if issuer name was parsed successfully, or
387 * a specific error code
388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100390 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391
392/**
393 * \brief Set the subject name for a Certificate
394 * Subject names should contain a comma-separated list
395 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000396 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397 *
398 * \param ctx CRT context to use
399 * \param subject_name subject name to set
400 *
401 * \return 0 if subject name was parsed successfully, or
402 * a specific error code
403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100405 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406
407/**
408 * \brief Set the subject public key for the certificate
409 *
410 * \param ctx CRT context to use
411 * \param key public key to include
412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414
415/**
416 * \brief Set the issuer key used for signing the certificate
417 *
418 * \param ctx CRT context to use
419 * \param key private key to sign with
420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422
423/**
424 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426 *
427 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100428 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431
432/**
433 * \brief Generic function to add to or replace an extension in the
434 * CRT
435 *
436 * \param ctx CRT context to use
437 * \param oid OID of the extension
438 * \param oid_len length of the OID
439 * \param critical if the extension is critical (per the RFC's definition)
440 * \param val value of the extension OCTET STRING
441 * \param val_len length of the value data
442 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 * \return 0 if successful, or a MBEDTLS_ERR_X509_MALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446 const char *oid, size_t oid_len,
447 int critical,
448 const unsigned char *val, size_t val_len );
449
450/**
451 * \brief Set the basicConstraints extension for a CRT
452 *
453 * \param ctx CRT context to use
454 * \param is_ca is this a CA certificate
455 * \param max_pathlen maximum length of certificate chains below this
456 * certificate (only for CA certificates, -1 is
457 * inlimited)
458 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 * \return 0 if successful, or a MBEDTLS_ERR_X509_MALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462 int is_ca, int max_pathlen );
463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200465/**
466 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468 * called before
469 *
470 * \param ctx CRT context to use
471 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 * \return 0 if successful, or a MBEDTLS_ERR_X509_MALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475
476/**
477 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479 * called before
480 *
481 * \param ctx CRT context to use
482 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 * \return 0 if successful, or a MBEDTLS_ERR_X509_MALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
486#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487
488/**
489 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 *
492 * \param ctx CRT context to use
493 * \param key_usage key usage flags to set
494 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 * \return 0 if successful, or MBEDTLS_ERR_X509_MALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx, unsigned char key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498
499/**
500 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502 *
503 * \param ctx CRT context to use
504 * \param ns_cert_type Netscape Cert Type flags to set
505 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 * \return 0 if successful, or MBEDTLS_ERR_X509_MALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509 unsigned char ns_cert_type );
510
511/**
512 * \brief Free the contents of a CRT write context
513 *
514 * \param ctx CRT context to free
515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
518/**
519 * \brief Write a built up certificate to a X509 DER structure
520 * Note: data is written at the end of the buffer! Use the
521 * return value to determine where you should start
522 * using the buffer
523 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100524 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 * \param buf buffer to write to
526 * \param size size of the buffer
527 * \param f_rng RNG function (for signature, see note)
528 * \param p_rng RNG parameter
529 *
530 * \return length of data written if successful, or a specific
531 * error code
532 *
533 * \note f_rng may be NULL if RSA is used for signature and the
534 * signature is made offline (otherwise f_rng is desirable
535 * for countermeasures against timing attacks).
536 * ECDSA signatures always require a non-NULL f_rng.
537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539 int (*f_rng)(void *, unsigned char *, size_t),
540 void *p_rng );
541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543/**
544 * \brief Write a built up certificate to a X509 PEM string
545 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100546 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547 * \param buf buffer to write to
548 * \param size size of the buffer
549 * \param f_rng RNG function (for signature, see note)
550 * \param p_rng RNG parameter
551 *
552 * \return 0 successful, or a specific error code
553 *
554 * \note f_rng may be NULL if RSA is used for signature and the
555 * signature is made offline (otherwise f_rng is desirable
556 * for countermeasures against timing attacks).
557 * ECDSA signatures always require a non-NULL f_rng.
558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560 int (*f_rng)(void *, unsigned char *, size_t),
561 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#endif /* MBEDTLS_PEM_WRITE_C */
563#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564
565#ifdef __cplusplus
566}
567#endif
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#endif /* mbedtls_x509_crt.h */