blob: 916dc3b331ecb8ea5a893d7b8e5ba1d5c2a6d2f0 [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. */
75 x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
76 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
82 unsigned char key_usage; /**< Optional key usage extension value: See the values below */
83
84 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
85
86 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */
87
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 */;
Manuel Pégourié-Gonnardb1d4eb12014-01-22 10:12:57 +010092#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
93 x509_buf sig_params; /**< Parameters for the signature algorithm */
94#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095
Paul Bakkerc559c7a2013-09-18 14:13:26 +020096 struct _x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097}
Paul Bakkerc559c7a2013-09-18 14:13:26 +020098x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099
100#define X509_CRT_VERSION_1 0
101#define X509_CRT_VERSION_2 1
102#define X509_CRT_VERSION_3 2
103
104#define X509_RFC5280_MAX_SERIAL_LEN 32
105#define X509_RFC5280_UTC_TIME_LEN 15
106
107/**
108 * Container for writing a certificate (CRT)
109 */
110typedef struct _x509write_cert
111{
112 int version;
113 mpi serial;
114 pk_context *subject_key;
115 pk_context *issuer_key;
116 asn1_named_data *subject;
117 asn1_named_data *issuer;
118 md_type_t md_alg;
119 char not_before[X509_RFC5280_UTC_TIME_LEN + 1];
120 char not_after[X509_RFC5280_UTC_TIME_LEN + 1];
121 asn1_named_data *extensions;
122}
123x509write_cert;
124
125#if defined(POLARSSL_X509_CRT_PARSE_C)
126/**
127 * \brief Parse a single DER formatted certificate and add it
128 * to the chained list.
129 *
130 * \param chain points to the start of the chain
131 * \param buf buffer holding the certificate DER data
132 * \param buflen size of the buffer
133 *
134 * \return 0 if successful, or a specific X509 or PEM error code
135 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200136int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200137 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138
139/**
140 * \brief Parse one or more certificates and add them
141 * to the chained list. Parses permissively. If some
142 * certificates can be parsed, the result is the number
143 * of failed certificates it encountered. If none complete
144 * correctly, the first error is returned.
145 *
146 * \param chain points to the start of the chain
147 * \param buf buffer holding the certificate data
148 * \param buflen size of the buffer
149 *
150 * \return 0 if all certificates parsed successfully, a positive number
151 * if partly successful or a specific X509 or PEM error code
152 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200153int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200154
155#if defined(POLARSSL_FS_IO)
156/**
157 * \brief Load one or more certificates and add them
158 * to the chained list. Parses permissively. If some
159 * certificates can be parsed, the result is the number
160 * of failed certificates it encountered. If none complete
161 * correctly, the first error is returned.
162 *
163 * \param chain points to the start of the chain
164 * \param path filename to read the certificates from
165 *
166 * \return 0 if all certificates parsed successfully, a positive number
167 * if partly successful or a specific X509 or PEM error code
168 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200169int x509_crt_parse_file( x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200170
171/**
172 * \brief Load one or more certificate files from a path and add them
173 * to the chained list. Parses permissively. If some
174 * certificates can be parsed, the result is the number
175 * of failed certificates it encountered. If none complete
176 * correctly, the first error is returned.
177 *
Manuel Pégourié-Gonnarde3339ce2013-11-28 17:16:41 +0100178 * \warning This function is NOT thread-safe unless
179 * POLARSSL_THREADING_PTHREADS is defined. If you're using an
180 * alternative threading implementation, you should either use
181 * this function only in the main thread, or mutex it.
182 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200183 * \param chain points to the start of the chain
184 * \param path directory / folder to read the certificate files from
185 *
186 * \return 0 if all certificates parsed successfully, a positive number
187 * if partly successful or a specific X509 or PEM error code
188 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200189int x509_crt_parse_path( x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200190#endif /* POLARSSL_FS_IO */
191
192/**
193 * \brief Returns an informational string about the
194 * certificate.
195 *
196 * \param buf Buffer to write to
197 * \param size Maximum size of buffer
198 * \param prefix A line prefix
199 * \param crt The X509 certificate to represent
200 *
201 * \return The amount of data written to the buffer, or -1 in
202 * case of an error.
203 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200204int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200205 const x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206
207/**
208 * \brief Verify the certificate signature
209 *
210 * The verify callback is a user-supplied callback that
211 * can clear / modify / add flags for a certificate. If set,
212 * the verification callback is called for each
213 * certificate in the chain (from the trust-ca down to the
214 * presented crt). The parameters for the callback are:
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200215 * (void *parameter, x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216 * int *flags). With the flags representing current flags for
217 * that specific certificate and the certificate depth from
218 * the bottom (Peer cert depth = 0).
219 *
220 * All flags left after returning from the callback
221 * are also returned to the application. The function should
222 * return 0 for anything but a fatal error.
223 *
224 * \param crt a certificate to be verified
225 * \param trust_ca the trusted CA chain
226 * \param ca_crl the CRL chain for trusted CA's
227 * \param cn expected Common Name (can be set to
228 * NULL if the CN must not be verified)
229 * \param flags result of the verification
230 * \param f_vrfy verification function
231 * \param p_vrfy verification parameter
232 *
233 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
234 * in which case *flags will have one or more of
235 * the following values set:
236 * BADCERT_EXPIRED --
237 * BADCERT_REVOKED --
238 * BADCERT_CN_MISMATCH --
239 * BADCERT_NOT_TRUSTED
240 * or another error in case of a fatal error encountered
241 * during the verification process.
242 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200243int x509_crt_verify( x509_crt *crt,
244 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200245 x509_crl *ca_crl,
246 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200247 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200248 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200250#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200251/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200252 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200253 *
254 * \param crt a certificate to be verified
255 * \param crl the CRL to verify against
256 *
257 * \return 1 if the certificate is revoked, 0 otherwise
258 *
259 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200260int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261#endif /* POLARSSL_X509_CRL_PARSE_C */
262
263/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200264 * \brief Initialize a certificate (chain)
265 *
266 * \param crt Certificate chain to initialize
267 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200268void x509_crt_init( x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200269
270/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271 * \brief Unallocate all certificate data
272 *
273 * \param crt Certificate chain to free
274 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200275void x509_crt_free( x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200276#endif /* POLARSSL_X509_CRT_PARSE_C */
277
278/* \} name */
279/* \} addtogroup x509_module */
280
281#if defined(POLARSSL_X509_CRT_WRITE_C)
282/**
283 * \brief Initialize a CRT writing context
284 *
285 * \param ctx CRT context to initialize
286 */
287void x509write_crt_init( x509write_cert *ctx );
288
289/**
290 * \brief Set the verion for a Certificate
291 * Default: X509_CRT_VERSION_3
292 *
293 * \param ctx CRT context to use
294 * \param version version to set (X509_CRT_VERSION_1, X509_CRT_VERSION_2 or
295 * X509_CRT_VERSION_3)
296 */
297void x509write_crt_set_version( x509write_cert *ctx, int version );
298
299/**
300 * \brief Set the serial number for a Certificate.
301 *
302 * \param ctx CRT context to use
303 * \param serial serial number to set
304 *
305 * \return 0 if successful
306 */
307int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial );
308
309/**
310 * \brief Set the validity period for a Certificate
311 * Timestamps should be in string format for UTC timezone
312 * i.e. "YYYYMMDDhhmmss"
313 * e.g. "20131231235959" for December 31st 2013
314 * at 23:59:59
315 *
316 * \param ctx CRT context to use
317 * \param not_before not_before timestamp
318 * \param not_after not_after timestamp
319 *
320 * \return 0 if timestamp was parsed successfully, or
321 * a specific error code
322 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100323int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
324 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325
326/**
327 * \brief Set the issuer name for a Certificate
328 * Issuer names should contain a comma-separated list
329 * of OID types and values:
330 * e.g. "C=NL,O=Offspark,CN=PolarSSL CA"
331 *
332 * \param ctx CRT context to use
333 * \param issuer_name issuer name to set
334 *
335 * \return 0 if issuer name was parsed successfully, or
336 * a specific error code
337 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100338int x509write_crt_set_issuer_name( x509write_cert *ctx,
339 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200340
341/**
342 * \brief Set the subject name for a Certificate
343 * Subject names should contain a comma-separated list
344 * of OID types and values:
345 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
346 *
347 * \param ctx CRT context to use
348 * \param subject_name subject name to set
349 *
350 * \return 0 if subject name was parsed successfully, or
351 * a specific error code
352 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100353int x509write_crt_set_subject_name( x509write_cert *ctx,
354 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355
356/**
357 * \brief Set the subject public key for the certificate
358 *
359 * \param ctx CRT context to use
360 * \param key public key to include
361 */
362void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key );
363
364/**
365 * \brief Set the issuer key used for signing the certificate
366 *
367 * \param ctx CRT context to use
368 * \param key private key to sign with
369 */
370void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key );
371
372/**
373 * \brief Set the MD algorithm to use for the signature
374 * (e.g. POLARSSL_MD_SHA1)
375 *
376 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100377 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378 */
379void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
380
381/**
382 * \brief Generic function to add to or replace an extension in the
383 * CRT
384 *
385 * \param ctx CRT context to use
386 * \param oid OID of the extension
387 * \param oid_len length of the OID
388 * \param critical if the extension is critical (per the RFC's definition)
389 * \param val value of the extension OCTET STRING
390 * \param val_len length of the value data
391 *
392 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
393 */
394int x509write_crt_set_extension( x509write_cert *ctx,
395 const char *oid, size_t oid_len,
396 int critical,
397 const unsigned char *val, size_t val_len );
398
399/**
400 * \brief Set the basicConstraints extension for a CRT
401 *
402 * \param ctx CRT context to use
403 * \param is_ca is this a CA certificate
404 * \param max_pathlen maximum length of certificate chains below this
405 * certificate (only for CA certificates, -1 is
406 * inlimited)
407 *
408 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
409 */
410int x509write_crt_set_basic_constraints( x509write_cert *ctx,
411 int is_ca, int max_pathlen );
412
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100413#if defined(POLARSSL_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414/**
415 * \brief Set the subjectKeyIdentifier extension for a CRT
416 * Requires that x509write_crt_set_subject_key() has been
417 * called before
418 *
419 * \param ctx CRT context to use
420 *
421 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
422 */
423int x509write_crt_set_subject_key_identifier( x509write_cert *ctx );
424
425/**
426 * \brief Set the authorityKeyIdentifier extension for a CRT
427 * Requires that x509write_crt_set_issuer_key() has been
428 * called before
429 *
430 * \param ctx CRT context to use
431 *
432 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
433 */
434int x509write_crt_set_authority_key_identifier( x509write_cert *ctx );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100435#endif /* POLARSSL_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436
437/**
438 * \brief Set the Key Usage Extension flags
439 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
440 *
441 * \param ctx CRT context to use
442 * \param key_usage key usage flags to set
443 *
444 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
445 */
446int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage );
447
448/**
449 * \brief Set the Netscape Cert Type flags
450 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
451 *
452 * \param ctx CRT context to use
453 * \param ns_cert_type Netscape Cert Type flags to set
454 *
455 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
456 */
457int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
458 unsigned char ns_cert_type );
459
460/**
461 * \brief Free the contents of a CRT write context
462 *
463 * \param ctx CRT context to free
464 */
465void x509write_crt_free( x509write_cert *ctx );
466
467/**
468 * \brief Write a built up certificate to a X509 DER structure
469 * Note: data is written at the end of the buffer! Use the
470 * return value to determine where you should start
471 * using the buffer
472 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100473 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474 * \param buf buffer to write to
475 * \param size size of the buffer
476 * \param f_rng RNG function (for signature, see note)
477 * \param p_rng RNG parameter
478 *
479 * \return length of data written if successful, or a specific
480 * error code
481 *
482 * \note f_rng may be NULL if RSA is used for signature and the
483 * signature is made offline (otherwise f_rng is desirable
484 * for countermeasures against timing attacks).
485 * ECDSA signatures always require a non-NULL f_rng.
486 */
487int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
488 int (*f_rng)(void *, unsigned char *, size_t),
489 void *p_rng );
490
491#if defined(POLARSSL_PEM_WRITE_C)
492/**
493 * \brief Write a built up certificate to a X509 PEM string
494 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100495 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496 * \param buf buffer to write to
497 * \param size size of the buffer
498 * \param f_rng RNG function (for signature, see note)
499 * \param p_rng RNG parameter
500 *
501 * \return 0 successful, or a specific error code
502 *
503 * \note f_rng may be NULL if RSA is used for signature and the
504 * signature is made offline (otherwise f_rng is desirable
505 * for countermeasures against timing attacks).
506 * ECDSA signatures always require a non-NULL f_rng.
507 */
508int x509write_crt_pem( x509write_cert *ctx, unsigned char *buf, size_t size,
509 int (*f_rng)(void *, unsigned char *, size_t),
510 void *p_rng );
511#endif /* POLARSSL_PEM_WRITE_C */
512#endif /* POLARSSL_X509_CRT_WRITE_C */
513
514#ifdef __cplusplus
515}
516#endif
517
518#endif /* x509_crt.h */