blob: 6b9268878246c38a89628c67d02e2b752a9df4a4 [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
83/*
84 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
85 */
86static int x509_get_version( unsigned char **p,
87 const unsigned char *end,
88 int *ver )
89{
90 int ret;
91 size_t len;
92
93 if( ( ret = asn1_get_tag( p, end, &len,
94 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
95 {
96 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
97 {
98 *ver = 0;
99 return( 0 );
100 }
101
102 return( ret );
103 }
104
105 end = *p + len;
106
107 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200108 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109
110 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200111 return( POLARSSL_ERR_X509_INVALID_VERSION +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
113
114 return( 0 );
115}
116
117/*
118 * Validity ::= SEQUENCE {
119 * notBefore Time,
120 * notAfter Time }
121 */
122static int x509_get_dates( unsigned char **p,
123 const unsigned char *end,
124 x509_time *from,
125 x509_time *to )
126{
127 int ret;
128 size_t len;
129
130 if( ( ret = asn1_get_tag( p, end, &len,
131 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200132 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200133
134 end = *p + len;
135
136 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
137 return( ret );
138
139 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
140 return( ret );
141
142 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200143 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200144 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
145
146 return( 0 );
147}
148
149/*
150 * X.509 v2/v3 unique identifier (not parsed)
151 */
152static int x509_get_uid( unsigned char **p,
153 const unsigned char *end,
154 x509_buf *uid, int n )
155{
156 int ret;
157
158 if( *p == end )
159 return( 0 );
160
161 uid->tag = **p;
162
163 if( ( ret = asn1_get_tag( p, end, &uid->len,
164 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
165 {
166 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
167 return( 0 );
168
169 return( ret );
170 }
171
172 uid->p = *p;
173 *p += uid->len;
174
175 return( 0 );
176}
177
178static int x509_get_basic_constraints( unsigned char **p,
179 const unsigned char *end,
180 int *ca_istrue,
181 int *max_pathlen )
182{
183 int ret;
184 size_t len;
185
186 /*
187 * BasicConstraints ::= SEQUENCE {
188 * cA BOOLEAN DEFAULT FALSE,
189 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
190 */
191 *ca_istrue = 0; /* DEFAULT FALSE */
192 *max_pathlen = 0; /* endless */
193
194 if( ( ret = asn1_get_tag( p, end, &len,
195 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200196 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200197
198 if( *p == end )
199 return 0;
200
201 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
202 {
203 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
204 ret = asn1_get_int( p, end, ca_istrue );
205
206 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200207 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200208
209 if( *ca_istrue != 0 )
210 *ca_istrue = 1;
211 }
212
213 if( *p == end )
214 return 0;
215
216 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200217 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200218
219 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200220 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
222
223 (*max_pathlen)++;
224
225 return 0;
226}
227
228static int x509_get_ns_cert_type( unsigned char **p,
229 const unsigned char *end,
230 unsigned char *ns_cert_type)
231{
232 int ret;
233 x509_bitstring bs = { 0, 0, NULL };
234
235 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200236 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200237
238 if( bs.len != 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200239 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200240 POLARSSL_ERR_ASN1_INVALID_LENGTH );
241
242 /* Get actual bitstring */
243 *ns_cert_type = *bs.p;
244 return 0;
245}
246
247static int x509_get_key_usage( unsigned char **p,
248 const unsigned char *end,
249 unsigned char *key_usage)
250{
251 int ret;
252 x509_bitstring bs = { 0, 0, NULL };
253
254 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200255 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256
257 if( bs.len < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200258 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200259 POLARSSL_ERR_ASN1_INVALID_LENGTH );
260
261 /* Get actual bitstring */
262 *key_usage = *bs.p;
263 return 0;
264}
265
266/*
267 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
268 *
269 * KeyPurposeId ::= OBJECT IDENTIFIER
270 */
271static int x509_get_ext_key_usage( unsigned char **p,
272 const unsigned char *end,
273 x509_sequence *ext_key_usage)
274{
275 int ret;
276
277 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200278 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200279
280 /* Sequence length must be >= 1 */
281 if( ext_key_usage->buf.p == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200282 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283 POLARSSL_ERR_ASN1_INVALID_LENGTH );
284
285 return 0;
286}
287
288/*
289 * SubjectAltName ::= GeneralNames
290 *
291 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
292 *
293 * GeneralName ::= CHOICE {
294 * otherName [0] OtherName,
295 * rfc822Name [1] IA5String,
296 * dNSName [2] IA5String,
297 * x400Address [3] ORAddress,
298 * directoryName [4] Name,
299 * ediPartyName [5] EDIPartyName,
300 * uniformResourceIdentifier [6] IA5String,
301 * iPAddress [7] OCTET STRING,
302 * registeredID [8] OBJECT IDENTIFIER }
303 *
304 * OtherName ::= SEQUENCE {
305 * type-id OBJECT IDENTIFIER,
306 * value [0] EXPLICIT ANY DEFINED BY type-id }
307 *
308 * EDIPartyName ::= SEQUENCE {
309 * nameAssigner [0] DirectoryString OPTIONAL,
310 * partyName [1] DirectoryString }
311 *
312 * NOTE: PolarSSL only parses and uses dNSName at this point.
313 */
314static int x509_get_subject_alt_name( unsigned char **p,
315 const unsigned char *end,
316 x509_sequence *subject_alt_name )
317{
318 int ret;
319 size_t len, tag_len;
320 asn1_buf *buf;
321 unsigned char tag;
322 asn1_sequence *cur = subject_alt_name;
323
324 /* Get main sequence tag */
325 if( ( ret = asn1_get_tag( p, end, &len,
326 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200327 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328
329 if( *p + len != end )
Paul Bakker51876562013-09-17 14:36:05 +0200330 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200331 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
332
333 while( *p < end )
334 {
335 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200336 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337 POLARSSL_ERR_ASN1_OUT_OF_DATA );
338
339 tag = **p;
340 (*p)++;
341 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200342 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343
344 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
Paul Bakker51876562013-09-17 14:36:05 +0200345 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200346 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
347
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200348 /* Skip everything but DNS name */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
350 {
351 *p += tag_len;
352 continue;
353 }
354
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200356 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357 {
358 cur->next = (asn1_sequence *) polarssl_malloc(
359 sizeof( asn1_sequence ) );
360
361 if( cur->next == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200362 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363 POLARSSL_ERR_ASN1_MALLOC_FAILED );
364
365 memset( cur->next, 0, sizeof( asn1_sequence ) );
366 cur = cur->next;
367 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200368
369 buf = &(cur->buf);
370 buf->tag = tag;
371 buf->p = *p;
372 buf->len = tag_len;
373 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374 }
375
376 /* Set final sequence entry's next pointer to NULL */
377 cur->next = NULL;
378
379 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200380 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
382
383 return( 0 );
384}
385
386/*
387 * X.509 v3 extensions
388 *
389 * TODO: Perform all of the basic constraints tests required by the RFC
390 * TODO: Set values for undetected extensions to a sane default?
391 *
392 */
393static int x509_get_crt_ext( unsigned char **p,
394 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200395 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396{
397 int ret;
398 size_t len;
399 unsigned char *end_ext_data, *end_ext_octet;
400
401 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
402 {
403 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
404 return( 0 );
405
406 return( ret );
407 }
408
409 while( *p < end )
410 {
411 /*
412 * Extension ::= SEQUENCE {
413 * extnID OBJECT IDENTIFIER,
414 * critical BOOLEAN DEFAULT FALSE,
415 * extnValue OCTET STRING }
416 */
417 x509_buf extn_oid = {0, 0, NULL};
418 int is_critical = 0; /* DEFAULT FALSE */
419 int ext_type = 0;
420
421 if( ( ret = asn1_get_tag( p, end, &len,
422 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200423 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424
425 end_ext_data = *p + len;
426
427 /* Get extension ID */
428 extn_oid.tag = **p;
429
430 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200431 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433 extn_oid.p = *p;
434 *p += extn_oid.len;
435
436 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200437 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438 POLARSSL_ERR_ASN1_OUT_OF_DATA );
439
440 /* Get optional critical */
441 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
442 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200443 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
445 /* Data should be octet string type */
446 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
447 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200448 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449
450 end_ext_octet = *p + len;
451
452 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200453 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
455
456 /*
457 * Detect supported extensions
458 */
459 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
460
461 if( ret != 0 )
462 {
463 /* No parser found, skip extension */
464 *p = end_ext_octet;
465
466#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
467 if( is_critical )
468 {
469 /* Data is marked as critical: fail */
Paul Bakker51876562013-09-17 14:36:05 +0200470 return ( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
472 }
473#endif
474 continue;
475 }
476
477 crt->ext_types |= ext_type;
478
479 switch( ext_type )
480 {
481 case EXT_BASIC_CONSTRAINTS:
482 /* Parse basic constraints */
483 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
484 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
485 return ( ret );
486 break;
487
488 case EXT_KEY_USAGE:
489 /* Parse key usage */
490 if( ( ret = x509_get_key_usage( p, end_ext_octet,
491 &crt->key_usage ) ) != 0 )
492 return ( ret );
493 break;
494
495 case EXT_EXTENDED_KEY_USAGE:
496 /* Parse extended key usage */
497 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
498 &crt->ext_key_usage ) ) != 0 )
499 return ( ret );
500 break;
501
502 case EXT_SUBJECT_ALT_NAME:
503 /* Parse subject alt name */
504 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
505 &crt->subject_alt_names ) ) != 0 )
506 return ( ret );
507 break;
508
509 case EXT_NS_CERT_TYPE:
510 /* Parse netscape certificate type */
511 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
512 &crt->ns_cert_type ) ) != 0 )
513 return ( ret );
514 break;
515
516 default:
517 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
518 }
519 }
520
521 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200522 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
524
525 return( 0 );
526}
527
528/*
529 * Parse and fill a single X.509 certificate in DER format
530 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200531static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200532 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533{
534 int ret;
535 size_t len;
536 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100537 x509_buf sig_params;
538
539 memset( &sig_params, 0, sizeof( x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540
541 /*
542 * Check for valid input
543 */
544 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200545 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546
547 p = (unsigned char *) polarssl_malloc( len = buflen );
548
549 if( p == NULL )
550 return( POLARSSL_ERR_X509_MALLOC_FAILED );
551
552 memcpy( p, buf, buflen );
553
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554 crt->raw.p = p;
555 crt->raw.len = len;
556 end = p + len;
557
558 /*
559 * Certificate ::= SEQUENCE {
560 * tbsCertificate TBSCertificate,
561 * signatureAlgorithm AlgorithmIdentifier,
562 * signatureValue BIT STRING }
563 */
564 if( ( ret = asn1_get_tag( &p, end, &len,
565 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
566 {
567 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200568 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569 }
570
571 if( len > (size_t) ( end - p ) )
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 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
576 }
577 crt_end = p + len;
578
579 /*
580 * TBSCertificate ::= SEQUENCE {
581 */
582 crt->tbs.p = p;
583
584 if( ( ret = asn1_get_tag( &p, end, &len,
585 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
586 {
587 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200588 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589 }
590
591 end = p + len;
592 crt->tbs.len = end - crt->tbs.p;
593
594 /*
595 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
596 *
597 * CertificateSerialNumber ::= INTEGER
598 *
599 * signature AlgorithmIdentifier
600 */
601 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
602 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100603 ( ret = x509_get_alg( &p, end, &crt->sig_oid1,
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100604 &sig_params ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605 {
606 x509_crt_free( crt );
607 return( ret );
608 }
609
610 crt->version++;
611
612 if( crt->version > 3 )
613 {
614 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200615 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200616 }
617
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +0100618 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
619 &crt->sig_pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620 {
621 x509_crt_free( crt );
622 return( ret );
623 }
624
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100625#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100626 if( crt->sig_pk == POLARSSL_PK_RSASSA_PSS )
627 {
628 int salt_len, trailer_field;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100629 md_type_t mgf_md;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100630
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100631 /* Make sure params are valid */
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100632 ret = x509_get_rsassa_pss_params( &sig_params,
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100633 &crt->sig_md, &mgf_md, &salt_len, &trailer_field );
634 if( ret != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100635 return( ret );
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100636
637 memcpy( &crt->sig_params, &sig_params, sizeof( x509_buf ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100638 }
639 else
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100640#endif
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100641 {
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100642 /* Make sure parameters are absent or NULL */
643 if( ( sig_params.tag != ASN1_NULL && sig_params.tag != 0 ) ||
644 sig_params.len != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100645 return( POLARSSL_ERR_X509_INVALID_ALG );
646 }
647
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 /*
649 * issuer Name
650 */
651 crt->issuer_raw.p = p;
652
653 if( ( ret = asn1_get_tag( &p, end, &len,
654 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
655 {
656 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200657 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658 }
659
660 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
661 {
662 x509_crt_free( crt );
663 return( ret );
664 }
665
666 crt->issuer_raw.len = p - crt->issuer_raw.p;
667
668 /*
669 * Validity ::= SEQUENCE {
670 * notBefore Time,
671 * notAfter Time }
672 *
673 */
674 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
675 &crt->valid_to ) ) != 0 )
676 {
677 x509_crt_free( crt );
678 return( ret );
679 }
680
681 /*
682 * subject Name
683 */
684 crt->subject_raw.p = p;
685
686 if( ( ret = asn1_get_tag( &p, end, &len,
687 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
688 {
689 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200690 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 }
692
693 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
694 {
695 x509_crt_free( crt );
696 return( ret );
697 }
698
699 crt->subject_raw.len = p - crt->subject_raw.p;
700
701 /*
702 * SubjectPublicKeyInfo
703 */
Paul Bakkerda771152013-09-16 22:45:03 +0200704 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705 {
706 x509_crt_free( crt );
707 return( ret );
708 }
709
710 /*
711 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
712 * -- If present, version shall be v2 or v3
713 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
714 * -- If present, version shall be v2 or v3
715 * extensions [3] EXPLICIT Extensions OPTIONAL
716 * -- If present, version shall be v3
717 */
718 if( crt->version == 2 || crt->version == 3 )
719 {
720 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
721 if( ret != 0 )
722 {
723 x509_crt_free( crt );
724 return( ret );
725 }
726 }
727
728 if( crt->version == 2 || crt->version == 3 )
729 {
730 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
731 if( ret != 0 )
732 {
733 x509_crt_free( crt );
734 return( ret );
735 }
736 }
737
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200738#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 if( crt->version == 3 )
740 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200741#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200742 ret = x509_get_crt_ext( &p, end, crt);
743 if( ret != 0 )
744 {
745 x509_crt_free( crt );
746 return( ret );
747 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200748#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200749 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200750#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200751
752 if( p != end )
753 {
754 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200755 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200756 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
757 }
758
759 end = crt_end;
760
761 /*
762 * }
763 * -- end of TBSCertificate
764 *
765 * signatureAlgorithm AlgorithmIdentifier,
766 * signatureValue BIT STRING
767 */
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100768 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 {
770 x509_crt_free( crt );
771 return( ret );
772 }
773
774 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100775 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0
776#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
777 ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100778 crt->sig_params.len != sig_params.len ||
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100779 memcmp( crt->sig_params.p, sig_params.p, sig_params.len ) != 0
780#endif
781 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200782 {
783 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200784 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785 }
786
787 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
788 {
789 x509_crt_free( crt );
790 return( ret );
791 }
792
793 if( p != end )
794 {
795 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200796 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
798 }
799
800 return( 0 );
801}
802
803/*
804 * Parse one X.509 certificate in DER format from a buffer and add them to a
805 * chained list
806 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200807int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200808 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200809{
810 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200811 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200812
813 /*
814 * Check for valid input
815 */
816 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200817 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818
819 while( crt->version != 0 && crt->next != NULL )
820 {
821 prev = crt;
822 crt = crt->next;
823 }
824
825 /*
826 * Add new certificate on the end of the chain if needed.
827 */
828 if ( crt->version != 0 && crt->next == NULL)
829 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200830 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200831
832 if( crt->next == NULL )
833 return( POLARSSL_ERR_X509_MALLOC_FAILED );
834
835 prev = crt;
836 crt = crt->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200837 x509_crt_init( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200838 }
839
Paul Bakkerddf26b42013-09-18 13:46:23 +0200840 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200841 {
842 if( prev )
843 prev->next = NULL;
844
845 if( crt != chain )
846 polarssl_free( crt );
847
848 return( ret );
849 }
850
851 return( 0 );
852}
853
854/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200855 * Parse one or more PEM certificates from a buffer and add them to the chained
856 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200857 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200858int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200859{
860 int success = 0, first_error = 0, total_failed = 0;
861 int buf_format = X509_FORMAT_DER;
862
863 /*
864 * Check for valid input
865 */
866 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200867 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868
869 /*
870 * Determine buffer content. Buffer contains either one DER certificate or
871 * one or more PEM certificates.
872 */
873#if defined(POLARSSL_PEM_PARSE_C)
874 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
875 buf_format = X509_FORMAT_PEM;
876#endif
877
878 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200879 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880
881#if defined(POLARSSL_PEM_PARSE_C)
882 if( buf_format == X509_FORMAT_PEM )
883 {
884 int ret;
885 pem_context pem;
886
887 while( buflen > 0 )
888 {
889 size_t use_len;
890 pem_init( &pem );
891
892 ret = pem_read_buffer( &pem,
893 "-----BEGIN CERTIFICATE-----",
894 "-----END CERTIFICATE-----",
895 buf, NULL, 0, &use_len );
896
897 if( ret == 0 )
898 {
899 /*
900 * Was PEM encoded
901 */
902 buflen -= use_len;
903 buf += use_len;
904 }
905 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
906 {
907 return( ret );
908 }
909 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
910 {
911 pem_free( &pem );
912
913 /*
914 * PEM header and footer were found
915 */
916 buflen -= use_len;
917 buf += use_len;
918
919 if( first_error == 0 )
920 first_error = ret;
921
922 continue;
923 }
924 else
925 break;
926
Paul Bakkerddf26b42013-09-18 13:46:23 +0200927 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928
929 pem_free( &pem );
930
931 if( ret != 0 )
932 {
933 /*
934 * Quit parsing on a memory error
935 */
936 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
937 return( ret );
938
939 if( first_error == 0 )
940 first_error = ret;
941
942 total_failed++;
943 continue;
944 }
945
946 success = 1;
947 }
948 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200949#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950
951 if( success )
952 return( total_failed );
953 else if( first_error )
954 return( first_error );
955 else
956 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
957}
958
959#if defined(POLARSSL_FS_IO)
960/*
961 * Load one or more certificates and add them to the chained list
962 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200963int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964{
965 int ret;
966 size_t n;
967 unsigned char *buf;
968
969 if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
970 return( ret );
971
Paul Bakkerddf26b42013-09-18 13:46:23 +0200972 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973
974 memset( buf, 0, n + 1 );
975 polarssl_free( buf );
976
977 return( ret );
978}
979
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100980#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100981static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
982#endif
983
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200984int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985{
986 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100987#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988 int w_ret;
989 WCHAR szDir[MAX_PATH];
990 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200991 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200992 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993
Paul Bakker9af723c2014-05-01 13:03:14 +0200994 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995 HANDLE hFind;
996
997 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200998 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999
Paul Bakker9af723c2014-05-01 13:03:14 +02001000 memset( szDir, 0, sizeof(szDir) );
1001 memset( filename, 0, MAX_PATH );
1002 memcpy( filename, path, len );
1003 filename[len++] = '\\';
1004 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005 filename[len++] = '*';
1006
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001007 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
1008 MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009
1010 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker4aa40d42013-10-11 10:49:24 +02001011 if (hFind == INVALID_HANDLE_VALUE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1013
1014 len = MAX_PATH - len;
1015 do
1016 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001017 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018
1019 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1020 continue;
1021
Paul Bakker9af723c2014-05-01 13:03:14 +02001022 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1023 lstrlenW(file_data.cFileName),
1024 p, len - 1,
1025 NULL, NULL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026
Paul Bakkerddf26b42013-09-18 13:46:23 +02001027 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 if( w_ret < 0 )
1029 ret++;
1030 else
1031 ret += w_ret;
1032 }
1033 while( FindNextFileW( hFind, &file_data ) != 0 );
1034
Paul Bakker4aa40d42013-10-11 10:49:24 +02001035 if (GetLastError() != ERROR_NO_MORE_FILES)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1037
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001039#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001040 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001042 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 char entry_name[255];
1044 DIR *dir = opendir( path );
1045
1046 if( dir == NULL)
1047 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1048
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001049#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001050 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1051 return( ret );
1052#endif
1053
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001054 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001056 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001057
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001058 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059 {
1060 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001061 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1062 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063 }
1064
1065 if( !S_ISREG( sb.st_mode ) )
1066 continue;
1067
1068 // Ignore parse errors
1069 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001070 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071 if( t_ret < 0 )
1072 ret++;
1073 else
1074 ret += t_ret;
1075 }
1076 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001077
1078cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001079#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001080 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1081 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1082#endif
1083
Paul Bakkerbe089b02013-10-14 15:51:50 +02001084#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085
1086 return( ret );
1087}
1088#endif /* POLARSSL_FS_IO */
1089
Paul Bakker6edcd412013-10-29 15:22:54 +01001090#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1091 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092#include <stdarg.h>
1093
1094#if !defined vsnprintf
1095#define vsnprintf _vsnprintf
1096#endif // vsnprintf
1097
1098/*
1099 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1100 * Result value is not size of buffer needed, but -1 if no fit is possible.
1101 *
1102 * This fuction tries to 'fix' this by at least suggesting enlarging the
1103 * size by 20.
1104 */
1105static int compat_snprintf(char *str, size_t size, const char *format, ...)
1106{
1107 va_list ap;
1108 int res = -1;
1109
1110 va_start( ap, format );
1111
1112 res = vsnprintf( str, size, format, ap );
1113
1114 va_end( ap );
1115
1116 // No quick fix possible
1117 if ( res < 0 )
1118 return( (int) size + 20 );
1119
1120 return res;
1121}
1122
1123#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001124#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001125
1126#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1127
1128#define SAFE_SNPRINTF() \
1129{ \
1130 if( ret == -1 ) \
1131 return( -1 ); \
1132 \
1133 if ( (unsigned int) ret > n ) { \
1134 p[n - 1] = '\0'; \
1135 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1136 } \
1137 \
1138 n -= (unsigned int) ret; \
1139 p += (unsigned int) ret; \
1140}
1141
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001142static int x509_info_subject_alt_name( char **buf, size_t *size,
1143 const x509_sequence *subject_alt_name )
1144{
1145 size_t i;
1146 size_t n = *size;
1147 char *p = *buf;
1148 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001149 const char *sep = "";
1150 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001151
1152 while( cur != NULL )
1153 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001154 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001155 {
1156 *p = '\0';
1157 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1158 }
1159
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001160 n -= cur->buf.len + sep_len;
1161 for( i = 0; i < sep_len; i++ )
1162 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001163 for( i = 0; i < cur->buf.len; i++ )
1164 *p++ = cur->buf.p[i];
1165
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001166 sep = ", ";
1167 sep_len = 2;
1168
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001169 cur = cur->next;
1170 }
1171
1172 *p = '\0';
1173
1174 *size = n;
1175 *buf = p;
1176
1177 return( 0 );
1178}
1179
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001180#define PRINT_ITEM(i) \
1181 { \
1182 ret = snprintf( p, n, "%s" i, sep ); \
1183 SAFE_SNPRINTF(); \
1184 sep = ", "; \
1185 }
1186
1187#define CERT_TYPE(type,name) \
1188 if( ns_cert_type & type ) \
1189 PRINT_ITEM( name );
1190
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001191static int x509_info_cert_type( char **buf, size_t *size,
1192 unsigned char ns_cert_type )
1193{
1194 int ret;
1195 size_t n = *size;
1196 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001197 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001198
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001199 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1200 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1201 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1202 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1203 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1204 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1205 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1206 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001207
1208 *size = n;
1209 *buf = p;
1210
1211 return( 0 );
1212}
1213
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001214#define KEY_USAGE(code,name) \
1215 if( key_usage & code ) \
1216 PRINT_ITEM( name );
1217
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001218static int x509_info_key_usage( char **buf, size_t *size,
1219 unsigned char key_usage )
1220{
1221 int ret;
1222 size_t n = *size;
1223 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001224 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001225
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001226 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1227 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1228 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1229 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1230 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1231 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1232 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001233
1234 *size = n;
1235 *buf = p;
1236
1237 return( 0 );
1238}
1239
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001240static int x509_info_ext_key_usage( char **buf, size_t *size,
1241 const x509_sequence *extended_key_usage )
1242{
1243 int ret;
1244 const char *desc;
1245 size_t n = *size;
1246 char *p = *buf;
1247 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001248 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001249
1250 while( cur != NULL )
1251 {
1252 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1253 desc = "???";
1254
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001255 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001256 SAFE_SNPRINTF();
1257
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001258 sep = ", ";
1259
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001260 cur = cur->next;
1261 }
1262
1263 *size = n;
1264 *buf = p;
1265
1266 return( 0 );
1267}
1268
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001269/*
1270 * Return an informational string about the certificate.
1271 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001272#define BEFORE_COLON 18
1273#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001274int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001275 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276{
1277 int ret;
1278 size_t n;
1279 char *p;
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001280 const char *desc = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 char key_size_str[BEFORE_COLON];
1282
1283 p = buf;
1284 n = size;
1285
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001286 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001287 prefix, crt->version );
1288 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001289 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001290 prefix );
1291 SAFE_SNPRINTF();
1292
Paul Bakker86d0c192013-09-18 11:11:02 +02001293 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001294 SAFE_SNPRINTF();
1295
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001296 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001297 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001298 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001299 SAFE_SNPRINTF();
1300
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001301 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001302 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001303 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 SAFE_SNPRINTF();
1305
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001306 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1308 crt->valid_from.year, crt->valid_from.mon,
1309 crt->valid_from.day, crt->valid_from.hour,
1310 crt->valid_from.min, crt->valid_from.sec );
1311 SAFE_SNPRINTF();
1312
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001313 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1315 crt->valid_to.year, crt->valid_to.mon,
1316 crt->valid_to.day, crt->valid_to.hour,
1317 crt->valid_to.min, crt->valid_to.sec );
1318 SAFE_SNPRINTF();
1319
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001320 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001321 SAFE_SNPRINTF();
1322
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001323 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1324 if( ret != 0 )
1325 ret = snprintf( p, n, "???" );
1326 else
1327 ret = snprintf( p, n, "%s", desc );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328 SAFE_SNPRINTF();
1329
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +01001330#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +01001331 if( crt->sig_pk == POLARSSL_PK_RSASSA_PSS )
1332 {
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +01001333 md_type_t md_alg, mgf_md;
1334 const md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +01001335 int salt_len, trailer_field;
1336
1337 if( ( ret = x509_get_rsassa_pss_params( &crt->sig_params,
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +01001338 &md_alg, &mgf_md, &salt_len, &trailer_field ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +01001339 return( ret );
1340
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +01001341 md_info = md_info_from_type( md_alg );
1342 mgf_md_info = md_info_from_type( mgf_md );
1343
1344 ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X, %d)",
1345 md_info ? md_info->name : "???",
1346 mgf_md_info ? mgf_md_info->name : "???",
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +01001347 salt_len, trailer_field );
1348 SAFE_SNPRINTF();
1349 }
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +01001350#endif /* POLARSSL_RSASSA_PSS_CERTIFICATES */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +01001351
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001352 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1354 pk_get_name( &crt->pk ) ) ) != 0 )
1355 {
1356 return( ret );
1357 }
1358
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001359 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360 (int) pk_get_size( &crt->pk ) );
1361 SAFE_SNPRINTF();
1362
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001363 /*
1364 * Optional extensions
1365 */
1366
1367 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1368 {
1369 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1370 crt->ca_istrue ? "true" : "false" );
1371 SAFE_SNPRINTF();
1372
1373 if( crt->max_pathlen > 0 )
1374 {
1375 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1376 SAFE_SNPRINTF();
1377 }
1378 }
1379
1380 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1381 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001382 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001383 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001384
1385 if( ( ret = x509_info_subject_alt_name( &p, &n,
1386 &crt->subject_alt_names ) ) != 0 )
1387 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001388 }
1389
1390 if( crt->ext_types & EXT_NS_CERT_TYPE )
1391 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001392 ret = snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001393 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001394
1395 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1396 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001397 }
1398
1399 if( crt->ext_types & EXT_KEY_USAGE )
1400 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001401 ret = snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001402 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001403
1404 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1405 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001406 }
1407
1408 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1409 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001410 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001411 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001412
1413 if( ( ret = x509_info_ext_key_usage( &p, &n,
1414 &crt->ext_key_usage ) ) != 0 )
1415 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001416 }
1417
1418 ret = snprintf( p, n, "\n" );
1419 SAFE_SNPRINTF();
1420
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421 return( (int) ( size - n ) );
1422}
1423
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001424#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1425int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1426{
1427 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1428 ( crt->key_usage & usage ) != usage )
1429 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1430
1431 return( 0 );
1432}
1433#endif
1434
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001435#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1436int x509_crt_check_extended_key_usage( const x509_crt *crt,
1437 const char *usage_oid,
1438 size_t usage_len )
1439{
1440 const x509_sequence *cur;
1441
1442 /* Extension is not mandatory, absent means no restriction */
1443 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1444 return( 0 );
1445
1446 /*
1447 * Look for the requested usage (or wildcard ANY) in our list
1448 */
1449 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1450 {
1451 const x509_buf *cur_oid = &cur->buf;
1452
1453 if( cur_oid->len == usage_len &&
1454 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1455 {
1456 return( 0 );
1457 }
1458
1459 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1460 return( 0 );
1461 }
1462
1463 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1464}
Paul Bakker9af723c2014-05-01 13:03:14 +02001465#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001466
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467#if defined(POLARSSL_X509_CRL_PARSE_C)
1468/*
1469 * Return 1 if the certificate is revoked, or 0 otherwise.
1470 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001471int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472{
1473 const x509_crl_entry *cur = &crl->entry;
1474
1475 while( cur != NULL && cur->serial.len != 0 )
1476 {
1477 if( crt->serial.len == cur->serial.len &&
1478 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1479 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001480 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001481 return( 1 );
1482 }
1483
1484 cur = cur->next;
1485 }
1486
1487 return( 0 );
1488}
1489
1490/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001491 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001492 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001493static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001494 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495{
1496 int flags = 0;
1497 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1498 const md_info_t *md_info;
1499
1500 if( ca == NULL )
1501 return( flags );
1502
1503 /*
1504 * TODO: What happens if no CRL is present?
1505 * Suggestion: Revocation state should be unknown if no CRL is present.
1506 * For backwards compatibility this is not yet implemented.
1507 */
1508
1509 while( crl_list != NULL )
1510 {
1511 if( crl_list->version == 0 ||
1512 crl_list->issuer_raw.len != ca->subject_raw.len ||
1513 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1514 crl_list->issuer_raw.len ) != 0 )
1515 {
1516 crl_list = crl_list->next;
1517 continue;
1518 }
1519
1520 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001521 * Check if the CA is configured to sign CRLs
1522 */
1523#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1524 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1525 {
1526 flags |= BADCRL_NOT_TRUSTED;
1527 break;
1528 }
1529#endif
1530
1531 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532 * Check if CRL is correctly signed by the trusted CA
1533 */
1534 md_info = md_info_from_type( crl_list->sig_md );
1535 if( md_info == NULL )
1536 {
1537 /*
1538 * Cannot check 'unknown' hash
1539 */
1540 flags |= BADCRL_NOT_TRUSTED;
1541 break;
1542 }
1543
1544 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1545
1546 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1547 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1548 crl_list->sig.p, crl_list->sig.len ) != 0 )
1549 {
1550 flags |= BADCRL_NOT_TRUSTED;
1551 break;
1552 }
1553
1554 /*
1555 * Check for validity of CRL (Do not drop out)
1556 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001557 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558 flags |= BADCRL_EXPIRED;
1559
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001560 if( x509_time_future( &crl_list->this_update ) )
1561 flags |= BADCRL_FUTURE;
1562
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001563 /*
1564 * Check if certificate is revoked
1565 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001566 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567 {
1568 flags |= BADCERT_REVOKED;
1569 break;
1570 }
1571
1572 crl_list = crl_list->next;
1573 }
1574 return flags;
1575}
1576#endif /* POLARSSL_X509_CRL_PARSE_C */
1577
1578// Equal == 0, inequal == 1
1579static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1580{
1581 size_t i;
1582 unsigned char diff;
1583 const unsigned char *n1 = s1, *n2 = s2;
1584
1585 for( i = 0; i < len; i++ )
1586 {
1587 diff = n1[i] ^ n2[i];
1588
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001589 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590 continue;
1591
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001592 if( diff == 32 &&
1593 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1594 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1595 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001597 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001598
1599 return( 1 );
1600 }
1601
1602 return( 0 );
1603}
1604
1605static int x509_wildcard_verify( const char *cn, x509_buf *name )
1606{
1607 size_t i;
1608 size_t cn_idx = 0;
1609
1610 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1611 return( 0 );
1612
1613 for( i = 0; i < strlen( cn ); ++i )
1614 {
1615 if( cn[i] == '.' )
1616 {
1617 cn_idx = i;
1618 break;
1619 }
1620 }
1621
1622 if( cn_idx == 0 )
1623 return( 0 );
1624
1625 if( strlen( cn ) - cn_idx == name->len - 1 &&
1626 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1627 {
1628 return( 1 );
1629 }
1630
1631 return( 0 );
1632}
1633
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001634/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001635 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1636 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001637 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001638static int x509_crt_check_parent( const x509_crt *child,
1639 const x509_crt *parent )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001640{
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001641 if( parent->version == 0 ||
1642 parent->ca_istrue == 0 ||
1643 child->issuer_raw.len != parent->subject_raw.len ||
1644 memcmp( child->issuer_raw.p, parent->subject_raw.p,
1645 child->issuer_raw.len ) != 0 )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001646 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001647 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001648 }
1649
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001650#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1651 if( x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
1652 return( -1 );
1653#endif
1654
1655 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001656}
1657
Paul Bakkerddf26b42013-09-18 13:46:23 +02001658static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001659 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001661 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001662 void *p_vrfy )
1663{
1664 int ret;
1665 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1666 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1667 const md_info_t *md_info;
1668
Paul Bakker86d0c192013-09-18 11:11:02 +02001669 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670 *flags |= BADCERT_EXPIRED;
1671
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001672 if( x509_time_future( &child->valid_from ) )
1673 *flags |= BADCERT_FUTURE;
1674
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675 /*
1676 * Child is the top of the chain. Check against the trust_ca list.
1677 */
1678 *flags |= BADCERT_NOT_TRUSTED;
1679
1680 md_info = md_info_from_type( child->sig_md );
1681 if( md_info == NULL )
1682 {
1683 /*
1684 * Cannot check 'unknown', no need to try any CA
1685 */
1686 trust_ca = NULL;
1687 }
1688 else
1689 md( md_info, child->tbs.p, child->tbs.len, hash );
1690
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001691 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001692 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001693 if( x509_crt_check_parent( child, trust_ca ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001694 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001695
1696 /*
1697 * Reduce path_len to check against if top of the chain is
1698 * the same as the trusted CA
1699 */
1700 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1701 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1702 child->issuer_raw.len ) == 0 )
1703 {
1704 check_path_cnt--;
1705 }
1706
1707 if( trust_ca->max_pathlen > 0 &&
1708 trust_ca->max_pathlen < check_path_cnt )
1709 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001710 continue;
1711 }
1712
1713 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1714 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1715 child->sig.p, child->sig.len ) != 0 )
1716 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001717 continue;
1718 }
1719
1720 /*
1721 * Top of chain is signed by a trusted CA
1722 */
1723 *flags &= ~BADCERT_NOT_TRUSTED;
1724 break;
1725 }
1726
1727 /*
1728 * If top of chain is not the same as the trusted CA send a verify request
1729 * to the callback for any issues with validity and CRL presence for the
1730 * trusted CA certificate.
1731 */
1732 if( trust_ca != NULL &&
1733 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1734 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1735 child->issuer_raw.len ) != 0 ) )
1736 {
1737#if defined(POLARSSL_X509_CRL_PARSE_C)
1738 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001739 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001740#else
1741 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001742#endif
1743
Paul Bakker86d0c192013-09-18 11:11:02 +02001744 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745 ca_flags |= BADCERT_EXPIRED;
1746
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001747 if( x509_time_future( &trust_ca->valid_from ) )
1748 ca_flags |= BADCERT_FUTURE;
1749
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001750 if( NULL != f_vrfy )
1751 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001752 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1753 &ca_flags ) ) != 0 )
1754 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001755 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001756 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001757 }
1758 }
1759
1760 /* Call callback on top cert */
1761 if( NULL != f_vrfy )
1762 {
1763 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1764 return( ret );
1765 }
1766
1767 *flags |= ca_flags;
1768
1769 return( 0 );
1770}
1771
Paul Bakkerddf26b42013-09-18 13:46:23 +02001772static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001773 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001774 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001775 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001776 void *p_vrfy )
1777{
1778 int ret;
1779 int parent_flags = 0;
1780 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001781 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001782 const md_info_t *md_info;
1783
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001784 if( x509_time_expired( &child->valid_to ) )
1785 *flags |= BADCERT_EXPIRED;
1786
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001787 if( x509_time_future( &child->valid_from ) )
1788 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001789
1790 md_info = md_info_from_type( child->sig_md );
1791 if( md_info == NULL )
1792 {
1793 /*
1794 * Cannot check 'unknown' hash
1795 */
1796 *flags |= BADCERT_NOT_TRUSTED;
1797 }
1798 else
1799 {
1800 md( md_info, child->tbs.p, child->tbs.len, hash );
1801
1802 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1803 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1804 child->sig.p, child->sig.len ) != 0 )
1805 {
1806 *flags |= BADCERT_NOT_TRUSTED;
1807 }
1808 }
1809
1810#if defined(POLARSSL_X509_CRL_PARSE_C)
1811 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001812 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001813#endif
1814
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001815 /* Look for a grandparent upwards the chain */
1816 for( grandparent = parent->next;
1817 grandparent != NULL;
1818 grandparent = grandparent->next )
1819 {
1820 if( x509_crt_check_parent( parent, grandparent ) == 0 )
1821 break;
1822 }
1823
1824 /* Is our parent part of the chain or at the top? */
1825 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001826 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001827 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1828 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001829 if( ret != 0 )
1830 return( ret );
1831 }
1832 else
1833 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001834 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1835 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001836 if( ret != 0 )
1837 return( ret );
1838 }
1839
1840 /* child is verified to be a child of the parent, call verify callback */
1841 if( NULL != f_vrfy )
1842 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1843 return( ret );
1844
1845 *flags |= parent_flags;
1846
1847 return( 0 );
1848}
1849
1850/*
1851 * Verify the certificate validity
1852 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001853int x509_crt_verify( x509_crt *crt,
1854 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001855 x509_crl *ca_crl,
1856 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001857 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001858 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001859{
1860 size_t cn_len;
1861 int ret;
1862 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001863 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001864 x509_name *name;
1865 x509_sequence *cur = NULL;
1866
1867 *flags = 0;
1868
1869 if( cn != NULL )
1870 {
1871 name = &crt->subject;
1872 cn_len = strlen( cn );
1873
1874 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1875 {
1876 cur = &crt->subject_alt_names;
1877
1878 while( cur != NULL )
1879 {
1880 if( cur->buf.len == cn_len &&
1881 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1882 break;
1883
1884 if( cur->buf.len > 2 &&
1885 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1886 x509_wildcard_verify( cn, &cur->buf ) )
1887 break;
1888
1889 cur = cur->next;
1890 }
1891
1892 if( cur == NULL )
1893 *flags |= BADCERT_CN_MISMATCH;
1894 }
1895 else
1896 {
1897 while( name != NULL )
1898 {
1899 if( OID_CMP( OID_AT_CN, &name->oid ) )
1900 {
1901 if( name->val.len == cn_len &&
1902 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1903 break;
1904
1905 if( name->val.len > 2 &&
1906 memcmp( name->val.p, "*.", 2 ) == 0 &&
1907 x509_wildcard_verify( cn, &name->val ) )
1908 break;
1909 }
1910
1911 name = name->next;
1912 }
1913
1914 if( name == NULL )
1915 *flags |= BADCERT_CN_MISMATCH;
1916 }
1917 }
1918
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001919 /* Look for a parent upwards the chain */
1920 for( parent = crt->next; parent != NULL; parent = parent->next )
1921 {
1922 if( x509_crt_check_parent( crt, parent ) == 0 )
1923 break;
1924 }
1925
1926 /* Are we part of the chain or at the top? */
1927 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001928 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001929 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1930 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001931 if( ret != 0 )
1932 return( ret );
1933 }
1934 else
1935 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001936 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1937 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001938 if( ret != 0 )
1939 return( ret );
1940 }
1941
1942 if( *flags != 0 )
1943 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1944
1945 return( 0 );
1946}
1947
1948/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001949 * Initialize a certificate chain
1950 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001951void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001952{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001953 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001954}
1955
1956/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001957 * Unallocate all certificate data
1958 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001959void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001960{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001961 x509_crt *cert_cur = crt;
1962 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001963 x509_name *name_cur;
1964 x509_name *name_prv;
1965 x509_sequence *seq_cur;
1966 x509_sequence *seq_prv;
1967
1968 if( crt == NULL )
1969 return;
1970
1971 do
1972 {
1973 pk_free( &cert_cur->pk );
1974
1975 name_cur = cert_cur->issuer.next;
1976 while( name_cur != NULL )
1977 {
1978 name_prv = name_cur;
1979 name_cur = name_cur->next;
1980 memset( name_prv, 0, sizeof( x509_name ) );
1981 polarssl_free( name_prv );
1982 }
1983
1984 name_cur = cert_cur->subject.next;
1985 while( name_cur != NULL )
1986 {
1987 name_prv = name_cur;
1988 name_cur = name_cur->next;
1989 memset( name_prv, 0, sizeof( x509_name ) );
1990 polarssl_free( name_prv );
1991 }
1992
1993 seq_cur = cert_cur->ext_key_usage.next;
1994 while( seq_cur != NULL )
1995 {
1996 seq_prv = seq_cur;
1997 seq_cur = seq_cur->next;
1998 memset( seq_prv, 0, sizeof( x509_sequence ) );
1999 polarssl_free( seq_prv );
2000 }
2001
2002 seq_cur = cert_cur->subject_alt_names.next;
2003 while( seq_cur != NULL )
2004 {
2005 seq_prv = seq_cur;
2006 seq_cur = seq_cur->next;
2007 memset( seq_prv, 0, sizeof( x509_sequence ) );
2008 polarssl_free( seq_prv );
2009 }
2010
2011 if( cert_cur->raw.p != NULL )
2012 {
2013 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
2014 polarssl_free( cert_cur->raw.p );
2015 }
2016
2017 cert_cur = cert_cur->next;
2018 }
2019 while( cert_cur != NULL );
2020
2021 cert_cur = crt;
2022 do
2023 {
2024 cert_prv = cert_cur;
2025 cert_cur = cert_cur->next;
2026
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002027 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002028 if( cert_prv != crt )
2029 polarssl_free( cert_prv );
2030 }
2031 while( cert_cur != NULL );
2032}
2033
Paul Bakker9af723c2014-05-01 13:03:14 +02002034#endif /* POLARSSL_X509_CRT_PARSE_C */