blob: 4fc35664d971de599f40cbea7851cbb1aff00412 [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_CRT_PARSE_C)
44
45#include "polarssl/x509_crt.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
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010058#if defined(POLARSSL_THREADING_C)
59#include "polarssl/threading.h"
60#endif
61
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#include <string.h>
63#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010064#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065#include <windows.h>
66#else
67#include <time.h>
68#endif
69
Paul Bakkerfa6a6202013-10-28 18:48:30 +010070#if defined(EFIX64) || defined(EFI32)
71#include <stdio.h>
72#endif
73
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#if defined(POLARSSL_FS_IO)
75#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020076#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#include <sys/types.h>
78#include <sys/stat.h>
79#include <dirent.h>
80#endif
81#endif
82
Paul Bakker34617722014-06-13 17:20:13 +020083/* Implementation that should never be optimized out by the compiler */
84static void polarssl_zeroize( void *v, size_t n ) {
85 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
86}
87
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088/*
89 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
90 */
91static int x509_get_version( unsigned char **p,
92 const unsigned char *end,
93 int *ver )
94{
95 int ret;
96 size_t len;
97
98 if( ( ret = asn1_get_tag( p, end, &len,
99 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
100 {
101 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
102 {
103 *ver = 0;
104 return( 0 );
105 }
106
107 return( ret );
108 }
109
110 end = *p + len;
111
112 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200113 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200114
115 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200116 return( POLARSSL_ERR_X509_INVALID_VERSION +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
118
119 return( 0 );
120}
121
122/*
123 * Validity ::= SEQUENCE {
124 * notBefore Time,
125 * notAfter Time }
126 */
127static int x509_get_dates( unsigned char **p,
128 const unsigned char *end,
129 x509_time *from,
130 x509_time *to )
131{
132 int ret;
133 size_t len;
134
135 if( ( ret = asn1_get_tag( p, end, &len,
136 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200137 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138
139 end = *p + len;
140
141 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
142 return( ret );
143
144 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
145 return( ret );
146
147 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200148 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200149 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
150
151 return( 0 );
152}
153
154/*
155 * X.509 v2/v3 unique identifier (not parsed)
156 */
157static int x509_get_uid( unsigned char **p,
158 const unsigned char *end,
159 x509_buf *uid, int n )
160{
161 int ret;
162
163 if( *p == end )
164 return( 0 );
165
166 uid->tag = **p;
167
168 if( ( ret = asn1_get_tag( p, end, &uid->len,
169 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
170 {
171 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
172 return( 0 );
173
174 return( ret );
175 }
176
177 uid->p = *p;
178 *p += uid->len;
179
180 return( 0 );
181}
182
183static int x509_get_basic_constraints( unsigned char **p,
184 const unsigned char *end,
185 int *ca_istrue,
186 int *max_pathlen )
187{
188 int ret;
189 size_t len;
190
191 /*
192 * BasicConstraints ::= SEQUENCE {
193 * cA BOOLEAN DEFAULT FALSE,
194 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
195 */
196 *ca_istrue = 0; /* DEFAULT FALSE */
197 *max_pathlen = 0; /* endless */
198
199 if( ( ret = asn1_get_tag( p, end, &len,
200 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200201 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200202
203 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200204 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200205
206 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
207 {
208 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
209 ret = asn1_get_int( p, end, ca_istrue );
210
211 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200212 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213
214 if( *ca_istrue != 0 )
215 *ca_istrue = 1;
216 }
217
218 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200219 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200220
221 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200222 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200223
224 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200225 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200226 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
227
228 (*max_pathlen)++;
229
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200230 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200231}
232
233static int x509_get_ns_cert_type( unsigned char **p,
234 const unsigned char *end,
235 unsigned char *ns_cert_type)
236{
237 int ret;
238 x509_bitstring bs = { 0, 0, NULL };
239
240 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200241 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242
243 if( bs.len != 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200244 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200245 POLARSSL_ERR_ASN1_INVALID_LENGTH );
246
247 /* Get actual bitstring */
248 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200249 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200250}
251
252static int x509_get_key_usage( unsigned char **p,
253 const unsigned char *end,
254 unsigned char *key_usage)
255{
256 int ret;
257 x509_bitstring bs = { 0, 0, NULL };
258
259 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200260 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261
262 if( bs.len < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200263 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264 POLARSSL_ERR_ASN1_INVALID_LENGTH );
265
266 /* Get actual bitstring */
267 *key_usage = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200268 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269}
270
271/*
272 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
273 *
274 * KeyPurposeId ::= OBJECT IDENTIFIER
275 */
276static int x509_get_ext_key_usage( unsigned char **p,
277 const unsigned char *end,
278 x509_sequence *ext_key_usage)
279{
280 int ret;
281
282 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200283 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284
285 /* Sequence length must be >= 1 */
286 if( ext_key_usage->buf.p == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200287 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 POLARSSL_ERR_ASN1_INVALID_LENGTH );
289
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200290 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291}
292
293/*
294 * SubjectAltName ::= GeneralNames
295 *
296 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
297 *
298 * GeneralName ::= CHOICE {
299 * otherName [0] OtherName,
300 * rfc822Name [1] IA5String,
301 * dNSName [2] IA5String,
302 * x400Address [3] ORAddress,
303 * directoryName [4] Name,
304 * ediPartyName [5] EDIPartyName,
305 * uniformResourceIdentifier [6] IA5String,
306 * iPAddress [7] OCTET STRING,
307 * registeredID [8] OBJECT IDENTIFIER }
308 *
309 * OtherName ::= SEQUENCE {
310 * type-id OBJECT IDENTIFIER,
311 * value [0] EXPLICIT ANY DEFINED BY type-id }
312 *
313 * EDIPartyName ::= SEQUENCE {
314 * nameAssigner [0] DirectoryString OPTIONAL,
315 * partyName [1] DirectoryString }
316 *
317 * NOTE: PolarSSL only parses and uses dNSName at this point.
318 */
319static int x509_get_subject_alt_name( unsigned char **p,
320 const unsigned char *end,
321 x509_sequence *subject_alt_name )
322{
323 int ret;
324 size_t len, tag_len;
325 asn1_buf *buf;
326 unsigned char tag;
327 asn1_sequence *cur = subject_alt_name;
328
329 /* Get main sequence tag */
330 if( ( ret = asn1_get_tag( p, end, &len,
331 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200332 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333
334 if( *p + len != end )
Paul Bakker51876562013-09-17 14:36:05 +0200335 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
337
338 while( *p < end )
339 {
340 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200341 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200342 POLARSSL_ERR_ASN1_OUT_OF_DATA );
343
344 tag = **p;
345 (*p)++;
346 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200347 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348
349 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
Paul Bakker51876562013-09-17 14:36:05 +0200350 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
352
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200353 /* Skip everything but DNS name */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
355 {
356 *p += tag_len;
357 continue;
358 }
359
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200361 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362 {
363 cur->next = (asn1_sequence *) polarssl_malloc(
364 sizeof( asn1_sequence ) );
365
366 if( cur->next == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200367 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368 POLARSSL_ERR_ASN1_MALLOC_FAILED );
369
370 memset( cur->next, 0, sizeof( asn1_sequence ) );
371 cur = cur->next;
372 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200373
374 buf = &(cur->buf);
375 buf->tag = tag;
376 buf->p = *p;
377 buf->len = tag_len;
378 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379 }
380
381 /* Set final sequence entry's next pointer to NULL */
382 cur->next = NULL;
383
384 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200385 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
387
388 return( 0 );
389}
390
391/*
392 * X.509 v3 extensions
393 *
394 * TODO: Perform all of the basic constraints tests required by the RFC
395 * TODO: Set values for undetected extensions to a sane default?
396 *
397 */
398static int x509_get_crt_ext( unsigned char **p,
399 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200400 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401{
402 int ret;
403 size_t len;
404 unsigned char *end_ext_data, *end_ext_octet;
405
406 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
407 {
408 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
409 return( 0 );
410
411 return( ret );
412 }
413
414 while( *p < end )
415 {
416 /*
417 * Extension ::= SEQUENCE {
418 * extnID OBJECT IDENTIFIER,
419 * critical BOOLEAN DEFAULT FALSE,
420 * extnValue OCTET STRING }
421 */
422 x509_buf extn_oid = {0, 0, NULL};
423 int is_critical = 0; /* DEFAULT FALSE */
424 int ext_type = 0;
425
426 if( ( ret = asn1_get_tag( p, end, &len,
427 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200428 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429
430 end_ext_data = *p + len;
431
432 /* Get extension ID */
433 extn_oid.tag = **p;
434
435 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200436 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437
438 extn_oid.p = *p;
439 *p += extn_oid.len;
440
441 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200442 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443 POLARSSL_ERR_ASN1_OUT_OF_DATA );
444
445 /* Get optional critical */
446 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
447 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200448 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449
450 /* Data should be octet string type */
451 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
452 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200453 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454
455 end_ext_octet = *p + len;
456
457 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200458 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
460
461 /*
462 * Detect supported extensions
463 */
464 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
465
466 if( ret != 0 )
467 {
468 /* No parser found, skip extension */
469 *p = end_ext_octet;
470
471#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
472 if( is_critical )
473 {
474 /* Data is marked as critical: fail */
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200475 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
477 }
478#endif
479 continue;
480 }
481
482 crt->ext_types |= ext_type;
483
484 switch( ext_type )
485 {
486 case EXT_BASIC_CONSTRAINTS:
487 /* Parse basic constraints */
488 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
489 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200490 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 break;
492
493 case EXT_KEY_USAGE:
494 /* Parse key usage */
495 if( ( ret = x509_get_key_usage( p, end_ext_octet,
496 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200497 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498 break;
499
500 case EXT_EXTENDED_KEY_USAGE:
501 /* Parse extended key usage */
502 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
503 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200504 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505 break;
506
507 case EXT_SUBJECT_ALT_NAME:
508 /* Parse subject alt name */
509 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
510 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200511 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512 break;
513
514 case EXT_NS_CERT_TYPE:
515 /* Parse netscape certificate type */
516 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
517 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200518 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519 break;
520
521 default:
522 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
523 }
524 }
525
526 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200527 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
529
530 return( 0 );
531}
532
533/*
534 * Parse and fill a single X.509 certificate in DER format
535 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200536static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200537 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538{
539 int ret;
540 size_t len;
541 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200542 x509_buf sig_params1, sig_params2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100543
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200544 memset( &sig_params1, 0, sizeof( x509_buf ) );
545 memset( &sig_params2, 0, sizeof( x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546
547 /*
548 * Check for valid input
549 */
550 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200551 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552
553 p = (unsigned char *) polarssl_malloc( len = buflen );
554
555 if( p == NULL )
556 return( POLARSSL_ERR_X509_MALLOC_FAILED );
557
558 memcpy( p, buf, buflen );
559
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560 crt->raw.p = p;
561 crt->raw.len = len;
562 end = p + len;
563
564 /*
565 * Certificate ::= SEQUENCE {
566 * tbsCertificate TBSCertificate,
567 * signatureAlgorithm AlgorithmIdentifier,
568 * signatureValue BIT STRING }
569 */
570 if( ( ret = asn1_get_tag( &p, end, &len,
571 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
572 {
573 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200574 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575 }
576
577 if( len > (size_t) ( end - p ) )
578 {
579 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200580 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
582 }
583 crt_end = p + len;
584
585 /*
586 * TBSCertificate ::= SEQUENCE {
587 */
588 crt->tbs.p = p;
589
590 if( ( ret = asn1_get_tag( &p, end, &len,
591 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
592 {
593 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200594 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595 }
596
597 end = p + len;
598 crt->tbs.len = end - crt->tbs.p;
599
600 /*
601 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
602 *
603 * CertificateSerialNumber ::= INTEGER
604 *
605 * signature AlgorithmIdentifier
606 */
607 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
608 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100609 ( ret = x509_get_alg( &p, end, &crt->sig_oid1,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200610 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611 {
612 x509_crt_free( crt );
613 return( ret );
614 }
615
616 crt->version++;
617
618 if( crt->version > 3 )
619 {
620 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200621 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622 }
623
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200624 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200625 &crt->sig_md, &crt->sig_pk,
626 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627 {
628 x509_crt_free( crt );
629 return( ret );
630 }
631
632 /*
633 * issuer Name
634 */
635 crt->issuer_raw.p = p;
636
637 if( ( ret = asn1_get_tag( &p, end, &len,
638 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
639 {
640 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200641 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 }
643
644 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
645 {
646 x509_crt_free( crt );
647 return( ret );
648 }
649
650 crt->issuer_raw.len = p - crt->issuer_raw.p;
651
652 /*
653 * Validity ::= SEQUENCE {
654 * notBefore Time,
655 * notAfter Time }
656 *
657 */
658 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
659 &crt->valid_to ) ) != 0 )
660 {
661 x509_crt_free( crt );
662 return( ret );
663 }
664
665 /*
666 * subject Name
667 */
668 crt->subject_raw.p = p;
669
670 if( ( ret = asn1_get_tag( &p, end, &len,
671 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
672 {
673 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200674 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675 }
676
677 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
678 {
679 x509_crt_free( crt );
680 return( ret );
681 }
682
683 crt->subject_raw.len = p - crt->subject_raw.p;
684
685 /*
686 * SubjectPublicKeyInfo
687 */
Paul Bakkerda771152013-09-16 22:45:03 +0200688 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689 {
690 x509_crt_free( crt );
691 return( ret );
692 }
693
694 /*
695 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
696 * -- If present, version shall be v2 or v3
697 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
698 * -- If present, version shall be v2 or v3
699 * extensions [3] EXPLICIT Extensions OPTIONAL
700 * -- If present, version shall be v3
701 */
702 if( crt->version == 2 || crt->version == 3 )
703 {
704 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
705 if( ret != 0 )
706 {
707 x509_crt_free( crt );
708 return( ret );
709 }
710 }
711
712 if( crt->version == 2 || crt->version == 3 )
713 {
714 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
715 if( ret != 0 )
716 {
717 x509_crt_free( crt );
718 return( ret );
719 }
720 }
721
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200722#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200723 if( crt->version == 3 )
724 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200725#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726 ret = x509_get_crt_ext( &p, end, crt);
727 if( ret != 0 )
728 {
729 x509_crt_free( crt );
730 return( ret );
731 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200732#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200734#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735
736 if( p != end )
737 {
738 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200739 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
741 }
742
743 end = crt_end;
744
745 /*
746 * }
747 * -- end of TBSCertificate
748 *
749 * signatureAlgorithm AlgorithmIdentifier,
750 * signatureValue BIT STRING
751 */
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200752 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753 {
754 x509_crt_free( crt );
755 return( ret );
756 }
757
758 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200759 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 ||
760 sig_params1.len != sig_params2.len ||
761 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762 {
763 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200764 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765 }
766
767 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
768 {
769 x509_crt_free( crt );
770 return( ret );
771 }
772
773 if( p != end )
774 {
775 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200776 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
778 }
779
780 return( 0 );
781}
782
783/*
784 * Parse one X.509 certificate in DER format from a buffer and add them to a
785 * chained list
786 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200787int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200788 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789{
790 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200791 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792
793 /*
794 * Check for valid input
795 */
796 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200797 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798
799 while( crt->version != 0 && crt->next != NULL )
800 {
801 prev = crt;
802 crt = crt->next;
803 }
804
805 /*
806 * Add new certificate on the end of the chain if needed.
807 */
808 if ( crt->version != 0 && crt->next == NULL)
809 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200810 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811
812 if( crt->next == NULL )
813 return( POLARSSL_ERR_X509_MALLOC_FAILED );
814
815 prev = crt;
816 crt = crt->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200817 x509_crt_init( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818 }
819
Paul Bakkerddf26b42013-09-18 13:46:23 +0200820 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821 {
822 if( prev )
823 prev->next = NULL;
824
825 if( crt != chain )
826 polarssl_free( crt );
827
828 return( ret );
829 }
830
831 return( 0 );
832}
833
834/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200835 * Parse one or more PEM certificates from a buffer and add them to the chained
836 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200838int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839{
840 int success = 0, first_error = 0, total_failed = 0;
841 int buf_format = X509_FORMAT_DER;
842
843 /*
844 * Check for valid input
845 */
846 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200847 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200848
849 /*
850 * Determine buffer content. Buffer contains either one DER certificate or
851 * one or more PEM certificates.
852 */
853#if defined(POLARSSL_PEM_PARSE_C)
854 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
855 buf_format = X509_FORMAT_PEM;
856#endif
857
858 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200859 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200860
861#if defined(POLARSSL_PEM_PARSE_C)
862 if( buf_format == X509_FORMAT_PEM )
863 {
864 int ret;
865 pem_context pem;
866
867 while( buflen > 0 )
868 {
869 size_t use_len;
870 pem_init( &pem );
871
872 ret = pem_read_buffer( &pem,
873 "-----BEGIN CERTIFICATE-----",
874 "-----END CERTIFICATE-----",
875 buf, NULL, 0, &use_len );
876
877 if( ret == 0 )
878 {
879 /*
880 * Was PEM encoded
881 */
882 buflen -= use_len;
883 buf += use_len;
884 }
885 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
886 {
887 return( ret );
888 }
889 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
890 {
891 pem_free( &pem );
892
893 /*
894 * PEM header and footer were found
895 */
896 buflen -= use_len;
897 buf += use_len;
898
899 if( first_error == 0 )
900 first_error = ret;
901
902 continue;
903 }
904 else
905 break;
906
Paul Bakkerddf26b42013-09-18 13:46:23 +0200907 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908
909 pem_free( &pem );
910
911 if( ret != 0 )
912 {
913 /*
914 * Quit parsing on a memory error
915 */
916 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
917 return( ret );
918
919 if( first_error == 0 )
920 first_error = ret;
921
922 total_failed++;
923 continue;
924 }
925
926 success = 1;
927 }
928 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200929#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930
931 if( success )
932 return( total_failed );
933 else if( first_error )
934 return( first_error );
935 else
936 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
937}
938
939#if defined(POLARSSL_FS_IO)
940/*
941 * Load one or more certificates and add them to the chained list
942 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200943int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944{
945 int ret;
946 size_t n;
947 unsigned char *buf;
948
949 if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
950 return( ret );
951
Paul Bakkerddf26b42013-09-18 13:46:23 +0200952 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953
Paul Bakker34617722014-06-13 17:20:13 +0200954 polarssl_zeroize( buf, n + 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 polarssl_free( buf );
956
957 return( ret );
958}
959
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100960#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100961static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
962#endif
963
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200964int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965{
966 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100967#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968 int w_ret;
969 WCHAR szDir[MAX_PATH];
970 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200971 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200972 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973
Paul Bakker9af723c2014-05-01 13:03:14 +0200974 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975 HANDLE hFind;
976
977 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200978 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979
Paul Bakker9af723c2014-05-01 13:03:14 +0200980 memset( szDir, 0, sizeof(szDir) );
981 memset( filename, 0, MAX_PATH );
982 memcpy( filename, path, len );
983 filename[len++] = '\\';
984 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 filename[len++] = '*';
986
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200987 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
988 MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989
990 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200991 if (hFind == INVALID_HANDLE_VALUE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
993
994 len = MAX_PATH - len;
995 do
996 {
Paul Bakker9af723c2014-05-01 13:03:14 +0200997 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998
999 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1000 continue;
1001
Paul Bakker9af723c2014-05-01 13:03:14 +02001002 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1003 lstrlenW(file_data.cFileName),
1004 p, len - 1,
1005 NULL, NULL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006
Paul Bakkerddf26b42013-09-18 13:46:23 +02001007 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008 if( w_ret < 0 )
1009 ret++;
1010 else
1011 ret += w_ret;
1012 }
1013 while( FindNextFileW( hFind, &file_data ) != 0 );
1014
Paul Bakker4aa40d42013-10-11 10:49:24 +02001015 if (GetLastError() != ERROR_NO_MORE_FILES)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1017
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001019#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001020 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001022 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 char entry_name[255];
1024 DIR *dir = opendir( path );
1025
1026 if( dir == NULL)
1027 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1028
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001029#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001030 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1031 return( ret );
1032#endif
1033
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001034 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001036 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001038 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039 {
1040 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001041 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1042 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 }
1044
1045 if( !S_ISREG( sb.st_mode ) )
1046 continue;
1047
1048 // Ignore parse errors
1049 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001050 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051 if( t_ret < 0 )
1052 ret++;
1053 else
1054 ret += t_ret;
1055 }
1056 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001057
1058cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001059#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001060 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1061 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1062#endif
1063
Paul Bakkerbe089b02013-10-14 15:51:50 +02001064#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065
1066 return( ret );
1067}
1068#endif /* POLARSSL_FS_IO */
1069
Paul Bakker6edcd412013-10-29 15:22:54 +01001070#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1071 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072#include <stdarg.h>
1073
1074#if !defined vsnprintf
1075#define vsnprintf _vsnprintf
1076#endif // vsnprintf
1077
1078/*
1079 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1080 * Result value is not size of buffer needed, but -1 if no fit is possible.
1081 *
1082 * This fuction tries to 'fix' this by at least suggesting enlarging the
1083 * size by 20.
1084 */
1085static int compat_snprintf(char *str, size_t size, const char *format, ...)
1086{
1087 va_list ap;
1088 int res = -1;
1089
1090 va_start( ap, format );
1091
1092 res = vsnprintf( str, size, format, ap );
1093
1094 va_end( ap );
1095
1096 // No quick fix possible
1097 if ( res < 0 )
1098 return( (int) size + 20 );
1099
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001100 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101}
1102
1103#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001104#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105
1106#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1107
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001108#define SAFE_SNPRINTF() \
1109{ \
1110 if( ret == -1 ) \
1111 return( -1 ); \
1112 \
1113 if ( (unsigned int) ret > n ) { \
1114 p[n - 1] = '\0'; \
1115 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
1116 } \
1117 \
1118 n -= (unsigned int) ret; \
1119 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120}
1121
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001122static int x509_info_subject_alt_name( char **buf, size_t *size,
1123 const x509_sequence *subject_alt_name )
1124{
1125 size_t i;
1126 size_t n = *size;
1127 char *p = *buf;
1128 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001129 const char *sep = "";
1130 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001131
1132 while( cur != NULL )
1133 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001134 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001135 {
1136 *p = '\0';
1137 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1138 }
1139
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001140 n -= cur->buf.len + sep_len;
1141 for( i = 0; i < sep_len; i++ )
1142 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001143 for( i = 0; i < cur->buf.len; i++ )
1144 *p++ = cur->buf.p[i];
1145
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001146 sep = ", ";
1147 sep_len = 2;
1148
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001149 cur = cur->next;
1150 }
1151
1152 *p = '\0';
1153
1154 *size = n;
1155 *buf = p;
1156
1157 return( 0 );
1158}
1159
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001160#define PRINT_ITEM(i) \
1161 { \
1162 ret = snprintf( p, n, "%s" i, sep ); \
1163 SAFE_SNPRINTF(); \
1164 sep = ", "; \
1165 }
1166
1167#define CERT_TYPE(type,name) \
1168 if( ns_cert_type & type ) \
1169 PRINT_ITEM( name );
1170
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001171static int x509_info_cert_type( char **buf, size_t *size,
1172 unsigned char ns_cert_type )
1173{
1174 int ret;
1175 size_t n = *size;
1176 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001177 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001178
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001179 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1180 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1181 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1182 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1183 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1184 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1185 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1186 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001187
1188 *size = n;
1189 *buf = p;
1190
1191 return( 0 );
1192}
1193
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001194#define KEY_USAGE(code,name) \
1195 if( key_usage & code ) \
1196 PRINT_ITEM( name );
1197
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001198static int x509_info_key_usage( char **buf, size_t *size,
1199 unsigned char key_usage )
1200{
1201 int ret;
1202 size_t n = *size;
1203 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001204 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001205
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001206 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1207 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1208 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1209 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1210 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1211 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1212 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001213
1214 *size = n;
1215 *buf = p;
1216
1217 return( 0 );
1218}
1219
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001220static int x509_info_ext_key_usage( char **buf, size_t *size,
1221 const x509_sequence *extended_key_usage )
1222{
1223 int ret;
1224 const char *desc;
1225 size_t n = *size;
1226 char *p = *buf;
1227 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001228 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001229
1230 while( cur != NULL )
1231 {
1232 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1233 desc = "???";
1234
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001235 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001236 SAFE_SNPRINTF();
1237
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001238 sep = ", ";
1239
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001240 cur = cur->next;
1241 }
1242
1243 *size = n;
1244 *buf = p;
1245
1246 return( 0 );
1247}
1248
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001249/*
1250 * Return an informational string about the certificate.
1251 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001252#define BEFORE_COLON 18
1253#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001254int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001255 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001256{
1257 int ret;
1258 size_t n;
1259 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001260 char key_size_str[BEFORE_COLON];
1261
1262 p = buf;
1263 n = size;
1264
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001265 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001266 prefix, crt->version );
1267 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001268 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001269 prefix );
1270 SAFE_SNPRINTF();
1271
Paul Bakker86d0c192013-09-18 11:11:02 +02001272 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273 SAFE_SNPRINTF();
1274
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001275 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001277 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001278 SAFE_SNPRINTF();
1279
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001280 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001282 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001283 SAFE_SNPRINTF();
1284
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001285 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1287 crt->valid_from.year, crt->valid_from.mon,
1288 crt->valid_from.day, crt->valid_from.hour,
1289 crt->valid_from.min, crt->valid_from.sec );
1290 SAFE_SNPRINTF();
1291
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001292 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001293 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1294 crt->valid_to.year, crt->valid_to.mon,
1295 crt->valid_to.day, crt->valid_to.hour,
1296 crt->valid_to.min, crt->valid_to.sec );
1297 SAFE_SNPRINTF();
1298
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001299 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001300 SAFE_SNPRINTF();
1301
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001302 ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001303 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 SAFE_SNPRINTF();
1305
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001306 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1308 pk_get_name( &crt->pk ) ) ) != 0 )
1309 {
1310 return( ret );
1311 }
1312
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001313 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 (int) pk_get_size( &crt->pk ) );
1315 SAFE_SNPRINTF();
1316
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001317 /*
1318 * Optional extensions
1319 */
1320
1321 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1322 {
1323 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1324 crt->ca_istrue ? "true" : "false" );
1325 SAFE_SNPRINTF();
1326
1327 if( crt->max_pathlen > 0 )
1328 {
1329 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1330 SAFE_SNPRINTF();
1331 }
1332 }
1333
1334 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1335 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001336 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001337 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001338
1339 if( ( ret = x509_info_subject_alt_name( &p, &n,
1340 &crt->subject_alt_names ) ) != 0 )
1341 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001342 }
1343
1344 if( crt->ext_types & EXT_NS_CERT_TYPE )
1345 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001346 ret = snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001347 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001348
1349 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1350 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001351 }
1352
1353 if( crt->ext_types & EXT_KEY_USAGE )
1354 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001355 ret = snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001356 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001357
1358 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1359 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001360 }
1361
1362 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1363 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001364 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001365 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001366
1367 if( ( ret = x509_info_ext_key_usage( &p, &n,
1368 &crt->ext_key_usage ) ) != 0 )
1369 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001370 }
1371
1372 ret = snprintf( p, n, "\n" );
1373 SAFE_SNPRINTF();
1374
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001375 return( (int) ( size - n ) );
1376}
1377
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001378#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1379int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1380{
1381 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1382 ( crt->key_usage & usage ) != usage )
1383 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1384
1385 return( 0 );
1386}
1387#endif
1388
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001389#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1390int x509_crt_check_extended_key_usage( const x509_crt *crt,
1391 const char *usage_oid,
1392 size_t usage_len )
1393{
1394 const x509_sequence *cur;
1395
1396 /* Extension is not mandatory, absent means no restriction */
1397 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1398 return( 0 );
1399
1400 /*
1401 * Look for the requested usage (or wildcard ANY) in our list
1402 */
1403 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1404 {
1405 const x509_buf *cur_oid = &cur->buf;
1406
1407 if( cur_oid->len == usage_len &&
1408 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1409 {
1410 return( 0 );
1411 }
1412
1413 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1414 return( 0 );
1415 }
1416
1417 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1418}
Paul Bakker9af723c2014-05-01 13:03:14 +02001419#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001420
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421#if defined(POLARSSL_X509_CRL_PARSE_C)
1422/*
1423 * Return 1 if the certificate is revoked, or 0 otherwise.
1424 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001425int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001426{
1427 const x509_crl_entry *cur = &crl->entry;
1428
1429 while( cur != NULL && cur->serial.len != 0 )
1430 {
1431 if( crt->serial.len == cur->serial.len &&
1432 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1433 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001434 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001435 return( 1 );
1436 }
1437
1438 cur = cur->next;
1439 }
1440
1441 return( 0 );
1442}
1443
1444/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001445 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001447static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001448 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449{
1450 int flags = 0;
1451 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1452 const md_info_t *md_info;
1453
1454 if( ca == NULL )
1455 return( flags );
1456
1457 /*
1458 * TODO: What happens if no CRL is present?
1459 * Suggestion: Revocation state should be unknown if no CRL is present.
1460 * For backwards compatibility this is not yet implemented.
1461 */
1462
1463 while( crl_list != NULL )
1464 {
1465 if( crl_list->version == 0 ||
1466 crl_list->issuer_raw.len != ca->subject_raw.len ||
1467 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1468 crl_list->issuer_raw.len ) != 0 )
1469 {
1470 crl_list = crl_list->next;
1471 continue;
1472 }
1473
1474 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001475 * Check if the CA is configured to sign CRLs
1476 */
1477#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1478 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1479 {
1480 flags |= BADCRL_NOT_TRUSTED;
1481 break;
1482 }
1483#endif
1484
1485 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486 * Check if CRL is correctly signed by the trusted CA
1487 */
1488 md_info = md_info_from_type( crl_list->sig_md );
1489 if( md_info == NULL )
1490 {
1491 /*
1492 * Cannot check 'unknown' hash
1493 */
1494 flags |= BADCRL_NOT_TRUSTED;
1495 break;
1496 }
1497
1498 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1499
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001500 if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1501 crl_list->sig_md, hash, md_info->size,
1502 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001503 {
1504 flags |= BADCRL_NOT_TRUSTED;
1505 break;
1506 }
1507
1508 /*
1509 * Check for validity of CRL (Do not drop out)
1510 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001511 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512 flags |= BADCRL_EXPIRED;
1513
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001514 if( x509_time_future( &crl_list->this_update ) )
1515 flags |= BADCRL_FUTURE;
1516
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517 /*
1518 * Check if certificate is revoked
1519 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001520 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001521 {
1522 flags |= BADCERT_REVOKED;
1523 break;
1524 }
1525
1526 crl_list = crl_list->next;
1527 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001528 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529}
1530#endif /* POLARSSL_X509_CRL_PARSE_C */
1531
1532// Equal == 0, inequal == 1
1533static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1534{
1535 size_t i;
1536 unsigned char diff;
1537 const unsigned char *n1 = s1, *n2 = s2;
1538
1539 for( i = 0; i < len; i++ )
1540 {
1541 diff = n1[i] ^ n2[i];
1542
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001543 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544 continue;
1545
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001546 if( diff == 32 &&
1547 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1548 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1549 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001550 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001551 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552
1553 return( 1 );
1554 }
1555
1556 return( 0 );
1557}
1558
1559static int x509_wildcard_verify( const char *cn, x509_buf *name )
1560{
1561 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001562 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001563
1564 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1565 return( 0 );
1566
Paul Bakker14b16c62014-05-28 11:33:54 +02001567 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001568 {
1569 if( cn[i] == '.' )
1570 {
1571 cn_idx = i;
1572 break;
1573 }
1574 }
1575
1576 if( cn_idx == 0 )
1577 return( 0 );
1578
Paul Bakker14b16c62014-05-28 11:33:54 +02001579 if( cn_len - cn_idx == name->len - 1 &&
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1581 {
1582 return( 1 );
1583 }
1584
1585 return( 0 );
1586}
1587
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001588/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001589 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1590 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001591 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001592static int x509_crt_check_parent( const x509_crt *child,
1593 const x509_crt *parent )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001594{
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001595 if( parent->version == 0 ||
1596 parent->ca_istrue == 0 ||
1597 child->issuer_raw.len != parent->subject_raw.len ||
1598 memcmp( child->issuer_raw.p, parent->subject_raw.p,
1599 child->issuer_raw.len ) != 0 )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001600 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001601 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001602 }
1603
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001604#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1605 if( x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
1606 return( -1 );
1607#endif
1608
1609 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001610}
1611
Paul Bakkerddf26b42013-09-18 13:46:23 +02001612static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001613 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001615 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616 void *p_vrfy )
1617{
1618 int ret;
1619 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1620 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1621 const md_info_t *md_info;
1622
Paul Bakker86d0c192013-09-18 11:11:02 +02001623 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624 *flags |= BADCERT_EXPIRED;
1625
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001626 if( x509_time_future( &child->valid_from ) )
1627 *flags |= BADCERT_FUTURE;
1628
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001629 /*
1630 * Child is the top of the chain. Check against the trust_ca list.
1631 */
1632 *flags |= BADCERT_NOT_TRUSTED;
1633
1634 md_info = md_info_from_type( child->sig_md );
1635 if( md_info == NULL )
1636 {
1637 /*
1638 * Cannot check 'unknown', no need to try any CA
1639 */
1640 trust_ca = NULL;
1641 }
1642 else
1643 md( md_info, child->tbs.p, child->tbs.len, hash );
1644
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001645 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001647 if( x509_crt_check_parent( child, trust_ca ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001648 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001649
1650 /*
1651 * Reduce path_len to check against if top of the chain is
1652 * the same as the trusted CA
1653 */
1654 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1655 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1656 child->issuer_raw.len ) == 0 )
1657 {
1658 check_path_cnt--;
1659 }
1660
1661 if( trust_ca->max_pathlen > 0 &&
1662 trust_ca->max_pathlen < check_path_cnt )
1663 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001664 continue;
1665 }
1666
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001667 if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1668 child->sig_md, hash, md_info->size,
1669 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671 continue;
1672 }
1673
1674 /*
1675 * Top of chain is signed by a trusted CA
1676 */
1677 *flags &= ~BADCERT_NOT_TRUSTED;
1678 break;
1679 }
1680
1681 /*
1682 * If top of chain is not the same as the trusted CA send a verify request
1683 * to the callback for any issues with validity and CRL presence for the
1684 * trusted CA certificate.
1685 */
1686 if( trust_ca != NULL &&
1687 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1688 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1689 child->issuer_raw.len ) != 0 ) )
1690 {
1691#if defined(POLARSSL_X509_CRL_PARSE_C)
1692 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001693 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001694#else
1695 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001696#endif
1697
Paul Bakker86d0c192013-09-18 11:11:02 +02001698 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001699 ca_flags |= BADCERT_EXPIRED;
1700
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001701 if( x509_time_future( &trust_ca->valid_from ) )
1702 ca_flags |= BADCERT_FUTURE;
1703
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001704 if( NULL != f_vrfy )
1705 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001706 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1707 &ca_flags ) ) != 0 )
1708 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001709 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001710 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001711 }
1712 }
1713
1714 /* Call callback on top cert */
1715 if( NULL != f_vrfy )
1716 {
1717 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1718 return( ret );
1719 }
1720
1721 *flags |= ca_flags;
1722
1723 return( 0 );
1724}
1725
Paul Bakkerddf26b42013-09-18 13:46:23 +02001726static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001727 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001728 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001729 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001730 void *p_vrfy )
1731{
1732 int ret;
1733 int parent_flags = 0;
1734 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001735 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736 const md_info_t *md_info;
1737
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001738 if( x509_time_expired( &child->valid_to ) )
1739 *flags |= BADCERT_EXPIRED;
1740
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001741 if( x509_time_future( &child->valid_from ) )
1742 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001743
1744 md_info = md_info_from_type( child->sig_md );
1745 if( md_info == NULL )
1746 {
1747 /*
1748 * Cannot check 'unknown' hash
1749 */
1750 *flags |= BADCERT_NOT_TRUSTED;
1751 }
1752 else
1753 {
1754 md( md_info, child->tbs.p, child->tbs.len, hash );
1755
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001756 if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1757 child->sig_md, hash, md_info->size,
1758 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001759 {
1760 *flags |= BADCERT_NOT_TRUSTED;
1761 }
1762 }
1763
1764#if defined(POLARSSL_X509_CRL_PARSE_C)
1765 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001766 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001767#endif
1768
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001769 /* Look for a grandparent upwards the chain */
1770 for( grandparent = parent->next;
1771 grandparent != NULL;
1772 grandparent = grandparent->next )
1773 {
1774 if( x509_crt_check_parent( parent, grandparent ) == 0 )
1775 break;
1776 }
1777
1778 /* Is our parent part of the chain or at the top? */
1779 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001780 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001781 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1782 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001783 if( ret != 0 )
1784 return( ret );
1785 }
1786 else
1787 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001788 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1789 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001790 if( ret != 0 )
1791 return( ret );
1792 }
1793
1794 /* child is verified to be a child of the parent, call verify callback */
1795 if( NULL != f_vrfy )
1796 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1797 return( ret );
1798
1799 *flags |= parent_flags;
1800
1801 return( 0 );
1802}
1803
1804/*
1805 * Verify the certificate validity
1806 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001807int x509_crt_verify( x509_crt *crt,
1808 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001809 x509_crl *ca_crl,
1810 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001811 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001812 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001813{
1814 size_t cn_len;
1815 int ret;
1816 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001817 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001818 x509_name *name;
1819 x509_sequence *cur = NULL;
1820
1821 *flags = 0;
1822
1823 if( cn != NULL )
1824 {
1825 name = &crt->subject;
1826 cn_len = strlen( cn );
1827
1828 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1829 {
1830 cur = &crt->subject_alt_names;
1831
1832 while( cur != NULL )
1833 {
1834 if( cur->buf.len == cn_len &&
1835 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1836 break;
1837
1838 if( cur->buf.len > 2 &&
1839 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1840 x509_wildcard_verify( cn, &cur->buf ) )
1841 break;
1842
1843 cur = cur->next;
1844 }
1845
1846 if( cur == NULL )
1847 *flags |= BADCERT_CN_MISMATCH;
1848 }
1849 else
1850 {
1851 while( name != NULL )
1852 {
1853 if( OID_CMP( OID_AT_CN, &name->oid ) )
1854 {
1855 if( name->val.len == cn_len &&
1856 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1857 break;
1858
1859 if( name->val.len > 2 &&
1860 memcmp( name->val.p, "*.", 2 ) == 0 &&
1861 x509_wildcard_verify( cn, &name->val ) )
1862 break;
1863 }
1864
1865 name = name->next;
1866 }
1867
1868 if( name == NULL )
1869 *flags |= BADCERT_CN_MISMATCH;
1870 }
1871 }
1872
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001873 /* Look for a parent upwards the chain */
1874 for( parent = crt->next; parent != NULL; parent = parent->next )
1875 {
1876 if( x509_crt_check_parent( crt, parent ) == 0 )
1877 break;
1878 }
1879
1880 /* Are we part of the chain or at the top? */
1881 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001882 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001883 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1884 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001885 if( ret != 0 )
1886 return( ret );
1887 }
1888 else
1889 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001890 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1891 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001892 if( ret != 0 )
1893 return( ret );
1894 }
1895
1896 if( *flags != 0 )
1897 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1898
1899 return( 0 );
1900}
1901
1902/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001903 * Initialize a certificate chain
1904 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001905void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001906{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001907 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001908}
1909
1910/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001911 * Unallocate all certificate data
1912 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001913void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001914{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001915 x509_crt *cert_cur = crt;
1916 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001917 x509_name *name_cur;
1918 x509_name *name_prv;
1919 x509_sequence *seq_cur;
1920 x509_sequence *seq_prv;
1921
1922 if( crt == NULL )
1923 return;
1924
1925 do
1926 {
1927 pk_free( &cert_cur->pk );
1928
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +02001929#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001930 polarssl_free( cert_cur->sig_opts );
1931#endif
1932
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001933 name_cur = cert_cur->issuer.next;
1934 while( name_cur != NULL )
1935 {
1936 name_prv = name_cur;
1937 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02001938 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001939 polarssl_free( name_prv );
1940 }
1941
1942 name_cur = cert_cur->subject.next;
1943 while( name_cur != NULL )
1944 {
1945 name_prv = name_cur;
1946 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02001947 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001948 polarssl_free( name_prv );
1949 }
1950
1951 seq_cur = cert_cur->ext_key_usage.next;
1952 while( seq_cur != NULL )
1953 {
1954 seq_prv = seq_cur;
1955 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02001956 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001957 polarssl_free( seq_prv );
1958 }
1959
1960 seq_cur = cert_cur->subject_alt_names.next;
1961 while( seq_cur != NULL )
1962 {
1963 seq_prv = seq_cur;
1964 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02001965 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001966 polarssl_free( seq_prv );
1967 }
1968
1969 if( cert_cur->raw.p != NULL )
1970 {
Paul Bakker34617722014-06-13 17:20:13 +02001971 polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001972 polarssl_free( cert_cur->raw.p );
1973 }
1974
1975 cert_cur = cert_cur->next;
1976 }
1977 while( cert_cur != NULL );
1978
1979 cert_cur = crt;
1980 do
1981 {
1982 cert_prv = cert_cur;
1983 cert_cur = cert_cur->next;
1984
Paul Bakker34617722014-06-13 17:20:13 +02001985 polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001986 if( cert_prv != crt )
1987 polarssl_free( cert_prv );
1988 }
1989 while( cert_cur != NULL );
1990}
1991
Paul Bakker9af723c2014-05-01 13:03:14 +02001992#endif /* POLARSSL_X509_CRT_PARSE_C */