blob: 4e7bbb7b3b4096fedb8a87b3a48fc495b37363ce [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 */;
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é-Gonnardcbf3ef32013-09-23 12:20:02 +0200247#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200248/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200249 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200250 *
251 * \param crt a certificate to be verified
252 * \param crl the CRL to verify against
253 *
254 * \return 1 if the certificate is revoked, 0 otherwise
255 *
256 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200257int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200258#endif /* POLARSSL_X509_CRL_PARSE_C */
259
260/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200261 * \brief Initialize a certificate (chain)
262 *
263 * \param crt Certificate chain to initialize
264 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200265void x509_crt_init( x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200266
267/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268 * \brief Unallocate all certificate data
269 *
270 * \param crt Certificate chain to free
271 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200272void x509_crt_free( x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200273#endif /* POLARSSL_X509_CRT_PARSE_C */
274
275/* \} name */
276/* \} addtogroup x509_module */
277
278#if defined(POLARSSL_X509_CRT_WRITE_C)
279/**
280 * \brief Initialize a CRT writing context
281 *
282 * \param ctx CRT context to initialize
283 */
284void x509write_crt_init( x509write_cert *ctx );
285
286/**
287 * \brief Set the verion for a Certificate
288 * Default: X509_CRT_VERSION_3
289 *
290 * \param ctx CRT context to use
291 * \param version version to set (X509_CRT_VERSION_1, X509_CRT_VERSION_2 or
292 * X509_CRT_VERSION_3)
293 */
294void x509write_crt_set_version( x509write_cert *ctx, int version );
295
296/**
297 * \brief Set the serial number for a Certificate.
298 *
299 * \param ctx CRT context to use
300 * \param serial serial number to set
301 *
302 * \return 0 if successful
303 */
304int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial );
305
306/**
307 * \brief Set the validity period for a Certificate
308 * Timestamps should be in string format for UTC timezone
309 * i.e. "YYYYMMDDhhmmss"
310 * e.g. "20131231235959" for December 31st 2013
311 * at 23:59:59
312 *
313 * \param ctx CRT context to use
314 * \param not_before not_before timestamp
315 * \param not_after not_after timestamp
316 *
317 * \return 0 if timestamp was parsed successfully, or
318 * a specific error code
319 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100320int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
321 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200322
323/**
324 * \brief Set the issuer name for a Certificate
325 * Issuer names should contain a comma-separated list
326 * of OID types and values:
327 * e.g. "C=NL,O=Offspark,CN=PolarSSL CA"
328 *
329 * \param ctx CRT context to use
330 * \param issuer_name issuer name to set
331 *
332 * \return 0 if issuer name was parsed successfully, or
333 * a specific error code
334 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100335int x509write_crt_set_issuer_name( x509write_cert *ctx,
336 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337
338/**
339 * \brief Set the subject name for a Certificate
340 * Subject names should contain a comma-separated list
341 * of OID types and values:
342 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
343 *
344 * \param ctx CRT context to use
345 * \param subject_name subject name to set
346 *
347 * \return 0 if subject name was parsed successfully, or
348 * a specific error code
349 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100350int x509write_crt_set_subject_name( x509write_cert *ctx,
351 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352
353/**
354 * \brief Set the subject public key for the certificate
355 *
356 * \param ctx CRT context to use
357 * \param key public key to include
358 */
359void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key );
360
361/**
362 * \brief Set the issuer key used for signing the certificate
363 *
364 * \param ctx CRT context to use
365 * \param key private key to sign with
366 */
367void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key );
368
369/**
370 * \brief Set the MD algorithm to use for the signature
371 * (e.g. POLARSSL_MD_SHA1)
372 *
373 * \param ctx CRT context to use
374 * \param md_ald MD algorithm to use
375 */
376void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
377
378/**
379 * \brief Generic function to add to or replace an extension in the
380 * CRT
381 *
382 * \param ctx CRT context to use
383 * \param oid OID of the extension
384 * \param oid_len length of the OID
385 * \param critical if the extension is critical (per the RFC's definition)
386 * \param val value of the extension OCTET STRING
387 * \param val_len length of the value data
388 *
389 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
390 */
391int x509write_crt_set_extension( x509write_cert *ctx,
392 const char *oid, size_t oid_len,
393 int critical,
394 const unsigned char *val, size_t val_len );
395
396/**
397 * \brief Set the basicConstraints extension for a CRT
398 *
399 * \param ctx CRT context to use
400 * \param is_ca is this a CA certificate
401 * \param max_pathlen maximum length of certificate chains below this
402 * certificate (only for CA certificates, -1 is
403 * inlimited)
404 *
405 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
406 */
407int x509write_crt_set_basic_constraints( x509write_cert *ctx,
408 int is_ca, int max_pathlen );
409
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100410#if defined(POLARSSL_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411/**
412 * \brief Set the subjectKeyIdentifier extension for a CRT
413 * Requires that x509write_crt_set_subject_key() has been
414 * called before
415 *
416 * \param ctx CRT context to use
417 *
418 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
419 */
420int x509write_crt_set_subject_key_identifier( x509write_cert *ctx );
421
422/**
423 * \brief Set the authorityKeyIdentifier extension for a CRT
424 * Requires that x509write_crt_set_issuer_key() has been
425 * called before
426 *
427 * \param ctx CRT context to use
428 *
429 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
430 */
431int x509write_crt_set_authority_key_identifier( x509write_cert *ctx );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100432#endif /* POLARSSL_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433
434/**
435 * \brief Set the Key Usage Extension flags
436 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
437 *
438 * \param ctx CRT context to use
439 * \param key_usage key usage flags to set
440 *
441 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
442 */
443int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage );
444
445/**
446 * \brief Set the Netscape Cert Type flags
447 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
448 *
449 * \param ctx CRT context to use
450 * \param ns_cert_type Netscape Cert Type flags to set
451 *
452 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
453 */
454int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
455 unsigned char ns_cert_type );
456
457/**
458 * \brief Free the contents of a CRT write context
459 *
460 * \param ctx CRT context to free
461 */
462void x509write_crt_free( x509write_cert *ctx );
463
464/**
465 * \brief Write a built up certificate to a X509 DER structure
466 * Note: data is written at the end of the buffer! Use the
467 * return value to determine where you should start
468 * using the buffer
469 *
470 * \param crt certificate to write away
471 * \param buf buffer to write to
472 * \param size size of the buffer
473 * \param f_rng RNG function (for signature, see note)
474 * \param p_rng RNG parameter
475 *
476 * \return length of data written if successful, or a specific
477 * error code
478 *
479 * \note f_rng may be NULL if RSA is used for signature and the
480 * signature is made offline (otherwise f_rng is desirable
481 * for countermeasures against timing attacks).
482 * ECDSA signatures always require a non-NULL f_rng.
483 */
484int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
485 int (*f_rng)(void *, unsigned char *, size_t),
486 void *p_rng );
487
488#if defined(POLARSSL_PEM_WRITE_C)
489/**
490 * \brief Write a built up certificate to a X509 PEM string
491 *
492 * \param crt certificate to write away
493 * \param buf buffer to write to
494 * \param size size of the buffer
495 * \param f_rng RNG function (for signature, see note)
496 * \param p_rng RNG parameter
497 *
498 * \return 0 successful, or a specific error code
499 *
500 * \note f_rng may be NULL if RSA is used for signature and the
501 * signature is made offline (otherwise f_rng is desirable
502 * for countermeasures against timing attacks).
503 * ECDSA signatures always require a non-NULL f_rng.
504 */
505int x509write_crt_pem( x509write_cert *ctx, unsigned char *buf, size_t size,
506 int (*f_rng)(void *, unsigned char *, size_t),
507 void *p_rng );
508#endif /* POLARSSL_PEM_WRITE_C */
509#endif /* POLARSSL_X509_CRT_WRITE_C */
510
511#ifdef __cplusplus
512}
513#endif
514
515#endif /* x509_crt.h */