blob: 1447689c8d47d9b0d071b72b17ee09b04526f17d [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 certificate and private key decoding
3 *
4 * Copyright (C) 2006-2013, Brainspark B.V.
5 *
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
37#include "polarssl/config.h"
38
39#if defined(POLARSSL_X509_CRT_PARSE_C)
40
41#include "polarssl/x509_crt.h"
42#include "polarssl/oid.h"
43#if defined(POLARSSL_PEM_PARSE_C)
44#include "polarssl/pem.h"
45#endif
46
47#if defined(POLARSSL_MEMORY_C)
48#include "polarssl/memory.h"
49#else
50#define polarssl_malloc malloc
51#define polarssl_free free
52#endif
53
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010054#if defined(POLARSSL_THREADING_C)
55#include "polarssl/threading.h"
56#endif
57
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058#include <string.h>
59#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010060#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#include <windows.h>
62#else
63#include <time.h>
64#endif
65
Paul Bakkerfa6a6202013-10-28 18:48:30 +010066#if defined(EFIX64) || defined(EFI32)
67#include <stdio.h>
68#endif
69
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070#if defined(POLARSSL_FS_IO)
71#include <stdio.h>
72#if !defined(_WIN32)
73#include <sys/types.h>
74#include <sys/stat.h>
75#include <dirent.h>
76#endif
77#endif
78
79/*
80 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
81 */
82static int x509_get_version( unsigned char **p,
83 const unsigned char *end,
84 int *ver )
85{
86 int ret;
87 size_t len;
88
89 if( ( ret = asn1_get_tag( p, end, &len,
90 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
91 {
92 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
93 {
94 *ver = 0;
95 return( 0 );
96 }
97
98 return( ret );
99 }
100
101 end = *p + len;
102
103 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200104 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105
106 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200107 return( POLARSSL_ERR_X509_INVALID_VERSION +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200108 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
109
110 return( 0 );
111}
112
113/*
114 * Validity ::= SEQUENCE {
115 * notBefore Time,
116 * notAfter Time }
117 */
118static int x509_get_dates( unsigned char **p,
119 const unsigned char *end,
120 x509_time *from,
121 x509_time *to )
122{
123 int ret;
124 size_t len;
125
126 if( ( ret = asn1_get_tag( p, end, &len,
127 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200128 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200129
130 end = *p + len;
131
132 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
133 return( ret );
134
135 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
136 return( ret );
137
138 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200139 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200140 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
141
142 return( 0 );
143}
144
145/*
146 * X.509 v2/v3 unique identifier (not parsed)
147 */
148static int x509_get_uid( unsigned char **p,
149 const unsigned char *end,
150 x509_buf *uid, int n )
151{
152 int ret;
153
154 if( *p == end )
155 return( 0 );
156
157 uid->tag = **p;
158
159 if( ( ret = asn1_get_tag( p, end, &uid->len,
160 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
161 {
162 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
163 return( 0 );
164
165 return( ret );
166 }
167
168 uid->p = *p;
169 *p += uid->len;
170
171 return( 0 );
172}
173
174static int x509_get_basic_constraints( unsigned char **p,
175 const unsigned char *end,
176 int *ca_istrue,
177 int *max_pathlen )
178{
179 int ret;
180 size_t len;
181
182 /*
183 * BasicConstraints ::= SEQUENCE {
184 * cA BOOLEAN DEFAULT FALSE,
185 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
186 */
187 *ca_istrue = 0; /* DEFAULT FALSE */
188 *max_pathlen = 0; /* endless */
189
190 if( ( ret = asn1_get_tag( p, end, &len,
191 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200192 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200193
194 if( *p == end )
195 return 0;
196
197 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
198 {
199 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
200 ret = asn1_get_int( p, end, ca_istrue );
201
202 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200203 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200204
205 if( *ca_istrue != 0 )
206 *ca_istrue = 1;
207 }
208
209 if( *p == end )
210 return 0;
211
212 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200213 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200214
215 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200216 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200217 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
218
219 (*max_pathlen)++;
220
221 return 0;
222}
223
224static int x509_get_ns_cert_type( unsigned char **p,
225 const unsigned char *end,
226 unsigned char *ns_cert_type)
227{
228 int ret;
229 x509_bitstring bs = { 0, 0, NULL };
230
231 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200232 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200233
234 if( bs.len != 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200235 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236 POLARSSL_ERR_ASN1_INVALID_LENGTH );
237
238 /* Get actual bitstring */
239 *ns_cert_type = *bs.p;
240 return 0;
241}
242
243static int x509_get_key_usage( unsigned char **p,
244 const unsigned char *end,
245 unsigned char *key_usage)
246{
247 int ret;
248 x509_bitstring bs = { 0, 0, NULL };
249
250 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200251 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200252
253 if( bs.len < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200254 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200255 POLARSSL_ERR_ASN1_INVALID_LENGTH );
256
257 /* Get actual bitstring */
258 *key_usage = *bs.p;
259 return 0;
260}
261
262/*
263 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
264 *
265 * KeyPurposeId ::= OBJECT IDENTIFIER
266 */
267static int x509_get_ext_key_usage( unsigned char **p,
268 const unsigned char *end,
269 x509_sequence *ext_key_usage)
270{
271 int ret;
272
273 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200274 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275
276 /* Sequence length must be >= 1 */
277 if( ext_key_usage->buf.p == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200278 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200279 POLARSSL_ERR_ASN1_INVALID_LENGTH );
280
281 return 0;
282}
283
284/*
285 * SubjectAltName ::= GeneralNames
286 *
287 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
288 *
289 * GeneralName ::= CHOICE {
290 * otherName [0] OtherName,
291 * rfc822Name [1] IA5String,
292 * dNSName [2] IA5String,
293 * x400Address [3] ORAddress,
294 * directoryName [4] Name,
295 * ediPartyName [5] EDIPartyName,
296 * uniformResourceIdentifier [6] IA5String,
297 * iPAddress [7] OCTET STRING,
298 * registeredID [8] OBJECT IDENTIFIER }
299 *
300 * OtherName ::= SEQUENCE {
301 * type-id OBJECT IDENTIFIER,
302 * value [0] EXPLICIT ANY DEFINED BY type-id }
303 *
304 * EDIPartyName ::= SEQUENCE {
305 * nameAssigner [0] DirectoryString OPTIONAL,
306 * partyName [1] DirectoryString }
307 *
308 * NOTE: PolarSSL only parses and uses dNSName at this point.
309 */
310static int x509_get_subject_alt_name( unsigned char **p,
311 const unsigned char *end,
312 x509_sequence *subject_alt_name )
313{
314 int ret;
315 size_t len, tag_len;
316 asn1_buf *buf;
317 unsigned char tag;
318 asn1_sequence *cur = subject_alt_name;
319
320 /* Get main sequence tag */
321 if( ( ret = asn1_get_tag( p, end, &len,
322 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200323 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200324
325 if( *p + len != end )
Paul Bakker51876562013-09-17 14:36:05 +0200326 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
328
329 while( *p < end )
330 {
331 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200332 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333 POLARSSL_ERR_ASN1_OUT_OF_DATA );
334
335 tag = **p;
336 (*p)++;
337 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200338 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339
340 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
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_UNEXPECTED_TAG );
343
344 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
345 {
346 *p += tag_len;
347 continue;
348 }
349
350 buf = &(cur->buf);
351 buf->tag = tag;
352 buf->p = *p;
353 buf->len = tag_len;
354 *p += buf->len;
355
356 /* Allocate and assign next pointer */
357 if (*p < end)
358 {
359 cur->next = (asn1_sequence *) polarssl_malloc(
360 sizeof( asn1_sequence ) );
361
362 if( cur->next == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200363 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 POLARSSL_ERR_ASN1_MALLOC_FAILED );
365
366 memset( cur->next, 0, sizeof( asn1_sequence ) );
367 cur = cur->next;
368 }
369 }
370
371 /* Set final sequence entry's next pointer to NULL */
372 cur->next = NULL;
373
374 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200375 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
377
378 return( 0 );
379}
380
381/*
382 * X.509 v3 extensions
383 *
384 * TODO: Perform all of the basic constraints tests required by the RFC
385 * TODO: Set values for undetected extensions to a sane default?
386 *
387 */
388static int x509_get_crt_ext( unsigned char **p,
389 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200390 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391{
392 int ret;
393 size_t len;
394 unsigned char *end_ext_data, *end_ext_octet;
395
396 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
397 {
398 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
399 return( 0 );
400
401 return( ret );
402 }
403
404 while( *p < end )
405 {
406 /*
407 * Extension ::= SEQUENCE {
408 * extnID OBJECT IDENTIFIER,
409 * critical BOOLEAN DEFAULT FALSE,
410 * extnValue OCTET STRING }
411 */
412 x509_buf extn_oid = {0, 0, NULL};
413 int is_critical = 0; /* DEFAULT FALSE */
414 int ext_type = 0;
415
416 if( ( ret = asn1_get_tag( p, end, &len,
417 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200418 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419
420 end_ext_data = *p + len;
421
422 /* Get extension ID */
423 extn_oid.tag = **p;
424
425 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200426 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427
428 extn_oid.p = *p;
429 *p += extn_oid.len;
430
431 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200432 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433 POLARSSL_ERR_ASN1_OUT_OF_DATA );
434
435 /* Get optional critical */
436 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
437 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200438 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439
440 /* Data should be octet string type */
441 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
442 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200443 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
445 end_ext_octet = *p + len;
446
447 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200448 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
450
451 /*
452 * Detect supported extensions
453 */
454 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
455
456 if( ret != 0 )
457 {
458 /* No parser found, skip extension */
459 *p = end_ext_octet;
460
461#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
462 if( is_critical )
463 {
464 /* Data is marked as critical: fail */
Paul Bakker51876562013-09-17 14:36:05 +0200465 return ( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
467 }
468#endif
469 continue;
470 }
471
472 crt->ext_types |= ext_type;
473
474 switch( ext_type )
475 {
476 case EXT_BASIC_CONSTRAINTS:
477 /* Parse basic constraints */
478 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
479 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
480 return ( ret );
481 break;
482
483 case EXT_KEY_USAGE:
484 /* Parse key usage */
485 if( ( ret = x509_get_key_usage( p, end_ext_octet,
486 &crt->key_usage ) ) != 0 )
487 return ( ret );
488 break;
489
490 case EXT_EXTENDED_KEY_USAGE:
491 /* Parse extended key usage */
492 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
493 &crt->ext_key_usage ) ) != 0 )
494 return ( ret );
495 break;
496
497 case EXT_SUBJECT_ALT_NAME:
498 /* Parse subject alt name */
499 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
500 &crt->subject_alt_names ) ) != 0 )
501 return ( ret );
502 break;
503
504 case EXT_NS_CERT_TYPE:
505 /* Parse netscape certificate type */
506 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
507 &crt->ns_cert_type ) ) != 0 )
508 return ( ret );
509 break;
510
511 default:
512 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
513 }
514 }
515
516 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200517 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
519
520 return( 0 );
521}
522
523/*
524 * Parse and fill a single X.509 certificate in DER format
525 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200526static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200527 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528{
529 int ret;
530 size_t len;
531 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnardb1d4eb12014-01-22 10:12:57 +0100532 x509_buf sig_params;
533
534 memset( &sig_params, 0, sizeof( x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535
536 /*
537 * Check for valid input
538 */
539 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200540 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541
542 p = (unsigned char *) polarssl_malloc( len = buflen );
543
544 if( p == NULL )
545 return( POLARSSL_ERR_X509_MALLOC_FAILED );
546
547 memcpy( p, buf, buflen );
548
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549 crt->raw.p = p;
550 crt->raw.len = len;
551 end = p + len;
552
553 /*
554 * Certificate ::= SEQUENCE {
555 * tbsCertificate TBSCertificate,
556 * signatureAlgorithm AlgorithmIdentifier,
557 * signatureValue BIT STRING }
558 */
559 if( ( ret = asn1_get_tag( &p, end, &len,
560 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
561 {
562 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200563 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564 }
565
566 if( len > (size_t) ( end - p ) )
567 {
568 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200569 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200570 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
571 }
572 crt_end = p + len;
573
574 /*
575 * TBSCertificate ::= SEQUENCE {
576 */
577 crt->tbs.p = p;
578
579 if( ( ret = asn1_get_tag( &p, end, &len,
580 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
581 {
582 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200583 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584 }
585
586 end = p + len;
587 crt->tbs.len = end - crt->tbs.p;
588
589 /*
590 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
591 *
592 * CertificateSerialNumber ::= INTEGER
593 *
594 * signature AlgorithmIdentifier
595 */
596 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
597 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
Manuel Pégourié-Gonnardb1d4eb12014-01-22 10:12:57 +0100598 ( ret = x509_get_alg( &p, end, &crt->sig_oid1,
599 &crt->sig_params ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200600 {
601 x509_crt_free( crt );
602 return( ret );
603 }
604
605 crt->version++;
606
607 if( crt->version > 3 )
608 {
609 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200610 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611 }
612
613 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
614 &crt->sig_pk ) ) != 0 )
615 {
616 x509_crt_free( crt );
617 return( ret );
618 }
619
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +0100620 if( crt->sig_pk == POLARSSL_PK_RSASSA_PSS )
621 {
622 int salt_len, trailer_field;
Manuel Pégourié-Gonnard3c1e8b52014-01-23 19:15:29 +0100623 md_type_t mgf_md;
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +0100624
Manuel Pégourié-Gonnard3c1e8b52014-01-23 19:15:29 +0100625 /* Make sure params are valid */
626 ret = x509_get_rsassa_pss_params( &crt->sig_params,
627 &crt->sig_md, &mgf_md, &salt_len, &trailer_field );
628 if( ret != 0 )
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +0100629 return( ret );
630 }
631 else
632 {
633 /* Make sure parameters were absent or NULL */
634 if( ( crt->sig_params.tag != ASN1_NULL && crt->sig_params.tag != 0 ) ||
635 crt->sig_params.len != 0 )
636 return( POLARSSL_ERR_X509_INVALID_ALG );
637 }
638
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639 /*
640 * issuer Name
641 */
642 crt->issuer_raw.p = p;
643
644 if( ( ret = asn1_get_tag( &p, end, &len,
645 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
646 {
647 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200648 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649 }
650
651 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
652 {
653 x509_crt_free( crt );
654 return( ret );
655 }
656
657 crt->issuer_raw.len = p - crt->issuer_raw.p;
658
659 /*
660 * Validity ::= SEQUENCE {
661 * notBefore Time,
662 * notAfter Time }
663 *
664 */
665 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
666 &crt->valid_to ) ) != 0 )
667 {
668 x509_crt_free( crt );
669 return( ret );
670 }
671
672 /*
673 * subject Name
674 */
675 crt->subject_raw.p = p;
676
677 if( ( ret = asn1_get_tag( &p, end, &len,
678 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
679 {
680 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200681 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682 }
683
684 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
685 {
686 x509_crt_free( crt );
687 return( ret );
688 }
689
690 crt->subject_raw.len = p - crt->subject_raw.p;
691
692 /*
693 * SubjectPublicKeyInfo
694 */
Paul Bakkerda771152013-09-16 22:45:03 +0200695 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696 {
697 x509_crt_free( crt );
698 return( ret );
699 }
700
701 /*
702 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
703 * -- If present, version shall be v2 or v3
704 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
705 * -- If present, version shall be v2 or v3
706 * extensions [3] EXPLICIT Extensions OPTIONAL
707 * -- If present, version shall be v3
708 */
709 if( crt->version == 2 || crt->version == 3 )
710 {
711 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
712 if( ret != 0 )
713 {
714 x509_crt_free( crt );
715 return( ret );
716 }
717 }
718
719 if( crt->version == 2 || crt->version == 3 )
720 {
721 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
722 if( ret != 0 )
723 {
724 x509_crt_free( crt );
725 return( ret );
726 }
727 }
728
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200729#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730 if( crt->version == 3 )
731 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200732#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733 ret = x509_get_crt_ext( &p, end, crt);
734 if( ret != 0 )
735 {
736 x509_crt_free( crt );
737 return( ret );
738 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200739#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200741#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200742
743 if( p != end )
744 {
745 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200746 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200747 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
748 }
749
750 end = crt_end;
751
752 /*
753 * }
754 * -- end of TBSCertificate
755 *
756 * signatureAlgorithm AlgorithmIdentifier,
757 * signatureValue BIT STRING
758 */
Manuel Pégourié-Gonnardb1d4eb12014-01-22 10:12:57 +0100759 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200760 {
761 x509_crt_free( crt );
762 return( ret );
763 }
764
765 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnardb1d4eb12014-01-22 10:12:57 +0100766 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 ||
767 crt->sig_params.len != sig_params.len ||
768 memcmp( crt->sig_params.p, sig_params.p, sig_params.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 {
770 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200771 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772 }
773
774 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
775 {
776 x509_crt_free( crt );
777 return( ret );
778 }
779
780 if( p != end )
781 {
782 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200783 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
785 }
786
787 return( 0 );
788}
789
790/*
791 * Parse one X.509 certificate in DER format from a buffer and add them to a
792 * chained list
793 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200794int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200795 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796{
797 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200798 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799
800 /*
801 * Check for valid input
802 */
803 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200804 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200805
806 while( crt->version != 0 && crt->next != NULL )
807 {
808 prev = crt;
809 crt = crt->next;
810 }
811
812 /*
813 * Add new certificate on the end of the chain if needed.
814 */
815 if ( crt->version != 0 && crt->next == NULL)
816 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200817 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818
819 if( crt->next == NULL )
820 return( POLARSSL_ERR_X509_MALLOC_FAILED );
821
822 prev = crt;
823 crt = crt->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200824 x509_crt_init( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200825 }
826
Paul Bakkerddf26b42013-09-18 13:46:23 +0200827 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200828 {
829 if( prev )
830 prev->next = NULL;
831
832 if( crt != chain )
833 polarssl_free( crt );
834
835 return( ret );
836 }
837
838 return( 0 );
839}
840
841/*
842 * Parse one or more PEM certificates from a buffer and add them to the chained list
843 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200844int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845{
846 int success = 0, first_error = 0, total_failed = 0;
847 int buf_format = X509_FORMAT_DER;
848
849 /*
850 * Check for valid input
851 */
852 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200853 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200854
855 /*
856 * Determine buffer content. Buffer contains either one DER certificate or
857 * one or more PEM certificates.
858 */
859#if defined(POLARSSL_PEM_PARSE_C)
860 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
861 buf_format = X509_FORMAT_PEM;
862#endif
863
864 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200865 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866
867#if defined(POLARSSL_PEM_PARSE_C)
868 if( buf_format == X509_FORMAT_PEM )
869 {
870 int ret;
871 pem_context pem;
872
873 while( buflen > 0 )
874 {
875 size_t use_len;
876 pem_init( &pem );
877
878 ret = pem_read_buffer( &pem,
879 "-----BEGIN CERTIFICATE-----",
880 "-----END CERTIFICATE-----",
881 buf, NULL, 0, &use_len );
882
883 if( ret == 0 )
884 {
885 /*
886 * Was PEM encoded
887 */
888 buflen -= use_len;
889 buf += use_len;
890 }
891 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
892 {
893 return( ret );
894 }
895 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
896 {
897 pem_free( &pem );
898
899 /*
900 * PEM header and footer were found
901 */
902 buflen -= use_len;
903 buf += use_len;
904
905 if( first_error == 0 )
906 first_error = ret;
907
908 continue;
909 }
910 else
911 break;
912
Paul Bakkerddf26b42013-09-18 13:46:23 +0200913 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914
915 pem_free( &pem );
916
917 if( ret != 0 )
918 {
919 /*
920 * Quit parsing on a memory error
921 */
922 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
923 return( ret );
924
925 if( first_error == 0 )
926 first_error = ret;
927
928 total_failed++;
929 continue;
930 }
931
932 success = 1;
933 }
934 }
935#endif
936
937 if( success )
938 return( total_failed );
939 else if( first_error )
940 return( first_error );
941 else
942 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
943}
944
945#if defined(POLARSSL_FS_IO)
946/*
947 * Load one or more certificates and add them to the chained list
948 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200949int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950{
951 int ret;
952 size_t n;
953 unsigned char *buf;
954
955 if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
956 return( ret );
957
Paul Bakkerddf26b42013-09-18 13:46:23 +0200958 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959
960 memset( buf, 0, n + 1 );
961 polarssl_free( buf );
962
963 return( ret );
964}
965
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100966#if defined(POLARSSL_THREADING_PTHREAD)
967static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
968#endif
969
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200970int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971{
972 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100973#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 int w_ret;
975 WCHAR szDir[MAX_PATH];
976 char filename[MAX_PATH];
977 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200978 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979
980 WIN32_FIND_DATAW file_data;
981 HANDLE hFind;
982
983 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200984 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985
986 memset( szDir, 0, sizeof(szDir) );
987 memset( filename, 0, MAX_PATH );
988 memcpy( filename, path, len );
989 filename[len++] = '\\';
990 p = filename + len;
991 filename[len++] = '*';
992
Paul Bakker1a56fc92013-12-19 13:51:24 +0100993 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir, MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994
995 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200996 if (hFind == INVALID_HANDLE_VALUE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
998
999 len = MAX_PATH - len;
1000 do
1001 {
1002 memset( p, 0, len );
1003
1004 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1005 continue;
1006
1007 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1008 lstrlenW(file_data.cFileName),
1009 p, len - 1,
1010 NULL, NULL );
1011
Paul Bakkerddf26b42013-09-18 13:46:23 +02001012 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 if( w_ret < 0 )
1014 ret++;
1015 else
1016 ret += w_ret;
1017 }
1018 while( FindNextFileW( hFind, &file_data ) != 0 );
1019
Paul Bakker4aa40d42013-10-11 10:49:24 +02001020 if (GetLastError() != ERROR_NO_MORE_FILES)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1022
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001024#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001025 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001027 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 char entry_name[255];
1029 DIR *dir = opendir( path );
1030
1031 if( dir == NULL)
1032 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1033
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001034#if defined(POLARSSL_THREADING_PTHREAD)
1035 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1036 return( ret );
1037#endif
1038
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001039 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001041 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001043 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 {
1045 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001046 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1047 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 }
1049
1050 if( !S_ISREG( sb.st_mode ) )
1051 continue;
1052
1053 // Ignore parse errors
1054 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001055 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056 if( t_ret < 0 )
1057 ret++;
1058 else
1059 ret += t_ret;
1060 }
1061 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001062
1063cleanup:
1064#if defined(POLARSSL_THREADING_PTHREAD)
1065 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1066 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1067#endif
1068
Paul Bakkerbe089b02013-10-14 15:51:50 +02001069#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070
1071 return( ret );
1072}
1073#endif /* POLARSSL_FS_IO */
1074
Paul Bakker6edcd412013-10-29 15:22:54 +01001075#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1076 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077#include <stdarg.h>
1078
1079#if !defined vsnprintf
1080#define vsnprintf _vsnprintf
1081#endif // vsnprintf
1082
1083/*
1084 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1085 * Result value is not size of buffer needed, but -1 if no fit is possible.
1086 *
1087 * This fuction tries to 'fix' this by at least suggesting enlarging the
1088 * size by 20.
1089 */
1090static int compat_snprintf(char *str, size_t size, const char *format, ...)
1091{
1092 va_list ap;
1093 int res = -1;
1094
1095 va_start( ap, format );
1096
1097 res = vsnprintf( str, size, format, ap );
1098
1099 va_end( ap );
1100
1101 // No quick fix possible
1102 if ( res < 0 )
1103 return( (int) size + 20 );
1104
1105 return res;
1106}
1107
1108#define snprintf compat_snprintf
1109#endif
1110
1111#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1112
1113#define SAFE_SNPRINTF() \
1114{ \
1115 if( ret == -1 ) \
1116 return( -1 ); \
1117 \
1118 if ( (unsigned int) ret > n ) { \
1119 p[n - 1] = '\0'; \
1120 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1121 } \
1122 \
1123 n -= (unsigned int) ret; \
1124 p += (unsigned int) ret; \
1125}
1126
1127/*
1128 * Return an informational string about the certificate.
1129 */
1130#define BEFORE_COLON 14
1131#define BC "14"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001132int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001133 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001134{
1135 int ret;
1136 size_t n;
1137 char *p;
1138 const char *desc = NULL;
1139 char key_size_str[BEFORE_COLON];
1140
1141 p = buf;
1142 n = size;
1143
1144 ret = snprintf( p, n, "%scert. version : %d\n",
1145 prefix, crt->version );
1146 SAFE_SNPRINTF();
1147 ret = snprintf( p, n, "%sserial number : ",
1148 prefix );
1149 SAFE_SNPRINTF();
1150
Paul Bakker86d0c192013-09-18 11:11:02 +02001151 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152 SAFE_SNPRINTF();
1153
1154 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
1155 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001156 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 SAFE_SNPRINTF();
1158
1159 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
1160 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001161 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162 SAFE_SNPRINTF();
1163
1164 ret = snprintf( p, n, "\n%sissued on : " \
1165 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1166 crt->valid_from.year, crt->valid_from.mon,
1167 crt->valid_from.day, crt->valid_from.hour,
1168 crt->valid_from.min, crt->valid_from.sec );
1169 SAFE_SNPRINTF();
1170
1171 ret = snprintf( p, n, "\n%sexpires on : " \
1172 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1173 crt->valid_to.year, crt->valid_to.mon,
1174 crt->valid_to.day, crt->valid_to.hour,
1175 crt->valid_to.min, crt->valid_to.sec );
1176 SAFE_SNPRINTF();
1177
1178 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
1179 SAFE_SNPRINTF();
1180
1181 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1182 if( ret != 0 )
1183 ret = snprintf( p, n, "???" );
1184 else
1185 ret = snprintf( p, n, "%s", desc );
1186 SAFE_SNPRINTF();
1187
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +01001188 if( crt->sig_pk == POLARSSL_PK_RSASSA_PSS )
1189 {
Manuel Pégourié-Gonnard3c1e8b52014-01-23 19:15:29 +01001190 md_type_t md_alg, mgf_md;
1191 const md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +01001192 int salt_len, trailer_field;
1193
1194 if( ( ret = x509_get_rsassa_pss_params( &crt->sig_params,
Manuel Pégourié-Gonnard3c1e8b52014-01-23 19:15:29 +01001195 &md_alg, &mgf_md, &salt_len, &trailer_field ) ) != 0 )
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +01001196 return( ret );
1197
Manuel Pégourié-Gonnard3c1e8b52014-01-23 19:15:29 +01001198 md_info = md_info_from_type( md_alg );
1199 mgf_md_info = md_info_from_type( mgf_md );
1200
1201 ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X, %d)",
1202 md_info ? md_info->name : "???",
1203 mgf_md_info ? mgf_md_info->name : "???",
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +01001204 salt_len, trailer_field );
1205 SAFE_SNPRINTF();
1206 }
1207
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001208 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1209 pk_get_name( &crt->pk ) ) ) != 0 )
1210 {
1211 return( ret );
1212 }
1213
1214 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
1215 (int) pk_get_size( &crt->pk ) );
1216 SAFE_SNPRINTF();
1217
1218 return( (int) ( size - n ) );
1219}
1220
1221#if defined(POLARSSL_X509_CRL_PARSE_C)
1222/*
1223 * Return 1 if the certificate is revoked, or 0 otherwise.
1224 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001225int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001226{
1227 const x509_crl_entry *cur = &crl->entry;
1228
1229 while( cur != NULL && cur->serial.len != 0 )
1230 {
1231 if( crt->serial.len == cur->serial.len &&
1232 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1233 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001234 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235 return( 1 );
1236 }
1237
1238 cur = cur->next;
1239 }
1240
1241 return( 0 );
1242}
1243
1244/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001245 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001246 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001247static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001248 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001249{
1250 int flags = 0;
1251 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1252 const md_info_t *md_info;
1253
1254 if( ca == NULL )
1255 return( flags );
1256
1257 /*
1258 * TODO: What happens if no CRL is present?
1259 * Suggestion: Revocation state should be unknown if no CRL is present.
1260 * For backwards compatibility this is not yet implemented.
1261 */
1262
1263 while( crl_list != NULL )
1264 {
1265 if( crl_list->version == 0 ||
1266 crl_list->issuer_raw.len != ca->subject_raw.len ||
1267 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1268 crl_list->issuer_raw.len ) != 0 )
1269 {
1270 crl_list = crl_list->next;
1271 continue;
1272 }
1273
1274 /*
1275 * Check if CRL is correctly signed by the trusted CA
1276 */
1277 md_info = md_info_from_type( crl_list->sig_md );
1278 if( md_info == NULL )
1279 {
1280 /*
1281 * Cannot check 'unknown' hash
1282 */
1283 flags |= BADCRL_NOT_TRUSTED;
1284 break;
1285 }
1286
1287 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1288
1289 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1290 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1291 crl_list->sig.p, crl_list->sig.len ) != 0 )
1292 {
1293 flags |= BADCRL_NOT_TRUSTED;
1294 break;
1295 }
1296
1297 /*
1298 * Check for validity of CRL (Do not drop out)
1299 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001300 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001301 flags |= BADCRL_EXPIRED;
1302
1303 /*
1304 * Check if certificate is revoked
1305 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001306 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 {
1308 flags |= BADCERT_REVOKED;
1309 break;
1310 }
1311
1312 crl_list = crl_list->next;
1313 }
1314 return flags;
1315}
1316#endif /* POLARSSL_X509_CRL_PARSE_C */
1317
1318// Equal == 0, inequal == 1
1319static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1320{
1321 size_t i;
1322 unsigned char diff;
1323 const unsigned char *n1 = s1, *n2 = s2;
1324
1325 for( i = 0; i < len; i++ )
1326 {
1327 diff = n1[i] ^ n2[i];
1328
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001329 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001330 continue;
1331
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001332 if( diff == 32 &&
1333 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1334 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1335 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001336 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001337 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001338
1339 return( 1 );
1340 }
1341
1342 return( 0 );
1343}
1344
1345static int x509_wildcard_verify( const char *cn, x509_buf *name )
1346{
1347 size_t i;
1348 size_t cn_idx = 0;
1349
1350 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1351 return( 0 );
1352
1353 for( i = 0; i < strlen( cn ); ++i )
1354 {
1355 if( cn[i] == '.' )
1356 {
1357 cn_idx = i;
1358 break;
1359 }
1360 }
1361
1362 if( cn_idx == 0 )
1363 return( 0 );
1364
1365 if( strlen( cn ) - cn_idx == name->len - 1 &&
1366 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1367 {
1368 return( 1 );
1369 }
1370
1371 return( 0 );
1372}
1373
Paul Bakkerddf26b42013-09-18 13:46:23 +02001374static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001375 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001377 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378 void *p_vrfy )
1379{
1380 int ret;
1381 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1382 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1383 const md_info_t *md_info;
1384
Paul Bakker86d0c192013-09-18 11:11:02 +02001385 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001386 *flags |= BADCERT_EXPIRED;
1387
1388 /*
1389 * Child is the top of the chain. Check against the trust_ca list.
1390 */
1391 *flags |= BADCERT_NOT_TRUSTED;
1392
1393 md_info = md_info_from_type( child->sig_md );
1394 if( md_info == NULL )
1395 {
1396 /*
1397 * Cannot check 'unknown', no need to try any CA
1398 */
1399 trust_ca = NULL;
1400 }
1401 else
1402 md( md_info, child->tbs.p, child->tbs.len, hash );
1403
1404 while( trust_ca != NULL )
1405 {
1406 if( trust_ca->version == 0 ||
1407 child->issuer_raw.len != trust_ca->subject_raw.len ||
1408 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
1409 child->issuer_raw.len ) != 0 )
1410 {
1411 trust_ca = trust_ca->next;
1412 continue;
1413 }
1414
1415 /*
1416 * Reduce path_len to check against if top of the chain is
1417 * the same as the trusted CA
1418 */
1419 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1420 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1421 child->issuer_raw.len ) == 0 )
1422 {
1423 check_path_cnt--;
1424 }
1425
1426 if( trust_ca->max_pathlen > 0 &&
1427 trust_ca->max_pathlen < check_path_cnt )
1428 {
1429 trust_ca = trust_ca->next;
1430 continue;
1431 }
1432
1433 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1434 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1435 child->sig.p, child->sig.len ) != 0 )
1436 {
1437 trust_ca = trust_ca->next;
1438 continue;
1439 }
1440
1441 /*
1442 * Top of chain is signed by a trusted CA
1443 */
1444 *flags &= ~BADCERT_NOT_TRUSTED;
1445 break;
1446 }
1447
1448 /*
1449 * If top of chain is not the same as the trusted CA send a verify request
1450 * to the callback for any issues with validity and CRL presence for the
1451 * trusted CA certificate.
1452 */
1453 if( trust_ca != NULL &&
1454 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1455 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1456 child->issuer_raw.len ) != 0 ) )
1457 {
1458#if defined(POLARSSL_X509_CRL_PARSE_C)
1459 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001460 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001461#else
1462 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463#endif
1464
Paul Bakker86d0c192013-09-18 11:11:02 +02001465 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466 ca_flags |= BADCERT_EXPIRED;
1467
1468 if( NULL != f_vrfy )
1469 {
1470 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
1471 return( ret );
1472 }
1473 }
1474
1475 /* Call callback on top cert */
1476 if( NULL != f_vrfy )
1477 {
1478 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1479 return( ret );
1480 }
1481
1482 *flags |= ca_flags;
1483
1484 return( 0 );
1485}
1486
Paul Bakkerddf26b42013-09-18 13:46:23 +02001487static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001488 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001489 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001490 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491 void *p_vrfy )
1492{
1493 int ret;
1494 int parent_flags = 0;
1495 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001496 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001497 const md_info_t *md_info;
1498
Paul Bakker86d0c192013-09-18 11:11:02 +02001499 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001500 *flags |= BADCERT_EXPIRED;
1501
1502 md_info = md_info_from_type( child->sig_md );
1503 if( md_info == NULL )
1504 {
1505 /*
1506 * Cannot check 'unknown' hash
1507 */
1508 *flags |= BADCERT_NOT_TRUSTED;
1509 }
1510 else
1511 {
1512 md( md_info, child->tbs.p, child->tbs.len, hash );
1513
1514 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1515 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1516 child->sig.p, child->sig.len ) != 0 )
1517 {
1518 *flags |= BADCERT_NOT_TRUSTED;
1519 }
1520 }
1521
1522#if defined(POLARSSL_X509_CRL_PARSE_C)
1523 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001524 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001525#endif
1526
1527 grandparent = parent->next;
1528
1529 while( grandparent != NULL )
1530 {
1531 if( grandparent->version == 0 ||
1532 grandparent->ca_istrue == 0 ||
1533 parent->issuer_raw.len != grandparent->subject_raw.len ||
1534 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
1535 parent->issuer_raw.len ) != 0 )
1536 {
1537 grandparent = grandparent->next;
1538 continue;
1539 }
1540 break;
1541 }
1542
1543 if( grandparent != NULL )
1544 {
1545 /*
1546 * Part of the chain
1547 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001548 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549 if( ret != 0 )
1550 return( ret );
1551 }
1552 else
1553 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001554 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555 if( ret != 0 )
1556 return( ret );
1557 }
1558
1559 /* child is verified to be a child of the parent, call verify callback */
1560 if( NULL != f_vrfy )
1561 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1562 return( ret );
1563
1564 *flags |= parent_flags;
1565
1566 return( 0 );
1567}
1568
1569/*
1570 * Verify the certificate validity
1571 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001572int x509_crt_verify( x509_crt *crt,
1573 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001574 x509_crl *ca_crl,
1575 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001576 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001577 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578{
1579 size_t cn_len;
1580 int ret;
1581 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001582 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583 x509_name *name;
1584 x509_sequence *cur = NULL;
1585
1586 *flags = 0;
1587
1588 if( cn != NULL )
1589 {
1590 name = &crt->subject;
1591 cn_len = strlen( cn );
1592
1593 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1594 {
1595 cur = &crt->subject_alt_names;
1596
1597 while( cur != NULL )
1598 {
1599 if( cur->buf.len == cn_len &&
1600 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1601 break;
1602
1603 if( cur->buf.len > 2 &&
1604 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1605 x509_wildcard_verify( cn, &cur->buf ) )
1606 break;
1607
1608 cur = cur->next;
1609 }
1610
1611 if( cur == NULL )
1612 *flags |= BADCERT_CN_MISMATCH;
1613 }
1614 else
1615 {
1616 while( name != NULL )
1617 {
1618 if( OID_CMP( OID_AT_CN, &name->oid ) )
1619 {
1620 if( name->val.len == cn_len &&
1621 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1622 break;
1623
1624 if( name->val.len > 2 &&
1625 memcmp( name->val.p, "*.", 2 ) == 0 &&
1626 x509_wildcard_verify( cn, &name->val ) )
1627 break;
1628 }
1629
1630 name = name->next;
1631 }
1632
1633 if( name == NULL )
1634 *flags |= BADCERT_CN_MISMATCH;
1635 }
1636 }
1637
1638 /*
1639 * Iterate upwards in the given cert chain, to find our crt parent.
1640 * Ignore any upper cert with CA != TRUE.
1641 */
1642 parent = crt->next;
1643
1644 while( parent != NULL && parent->version != 0 )
1645 {
1646 if( parent->ca_istrue == 0 ||
1647 crt->issuer_raw.len != parent->subject_raw.len ||
1648 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
1649 crt->issuer_raw.len ) != 0 )
1650 {
1651 parent = parent->next;
1652 continue;
1653 }
1654 break;
1655 }
1656
1657 if( parent != NULL )
1658 {
1659 /*
1660 * Part of the chain
1661 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001662 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001663 if( ret != 0 )
1664 return( ret );
1665 }
1666 else
1667 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001668 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001669 if( ret != 0 )
1670 return( ret );
1671 }
1672
1673 if( *flags != 0 )
1674 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1675
1676 return( 0 );
1677}
1678
1679/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001680 * Initialize a certificate chain
1681 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001682void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001683{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001684 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001685}
1686
1687/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001688 * Unallocate all certificate data
1689 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001690void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001691{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001692 x509_crt *cert_cur = crt;
1693 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001694 x509_name *name_cur;
1695 x509_name *name_prv;
1696 x509_sequence *seq_cur;
1697 x509_sequence *seq_prv;
1698
1699 if( crt == NULL )
1700 return;
1701
1702 do
1703 {
1704 pk_free( &cert_cur->pk );
1705
1706 name_cur = cert_cur->issuer.next;
1707 while( name_cur != NULL )
1708 {
1709 name_prv = name_cur;
1710 name_cur = name_cur->next;
1711 memset( name_prv, 0, sizeof( x509_name ) );
1712 polarssl_free( name_prv );
1713 }
1714
1715 name_cur = cert_cur->subject.next;
1716 while( name_cur != NULL )
1717 {
1718 name_prv = name_cur;
1719 name_cur = name_cur->next;
1720 memset( name_prv, 0, sizeof( x509_name ) );
1721 polarssl_free( name_prv );
1722 }
1723
1724 seq_cur = cert_cur->ext_key_usage.next;
1725 while( seq_cur != NULL )
1726 {
1727 seq_prv = seq_cur;
1728 seq_cur = seq_cur->next;
1729 memset( seq_prv, 0, sizeof( x509_sequence ) );
1730 polarssl_free( seq_prv );
1731 }
1732
1733 seq_cur = cert_cur->subject_alt_names.next;
1734 while( seq_cur != NULL )
1735 {
1736 seq_prv = seq_cur;
1737 seq_cur = seq_cur->next;
1738 memset( seq_prv, 0, sizeof( x509_sequence ) );
1739 polarssl_free( seq_prv );
1740 }
1741
1742 if( cert_cur->raw.p != NULL )
1743 {
1744 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1745 polarssl_free( cert_cur->raw.p );
1746 }
1747
1748 cert_cur = cert_cur->next;
1749 }
1750 while( cert_cur != NULL );
1751
1752 cert_cur = crt;
1753 do
1754 {
1755 cert_prv = cert_cur;
1756 cert_cur = cert_cur->next;
1757
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001758 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001759 if( cert_prv != crt )
1760 polarssl_free( cert_prv );
1761 }
1762 while( cert_cur != NULL );
1763}
1764
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001765#endif