blob: a55667607d04d93ba78367495b07c7b7239a23f3 [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 *
175 * \param chain points to the start of the chain
176 * \param path directory / folder to read the certificate files from
177 *
178 * \return 0 if all certificates parsed successfully, a positive number
179 * if partly successful or a specific X509 or PEM error code
180 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200181int x509_crt_parse_path( x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200182#endif /* POLARSSL_FS_IO */
183
184/**
185 * \brief Returns an informational string about the
186 * certificate.
187 *
188 * \param buf Buffer to write to
189 * \param size Maximum size of buffer
190 * \param prefix A line prefix
191 * \param crt The X509 certificate to represent
192 *
193 * \return The amount of data written to the buffer, or -1 in
194 * case of an error.
195 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200196int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200197 const x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200198
199/**
200 * \brief Verify the certificate signature
201 *
202 * The verify callback is a user-supplied callback that
203 * can clear / modify / add flags for a certificate. If set,
204 * the verification callback is called for each
205 * certificate in the chain (from the trust-ca down to the
206 * presented crt). The parameters for the callback are:
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200207 * (void *parameter, x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200208 * int *flags). With the flags representing current flags for
209 * that specific certificate and the certificate depth from
210 * the bottom (Peer cert depth = 0).
211 *
212 * All flags left after returning from the callback
213 * are also returned to the application. The function should
214 * return 0 for anything but a fatal error.
215 *
216 * \param crt a certificate to be verified
217 * \param trust_ca the trusted CA chain
218 * \param ca_crl the CRL chain for trusted CA's
219 * \param cn expected Common Name (can be set to
220 * NULL if the CN must not be verified)
221 * \param flags result of the verification
222 * \param f_vrfy verification function
223 * \param p_vrfy verification parameter
224 *
225 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
226 * in which case *flags will have one or more of
227 * the following values set:
228 * BADCERT_EXPIRED --
229 * BADCERT_REVOKED --
230 * BADCERT_CN_MISMATCH --
231 * BADCERT_NOT_TRUSTED
232 * or another error in case of a fatal error encountered
233 * during the verification process.
234 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200235int x509_crt_verify( x509_crt *crt,
236 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200237 x509_crl *ca_crl,
238 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200239 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200240 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200241
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200242#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200243/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200244 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200245 *
246 * \param crt a certificate to be verified
247 * \param crl the CRL to verify against
248 *
249 * \return 1 if the certificate is revoked, 0 otherwise
250 *
251 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200252int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200253#endif /* POLARSSL_X509_CRL_PARSE_C */
254
255/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200256 * \brief Initialize a certificate (chain)
257 *
258 * \param crt Certificate chain to initialize
259 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200260void x509_crt_init( x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200261
262/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200263 * \brief Unallocate all certificate data
264 *
265 * \param crt Certificate chain to free
266 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200267void x509_crt_free( x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268#endif /* POLARSSL_X509_CRT_PARSE_C */
269
270/* \} name */
271/* \} addtogroup x509_module */
272
273#if defined(POLARSSL_X509_CRT_WRITE_C)
274/**
275 * \brief Initialize a CRT writing context
276 *
277 * \param ctx CRT context to initialize
278 */
279void x509write_crt_init( x509write_cert *ctx );
280
281/**
282 * \brief Set the verion for a Certificate
283 * Default: X509_CRT_VERSION_3
284 *
285 * \param ctx CRT context to use
286 * \param version version to set (X509_CRT_VERSION_1, X509_CRT_VERSION_2 or
287 * X509_CRT_VERSION_3)
288 */
289void x509write_crt_set_version( x509write_cert *ctx, int version );
290
291/**
292 * \brief Set the serial number for a Certificate.
293 *
294 * \param ctx CRT context to use
295 * \param serial serial number to set
296 *
297 * \return 0 if successful
298 */
299int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial );
300
301/**
302 * \brief Set the validity period for a Certificate
303 * Timestamps should be in string format for UTC timezone
304 * i.e. "YYYYMMDDhhmmss"
305 * e.g. "20131231235959" for December 31st 2013
306 * at 23:59:59
307 *
308 * \param ctx CRT context to use
309 * \param not_before not_before timestamp
310 * \param not_after not_after timestamp
311 *
312 * \return 0 if timestamp was parsed successfully, or
313 * a specific error code
314 */
315int x509write_crt_set_validity( x509write_cert *ctx, char *not_before,
316 char *not_after );
317
318/**
319 * \brief Set the issuer name for a Certificate
320 * Issuer names should contain a comma-separated list
321 * of OID types and values:
322 * e.g. "C=NL,O=Offspark,CN=PolarSSL CA"
323 *
324 * \param ctx CRT context to use
325 * \param issuer_name issuer name to set
326 *
327 * \return 0 if issuer name was parsed successfully, or
328 * a specific error code
329 */
330int x509write_crt_set_issuer_name( x509write_cert *ctx, char *issuer_name );
331
332/**
333 * \brief Set the subject name for a Certificate
334 * Subject names should contain a comma-separated list
335 * of OID types and values:
336 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
337 *
338 * \param ctx CRT context to use
339 * \param subject_name subject name to set
340 *
341 * \return 0 if subject name was parsed successfully, or
342 * a specific error code
343 */
344int x509write_crt_set_subject_name( x509write_cert *ctx, char *subject_name );
345
346/**
347 * \brief Set the subject public key for the certificate
348 *
349 * \param ctx CRT context to use
350 * \param key public key to include
351 */
352void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key );
353
354/**
355 * \brief Set the issuer key used for signing the certificate
356 *
357 * \param ctx CRT context to use
358 * \param key private key to sign with
359 */
360void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key );
361
362/**
363 * \brief Set the MD algorithm to use for the signature
364 * (e.g. POLARSSL_MD_SHA1)
365 *
366 * \param ctx CRT context to use
367 * \param md_ald MD algorithm to use
368 */
369void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
370
371/**
372 * \brief Generic function to add to or replace an extension in the
373 * CRT
374 *
375 * \param ctx CRT context to use
376 * \param oid OID of the extension
377 * \param oid_len length of the OID
378 * \param critical if the extension is critical (per the RFC's definition)
379 * \param val value of the extension OCTET STRING
380 * \param val_len length of the value data
381 *
382 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
383 */
384int x509write_crt_set_extension( x509write_cert *ctx,
385 const char *oid, size_t oid_len,
386 int critical,
387 const unsigned char *val, size_t val_len );
388
389/**
390 * \brief Set the basicConstraints extension for a CRT
391 *
392 * \param ctx CRT context to use
393 * \param is_ca is this a CA certificate
394 * \param max_pathlen maximum length of certificate chains below this
395 * certificate (only for CA certificates, -1 is
396 * inlimited)
397 *
398 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
399 */
400int x509write_crt_set_basic_constraints( x509write_cert *ctx,
401 int is_ca, int max_pathlen );
402
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100403#if defined(POLARSSL_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404/**
405 * \brief Set the subjectKeyIdentifier extension for a CRT
406 * Requires that x509write_crt_set_subject_key() has been
407 * called before
408 *
409 * \param ctx CRT context to use
410 *
411 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
412 */
413int x509write_crt_set_subject_key_identifier( x509write_cert *ctx );
414
415/**
416 * \brief Set the authorityKeyIdentifier extension for a CRT
417 * Requires that x509write_crt_set_issuer_key() has been
418 * called before
419 *
420 * \param ctx CRT context to use
421 *
422 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
423 */
424int x509write_crt_set_authority_key_identifier( x509write_cert *ctx );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100425#endif /* POLARSSL_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426
427/**
428 * \brief Set the Key Usage Extension flags
429 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
430 *
431 * \param ctx CRT context to use
432 * \param key_usage key usage flags to set
433 *
434 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
435 */
436int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage );
437
438/**
439 * \brief Set the Netscape Cert Type flags
440 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
441 *
442 * \param ctx CRT context to use
443 * \param ns_cert_type Netscape Cert Type flags to set
444 *
445 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
446 */
447int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
448 unsigned char ns_cert_type );
449
450/**
451 * \brief Free the contents of a CRT write context
452 *
453 * \param ctx CRT context to free
454 */
455void x509write_crt_free( x509write_cert *ctx );
456
457/**
458 * \brief Write a built up certificate to a X509 DER structure
459 * Note: data is written at the end of the buffer! Use the
460 * return value to determine where you should start
461 * using the buffer
462 *
463 * \param crt certificate to write away
464 * \param buf buffer to write to
465 * \param size size of the buffer
466 * \param f_rng RNG function (for signature, see note)
467 * \param p_rng RNG parameter
468 *
469 * \return length of data written if successful, or a specific
470 * error code
471 *
472 * \note f_rng may be NULL if RSA is used for signature and the
473 * signature is made offline (otherwise f_rng is desirable
474 * for countermeasures against timing attacks).
475 * ECDSA signatures always require a non-NULL f_rng.
476 */
477int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
478 int (*f_rng)(void *, unsigned char *, size_t),
479 void *p_rng );
480
481#if defined(POLARSSL_PEM_WRITE_C)
482/**
483 * \brief Write a built up certificate to a X509 PEM string
484 *
485 * \param crt certificate to write away
486 * \param buf buffer to write to
487 * \param size size of the buffer
488 * \param f_rng RNG function (for signature, see note)
489 * \param p_rng RNG parameter
490 *
491 * \return 0 successful, or a specific error code
492 *
493 * \note f_rng may be NULL if RSA is used for signature and the
494 * signature is made offline (otherwise f_rng is desirable
495 * for countermeasures against timing attacks).
496 * ECDSA signatures always require a non-NULL f_rng.
497 */
498int x509write_crt_pem( x509write_cert *ctx, unsigned char *buf, size_t size,
499 int (*f_rng)(void *, unsigned char *, size_t),
500 void *p_rng );
501#endif /* POLARSSL_PEM_WRITE_C */
502#endif /* POLARSSL_X509_CRT_WRITE_C */
503
504#ifdef __cplusplus
505}
506#endif
507
508#endif /* x509_crt.h */