blob: afba7b2e87e240ad2c606397f6893e98a99ca2ce [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 Certidicate Revocation List (CRL) parsing
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The ITU-T X.509 standard defines a certificate format for PKI.
24 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020025 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
26 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
27 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028 *
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
30 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
31 */
32
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
36#include POLARSSL_CONFIG_FILE
37#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038
39#if defined(POLARSSL_X509_CRL_PARSE_C)
40
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/x509_crl.h"
42#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000043
44#include <string.h>
45
Paul Bakker7c6b2c32013-09-16 13:49:26 +020046#if defined(POLARSSL_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020048#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052#else
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000054#include <stdio.h>
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#define polarssl_free free
Rich Evansfac657f2015-01-30 11:00:01 +000056#define polarssl_malloc malloc
57#define polarssl_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058#endif
59
Paul Bakkerfa6a6202013-10-28 18:48:30 +010060#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#include <windows.h>
62#else
63#include <time.h>
64#endif
65
Paul Bakkerfa6a6202013-10-28 18:48:30 +010066#if defined(POLARSSL_FS_IO) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#include <stdio.h>
68#endif
69
Paul Bakker34617722014-06-13 17:20:13 +020070/* Implementation that should never be optimized out by the compiler */
71static void polarssl_zeroize( void *v, size_t n ) {
72 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
73}
74
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075/*
76 * Version ::= INTEGER { v1(0), v2(1) }
77 */
78static int x509_crl_get_version( unsigned char **p,
79 const unsigned char *end,
80 int *ver )
81{
82 int ret;
83
84 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
85 {
86 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
87 {
88 *ver = 0;
89 return( 0 );
90 }
91
Paul Bakker51876562013-09-17 14:36:05 +020092 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093 }
94
95 return( 0 );
96}
97
98/*
99 * X.509 CRL v2 extensions (no extensions parsed yet.)
100 */
101static int x509_get_crl_ext( unsigned char **p,
102 const unsigned char *end,
103 x509_buf *ext )
104{
105 int ret;
106 size_t len = 0;
107
108 /* Get explicit tag */
109 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
110 {
111 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
112 return( 0 );
113
114 return( ret );
115 }
116
117 while( *p < end )
118 {
119 if( ( ret = asn1_get_tag( p, end, &len,
120 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200121 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122
123 *p += len;
124 }
125
126 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200127 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200128 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
129
130 return( 0 );
131}
132
133/*
134 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
135 */
136static int x509_get_crl_entry_ext( unsigned char **p,
137 const unsigned char *end,
138 x509_buf *ext )
139{
140 int ret;
141 size_t len = 0;
142
143 /* OPTIONAL */
Paul Bakker66d5d072014-06-17 16:39:18 +0200144 if( end <= *p )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 return( 0 );
146
147 ext->tag = **p;
148 ext->p = *p;
149
150 /*
151 * Get CRL-entry extension sequence header
152 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
153 */
154 if( ( ret = asn1_get_tag( p, end, &ext->len,
155 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
156 {
157 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
158 {
159 ext->p = NULL;
160 return( 0 );
161 }
Paul Bakker51876562013-09-17 14:36:05 +0200162 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163 }
164
Paul Bakker9af723c2014-05-01 13:03:14 +0200165 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200166
167 if( end != *p + ext->len )
Paul Bakker51876562013-09-17 14:36:05 +0200168 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200169 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
170
171 while( *p < end )
172 {
173 if( ( ret = asn1_get_tag( p, end, &len,
174 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200175 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200176
177 *p += len;
178 }
179
180 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200181 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200182 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
183
184 return( 0 );
185}
186
187/*
188 * X.509 CRL Entries
189 */
190static int x509_get_entries( unsigned char **p,
191 const unsigned char *end,
192 x509_crl_entry *entry )
193{
194 int ret;
195 size_t entry_len;
196 x509_crl_entry *cur_entry = entry;
197
198 if( *p == end )
199 return( 0 );
200
201 if( ( ret = asn1_get_tag( p, end, &entry_len,
202 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
203 {
204 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
205 return( 0 );
206
207 return( ret );
208 }
209
210 end = *p + entry_len;
211
212 while( *p < end )
213 {
214 size_t len2;
215 const unsigned char *end2;
216
217 if( ( ret = asn1_get_tag( p, end, &len2,
218 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
219 {
220 return( ret );
221 }
222
223 cur_entry->raw.tag = **p;
224 cur_entry->raw.p = *p;
225 cur_entry->raw.len = len2;
226 end2 = *p + len2;
227
228 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
229 return( ret );
230
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200231 if( ( ret = x509_get_time( p, end2,
232 &cur_entry->revocation_date ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200233 return( ret );
234
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200235 if( ( ret = x509_get_crl_entry_ext( p, end2,
236 &cur_entry->entry_ext ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200237 return( ret );
238
Paul Bakker66d5d072014-06-17 16:39:18 +0200239 if( *p < end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200240 {
241 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
242
243 if( cur_entry->next == NULL )
244 return( POLARSSL_ERR_X509_MALLOC_FAILED );
245
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100246 memset( cur_entry->next, 0, sizeof( x509_crl_entry ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247 cur_entry = cur_entry->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200248 }
249 }
250
251 return( 0 );
252}
253
254/*
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100255 * Parse one CRLs in DER format and append it to the chained list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256 */
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100257int x509_crl_parse_der( x509_crl *chain,
258 const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200259{
260 int ret;
261 size_t len;
262 unsigned char *p, *end;
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200263 x509_buf sig_params1, sig_params2;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100264 x509_crl *crl = chain;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265
266 /*
267 * Check for valid input
268 */
269 if( crl == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200270 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100272 memset( &sig_params1, 0, sizeof( x509_buf ) );
273 memset( &sig_params2, 0, sizeof( x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200274
275 /*
276 * Add new CRL on the end of the chain if needed.
277 */
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100278 while( crl->version != 0 && crl->next != NULL )
279 crl = crl->next;
280
Paul Bakker66d5d072014-06-17 16:39:18 +0200281 if( crl->version != 0 && crl->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200282 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500283 crl->next = polarssl_malloc( sizeof( x509_crl ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284
285 if( crl->next == NULL )
286 {
287 x509_crl_free( crl );
288 return( POLARSSL_ERR_X509_MALLOC_FAILED );
289 }
290
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100291 x509_crl_init( crl->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200292 crl = crl->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293 }
294
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100295 /*
296 * Copy raw DER-encoded CRL
297 */
298 if( ( p = polarssl_malloc( buflen ) ) == NULL )
299 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100301 memcpy( p, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200302
303 crl->raw.p = p;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100304 crl->raw.len = buflen;
305
306 end = p + buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307
308 /*
309 * CertificateList ::= SEQUENCE {
310 * tbsCertList TBSCertList,
311 * signatureAlgorithm AlgorithmIdentifier,
312 * signatureValue BIT STRING }
313 */
314 if( ( ret = asn1_get_tag( &p, end, &len,
315 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
316 {
317 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200318 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200319 }
320
321 if( len != (size_t) ( end - p ) )
322 {
323 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200324 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
326 }
327
328 /*
329 * TBSCertList ::= SEQUENCE {
330 */
331 crl->tbs.p = p;
332
333 if( ( ret = asn1_get_tag( &p, end, &len,
334 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
335 {
336 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200337 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200338 }
339
340 end = p + len;
341 crl->tbs.len = end - crl->tbs.p;
342
343 /*
344 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
345 * -- if present, MUST be v2
346 *
347 * signature AlgorithmIdentifier
348 */
349 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200350 ( ret = x509_get_alg( &p, end, &crl->sig_oid1, &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351 {
352 x509_crl_free( crl );
353 return( ret );
354 }
355
356 crl->version++;
357
358 if( crl->version > 2 )
359 {
360 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200361 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362 }
363
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200364 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200365 &crl->sig_md, &crl->sig_pk,
366 &crl->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367 {
368 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200369 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370 }
371
372 /*
373 * issuer Name
374 */
375 crl->issuer_raw.p = p;
376
377 if( ( ret = asn1_get_tag( &p, end, &len,
378 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
379 {
380 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200381 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382 }
383
384 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
385 {
386 x509_crl_free( crl );
387 return( ret );
388 }
389
390 crl->issuer_raw.len = p - crl->issuer_raw.p;
391
392 /*
393 * thisUpdate Time
394 * nextUpdate Time OPTIONAL
395 */
396 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
397 {
398 x509_crl_free( crl );
399 return( ret );
400 }
401
402 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
403 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200404 if( ret != ( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker66d5d072014-06-17 16:39:18 +0200406 ret != ( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
408 {
409 x509_crl_free( crl );
410 return( ret );
411 }
412 }
413
414 /*
415 * revokedCertificates SEQUENCE OF SEQUENCE {
416 * userCertificate CertificateSerialNumber,
417 * revocationDate Time,
418 * crlEntryExtensions Extensions OPTIONAL
419 * -- if present, MUST be v2
420 * } OPTIONAL
421 */
422 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
423 {
424 x509_crl_free( crl );
425 return( ret );
426 }
427
428 /*
429 * crlExtensions EXPLICIT Extensions OPTIONAL
430 * -- if present, MUST be v2
431 */
432 if( crl->version == 2 )
433 {
434 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
435
436 if( ret != 0 )
437 {
438 x509_crl_free( crl );
439 return( ret );
440 }
441 }
442
443 if( p != end )
444 {
445 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200446 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
448 }
449
450 end = crl->raw.p + crl->raw.len;
451
452 /*
453 * signatureAlgorithm AlgorithmIdentifier,
454 * signatureValue BIT STRING
455 */
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200456 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457 {
458 x509_crl_free( crl );
459 return( ret );
460 }
461
462 if( crl->sig_oid1.len != crl->sig_oid2.len ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200463 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 ||
464 sig_params1.len != sig_params2.len ||
Paul Bakker66d5d072014-06-17 16:39:18 +0200465 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466 {
467 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200468 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469 }
470
471 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
472 {
473 x509_crl_free( crl );
474 return( ret );
475 }
476
477 if( p != end )
478 {
479 x509_crl_free( crl );
Paul Bakker51876562013-09-17 14:36:05 +0200480 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
482 }
483
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100484 return( 0 );
485}
486
487/*
488 * Parse one or more CRLs and add them to the chained list
489 */
490int x509_crl_parse( x509_crl *chain, const unsigned char *buf, size_t buflen )
491{
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100492#if defined(POLARSSL_PEM_PARSE_C)
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100493 int ret;
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100494 size_t use_len;
495 pem_context pem;
496 int is_pem = 0;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100497
498 if( chain == NULL || buf == NULL )
499 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
500
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100501 do
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502 {
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100503 pem_init( &pem );
504 ret = pem_read_buffer( &pem,
505 "-----BEGIN X509 CRL-----",
506 "-----END X509 CRL-----",
507 buf, NULL, 0, &use_len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100509 if( ret == 0 )
510 {
511 /*
512 * Was PEM encoded
513 */
514 is_pem = 1;
515
516 buflen -= use_len;
517 buf += use_len;
518
519 if( ( ret = x509_crl_parse_der( chain,
520 pem.buf, pem.buflen ) ) != 0 )
521 {
522 return( ret );
523 }
524
525 pem_free( &pem );
526 }
527 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
528 {
529 pem_free( &pem );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100530 return( ret );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100531 }
532 }
533 while( is_pem && buflen > 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100535 if( is_pem )
536 return( 0 );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100537 else
538#endif /* POLARSSL_PEM_PARSE_C */
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100539 return( x509_crl_parse_der( chain, buf, buflen ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540}
541
542#if defined(POLARSSL_FS_IO)
543/*
544 * Load one or more CRLs and add them to the chained list
545 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200546int x509_crl_parse_file( x509_crl *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547{
548 int ret;
549 size_t n;
550 unsigned char *buf;
551
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100552 if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553 return( ret );
554
Paul Bakkerddf26b42013-09-18 13:46:23 +0200555 ret = x509_crl_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556
Paul Bakker34617722014-06-13 17:20:13 +0200557 polarssl_zeroize( buf, n + 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558 polarssl_free( buf );
559
560 return( ret );
561}
562#endif /* POLARSSL_FS_IO */
563
Paul Bakker6edcd412013-10-29 15:22:54 +0100564#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
565 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566#include <stdarg.h>
567
568#if !defined vsnprintf
569#define vsnprintf _vsnprintf
570#endif // vsnprintf
571
572/*
573 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
574 * Result value is not size of buffer needed, but -1 if no fit is possible.
575 *
576 * This fuction tries to 'fix' this by at least suggesting enlarging the
577 * size by 20.
578 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200579static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580{
581 va_list ap;
582 int res = -1;
583
584 va_start( ap, format );
585
586 res = vsnprintf( str, size, format, ap );
587
588 va_end( ap );
589
590 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +0200591 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592 return( (int) size + 20 );
593
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200594 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595}
596
597#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200598#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599
600#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
601
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200602#define SAFE_SNPRINTF() \
603{ \
604 if( ret == -1 ) \
605 return( -1 ); \
606 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200607 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200608 p[n - 1] = '\0'; \
609 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
610 } \
611 \
612 n -= (unsigned int) ret; \
613 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614}
615
616/*
617 * Return an informational string about the certificate.
618 */
619#define BEFORE_COLON 14
620#define BC "14"
621/*
622 * Return an informational string about the CRL.
623 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200624int x509_crl_info( char *buf, size_t size, const char *prefix,
625 const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626{
627 int ret;
628 size_t n;
629 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 const x509_crl_entry *entry;
631
632 p = buf;
633 n = size;
634
Rich Evansfac657f2015-01-30 11:00:01 +0000635 ret = polarssl_snprintf( p, n, "%sCRL version : %d",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636 prefix, crl->version );
637 SAFE_SNPRINTF();
638
Rich Evansfac657f2015-01-30 11:00:01 +0000639 ret = polarssl_snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +0200641 ret = x509_dn_gets( p, n, &crl->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 SAFE_SNPRINTF();
643
Rich Evansfac657f2015-01-30 11:00:01 +0000644 ret = polarssl_snprintf( p, n, "\n%sthis update : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
646 crl->this_update.year, crl->this_update.mon,
647 crl->this_update.day, crl->this_update.hour,
648 crl->this_update.min, crl->this_update.sec );
649 SAFE_SNPRINTF();
650
Rich Evansfac657f2015-01-30 11:00:01 +0000651 ret = polarssl_snprintf( p, n, "\n%snext update : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
653 crl->next_update.year, crl->next_update.mon,
654 crl->next_update.day, crl->next_update.hour,
655 crl->next_update.min, crl->next_update.sec );
656 SAFE_SNPRINTF();
657
658 entry = &crl->entry;
659
Rich Evansfac657f2015-01-30 11:00:01 +0000660 ret = polarssl_snprintf( p, n, "\n%sRevoked certificates:",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661 prefix );
662 SAFE_SNPRINTF();
663
664 while( entry != NULL && entry->raw.len != 0 )
665 {
Rich Evansfac657f2015-01-30 11:00:01 +0000666 ret = polarssl_snprintf( p, n, "\n%sserial number: ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200667 prefix );
668 SAFE_SNPRINTF();
669
Paul Bakker66d5d072014-06-17 16:39:18 +0200670 ret = x509_serial_gets( p, n, &entry->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671 SAFE_SNPRINTF();
672
Rich Evansfac657f2015-01-30 11:00:01 +0000673 ret = polarssl_snprintf( p, n, " revocation date: " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 "%04d-%02d-%02d %02d:%02d:%02d",
675 entry->revocation_date.year, entry->revocation_date.mon,
676 entry->revocation_date.day, entry->revocation_date.hour,
677 entry->revocation_date.min, entry->revocation_date.sec );
678 SAFE_SNPRINTF();
679
680 entry = entry->next;
681 }
682
Rich Evansfac657f2015-01-30 11:00:01 +0000683 ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684 SAFE_SNPRINTF();
685
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200686 ret = x509_sig_alg_gets( p, n, &crl->sig_oid1, crl->sig_pk, crl->sig_md,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +0200687 crl->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688 SAFE_SNPRINTF();
689
Rich Evansfac657f2015-01-30 11:00:01 +0000690 ret = polarssl_snprintf( p, n, "\n" );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 SAFE_SNPRINTF();
692
693 return( (int) ( size - n ) );
694}
695
696/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200697 * Initialize a CRL chain
698 */
699void x509_crl_init( x509_crl *crl )
700{
701 memset( crl, 0, sizeof(x509_crl) );
702}
703
704/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705 * Unallocate all CRL data
706 */
707void x509_crl_free( x509_crl *crl )
708{
709 x509_crl *crl_cur = crl;
710 x509_crl *crl_prv;
711 x509_name *name_cur;
712 x509_name *name_prv;
713 x509_crl_entry *entry_cur;
714 x509_crl_entry *entry_prv;
715
716 if( crl == NULL )
717 return;
718
719 do
720 {
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200721#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200722 polarssl_free( crl_cur->sig_opts );
723#endif
724
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725 name_cur = crl_cur->issuer.next;
726 while( name_cur != NULL )
727 {
728 name_prv = name_cur;
729 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +0200730 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731 polarssl_free( name_prv );
732 }
733
734 entry_cur = crl_cur->entry.next;
735 while( entry_cur != NULL )
736 {
737 entry_prv = entry_cur;
738 entry_cur = entry_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +0200739 polarssl_zeroize( entry_prv, sizeof( x509_crl_entry ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740 polarssl_free( entry_prv );
741 }
742
743 if( crl_cur->raw.p != NULL )
744 {
Paul Bakker34617722014-06-13 17:20:13 +0200745 polarssl_zeroize( crl_cur->raw.p, crl_cur->raw.len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746 polarssl_free( crl_cur->raw.p );
747 }
748
749 crl_cur = crl_cur->next;
750 }
751 while( crl_cur != NULL );
752
753 crl_cur = crl;
754 do
755 {
756 crl_prv = crl_cur;
757 crl_cur = crl_cur->next;
758
Paul Bakker34617722014-06-13 17:20:13 +0200759 polarssl_zeroize( crl_prv, sizeof( x509_crl ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200760 if( crl_prv != crl )
761 polarssl_free( crl_prv );
762 }
763 while( crl_cur != NULL );
764}
765
Paul Bakker9af723c2014-05-01 13:03:14 +0200766#endif /* POLARSSL_X509_CRL_PARSE_C */