blob: 26d351ae387150dc5c22202bebf5171ce8520347 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The ITU-T X.509 standard defines a certificate format for PKI.
27 *
28 * http://www.ietf.org/rfc/rfc3279.txt
29 * http://www.ietf.org/rfc/rfc3280.txt
30 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020039#else
40#include POLARSSL_CONFIG_FILE
41#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042
43#if defined(POLARSSL_X509_CRL_PARSE_C)
44
45#include "polarssl/x509_crl.h"
46#include "polarssl/oid.h"
47#if defined(POLARSSL_PEM_PARSE_C)
48#include "polarssl/pem.h"
49#endif
50
Paul Bakker7dc4c442014-02-01 22:50:26 +010051#if defined(POLARSSL_PLATFORM_C)
52#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#else
54#define polarssl_malloc malloc
55#define polarssl_free free
56#endif
57
58#include <string.h>
59#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010060#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
61
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#include <windows.h>
63#else
64#include <time.h>
65#endif
66
Paul Bakkerfa6a6202013-10-28 18:48:30 +010067#if defined(POLARSSL_FS_IO) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#include <stdio.h>
69#endif
70
71/*
72 * Version ::= INTEGER { v1(0), v2(1) }
73 */
74static int x509_crl_get_version( unsigned char **p,
75 const unsigned char *end,
76 int *ver )
77{
78 int ret;
79
80 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
81 {
82 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
83 {
84 *ver = 0;
85 return( 0 );
86 }
87
Paul Bakker51876562013-09-17 14:36:05 +020088 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089 }
90
91 return( 0 );
92}
93
94/*
95 * X.509 CRL v2 extensions (no extensions parsed yet.)
96 */
97static int x509_get_crl_ext( unsigned char **p,
98 const unsigned char *end,
99 x509_buf *ext )
100{
101 int ret;
102 size_t len = 0;
103
104 /* Get explicit tag */
105 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
106 {
107 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
108 return( 0 );
109
110 return( ret );
111 }
112
113 while( *p < end )
114 {
115 if( ( ret = asn1_get_tag( p, end, &len,
116 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200117 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200118
119 *p += len;
120 }
121
122 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200123 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200124 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
125
126 return( 0 );
127}
128
129/*
130 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
131 */
132static int x509_get_crl_entry_ext( unsigned char **p,
133 const unsigned char *end,
134 x509_buf *ext )
135{
136 int ret;
137 size_t len = 0;
138
139 /* OPTIONAL */
140 if (end <= *p)
141 return( 0 );
142
143 ext->tag = **p;
144 ext->p = *p;
145
146 /*
147 * Get CRL-entry extension sequence header
148 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
149 */
150 if( ( ret = asn1_get_tag( p, end, &ext->len,
151 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
152 {
153 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
154 {
155 ext->p = NULL;
156 return( 0 );
157 }
Paul Bakker51876562013-09-17 14:36:05 +0200158 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200159 }
160
Paul Bakker9af723c2014-05-01 13:03:14 +0200161 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200162
163 if( end != *p + ext->len )
Paul Bakker51876562013-09-17 14:36:05 +0200164 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
166
167 while( *p < end )
168 {
169 if( ( ret = asn1_get_tag( p, end, &len,
170 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200171 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172
173 *p += len;
174 }
175
176 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200177 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200178 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
179
180 return( 0 );
181}
182
183/*
184 * X.509 CRL Entries
185 */
186static int x509_get_entries( unsigned char **p,
187 const unsigned char *end,
188 x509_crl_entry *entry )
189{
190 int ret;
191 size_t entry_len;
192 x509_crl_entry *cur_entry = entry;
193
194 if( *p == end )
195 return( 0 );
196
197 if( ( ret = asn1_get_tag( p, end, &entry_len,
198 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
199 {
200 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
201 return( 0 );
202
203 return( ret );
204 }
205
206 end = *p + entry_len;
207
208 while( *p < end )
209 {
210 size_t len2;
211 const unsigned char *end2;
212
213 if( ( ret = asn1_get_tag( p, end, &len2,
214 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
215 {
216 return( ret );
217 }
218
219 cur_entry->raw.tag = **p;
220 cur_entry->raw.p = *p;
221 cur_entry->raw.len = len2;
222 end2 = *p + len2;
223
224 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
225 return( ret );
226
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200227 if( ( ret = x509_get_time( p, end2,
228 &cur_entry->revocation_date ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229 return( ret );
230
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200231 if( ( ret = x509_get_crl_entry_ext( p, end2,
232 &cur_entry->entry_ext ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200233 return( ret );
234
235 if ( *p < end )
236 {
237 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
238
239 if( cur_entry->next == NULL )
240 return( POLARSSL_ERR_X509_MALLOC_FAILED );
241
242 cur_entry = cur_entry->next;
243 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
244 }
245 }
246
247 return( 0 );
248}
249
250/*
251 * Parse one or more CRLs and add them to the chained list
252 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200253int x509_crl_parse( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254{
255 int ret;
256 size_t len;
257 unsigned char *p, *end;
258 x509_crl *crl;
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200259 x509_buf sig_params1, sig_params2;
Manuel Pégourié-Gonnard8e42ff62014-01-24 15:56:20 +0100260
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261#if defined(POLARSSL_PEM_PARSE_C)
262 size_t use_len;
263 pem_context pem;
264#endif
265
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200266 memset( &sig_params1, 0, sizeof( x509_buf ) );
267 memset( &sig_params2, 0, sizeof( x509_buf ) );
Manuel Pégourié-Gonnard8e42ff62014-01-24 15:56:20 +0100268
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269 crl = chain;
270
271 /*
272 * Check for valid input
273 */
274 if( crl == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200275 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200276
277 while( crl->version != 0 && crl->next != NULL )
278 crl = crl->next;
279
280 /*
281 * Add new CRL on the end of the chain if needed.
282 */
283 if ( crl->version != 0 && crl->next == NULL)
284 {
285 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
286
287 if( crl->next == NULL )
288 {
289 x509_crl_free( crl );
290 return( POLARSSL_ERR_X509_MALLOC_FAILED );
291 }
292
293 crl = crl->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200294 x509_crl_init( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200295 }
296
297#if defined(POLARSSL_PEM_PARSE_C)
298 pem_init( &pem );
299 ret = pem_read_buffer( &pem,
300 "-----BEGIN X509 CRL-----",
301 "-----END X509 CRL-----",
302 buf, NULL, 0, &use_len );
303
304 if( ret == 0 )
305 {
306 /*
307 * Was PEM encoded
308 */
309 buflen -= use_len;
310 buf += use_len;
311
312 /*
313 * Steal PEM buffer
314 */
315 p = pem.buf;
316 pem.buf = NULL;
317 len = pem.buflen;
318 pem_free( &pem );
319 }
320 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
321 {
322 pem_free( &pem );
323 return( ret );
324 }
325 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200326#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327 {
328 /*
329 * nope, copy the raw DER data
330 */
331 p = (unsigned char *) polarssl_malloc( len = buflen );
332
333 if( p == NULL )
334 return( POLARSSL_ERR_X509_MALLOC_FAILED );
335
336 memcpy( p, buf, buflen );
337
338 buflen = 0;
339 }
340
341 crl->raw.p = p;
342 crl->raw.len = len;
343 end = p + len;
344
345 /*
346 * CertificateList ::= SEQUENCE {
347 * tbsCertList TBSCertList,
348 * signatureAlgorithm AlgorithmIdentifier,
349 * signatureValue BIT STRING }
350 */
351 if( ( ret = asn1_get_tag( &p, end, &len,
352 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
353 {
354 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200355 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356 }
357
358 if( len != (size_t) ( end - p ) )
359 {
360 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200361 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
363 }
364
365 /*
366 * TBSCertList ::= SEQUENCE {
367 */
368 crl->tbs.p = p;
369
370 if( ( ret = asn1_get_tag( &p, end, &len,
371 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
372 {
373 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200374 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375 }
376
377 end = p + len;
378 crl->tbs.len = end - crl->tbs.p;
379
380 /*
381 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
382 * -- if present, MUST be v2
383 *
384 * signature AlgorithmIdentifier
385 */
386 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200387 ( ret = x509_get_alg( &p, end, &crl->sig_oid1, &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388 {
389 x509_crl_free( crl );
390 return( ret );
391 }
392
393 crl->version++;
394
395 if( crl->version > 2 )
396 {
397 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200398 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399 }
400
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200401 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200402 &crl->sig_md, &crl->sig_pk,
403 &crl->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404 {
405 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200406 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407 }
408
409 /*
410 * issuer Name
411 */
412 crl->issuer_raw.p = p;
413
414 if( ( ret = asn1_get_tag( &p, end, &len,
415 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
416 {
417 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200418 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419 }
420
421 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
422 {
423 x509_crl_free( crl );
424 return( ret );
425 }
426
427 crl->issuer_raw.len = p - crl->issuer_raw.p;
428
429 /*
430 * thisUpdate Time
431 * nextUpdate Time OPTIONAL
432 */
433 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
434 {
435 x509_crl_free( crl );
436 return( ret );
437 }
438
439 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
440 {
Paul Bakker51876562013-09-17 14:36:05 +0200441 if ( ret != ( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker51876562013-09-17 14:36:05 +0200443 ret != ( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
445 {
446 x509_crl_free( crl );
447 return( ret );
448 }
449 }
450
451 /*
452 * revokedCertificates SEQUENCE OF SEQUENCE {
453 * userCertificate CertificateSerialNumber,
454 * revocationDate Time,
455 * crlEntryExtensions Extensions OPTIONAL
456 * -- if present, MUST be v2
457 * } OPTIONAL
458 */
459 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
460 {
461 x509_crl_free( crl );
462 return( ret );
463 }
464
465 /*
466 * crlExtensions EXPLICIT Extensions OPTIONAL
467 * -- if present, MUST be v2
468 */
469 if( crl->version == 2 )
470 {
471 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
472
473 if( ret != 0 )
474 {
475 x509_crl_free( crl );
476 return( ret );
477 }
478 }
479
480 if( p != end )
481 {
482 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200483 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
485 }
486
487 end = crl->raw.p + crl->raw.len;
488
489 /*
490 * signatureAlgorithm AlgorithmIdentifier,
491 * signatureValue BIT STRING
492 */
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200493 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494 {
495 x509_crl_free( crl );
496 return( ret );
497 }
498
499 if( crl->sig_oid1.len != crl->sig_oid2.len ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200500 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 ||
501 sig_params1.len != sig_params2.len ||
502 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503 {
504 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200505 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506 }
507
508 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
509 {
510 x509_crl_free( crl );
511 return( ret );
512 }
513
514 if( p != end )
515 {
516 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200517 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
519 }
520
521 if( buflen > 0 )
522 {
523 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
524
525 if( crl->next == NULL )
526 {
527 x509_crl_free( crl );
528 return( POLARSSL_ERR_X509_MALLOC_FAILED );
529 }
530
531 crl = crl->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200532 x509_crl_init( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533
Paul Bakkerddf26b42013-09-18 13:46:23 +0200534 return( x509_crl_parse( crl, buf, buflen ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535 }
536
537 return( 0 );
538}
539
540#if defined(POLARSSL_FS_IO)
541/*
542 * Load one or more CRLs and add them to the chained list
543 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200544int x509_crl_parse_file( x509_crl *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545{
546 int ret;
547 size_t n;
548 unsigned char *buf;
549
550 if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
551 return( ret );
552
Paul Bakkerddf26b42013-09-18 13:46:23 +0200553 ret = x509_crl_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554
555 memset( buf, 0, n + 1 );
556 polarssl_free( buf );
557
558 return( ret );
559}
560#endif /* POLARSSL_FS_IO */
561
Paul Bakker6edcd412013-10-29 15:22:54 +0100562#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
563 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564#include <stdarg.h>
565
566#if !defined vsnprintf
567#define vsnprintf _vsnprintf
568#endif // vsnprintf
569
570/*
571 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
572 * Result value is not size of buffer needed, but -1 if no fit is possible.
573 *
574 * This fuction tries to 'fix' this by at least suggesting enlarging the
575 * size by 20.
576 */
577static int compat_snprintf(char *str, size_t size, const char *format, ...)
578{
579 va_list ap;
580 int res = -1;
581
582 va_start( ap, format );
583
584 res = vsnprintf( str, size, format, ap );
585
586 va_end( ap );
587
588 // No quick fix possible
589 if ( res < 0 )
590 return( (int) size + 20 );
591
592 return res;
593}
594
595#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200596#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597
598#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
599
600#define SAFE_SNPRINTF() \
601{ \
602 if( ret == -1 ) \
603 return( -1 ); \
604 \
605 if ( (unsigned int) ret > n ) { \
606 p[n - 1] = '\0'; \
607 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
608 } \
609 \
610 n -= (unsigned int) ret; \
611 p += (unsigned int) ret; \
612}
613
614/*
615 * Return an informational string about the certificate.
616 */
617#define BEFORE_COLON 14
618#define BC "14"
619/*
620 * Return an informational string about the CRL.
621 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200622int x509_crl_info( char *buf, size_t size, const char *prefix,
623 const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624{
625 int ret;
626 size_t n;
627 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628 const x509_crl_entry *entry;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100629#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200630 const void *sig_opts = crl->sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100631#else
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200632 const void *sig_opts = NULL;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100633#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634
635 p = buf;
636 n = size;
637
638 ret = snprintf( p, n, "%sCRL version : %d",
639 prefix, crl->version );
640 SAFE_SNPRINTF();
641
642 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
643 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +0200644 ret = x509_dn_gets( p, n, &crl->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645 SAFE_SNPRINTF();
646
647 ret = snprintf( p, n, "\n%sthis update : " \
648 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
649 crl->this_update.year, crl->this_update.mon,
650 crl->this_update.day, crl->this_update.hour,
651 crl->this_update.min, crl->this_update.sec );
652 SAFE_SNPRINTF();
653
654 ret = snprintf( p, n, "\n%snext update : " \
655 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
656 crl->next_update.year, crl->next_update.mon,
657 crl->next_update.day, crl->next_update.hour,
658 crl->next_update.min, crl->next_update.sec );
659 SAFE_SNPRINTF();
660
661 entry = &crl->entry;
662
663 ret = snprintf( p, n, "\n%sRevoked certificates:",
664 prefix );
665 SAFE_SNPRINTF();
666
667 while( entry != NULL && entry->raw.len != 0 )
668 {
669 ret = snprintf( p, n, "\n%sserial number: ",
670 prefix );
671 SAFE_SNPRINTF();
672
Paul Bakker86d0c192013-09-18 11:11:02 +0200673 ret = x509_serial_gets( p, n, &entry->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 SAFE_SNPRINTF();
675
676 ret = snprintf( p, n, " revocation date: " \
677 "%04d-%02d-%02d %02d:%02d:%02d",
678 entry->revocation_date.year, entry->revocation_date.mon,
679 entry->revocation_date.day, entry->revocation_date.hour,
680 entry->revocation_date.min, entry->revocation_date.sec );
681 SAFE_SNPRINTF();
682
683 entry = entry->next;
684 }
685
686 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
687 SAFE_SNPRINTF();
688
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200689 ret = x509_sig_alg_gets( p, n, &crl->sig_oid1, crl->sig_pk, crl->sig_md,
690 sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 SAFE_SNPRINTF();
692
693 ret = snprintf( p, n, "\n" );
694 SAFE_SNPRINTF();
695
696 return( (int) ( size - n ) );
697}
698
699/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200700 * Initialize a CRL chain
701 */
702void x509_crl_init( x509_crl *crl )
703{
704 memset( crl, 0, sizeof(x509_crl) );
705}
706
707/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 * Unallocate all CRL data
709 */
710void x509_crl_free( x509_crl *crl )
711{
712 x509_crl *crl_cur = crl;
713 x509_crl *crl_prv;
714 x509_name *name_cur;
715 x509_name *name_prv;
716 x509_crl_entry *entry_cur;
717 x509_crl_entry *entry_prv;
718
719 if( crl == NULL )
720 return;
721
722 do
723 {
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200724#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
725 polarssl_free( crl_cur->sig_opts );
726#endif
727
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728 name_cur = crl_cur->issuer.next;
729 while( name_cur != NULL )
730 {
731 name_prv = name_cur;
732 name_cur = name_cur->next;
733 memset( name_prv, 0, sizeof( x509_name ) );
734 polarssl_free( name_prv );
735 }
736
737 entry_cur = crl_cur->entry.next;
738 while( entry_cur != NULL )
739 {
740 entry_prv = entry_cur;
741 entry_cur = entry_cur->next;
742 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
743 polarssl_free( entry_prv );
744 }
745
746 if( crl_cur->raw.p != NULL )
747 {
748 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
749 polarssl_free( crl_cur->raw.p );
750 }
751
752 crl_cur = crl_cur->next;
753 }
754 while( crl_cur != NULL );
755
756 crl_cur = crl;
757 do
758 {
759 crl_prv = crl_cur;
760 crl_cur = crl_cur->next;
761
762 memset( crl_prv, 0, sizeof( x509_crl ) );
763 if( crl_prv != crl )
764 polarssl_free( crl_prv );
765 }
766 while( crl_cur != NULL );
767}
768
Paul Bakker9af723c2014-05-01 13:03:14 +0200769#endif /* POLARSSL_X509_CRL_PARSE_C */