blob: f53573c7f7961137c1580620fe05ec442e53b2df [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file x509_crt.h
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
4 * \brief X.509 certificate parsing and writing
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_X509_CRT_H
24#define MBEDTLS_X509_CRT_H
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031
32#include "x509.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033#include "x509_crl.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034
35/**
36 * \addtogroup x509_module
37 * \{
38 */
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/**
45 * \name Structures and functions for parsing and writing X.509 certificates
46 * \{
47 */
48
49/**
50 * Container for an X.509 certificate. The certificate may be chained.
51 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052typedef struct mbedtls_x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
55 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020057 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
59 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
62 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
65 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
68 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
73 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
74 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
75 mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076
77 int ext_types; /**< Bit string containing detected and parsed extensions */
78 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
79 int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. Path length is 1 higher than RFC 5280 'meaning', so 1+ */
80
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +010081 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020085 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
88 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
89 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
90 void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094mbedtls_x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020096/**
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020097 * Build flag from an algorithm/curve identifier (pk, md, ecp)
98 * Since 0 is always XXX_NONE, ignore it.
99 */
100#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( id - 1 ) )
101
102/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200103 * Security profile for certificate verification.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200104 *
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200105 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200106 */
107typedef struct
108{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200109 uint32_t allowed_mds; /**< MDs for signatures */
110 uint32_t allowed_pks; /**< PK algs for signatures */
111 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
112 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200113}
114mbedtls_x509_crt_profile;
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#define MBEDTLS_X509_CRT_VERSION_1 0
117#define MBEDTLS_X509_CRT_VERSION_2 1
118#define MBEDTLS_X509_CRT_VERSION_3 2
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
121#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122
123/**
124 * Container for writing a certificate (CRT)
125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126typedef struct mbedtls_x509write_cert
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127{
128 int version;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_mpi serial;
130 mbedtls_pk_context *subject_key;
131 mbedtls_pk_context *issuer_key;
132 mbedtls_asn1_named_data *subject;
133 mbedtls_asn1_named_data *issuer;
134 mbedtls_md_type_t md_alg;
135 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
136 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
137 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139mbedtls_x509write_cert;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200142/**
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200143 * Default security profile. Should provide a good balance between security
144 * and compatibility with current deployments.
145 */
146extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
147
148/**
149 * Expected next default profile. Recommended for new deployments.
150 * Currently targets a 128-bit security level, except for RSA-2048.
151 */
152extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
153
154/**
155 * NSA Suite B profile.
156 */
157extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
158
159/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200160 * \brief Parse a single DER formatted certificate and add it
161 * to the chained list.
162 *
163 * \param chain points to the start of the chain
164 * \param buf buffer holding the certificate DER data
165 * \param buflen size of the buffer
166 *
167 * \return 0 if successful, or a specific X509 or PEM error code
168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200170 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200171
172/**
173 * \brief Parse one or more certificates and add them
174 * to the chained list. Parses permissively. If some
175 * certificates can be parsed, the result is the number
176 * of failed certificates it encountered. If none complete
177 * correctly, the first error is returned.
178 *
179 * \param chain points to the start of the chain
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200180 * \param buf buffer holding the certificate data in PEM or DER format
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200181 * \param buflen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200182 * (including the terminating null byte for PEM data)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200183 *
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( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200190/**
191 * \brief Load one or more certificates and add them
192 * to the chained list. Parses permissively. If some
193 * certificates can be parsed, the result is the number
194 * of failed certificates it encountered. If none complete
195 * correctly, the first error is returned.
196 *
197 * \param chain points to the start of the chain
198 * \param path filename to read the certificates from
199 *
200 * \return 0 if all certificates parsed successfully, a positive number
201 * if partly successful or a specific X509 or PEM error code
202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200204
205/**
206 * \brief Load one or more certificate files from a path and add them
207 * to the chained list. Parses permissively. If some
208 * certificates can be parsed, the result is the number
209 * of failed certificates it encountered. If none complete
210 * correctly, the first error is returned.
211 *
212 * \param chain points to the start of the chain
213 * \param path directory / folder to read the certificate files from
214 *
215 * \return 0 if all certificates parsed successfully, a positive number
216 * if partly successful or a specific X509 or PEM error code
217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
219#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200220
221/**
222 * \brief Returns an informational string about the
223 * certificate.
224 *
225 * \param buf Buffer to write to
226 * \param size Maximum size of buffer
227 * \param prefix A line prefix
228 * \param crt The X509 certificate to represent
229 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200230 * \return The length of the string written (not including the
231 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
234 const mbedtls_x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200235
236/**
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100237 * \brief Returns an informational string about the
238 * verification status of a certificate.
239 *
240 * \param buf Buffer to write to
241 * \param size Maximum size of buffer
242 * \param prefix A line prefix
243 * \param flags Verification flags created by mbedtls_x509_crt_verify()
244 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200245 * \return The length of the string written (not including the
246 * terminated nul byte), or a negative error code.
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100247 */
248int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200249 uint32_t flags );
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200250
251/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200252 * \brief Verify the certificate signature
253 *
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200254 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
Manuel Pégourié-Gonnard95a3c6e2016-01-28 17:27:45 +0100255 * default security profile, so see that function for details.
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200256 *
Manuel Pégourié-Gonnard95a3c6e2016-01-28 17:27:45 +0100257 * \param crt a certificate (chain) to be verified
258 * Must start with the end entity certificate, then
259 * intermediate CA certificates if any.
260 * \param trust_ca the list of trusted root CAs
261 * \param ca_crl the list of CRLs for trusted CAs
262 * \param exp_name expected name to be checked against the subjectAltName
263 * extension if present, or the Common Name of the Subject.
264 * Usually a DNS name; see note below for other name types.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265 * \param flags result of the verification
Manuel Pégourié-Gonnard95a3c6e2016-01-28 17:27:45 +0100266 * \param f_vrfy verification callback
267 * \param p_vrfy context for the verification callback
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268 *
Manuel Pégourié-Gonnard95a3c6e2016-01-28 17:27:45 +0100269 * \return see \c mbedtls_x509_crt_verify_with_profile()
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
272 mbedtls_x509_crt *trust_ca,
273 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +0100274 const char *exp_name, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200275 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200276 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200277
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200278/**
279 * \brief Verify the certificate signature according to profile
280 *
281 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
282 * security profile.
283 *
Manuel Pégourié-Gonnard95a3c6e2016-01-28 17:27:45 +0100284 * \note In case verification failed, the results can be displayed
285 * using \c mbedtls_x509_crt_verify_info()
286 *
287 * \note The verify callback is a user-supplied callback that
288 * can clear / modify / add flags for a certificate. If set,
289 * the verification callback is called for each
290 * certificate in the chain (from the trust-ca down to the
291 * presented crt). The parameters for the callback are:
292 * (void *context, mbedtls_x509_crt *crt, int certificate_depth,
293 * int *flags). With the flags representing current flags for
294 * that specific certificate and the certificate depth from
295 * the bottom (Peer cert depth = 0). All flags left after
296 * returning from the callback are also returned to the
297 * application. The function should return 0 for anything but
298 * a fatal error.
299 *
300 * \param crt a certificate (chain) to be verified
301 * Must start with the end entity certificate, then
302 * intermediate CA certificates if any.
303 * \param trust_ca the list of trusted root CAs
304 * \param ca_crl the list of CRLs for trusted CAs
305 * \param profile security profile for verification, see note below
306 * \param exp_name expected name to be checked against the subjectAltName
307 * extension if present, or the Common Name of the Subject.
308 * Usually a DNS name; see note below for other name types.
309 * \param flags result of the verification
310 * \param f_vrfy verification callback
311 * \param p_vrfy context for the verification callback
312 *
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200313 * \note The restrictions on keys (RSA minimum size, allowed curves
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200314 * for ECDSA) apply to all certificates: trusted root,
315 * intermediate CAs if any, and end entity certificate.
Manuel Pégourié-Gonnard27716cc2015-06-17 11:49:39 +0200316 *
Manuel Pégourié-Gonnard9855d632016-01-29 11:50:56 +0100317 * \note If MBEDTLS_X509_SAN_IP_ADDRESS_SUPPORT is enabled, instead
318 * of a DNS name, exp_name can also contain the string "IP:"
319 * followed by an IP address. This address must be either an
320 * IPv4 address in dotted decimal notation, or an IPv6
321 * address consisting of exactly 8 groups of 4 hexadecimal
322 * digits separated by colons. For example, if the expected
323 * IPv6 is fe80::1, then exp_name must be the string
324 * "IP:fe80:0000:0000:0000:0000:0000:0000:0001" or its
325 * uppercase equivalent. If the expected IPv4 is 127.0.0.1,
326 * then exp_name should be "IP:127.0.0.1".
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200327 *
328 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
329 * in which case *flags will have one or more
330 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
331 * set,
332 * or another error in case of a fatal error encountered
333 * during the verification process.
334 */
335int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
336 mbedtls_x509_crt *trust_ca,
337 mbedtls_x509_crl *ca_crl,
338 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +0100339 const char *exp_name, uint32_t *flags,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +0200340 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
341 void *p_vrfy );
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200344/**
345 * \brief Check usage of certificate against keyUsage extension.
346 *
347 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200348 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
349 * before using the certificate to perform an RSA key
350 * exchange).
351 *
352 * \note Except for decipherOnly and encipherOnly, a bit set in the
353 * usage argument means this bit MUST be set in the
354 * certificate. For decipherOnly and encipherOnly, it means
355 * that bit MAY be set.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200356 *
357 * \return 0 is these uses of the certificate are allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200359 * is present but does not match the usage argument.
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200360 *
361 * \note You should only call this function on leaf certificates, on
362 * (intermediate) CAs the keyUsage extension is automatically
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 * checked by \c mbedtls_x509_crt_verify().
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200364 */
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +0200365int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
366 unsigned int usage );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200370/**
371 * \brief Check usage of certificate against extentedJeyUsage.
372 *
373 * \param crt Leaf certificate used.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or MBEDTLS_OID_CLIENT_AUTH).
375 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200376 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200377 * \return 0 if this use of the certificate is allowed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200379 *
380 * \note Usually only makes sense on leaf certificates.
381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200383 const char *usage_oid,
384 size_t usage_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200389 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390 *
391 * \param crt a certificate to be verified
392 * \param crl the CRL to verify against
393 *
394 * \return 1 if the certificate is revoked, 0 otherwise
395 *
396 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100397int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399
400/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200401 * \brief Initialize a certificate (chain)
402 *
403 * \param crt Certificate chain to initialize
404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200406
407/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408 * \brief Unallocate all certificate data
409 *
410 * \param crt Certificate chain to free
411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
413#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414
415/* \} name */
416/* \} addtogroup x509_module */
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419/**
420 * \brief Initialize a CRT writing context
421 *
422 * \param ctx CRT context to initialize
423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
426/**
427 * \brief Set the verion for a Certificate
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 * Default: MBEDTLS_X509_CRT_VERSION_3
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429 *
430 * \param ctx CRT context to use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
432 * MBEDTLS_X509_CRT_VERSION_3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
436/**
437 * \brief Set the serial number for a Certificate.
438 *
439 * \param ctx CRT context to use
440 * \param serial serial number to set
441 *
442 * \return 0 if successful
443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
446/**
447 * \brief Set the validity period for a Certificate
448 * Timestamps should be in string format for UTC timezone
449 * i.e. "YYYYMMDDhhmmss"
450 * e.g. "20131231235959" for December 31st 2013
451 * at 23:59:59
452 *
453 * \param ctx CRT context to use
454 * \param not_before not_before timestamp
455 * \param not_after not_after timestamp
456 *
457 * \return 0 if timestamp was parsed successfully, or
458 * a specific error code
459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
Paul Bakker50dc8502013-10-28 21:19:10 +0100461 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462
463/**
464 * \brief Set the issuer name for a Certificate
465 * Issuer names should contain a comma-separated list
466 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000467 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468 *
469 * \param ctx CRT context to use
470 * \param issuer_name issuer name to set
471 *
472 * \return 0 if issuer name was parsed successfully, or
473 * a specific error code
474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100476 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477
478/**
479 * \brief Set the subject name for a Certificate
480 * Subject names should contain a comma-separated list
481 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000482 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483 *
484 * \param ctx CRT context to use
485 * \param subject_name subject name to set
486 *
487 * \return 0 if subject name was parsed successfully, or
488 * a specific error code
489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100491 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492
493/**
494 * \brief Set the subject public key for the certificate
495 *
496 * \param ctx CRT context to use
497 * \param key public key to include
498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500
501/**
502 * \brief Set the issuer key used for signing the certificate
503 *
504 * \param ctx CRT context to use
505 * \param key private key to sign with
506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508
509/**
510 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512 *
513 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100514 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
518/**
519 * \brief Generic function to add to or replace an extension in the
520 * CRT
521 *
522 * \param ctx CRT context to use
523 * \param oid OID of the extension
524 * \param oid_len length of the OID
525 * \param critical if the extension is critical (per the RFC's definition)
526 * \param val value of the extension OCTET STRING
527 * \param val_len length of the value data
528 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200529 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532 const char *oid, size_t oid_len,
533 int critical,
534 const unsigned char *val, size_t val_len );
535
536/**
537 * \brief Set the basicConstraints extension for a CRT
538 *
539 * \param ctx CRT context to use
540 * \param is_ca is this a CA certificate
541 * \param max_pathlen maximum length of certificate chains below this
542 * certificate (only for CA certificates, -1 is
543 * inlimited)
544 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200545 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548 int is_ca, int max_pathlen );
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550#if defined(MBEDTLS_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551/**
552 * \brief Set the subjectKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 * Requires that mbedtls_x509write_crt_set_subject_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554 * called before
555 *
556 * \param ctx CRT context to use
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_subject_key_identifier( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561
562/**
563 * \brief Set the authorityKeyIdentifier extension for a CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565 * called before
566 *
567 * \param ctx CRT context to use
568 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200569 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
572#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573
574/**
575 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577 *
578 * \param ctx CRT context to use
579 * \param key_usage key usage flags to set
580 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200581 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582 */
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200583int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
584 unsigned int key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585
586/**
587 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589 *
590 * \param ctx CRT context to use
591 * \param ns_cert_type Netscape Cert Type flags to set
592 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200593 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200596 unsigned char ns_cert_type );
597
598/**
599 * \brief Free the contents of a CRT write context
600 *
601 * \param ctx CRT context to free
602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200604
605/**
606 * \brief Write a built up certificate to a X509 DER structure
607 * Note: data is written at the end of the buffer! Use the
608 * return value to determine where you should start
609 * using the buffer
610 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100611 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612 * \param buf buffer to write to
613 * \param size size of the buffer
614 * \param f_rng RNG function (for signature, see note)
615 * \param p_rng RNG parameter
616 *
617 * \return length of data written if successful, or a specific
618 * error code
619 *
620 * \note f_rng may be NULL if RSA is used for signature and the
621 * signature is made offline (otherwise f_rng is desirable
622 * for countermeasures against timing attacks).
623 * ECDSA signatures always require a non-NULL f_rng.
624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 int (*f_rng)(void *, unsigned char *, size_t),
627 void *p_rng );
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630/**
631 * \brief Write a built up certificate to a X509 PEM string
632 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100633 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634 * \param buf buffer to write to
635 * \param size size of the buffer
636 * \param f_rng RNG function (for signature, see note)
637 * \param p_rng RNG parameter
638 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200639 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 *
641 * \note f_rng may be NULL if RSA is used for signature and the
642 * signature is made offline (otherwise f_rng is desirable
643 * for countermeasures against timing attacks).
644 * ECDSA signatures always require a non-NULL f_rng.
645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647 int (*f_rng)(void *, unsigned char *, size_t),
648 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#endif /* MBEDTLS_PEM_WRITE_C */
650#endif /* MBEDTLS_X509_CRT_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651
652#ifdef __cplusplus
653}
654#endif
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#endif /* mbedtls_x509_crt.h */