blob: a5ab1789aee34f7c9189da6275a9f2b51d649839 [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 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100315int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
316 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200317
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 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100330int x509write_crt_set_issuer_name( x509write_cert *ctx,
331 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200332
333/**
334 * \brief Set the subject name for a Certificate
335 * Subject names should contain a comma-separated list
336 * of OID types and values:
337 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
338 *
339 * \param ctx CRT context to use
340 * \param subject_name subject name to set
341 *
342 * \return 0 if subject name was parsed successfully, or
343 * a specific error code
344 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100345int x509write_crt_set_subject_name( x509write_cert *ctx,
346 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347
348/**
349 * \brief Set the subject public key for the certificate
350 *
351 * \param ctx CRT context to use
352 * \param key public key to include
353 */
354void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key );
355
356/**
357 * \brief Set the issuer key used for signing the certificate
358 *
359 * \param ctx CRT context to use
360 * \param key private key to sign with
361 */
362void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key );
363
364/**
365 * \brief Set the MD algorithm to use for the signature
366 * (e.g. POLARSSL_MD_SHA1)
367 *
368 * \param ctx CRT context to use
369 * \param md_ald MD algorithm to use
370 */
371void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
372
373/**
374 * \brief Generic function to add to or replace an extension in the
375 * CRT
376 *
377 * \param ctx CRT context to use
378 * \param oid OID of the extension
379 * \param oid_len length of the OID
380 * \param critical if the extension is critical (per the RFC's definition)
381 * \param val value of the extension OCTET STRING
382 * \param val_len length of the value data
383 *
384 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
385 */
386int x509write_crt_set_extension( x509write_cert *ctx,
387 const char *oid, size_t oid_len,
388 int critical,
389 const unsigned char *val, size_t val_len );
390
391/**
392 * \brief Set the basicConstraints extension for a CRT
393 *
394 * \param ctx CRT context to use
395 * \param is_ca is this a CA certificate
396 * \param max_pathlen maximum length of certificate chains below this
397 * certificate (only for CA certificates, -1 is
398 * inlimited)
399 *
400 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
401 */
402int x509write_crt_set_basic_constraints( x509write_cert *ctx,
403 int is_ca, int max_pathlen );
404
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100405#if defined(POLARSSL_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406/**
407 * \brief Set the subjectKeyIdentifier extension for a CRT
408 * Requires that x509write_crt_set_subject_key() has been
409 * called before
410 *
411 * \param ctx CRT context to use
412 *
413 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
414 */
415int x509write_crt_set_subject_key_identifier( x509write_cert *ctx );
416
417/**
418 * \brief Set the authorityKeyIdentifier extension for a CRT
419 * Requires that x509write_crt_set_issuer_key() has been
420 * called before
421 *
422 * \param ctx CRT context to use
423 *
424 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
425 */
426int x509write_crt_set_authority_key_identifier( x509write_cert *ctx );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100427#endif /* POLARSSL_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
429/**
430 * \brief Set the Key Usage Extension flags
431 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
432 *
433 * \param ctx CRT context to use
434 * \param key_usage key usage flags to set
435 *
436 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
437 */
438int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage );
439
440/**
441 * \brief Set the Netscape Cert Type flags
442 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
443 *
444 * \param ctx CRT context to use
445 * \param ns_cert_type Netscape Cert Type flags to set
446 *
447 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
448 */
449int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
450 unsigned char ns_cert_type );
451
452/**
453 * \brief Free the contents of a CRT write context
454 *
455 * \param ctx CRT context to free
456 */
457void x509write_crt_free( x509write_cert *ctx );
458
459/**
460 * \brief Write a built up certificate to a X509 DER structure
461 * Note: data is written at the end of the buffer! Use the
462 * return value to determine where you should start
463 * using the buffer
464 *
465 * \param crt certificate to write away
466 * \param buf buffer to write to
467 * \param size size of the buffer
468 * \param f_rng RNG function (for signature, see note)
469 * \param p_rng RNG parameter
470 *
471 * \return length of data written if successful, or a specific
472 * error code
473 *
474 * \note f_rng may be NULL if RSA is used for signature and the
475 * signature is made offline (otherwise f_rng is desirable
476 * for countermeasures against timing attacks).
477 * ECDSA signatures always require a non-NULL f_rng.
478 */
479int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
480 int (*f_rng)(void *, unsigned char *, size_t),
481 void *p_rng );
482
483#if defined(POLARSSL_PEM_WRITE_C)
484/**
485 * \brief Write a built up certificate to a X509 PEM string
486 *
487 * \param crt certificate to write away
488 * \param buf buffer to write to
489 * \param size size of the buffer
490 * \param f_rng RNG function (for signature, see note)
491 * \param p_rng RNG parameter
492 *
493 * \return 0 successful, or a specific error code
494 *
495 * \note f_rng may be NULL if RSA is used for signature and the
496 * signature is made offline (otherwise f_rng is desirable
497 * for countermeasures against timing attacks).
498 * ECDSA signatures always require a non-NULL f_rng.
499 */
500int x509write_crt_pem( x509write_cert *ctx, unsigned char *buf, size_t size,
501 int (*f_rng)(void *, unsigned char *, size_t),
502 void *p_rng );
503#endif /* POLARSSL_PEM_WRITE_C */
504#endif /* POLARSSL_X509_CRT_WRITE_C */
505
506#ifdef __cplusplus
507}
508#endif
509
510#endif /* x509_crt.h */