blob: 55042ec53fbd07ca117d92b6c6975933f3b78b8f [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
34#if defined(POLARSSL_X509_CRL_PARSE_C)
35#include "x509_crl.h"
36#endif
37
38/**
39 * \addtogroup x509_module
40 * \{
41 */
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47/**
48 * \name Structures and functions for parsing and writing X.509 certificates
49 * \{
50 */
51
52/**
53 * Container for an X.509 certificate. The certificate may be chained.
54 */
55typedef struct _x509_cert
56{
57 x509_buf raw; /**< The raw certificate data (DER). */
58 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
59
60 int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
61 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
62 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
63
64 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
65 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
66
67 x509_name issuer; /**< The parsed issuer data (named information object). */
68 x509_name subject; /**< The parsed subject data (named information object). */
69
70 x509_time valid_from; /**< Start time of certificate validity. */
71 x509_time valid_to; /**< End time of certificate validity. */
72
73 pk_context pk; /**< Container for the public key context. */
74
75 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
76 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
77 x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
78 x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
79
80 int ext_types; /**< Bit string containing detected and parsed extensions */
81 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
82 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+ */
83
84 unsigned char key_usage; /**< Optional key usage extension value: See the values below */
85
86 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
87
88 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */
89
90 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
91 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
92 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
93 pk_type_t sig_pk /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. POLARSSL_PK_RSA */;
94
95 struct _x509_cert *next; /**< Next certificate in the CA-chain. */
96}
97x509_cert;
98
99#define X509_CRT_VERSION_1 0
100#define X509_CRT_VERSION_2 1
101#define X509_CRT_VERSION_3 2
102
103#define X509_RFC5280_MAX_SERIAL_LEN 32
104#define X509_RFC5280_UTC_TIME_LEN 15
105
106/**
107 * Container for writing a certificate (CRT)
108 */
109typedef struct _x509write_cert
110{
111 int version;
112 mpi serial;
113 pk_context *subject_key;
114 pk_context *issuer_key;
115 asn1_named_data *subject;
116 asn1_named_data *issuer;
117 md_type_t md_alg;
118 char not_before[X509_RFC5280_UTC_TIME_LEN + 1];
119 char not_after[X509_RFC5280_UTC_TIME_LEN + 1];
120 asn1_named_data *extensions;
121}
122x509write_cert;
123
124#if defined(POLARSSL_X509_CRT_PARSE_C)
125/**
126 * \brief Parse a single DER formatted certificate and add it
127 * to the chained list.
128 *
129 * \param chain points to the start of the chain
130 * \param buf buffer holding the certificate DER data
131 * \param buflen size of the buffer
132 *
133 * \return 0 if successful, or a specific X509 or PEM error code
134 */
135int x509parse_crt_der( x509_cert *chain, const unsigned char *buf,
136 size_t buflen );
137
138/**
139 * \brief Parse one or more certificates and add them
140 * to the chained list. Parses permissively. If some
141 * certificates can be parsed, the result is the number
142 * of failed certificates it encountered. If none complete
143 * correctly, the first error is returned.
144 *
145 * \param chain points to the start of the chain
146 * \param buf buffer holding the certificate data
147 * \param buflen size of the buffer
148 *
149 * \return 0 if all certificates parsed successfully, a positive number
150 * if partly successful or a specific X509 or PEM error code
151 */
152int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen );
153
154#if defined(POLARSSL_FS_IO)
155/**
156 * \brief Load one or more certificates and add them
157 * to the chained list. Parses permissively. If some
158 * certificates can be parsed, the result is the number
159 * of failed certificates it encountered. If none complete
160 * correctly, the first error is returned.
161 *
162 * \param chain points to the start of the chain
163 * \param path filename to read the certificates from
164 *
165 * \return 0 if all certificates parsed successfully, a positive number
166 * if partly successful or a specific X509 or PEM error code
167 */
168int x509parse_crtfile( x509_cert *chain, const char *path );
169
170/**
171 * \brief Load one or more certificate files from a path and add them
172 * to the chained list. Parses permissively. If some
173 * certificates can be parsed, the result is the number
174 * of failed certificates it encountered. If none complete
175 * correctly, the first error is returned.
176 *
177 * \param chain points to the start of the chain
178 * \param path directory / folder to read the certificate files from
179 *
180 * \return 0 if all certificates parsed successfully, a positive number
181 * if partly successful or a specific X509 or PEM error code
182 */
183int x509parse_crtpath( x509_cert *chain, const char *path );
184#endif /* POLARSSL_FS_IO */
185
186/**
187 * \brief Returns an informational string about the
188 * certificate.
189 *
190 * \param buf Buffer to write to
191 * \param size Maximum size of buffer
192 * \param prefix A line prefix
193 * \param crt The X509 certificate to represent
194 *
195 * \return The amount of data written to the buffer, or -1 in
196 * case of an error.
197 */
198int x509parse_cert_info( char *buf, size_t size, const char *prefix,
199 const x509_cert *crt );
200
201/**
202 * \brief Verify the certificate signature
203 *
204 * The verify callback is a user-supplied callback that
205 * can clear / modify / add flags for a certificate. If set,
206 * the verification callback is called for each
207 * certificate in the chain (from the trust-ca down to the
208 * presented crt). The parameters for the callback are:
209 * (void *parameter, x509_cert *crt, int certificate_depth,
210 * int *flags). With the flags representing current flags for
211 * that specific certificate and the certificate depth from
212 * the bottom (Peer cert depth = 0).
213 *
214 * All flags left after returning from the callback
215 * are also returned to the application. The function should
216 * return 0 for anything but a fatal error.
217 *
218 * \param crt a certificate to be verified
219 * \param trust_ca the trusted CA chain
220 * \param ca_crl the CRL chain for trusted CA's
221 * \param cn expected Common Name (can be set to
222 * NULL if the CN must not be verified)
223 * \param flags result of the verification
224 * \param f_vrfy verification function
225 * \param p_vrfy verification parameter
226 *
227 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
228 * in which case *flags will have one or more of
229 * the following values set:
230 * BADCERT_EXPIRED --
231 * BADCERT_REVOKED --
232 * BADCERT_CN_MISMATCH --
233 * BADCERT_NOT_TRUSTED
234 * or another error in case of a fatal error encountered
235 * during the verification process.
236 */
237int x509parse_verify( x509_cert *crt,
238 x509_cert *trust_ca,
239 x509_crl *ca_crl,
240 const char *cn, int *flags,
241 int (*f_vrfy)(void *, x509_cert *, int, int *),
242 void *p_vrfy );
243
244#if defined(POLARSSL_X509_CRL_PARSE_C)
245/**
246 * \brief Verify the certificate signature
247 *
248 * \param crt a certificate to be verified
249 * \param crl the CRL to verify against
250 *
251 * \return 1 if the certificate is revoked, 0 otherwise
252 *
253 */
254int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
255#endif /* POLARSSL_X509_CRL_PARSE_C */
256
257/**
258 * \brief Unallocate all certificate data
259 *
260 * \param crt Certificate chain to free
261 */
262void x509_crt_free( x509_cert *crt );
263#endif /* POLARSSL_X509_CRT_PARSE_C */
264
265/* \} name */
266/* \} addtogroup x509_module */
267
268#if defined(POLARSSL_X509_CRT_WRITE_C)
269/**
270 * \brief Initialize a CRT writing context
271 *
272 * \param ctx CRT context to initialize
273 */
274void x509write_crt_init( x509write_cert *ctx );
275
276/**
277 * \brief Set the verion for a Certificate
278 * Default: X509_CRT_VERSION_3
279 *
280 * \param ctx CRT context to use
281 * \param version version to set (X509_CRT_VERSION_1, X509_CRT_VERSION_2 or
282 * X509_CRT_VERSION_3)
283 */
284void x509write_crt_set_version( x509write_cert *ctx, int version );
285
286/**
287 * \brief Set the serial number for a Certificate.
288 *
289 * \param ctx CRT context to use
290 * \param serial serial number to set
291 *
292 * \return 0 if successful
293 */
294int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial );
295
296/**
297 * \brief Set the validity period for a Certificate
298 * Timestamps should be in string format for UTC timezone
299 * i.e. "YYYYMMDDhhmmss"
300 * e.g. "20131231235959" for December 31st 2013
301 * at 23:59:59
302 *
303 * \param ctx CRT context to use
304 * \param not_before not_before timestamp
305 * \param not_after not_after timestamp
306 *
307 * \return 0 if timestamp was parsed successfully, or
308 * a specific error code
309 */
310int x509write_crt_set_validity( x509write_cert *ctx, char *not_before,
311 char *not_after );
312
313/**
314 * \brief Set the issuer name for a Certificate
315 * Issuer names should contain a comma-separated list
316 * of OID types and values:
317 * e.g. "C=NL,O=Offspark,CN=PolarSSL CA"
318 *
319 * \param ctx CRT context to use
320 * \param issuer_name issuer name to set
321 *
322 * \return 0 if issuer name was parsed successfully, or
323 * a specific error code
324 */
325int x509write_crt_set_issuer_name( x509write_cert *ctx, char *issuer_name );
326
327/**
328 * \brief Set the subject name for a Certificate
329 * Subject names should contain a comma-separated list
330 * of OID types and values:
331 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
332 *
333 * \param ctx CRT context to use
334 * \param subject_name subject name to set
335 *
336 * \return 0 if subject name was parsed successfully, or
337 * a specific error code
338 */
339int x509write_crt_set_subject_name( x509write_cert *ctx, char *subject_name );
340
341/**
342 * \brief Set the subject public key for the certificate
343 *
344 * \param ctx CRT context to use
345 * \param key public key to include
346 */
347void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key );
348
349/**
350 * \brief Set the issuer key used for signing the certificate
351 *
352 * \param ctx CRT context to use
353 * \param key private key to sign with
354 */
355void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key );
356
357/**
358 * \brief Set the MD algorithm to use for the signature
359 * (e.g. POLARSSL_MD_SHA1)
360 *
361 * \param ctx CRT context to use
362 * \param md_ald MD algorithm to use
363 */
364void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
365
366/**
367 * \brief Generic function to add to or replace an extension in the
368 * CRT
369 *
370 * \param ctx CRT context to use
371 * \param oid OID of the extension
372 * \param oid_len length of the OID
373 * \param critical if the extension is critical (per the RFC's definition)
374 * \param val value of the extension OCTET STRING
375 * \param val_len length of the value data
376 *
377 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
378 */
379int x509write_crt_set_extension( x509write_cert *ctx,
380 const char *oid, size_t oid_len,
381 int critical,
382 const unsigned char *val, size_t val_len );
383
384/**
385 * \brief Set the basicConstraints extension for a CRT
386 *
387 * \param ctx CRT context to use
388 * \param is_ca is this a CA certificate
389 * \param max_pathlen maximum length of certificate chains below this
390 * certificate (only for CA certificates, -1 is
391 * inlimited)
392 *
393 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
394 */
395int x509write_crt_set_basic_constraints( x509write_cert *ctx,
396 int is_ca, int max_pathlen );
397
398/**
399 * \brief Set the subjectKeyIdentifier extension for a CRT
400 * Requires that x509write_crt_set_subject_key() has been
401 * called before
402 *
403 * \param ctx CRT context to use
404 *
405 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
406 */
407int x509write_crt_set_subject_key_identifier( x509write_cert *ctx );
408
409/**
410 * \brief Set the authorityKeyIdentifier extension for a CRT
411 * Requires that x509write_crt_set_issuer_key() has been
412 * called before
413 *
414 * \param ctx CRT context to use
415 *
416 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
417 */
418int x509write_crt_set_authority_key_identifier( x509write_cert *ctx );
419
420/**
421 * \brief Set the Key Usage Extension flags
422 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
423 *
424 * \param ctx CRT context to use
425 * \param key_usage key usage flags to set
426 *
427 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
428 */
429int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage );
430
431/**
432 * \brief Set the Netscape Cert Type flags
433 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
434 *
435 * \param ctx CRT context to use
436 * \param ns_cert_type Netscape Cert Type flags to set
437 *
438 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
439 */
440int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
441 unsigned char ns_cert_type );
442
443/**
444 * \brief Free the contents of a CRT write context
445 *
446 * \param ctx CRT context to free
447 */
448void x509write_crt_free( x509write_cert *ctx );
449
450/**
451 * \brief Write a built up certificate to a X509 DER structure
452 * Note: data is written at the end of the buffer! Use the
453 * return value to determine where you should start
454 * using the buffer
455 *
456 * \param crt certificate to write away
457 * \param buf buffer to write to
458 * \param size size of the buffer
459 * \param f_rng RNG function (for signature, see note)
460 * \param p_rng RNG parameter
461 *
462 * \return length of data written if successful, or a specific
463 * error code
464 *
465 * \note f_rng may be NULL if RSA is used for signature and the
466 * signature is made offline (otherwise f_rng is desirable
467 * for countermeasures against timing attacks).
468 * ECDSA signatures always require a non-NULL f_rng.
469 */
470int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
471 int (*f_rng)(void *, unsigned char *, size_t),
472 void *p_rng );
473
474#if defined(POLARSSL_PEM_WRITE_C)
475/**
476 * \brief Write a built up certificate to a X509 PEM string
477 *
478 * \param crt certificate to write away
479 * \param buf buffer to write to
480 * \param size size of the buffer
481 * \param f_rng RNG function (for signature, see note)
482 * \param p_rng RNG parameter
483 *
484 * \return 0 successful, or a specific error code
485 *
486 * \note f_rng may be NULL if RSA is used for signature and the
487 * signature is made offline (otherwise f_rng is desirable
488 * for countermeasures against timing attacks).
489 * ECDSA signatures always require a non-NULL f_rng.
490 */
491int x509write_crt_pem( x509write_cert *ctx, unsigned char *buf, size_t size,
492 int (*f_rng)(void *, unsigned char *, size_t),
493 void *p_rng );
494#endif /* POLARSSL_PEM_WRITE_C */
495#endif /* POLARSSL_X509_CRT_WRITE_C */
496
497#ifdef __cplusplus
498}
499#endif
500
501#endif /* x509_crt.h */