blob: e3443d0168ceb70eabccab70a5cc850c200bcf91 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/**
2 * \file x509_crt.h
3 *
4 * \brief X.509 certificate parsing and writing
5 *
6 * Copyright (C) 2006-2013, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_X509_CRT_H
28#define POLARSSL_X509_CRT_H
29
30#include "config.h"
31
32#include "x509.h"
33
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 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +020053typedef struct _x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054{
55 x509_buf raw; /**< The raw certificate data (DER). */
56 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
57
58 int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
59 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
60 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
61
62 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
63 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
64
65 x509_name issuer; /**< The parsed issuer data (named information object). */
66 x509_name subject; /**< The parsed subject data (named information object). */
67
68 x509_time valid_from; /**< Start time of certificate validity. */
69 x509_time valid_to; /**< End time of certificate validity. */
70
71 pk_context pk; /**< Container for the public key context. */
72
73 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
74 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020075 x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076 x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
77
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é-Gonnardb28487d2014-04-01 12:19:09 +020082 unsigned char key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083
84 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
85
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
88 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
89 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
90 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
91 pk_type_t sig_pk /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. POLARSSL_PK_RSA */;
92
Paul Bakkerc559c7a2013-09-18 14:13:26 +020093 struct _x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094}
Paul Bakkerc559c7a2013-09-18 14:13:26 +020095x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096
97#define X509_CRT_VERSION_1 0
98#define X509_CRT_VERSION_2 1
99#define X509_CRT_VERSION_3 2
100
101#define X509_RFC5280_MAX_SERIAL_LEN 32
102#define X509_RFC5280_UTC_TIME_LEN 15
103
104/**
105 * Container for writing a certificate (CRT)
106 */
107typedef struct _x509write_cert
108{
109 int version;
110 mpi serial;
111 pk_context *subject_key;
112 pk_context *issuer_key;
113 asn1_named_data *subject;
114 asn1_named_data *issuer;
115 md_type_t md_alg;
116 char not_before[X509_RFC5280_UTC_TIME_LEN + 1];
117 char not_after[X509_RFC5280_UTC_TIME_LEN + 1];
118 asn1_named_data *extensions;
119}
120x509write_cert;
121
122#if defined(POLARSSL_X509_CRT_PARSE_C)
123/**
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 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200133int x509_crt_parse_der( 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
144 * \param buf buffer holding the certificate data
145 * \param buflen size of the buffer
146 *
147 * \return 0 if all certificates parsed successfully, a positive number
148 * if partly successful or a specific X509 or PEM error code
149 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200150int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200151
152#if defined(POLARSSL_FS_IO)
153/**
154 * \brief Load one or more certificates and add them
155 * to the chained list. Parses permissively. If some
156 * certificates can be parsed, the result is the number
157 * of failed certificates it encountered. If none complete
158 * correctly, the first error is returned.
159 *
160 * \param chain points to the start of the chain
161 * \param path filename to read the certificates from
162 *
163 * \return 0 if all certificates parsed successfully, a positive number
164 * if partly successful or a specific X509 or PEM error code
165 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200166int x509_crt_parse_file( x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200167
168/**
169 * \brief Load one or more certificate files from a path and add them
170 * to the chained list. Parses permissively. If some
171 * certificates can be parsed, the result is the number
172 * of failed certificates it encountered. If none complete
173 * correctly, the first error is returned.
174 *
Manuel Pégourié-Gonnarde3339ce2013-11-28 17:16:41 +0100175 * \warning This function is NOT thread-safe unless
176 * POLARSSL_THREADING_PTHREADS is defined. If you're using an
177 * alternative threading implementation, you should either use
178 * this function only in the main thread, or mutex it.
179 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200180 * \param chain points to the start of the chain
181 * \param path directory / folder to read the certificate files from
182 *
183 * \return 0 if all certificates parsed successfully, a positive number
184 * if partly successful or a specific X509 or PEM error code
185 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200186int x509_crt_parse_path( x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200187#endif /* POLARSSL_FS_IO */
188
189/**
190 * \brief Returns an informational string about the
191 * certificate.
192 *
193 * \param buf Buffer to write to
194 * \param size Maximum size of buffer
195 * \param prefix A line prefix
196 * \param crt The X509 certificate to represent
197 *
198 * \return The amount of data written to the buffer, or -1 in
199 * case of an error.
200 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200201int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200202 const x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200203
204/**
205 * \brief Verify the certificate signature
206 *
207 * The verify callback is a user-supplied callback that
208 * can clear / modify / add flags for a certificate. If set,
209 * the verification callback is called for each
210 * certificate in the chain (from the trust-ca down to the
211 * presented crt). The parameters for the callback are:
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200212 * (void *parameter, x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213 * int *flags). With the flags representing current flags for
214 * that specific certificate and the certificate depth from
215 * the bottom (Peer cert depth = 0).
216 *
217 * All flags left after returning from the callback
218 * are also returned to the application. The function should
219 * return 0 for anything but a fatal error.
220 *
221 * \param crt a certificate to be verified
222 * \param trust_ca the trusted CA chain
223 * \param ca_crl the CRL chain for trusted CA's
224 * \param cn expected Common Name (can be set to
225 * NULL if the CN must not be verified)
226 * \param flags result of the verification
227 * \param f_vrfy verification function
228 * \param p_vrfy verification parameter
229 *
230 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
231 * in which case *flags will have one or more of
232 * the following values set:
233 * BADCERT_EXPIRED --
234 * BADCERT_REVOKED --
235 * BADCERT_CN_MISMATCH --
236 * BADCERT_NOT_TRUSTED
237 * or another error in case of a fatal error encountered
238 * during the verification process.
239 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200240int x509_crt_verify( x509_crt *crt,
241 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200242 x509_crl *ca_crl,
243 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200244 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200245 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200246
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200247#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
248/**
249 * \brief Check usage of certificate against keyUsage extension.
250 *
251 * \param crt Leaf certificate used.
252 * \param usage Intended usage(s) (eg KU_KEY_ENCIPHERMENT before using the
253 * certificate to perform an RSA key exchange).
254 *
255 * \return 0 is these uses of the certificate are allowed,
256 * POLARSSL_ERR_X509_BAD_INPUT_DATA if the keyUsage extenson
257 * is present but does not contain all the bits set in the
258 * usage argument.
259 *
260 * \note You should only call this function on leaf certificates, on
261 * (intermediate) CAs the keyUsage extension is automatically
262 * checked by \c x509_crt_verify().
263 */
264int x509_crt_check_key_usage( const x509_crt *crt, int usage );
265#endif /* POLARSSL_X509_CHECK_KEY_USAGE) */
266
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200267#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200269 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200270 *
271 * \param crt a certificate to be verified
272 * \param crl the CRL to verify against
273 *
274 * \return 1 if the certificate is revoked, 0 otherwise
275 *
276 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200277int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278#endif /* POLARSSL_X509_CRL_PARSE_C */
279
280/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200281 * \brief Initialize a certificate (chain)
282 *
283 * \param crt Certificate chain to initialize
284 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200285void x509_crt_init( x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200286
287/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 * \brief Unallocate all certificate data
289 *
290 * \param crt Certificate chain to free
291 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200292void x509_crt_free( x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293#endif /* POLARSSL_X509_CRT_PARSE_C */
294
295/* \} name */
296/* \} addtogroup x509_module */
297
298#if defined(POLARSSL_X509_CRT_WRITE_C)
299/**
300 * \brief Initialize a CRT writing context
301 *
302 * \param ctx CRT context to initialize
303 */
304void x509write_crt_init( x509write_cert *ctx );
305
306/**
307 * \brief Set the verion for a Certificate
308 * Default: X509_CRT_VERSION_3
309 *
310 * \param ctx CRT context to use
311 * \param version version to set (X509_CRT_VERSION_1, X509_CRT_VERSION_2 or
312 * X509_CRT_VERSION_3)
313 */
314void x509write_crt_set_version( x509write_cert *ctx, int version );
315
316/**
317 * \brief Set the serial number for a Certificate.
318 *
319 * \param ctx CRT context to use
320 * \param serial serial number to set
321 *
322 * \return 0 if successful
323 */
324int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial );
325
326/**
327 * \brief Set the validity period for a Certificate
328 * Timestamps should be in string format for UTC timezone
329 * i.e. "YYYYMMDDhhmmss"
330 * e.g. "20131231235959" for December 31st 2013
331 * at 23:59:59
332 *
333 * \param ctx CRT context to use
334 * \param not_before not_before timestamp
335 * \param not_after not_after timestamp
336 *
337 * \return 0 if timestamp was parsed successfully, or
338 * a specific error code
339 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100340int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
341 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200342
343/**
344 * \brief Set the issuer name for a Certificate
345 * Issuer names should contain a comma-separated list
346 * of OID types and values:
347 * e.g. "C=NL,O=Offspark,CN=PolarSSL CA"
348 *
349 * \param ctx CRT context to use
350 * \param issuer_name issuer name to set
351 *
352 * \return 0 if issuer name was parsed successfully, or
353 * a specific error code
354 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100355int x509write_crt_set_issuer_name( x509write_cert *ctx,
356 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357
358/**
359 * \brief Set the subject name for a Certificate
360 * Subject names should contain a comma-separated list
361 * of OID types and values:
362 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
363 *
364 * \param ctx CRT context to use
365 * \param subject_name subject name to set
366 *
367 * \return 0 if subject name was parsed successfully, or
368 * a specific error code
369 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100370int x509write_crt_set_subject_name( x509write_cert *ctx,
371 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372
373/**
374 * \brief Set the subject public key for the certificate
375 *
376 * \param ctx CRT context to use
377 * \param key public key to include
378 */
379void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key );
380
381/**
382 * \brief Set the issuer key used for signing the certificate
383 *
384 * \param ctx CRT context to use
385 * \param key private key to sign with
386 */
387void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key );
388
389/**
390 * \brief Set the MD algorithm to use for the signature
391 * (e.g. POLARSSL_MD_SHA1)
392 *
393 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100394 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200395 */
396void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
397
398/**
399 * \brief Generic function to add to or replace an extension in the
400 * CRT
401 *
402 * \param ctx CRT context to use
403 * \param oid OID of the extension
404 * \param oid_len length of the OID
405 * \param critical if the extension is critical (per the RFC's definition)
406 * \param val value of the extension OCTET STRING
407 * \param val_len length of the value data
408 *
409 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
410 */
411int x509write_crt_set_extension( x509write_cert *ctx,
412 const char *oid, size_t oid_len,
413 int critical,
414 const unsigned char *val, size_t val_len );
415
416/**
417 * \brief Set the basicConstraints extension for a CRT
418 *
419 * \param ctx CRT context to use
420 * \param is_ca is this a CA certificate
421 * \param max_pathlen maximum length of certificate chains below this
422 * certificate (only for CA certificates, -1 is
423 * inlimited)
424 *
425 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
426 */
427int x509write_crt_set_basic_constraints( x509write_cert *ctx,
428 int is_ca, int max_pathlen );
429
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100430#if defined(POLARSSL_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431/**
432 * \brief Set the subjectKeyIdentifier extension for a CRT
433 * Requires that x509write_crt_set_subject_key() has been
434 * called before
435 *
436 * \param ctx CRT context to use
437 *
438 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
439 */
440int x509write_crt_set_subject_key_identifier( x509write_cert *ctx );
441
442/**
443 * \brief Set the authorityKeyIdentifier extension for a CRT
444 * Requires that x509write_crt_set_issuer_key() has been
445 * called before
446 *
447 * \param ctx CRT context to use
448 *
449 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
450 */
451int x509write_crt_set_authority_key_identifier( x509write_cert *ctx );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100452#endif /* POLARSSL_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453
454/**
455 * \brief Set the Key Usage Extension flags
456 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
457 *
458 * \param ctx CRT context to use
459 * \param key_usage key usage flags to set
460 *
461 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
462 */
463int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage );
464
465/**
466 * \brief Set the Netscape Cert Type flags
467 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
468 *
469 * \param ctx CRT context to use
470 * \param ns_cert_type Netscape Cert Type flags to set
471 *
472 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
473 */
474int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
475 unsigned char ns_cert_type );
476
477/**
478 * \brief Free the contents of a CRT write context
479 *
480 * \param ctx CRT context to free
481 */
482void x509write_crt_free( x509write_cert *ctx );
483
484/**
485 * \brief Write a built up certificate to a X509 DER structure
486 * Note: data is written at the end of the buffer! Use the
487 * return value to determine where you should start
488 * using the buffer
489 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100490 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 * \param buf buffer to write to
492 * \param size size of the buffer
493 * \param f_rng RNG function (for signature, see note)
494 * \param p_rng RNG parameter
495 *
496 * \return length of data written if successful, or a specific
497 * error code
498 *
499 * \note f_rng may be NULL if RSA is used for signature and the
500 * signature is made offline (otherwise f_rng is desirable
501 * for countermeasures against timing attacks).
502 * ECDSA signatures always require a non-NULL f_rng.
503 */
504int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
505 int (*f_rng)(void *, unsigned char *, size_t),
506 void *p_rng );
507
508#if defined(POLARSSL_PEM_WRITE_C)
509/**
510 * \brief Write a built up certificate to a X509 PEM string
511 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100512 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513 * \param buf buffer to write to
514 * \param size size of the buffer
515 * \param f_rng RNG function (for signature, see note)
516 * \param p_rng RNG parameter
517 *
518 * \return 0 successful, or a specific error code
519 *
520 * \note f_rng may be NULL if RSA is used for signature and the
521 * signature is made offline (otherwise f_rng is desirable
522 * for countermeasures against timing attacks).
523 * ECDSA signatures always require a non-NULL f_rng.
524 */
525int x509write_crt_pem( x509write_cert *ctx, unsigned char *buf, size_t size,
526 int (*f_rng)(void *, unsigned char *, size_t),
527 void *p_rng );
528#endif /* POLARSSL_PEM_WRITE_C */
529#endif /* POLARSSL_X509_CRT_WRITE_C */
530
531#ifdef __cplusplus
532}
533#endif
534
535#endif /* x509_crt.h */