blob: dc71c1405e95f22b1174b424bdd449275104c96c [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,
Manuel Pégourié-Gonnardce7c6fd2014-01-24 14:37:29 +0100599 &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é-Gonnardce7c6fd2014-01-24 14:37:29 +0100620#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +0100621 if( crt->sig_pk == POLARSSL_PK_RSASSA_PSS )
622 {
623 int salt_len, trailer_field;
Manuel Pégourié-Gonnard3c1e8b52014-01-23 19:15:29 +0100624 md_type_t mgf_md;
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +0100625
Manuel Pégourié-Gonnard3c1e8b52014-01-23 19:15:29 +0100626 /* Make sure params are valid */
Manuel Pégourié-Gonnardce7c6fd2014-01-24 14:37:29 +0100627 ret = x509_get_rsassa_pss_params( &sig_params,
Manuel Pégourié-Gonnard3c1e8b52014-01-23 19:15:29 +0100628 &crt->sig_md, &mgf_md, &salt_len, &trailer_field );
629 if( ret != 0 )
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +0100630 return( ret );
Manuel Pégourié-Gonnardce7c6fd2014-01-24 14:37:29 +0100631
632 memcpy( &crt->sig_params, &sig_params, sizeof( x509_buf ) );
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +0100633 }
634 else
Manuel Pégourié-Gonnardce7c6fd2014-01-24 14:37:29 +0100635#endif
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +0100636 {
Manuel Pégourié-Gonnardce7c6fd2014-01-24 14:37:29 +0100637 /* Make sure parameters are absent or NULL */
638 if( ( sig_params.tag != ASN1_NULL && sig_params.tag != 0 ) ||
639 sig_params.len != 0 )
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +0100640 return( POLARSSL_ERR_X509_INVALID_ALG );
641 }
642
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643 /*
644 * issuer Name
645 */
646 crt->issuer_raw.p = p;
647
648 if( ( ret = asn1_get_tag( &p, end, &len,
649 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
650 {
651 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200652 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653 }
654
655 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
656 {
657 x509_crt_free( crt );
658 return( ret );
659 }
660
661 crt->issuer_raw.len = p - crt->issuer_raw.p;
662
663 /*
664 * Validity ::= SEQUENCE {
665 * notBefore Time,
666 * notAfter Time }
667 *
668 */
669 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
670 &crt->valid_to ) ) != 0 )
671 {
672 x509_crt_free( crt );
673 return( ret );
674 }
675
676 /*
677 * subject Name
678 */
679 crt->subject_raw.p = p;
680
681 if( ( ret = asn1_get_tag( &p, end, &len,
682 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
683 {
684 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200685 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686 }
687
688 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
689 {
690 x509_crt_free( crt );
691 return( ret );
692 }
693
694 crt->subject_raw.len = p - crt->subject_raw.p;
695
696 /*
697 * SubjectPublicKeyInfo
698 */
Paul Bakkerda771152013-09-16 22:45:03 +0200699 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700 {
701 x509_crt_free( crt );
702 return( ret );
703 }
704
705 /*
706 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
707 * -- If present, version shall be v2 or v3
708 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
709 * -- If present, version shall be v2 or v3
710 * extensions [3] EXPLICIT Extensions OPTIONAL
711 * -- If present, version shall be v3
712 */
713 if( crt->version == 2 || crt->version == 3 )
714 {
715 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
716 if( ret != 0 )
717 {
718 x509_crt_free( crt );
719 return( ret );
720 }
721 }
722
723 if( crt->version == 2 || crt->version == 3 )
724 {
725 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
726 if( ret != 0 )
727 {
728 x509_crt_free( crt );
729 return( ret );
730 }
731 }
732
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200733#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200734 if( crt->version == 3 )
735 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200736#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737 ret = x509_get_crt_ext( &p, end, crt);
738 if( ret != 0 )
739 {
740 x509_crt_free( crt );
741 return( ret );
742 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200743#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200744 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200745#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746
747 if( p != end )
748 {
749 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200750 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200751 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
752 }
753
754 end = crt_end;
755
756 /*
757 * }
758 * -- end of TBSCertificate
759 *
760 * signatureAlgorithm AlgorithmIdentifier,
761 * signatureValue BIT STRING
762 */
Manuel Pégourié-Gonnardb1d4eb12014-01-22 10:12:57 +0100763 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764 {
765 x509_crt_free( crt );
766 return( ret );
767 }
768
769 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnardce7c6fd2014-01-24 14:37:29 +0100770 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0
771#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
772 ||
Manuel Pégourié-Gonnardb1d4eb12014-01-22 10:12:57 +0100773 crt->sig_params.len != sig_params.len ||
Manuel Pégourié-Gonnardce7c6fd2014-01-24 14:37:29 +0100774 memcmp( crt->sig_params.p, sig_params.p, sig_params.len ) != 0
775#endif
776 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777 {
778 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200779 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780 }
781
782 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
783 {
784 x509_crt_free( crt );
785 return( ret );
786 }
787
788 if( p != end )
789 {
790 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200791 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
793 }
794
795 return( 0 );
796}
797
798/*
799 * Parse one X.509 certificate in DER format from a buffer and add them to a
800 * chained list
801 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200802int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200803 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804{
805 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200806 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807
808 /*
809 * Check for valid input
810 */
811 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200812 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813
814 while( crt->version != 0 && crt->next != NULL )
815 {
816 prev = crt;
817 crt = crt->next;
818 }
819
820 /*
821 * Add new certificate on the end of the chain if needed.
822 */
823 if ( crt->version != 0 && crt->next == NULL)
824 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200825 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826
827 if( crt->next == NULL )
828 return( POLARSSL_ERR_X509_MALLOC_FAILED );
829
830 prev = crt;
831 crt = crt->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200832 x509_crt_init( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200833 }
834
Paul Bakkerddf26b42013-09-18 13:46:23 +0200835 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836 {
837 if( prev )
838 prev->next = NULL;
839
840 if( crt != chain )
841 polarssl_free( crt );
842
843 return( ret );
844 }
845
846 return( 0 );
847}
848
849/*
850 * Parse one or more PEM certificates from a buffer and add them to the chained list
851 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200852int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200853{
854 int success = 0, first_error = 0, total_failed = 0;
855 int buf_format = X509_FORMAT_DER;
856
857 /*
858 * Check for valid input
859 */
860 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200861 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200862
863 /*
864 * Determine buffer content. Buffer contains either one DER certificate or
865 * one or more PEM certificates.
866 */
867#if defined(POLARSSL_PEM_PARSE_C)
868 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
869 buf_format = X509_FORMAT_PEM;
870#endif
871
872 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200873 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874
875#if defined(POLARSSL_PEM_PARSE_C)
876 if( buf_format == X509_FORMAT_PEM )
877 {
878 int ret;
879 pem_context pem;
880
881 while( buflen > 0 )
882 {
883 size_t use_len;
884 pem_init( &pem );
885
886 ret = pem_read_buffer( &pem,
887 "-----BEGIN CERTIFICATE-----",
888 "-----END CERTIFICATE-----",
889 buf, NULL, 0, &use_len );
890
891 if( ret == 0 )
892 {
893 /*
894 * Was PEM encoded
895 */
896 buflen -= use_len;
897 buf += use_len;
898 }
899 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
900 {
901 return( ret );
902 }
903 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
904 {
905 pem_free( &pem );
906
907 /*
908 * PEM header and footer were found
909 */
910 buflen -= use_len;
911 buf += use_len;
912
913 if( first_error == 0 )
914 first_error = ret;
915
916 continue;
917 }
918 else
919 break;
920
Paul Bakkerddf26b42013-09-18 13:46:23 +0200921 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922
923 pem_free( &pem );
924
925 if( ret != 0 )
926 {
927 /*
928 * Quit parsing on a memory error
929 */
930 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
931 return( ret );
932
933 if( first_error == 0 )
934 first_error = ret;
935
936 total_failed++;
937 continue;
938 }
939
940 success = 1;
941 }
942 }
943#endif
944
945 if( success )
946 return( total_failed );
947 else if( first_error )
948 return( first_error );
949 else
950 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
951}
952
953#if defined(POLARSSL_FS_IO)
954/*
955 * Load one or more certificates and add them to the chained list
956 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200957int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958{
959 int ret;
960 size_t n;
961 unsigned char *buf;
962
963 if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
964 return( ret );
965
Paul Bakkerddf26b42013-09-18 13:46:23 +0200966 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967
968 memset( buf, 0, n + 1 );
969 polarssl_free( buf );
970
971 return( ret );
972}
973
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100974#if defined(POLARSSL_THREADING_PTHREAD)
975static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
976#endif
977
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200978int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979{
980 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100981#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 int w_ret;
983 WCHAR szDir[MAX_PATH];
984 char filename[MAX_PATH];
985 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200986 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987
988 WIN32_FIND_DATAW file_data;
989 HANDLE hFind;
990
991 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200992 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993
994 memset( szDir, 0, sizeof(szDir) );
995 memset( filename, 0, MAX_PATH );
996 memcpy( filename, path, len );
997 filename[len++] = '\\';
998 p = filename + len;
999 filename[len++] = '*';
1000
Paul Bakker1a56fc92013-12-19 13:51:24 +01001001 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir, MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002
1003 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker4aa40d42013-10-11 10:49:24 +02001004 if (hFind == INVALID_HANDLE_VALUE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1006
1007 len = MAX_PATH - len;
1008 do
1009 {
1010 memset( p, 0, len );
1011
1012 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1013 continue;
1014
1015 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1016 lstrlenW(file_data.cFileName),
1017 p, len - 1,
1018 NULL, NULL );
1019
Paul Bakkerddf26b42013-09-18 13:46:23 +02001020 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 if( w_ret < 0 )
1022 ret++;
1023 else
1024 ret += w_ret;
1025 }
1026 while( FindNextFileW( hFind, &file_data ) != 0 );
1027
Paul Bakker4aa40d42013-10-11 10:49:24 +02001028 if (GetLastError() != ERROR_NO_MORE_FILES)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1030
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001032#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001033 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001035 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036 char entry_name[255];
1037 DIR *dir = opendir( path );
1038
1039 if( dir == NULL)
1040 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1041
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001042#if defined(POLARSSL_THREADING_PTHREAD)
1043 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1044 return( ret );
1045#endif
1046
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001047 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001049 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001051 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001052 {
1053 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001054 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1055 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056 }
1057
1058 if( !S_ISREG( sb.st_mode ) )
1059 continue;
1060
1061 // Ignore parse errors
1062 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001063 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001064 if( t_ret < 0 )
1065 ret++;
1066 else
1067 ret += t_ret;
1068 }
1069 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001070
1071cleanup:
1072#if defined(POLARSSL_THREADING_PTHREAD)
1073 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1074 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1075#endif
1076
Paul Bakkerbe089b02013-10-14 15:51:50 +02001077#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078
1079 return( ret );
1080}
1081#endif /* POLARSSL_FS_IO */
1082
Paul Bakker6edcd412013-10-29 15:22:54 +01001083#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1084 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085#include <stdarg.h>
1086
1087#if !defined vsnprintf
1088#define vsnprintf _vsnprintf
1089#endif // vsnprintf
1090
1091/*
1092 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1093 * Result value is not size of buffer needed, but -1 if no fit is possible.
1094 *
1095 * This fuction tries to 'fix' this by at least suggesting enlarging the
1096 * size by 20.
1097 */
1098static int compat_snprintf(char *str, size_t size, const char *format, ...)
1099{
1100 va_list ap;
1101 int res = -1;
1102
1103 va_start( ap, format );
1104
1105 res = vsnprintf( str, size, format, ap );
1106
1107 va_end( ap );
1108
1109 // No quick fix possible
1110 if ( res < 0 )
1111 return( (int) size + 20 );
1112
1113 return res;
1114}
1115
1116#define snprintf compat_snprintf
1117#endif
1118
1119#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1120
1121#define SAFE_SNPRINTF() \
1122{ \
1123 if( ret == -1 ) \
1124 return( -1 ); \
1125 \
1126 if ( (unsigned int) ret > n ) { \
1127 p[n - 1] = '\0'; \
1128 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1129 } \
1130 \
1131 n -= (unsigned int) ret; \
1132 p += (unsigned int) ret; \
1133}
1134
1135/*
1136 * Return an informational string about the certificate.
1137 */
1138#define BEFORE_COLON 14
1139#define BC "14"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001140int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001141 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142{
1143 int ret;
1144 size_t n;
1145 char *p;
1146 const char *desc = NULL;
1147 char key_size_str[BEFORE_COLON];
1148
1149 p = buf;
1150 n = size;
1151
1152 ret = snprintf( p, n, "%scert. version : %d\n",
1153 prefix, crt->version );
1154 SAFE_SNPRINTF();
1155 ret = snprintf( p, n, "%sserial number : ",
1156 prefix );
1157 SAFE_SNPRINTF();
1158
Paul Bakker86d0c192013-09-18 11:11:02 +02001159 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160 SAFE_SNPRINTF();
1161
1162 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
1163 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001164 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001165 SAFE_SNPRINTF();
1166
1167 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
1168 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001169 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170 SAFE_SNPRINTF();
1171
1172 ret = snprintf( p, n, "\n%sissued on : " \
1173 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1174 crt->valid_from.year, crt->valid_from.mon,
1175 crt->valid_from.day, crt->valid_from.hour,
1176 crt->valid_from.min, crt->valid_from.sec );
1177 SAFE_SNPRINTF();
1178
1179 ret = snprintf( p, n, "\n%sexpires on : " \
1180 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1181 crt->valid_to.year, crt->valid_to.mon,
1182 crt->valid_to.day, crt->valid_to.hour,
1183 crt->valid_to.min, crt->valid_to.sec );
1184 SAFE_SNPRINTF();
1185
1186 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
1187 SAFE_SNPRINTF();
1188
1189 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1190 if( ret != 0 )
1191 ret = snprintf( p, n, "???" );
1192 else
1193 ret = snprintf( p, n, "%s", desc );
1194 SAFE_SNPRINTF();
1195
Manuel Pégourié-Gonnardce7c6fd2014-01-24 14:37:29 +01001196#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +01001197 if( crt->sig_pk == POLARSSL_PK_RSASSA_PSS )
1198 {
Manuel Pégourié-Gonnard3c1e8b52014-01-23 19:15:29 +01001199 md_type_t md_alg, mgf_md;
1200 const md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +01001201 int salt_len, trailer_field;
1202
1203 if( ( ret = x509_get_rsassa_pss_params( &crt->sig_params,
Manuel Pégourié-Gonnard3c1e8b52014-01-23 19:15:29 +01001204 &md_alg, &mgf_md, &salt_len, &trailer_field ) ) != 0 )
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +01001205 return( ret );
1206
Manuel Pégourié-Gonnard3c1e8b52014-01-23 19:15:29 +01001207 md_info = md_info_from_type( md_alg );
1208 mgf_md_info = md_info_from_type( mgf_md );
1209
1210 ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X, %d)",
1211 md_info ? md_info->name : "???",
1212 mgf_md_info ? mgf_md_info->name : "???",
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +01001213 salt_len, trailer_field );
1214 SAFE_SNPRINTF();
1215 }
Manuel Pégourié-Gonnardce7c6fd2014-01-24 14:37:29 +01001216#endif /* POLARSSL_RSASSA_PSS_CERTIFICATES */
Manuel Pégourié-Gonnardd9fd87b2014-01-23 16:24:44 +01001217
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001218 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1219 pk_get_name( &crt->pk ) ) ) != 0 )
1220 {
1221 return( ret );
1222 }
1223
1224 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
1225 (int) pk_get_size( &crt->pk ) );
1226 SAFE_SNPRINTF();
1227
1228 return( (int) ( size - n ) );
1229}
1230
1231#if defined(POLARSSL_X509_CRL_PARSE_C)
1232/*
1233 * Return 1 if the certificate is revoked, or 0 otherwise.
1234 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001235int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001236{
1237 const x509_crl_entry *cur = &crl->entry;
1238
1239 while( cur != NULL && cur->serial.len != 0 )
1240 {
1241 if( crt->serial.len == cur->serial.len &&
1242 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1243 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001244 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245 return( 1 );
1246 }
1247
1248 cur = cur->next;
1249 }
1250
1251 return( 0 );
1252}
1253
1254/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001255 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001256 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001257static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001258 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001259{
1260 int flags = 0;
1261 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1262 const md_info_t *md_info;
1263
1264 if( ca == NULL )
1265 return( flags );
1266
1267 /*
1268 * TODO: What happens if no CRL is present?
1269 * Suggestion: Revocation state should be unknown if no CRL is present.
1270 * For backwards compatibility this is not yet implemented.
1271 */
1272
1273 while( crl_list != NULL )
1274 {
1275 if( crl_list->version == 0 ||
1276 crl_list->issuer_raw.len != ca->subject_raw.len ||
1277 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1278 crl_list->issuer_raw.len ) != 0 )
1279 {
1280 crl_list = crl_list->next;
1281 continue;
1282 }
1283
1284 /*
1285 * Check if CRL is correctly signed by the trusted CA
1286 */
1287 md_info = md_info_from_type( crl_list->sig_md );
1288 if( md_info == NULL )
1289 {
1290 /*
1291 * Cannot check 'unknown' hash
1292 */
1293 flags |= BADCRL_NOT_TRUSTED;
1294 break;
1295 }
1296
1297 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1298
1299 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1300 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1301 crl_list->sig.p, crl_list->sig.len ) != 0 )
1302 {
1303 flags |= BADCRL_NOT_TRUSTED;
1304 break;
1305 }
1306
1307 /*
1308 * Check for validity of CRL (Do not drop out)
1309 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001310 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001311 flags |= BADCRL_EXPIRED;
1312
1313 /*
1314 * Check if certificate is revoked
1315 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001316 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317 {
1318 flags |= BADCERT_REVOKED;
1319 break;
1320 }
1321
1322 crl_list = crl_list->next;
1323 }
1324 return flags;
1325}
1326#endif /* POLARSSL_X509_CRL_PARSE_C */
1327
1328// Equal == 0, inequal == 1
1329static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1330{
1331 size_t i;
1332 unsigned char diff;
1333 const unsigned char *n1 = s1, *n2 = s2;
1334
1335 for( i = 0; i < len; i++ )
1336 {
1337 diff = n1[i] ^ n2[i];
1338
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001339 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001340 continue;
1341
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001342 if( diff == 32 &&
1343 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1344 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1345 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001346 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001347 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001348
1349 return( 1 );
1350 }
1351
1352 return( 0 );
1353}
1354
1355static int x509_wildcard_verify( const char *cn, x509_buf *name )
1356{
1357 size_t i;
1358 size_t cn_idx = 0;
1359
1360 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1361 return( 0 );
1362
1363 for( i = 0; i < strlen( cn ); ++i )
1364 {
1365 if( cn[i] == '.' )
1366 {
1367 cn_idx = i;
1368 break;
1369 }
1370 }
1371
1372 if( cn_idx == 0 )
1373 return( 0 );
1374
1375 if( strlen( cn ) - cn_idx == name->len - 1 &&
1376 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1377 {
1378 return( 1 );
1379 }
1380
1381 return( 0 );
1382}
1383
Paul Bakkerddf26b42013-09-18 13:46:23 +02001384static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001385 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001386 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001387 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001388 void *p_vrfy )
1389{
1390 int ret;
1391 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1392 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1393 const md_info_t *md_info;
1394
Paul Bakker86d0c192013-09-18 11:11:02 +02001395 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001396 *flags |= BADCERT_EXPIRED;
1397
1398 /*
1399 * Child is the top of the chain. Check against the trust_ca list.
1400 */
1401 *flags |= BADCERT_NOT_TRUSTED;
1402
1403 md_info = md_info_from_type( child->sig_md );
1404 if( md_info == NULL )
1405 {
1406 /*
1407 * Cannot check 'unknown', no need to try any CA
1408 */
1409 trust_ca = NULL;
1410 }
1411 else
1412 md( md_info, child->tbs.p, child->tbs.len, hash );
1413
1414 while( trust_ca != NULL )
1415 {
1416 if( trust_ca->version == 0 ||
1417 child->issuer_raw.len != trust_ca->subject_raw.len ||
1418 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
1419 child->issuer_raw.len ) != 0 )
1420 {
1421 trust_ca = trust_ca->next;
1422 continue;
1423 }
1424
1425 /*
1426 * Reduce path_len to check against if top of the chain is
1427 * the same as the trusted CA
1428 */
1429 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1430 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1431 child->issuer_raw.len ) == 0 )
1432 {
1433 check_path_cnt--;
1434 }
1435
1436 if( trust_ca->max_pathlen > 0 &&
1437 trust_ca->max_pathlen < check_path_cnt )
1438 {
1439 trust_ca = trust_ca->next;
1440 continue;
1441 }
1442
1443 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1444 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1445 child->sig.p, child->sig.len ) != 0 )
1446 {
1447 trust_ca = trust_ca->next;
1448 continue;
1449 }
1450
1451 /*
1452 * Top of chain is signed by a trusted CA
1453 */
1454 *flags &= ~BADCERT_NOT_TRUSTED;
1455 break;
1456 }
1457
1458 /*
1459 * If top of chain is not the same as the trusted CA send a verify request
1460 * to the callback for any issues with validity and CRL presence for the
1461 * trusted CA certificate.
1462 */
1463 if( trust_ca != NULL &&
1464 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1465 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1466 child->issuer_raw.len ) != 0 ) )
1467 {
1468#if defined(POLARSSL_X509_CRL_PARSE_C)
1469 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001470 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001471#else
1472 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001473#endif
1474
Paul Bakker86d0c192013-09-18 11:11:02 +02001475 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001476 ca_flags |= BADCERT_EXPIRED;
1477
1478 if( NULL != f_vrfy )
1479 {
1480 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
1481 return( ret );
1482 }
1483 }
1484
1485 /* Call callback on top cert */
1486 if( NULL != f_vrfy )
1487 {
1488 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1489 return( ret );
1490 }
1491
1492 *flags |= ca_flags;
1493
1494 return( 0 );
1495}
1496
Paul Bakkerddf26b42013-09-18 13:46:23 +02001497static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001498 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001499 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001500 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001501 void *p_vrfy )
1502{
1503 int ret;
1504 int parent_flags = 0;
1505 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001506 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507 const md_info_t *md_info;
1508
Paul Bakker86d0c192013-09-18 11:11:02 +02001509 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510 *flags |= BADCERT_EXPIRED;
1511
1512 md_info = md_info_from_type( child->sig_md );
1513 if( md_info == NULL )
1514 {
1515 /*
1516 * Cannot check 'unknown' hash
1517 */
1518 *flags |= BADCERT_NOT_TRUSTED;
1519 }
1520 else
1521 {
1522 md( md_info, child->tbs.p, child->tbs.len, hash );
1523
1524 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1525 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1526 child->sig.p, child->sig.len ) != 0 )
1527 {
1528 *flags |= BADCERT_NOT_TRUSTED;
1529 }
1530 }
1531
1532#if defined(POLARSSL_X509_CRL_PARSE_C)
1533 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001534 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001535#endif
1536
1537 grandparent = parent->next;
1538
1539 while( grandparent != NULL )
1540 {
1541 if( grandparent->version == 0 ||
1542 grandparent->ca_istrue == 0 ||
1543 parent->issuer_raw.len != grandparent->subject_raw.len ||
1544 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
1545 parent->issuer_raw.len ) != 0 )
1546 {
1547 grandparent = grandparent->next;
1548 continue;
1549 }
1550 break;
1551 }
1552
1553 if( grandparent != NULL )
1554 {
1555 /*
1556 * Part of the chain
1557 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001558 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 +02001559 if( ret != 0 )
1560 return( ret );
1561 }
1562 else
1563 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001564 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 +02001565 if( ret != 0 )
1566 return( ret );
1567 }
1568
1569 /* child is verified to be a child of the parent, call verify callback */
1570 if( NULL != f_vrfy )
1571 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1572 return( ret );
1573
1574 *flags |= parent_flags;
1575
1576 return( 0 );
1577}
1578
1579/*
1580 * Verify the certificate validity
1581 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001582int x509_crt_verify( x509_crt *crt,
1583 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001584 x509_crl *ca_crl,
1585 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001586 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001587 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001588{
1589 size_t cn_len;
1590 int ret;
1591 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001592 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593 x509_name *name;
1594 x509_sequence *cur = NULL;
1595
1596 *flags = 0;
1597
1598 if( cn != NULL )
1599 {
1600 name = &crt->subject;
1601 cn_len = strlen( cn );
1602
1603 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1604 {
1605 cur = &crt->subject_alt_names;
1606
1607 while( cur != NULL )
1608 {
1609 if( cur->buf.len == cn_len &&
1610 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1611 break;
1612
1613 if( cur->buf.len > 2 &&
1614 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1615 x509_wildcard_verify( cn, &cur->buf ) )
1616 break;
1617
1618 cur = cur->next;
1619 }
1620
1621 if( cur == NULL )
1622 *flags |= BADCERT_CN_MISMATCH;
1623 }
1624 else
1625 {
1626 while( name != NULL )
1627 {
1628 if( OID_CMP( OID_AT_CN, &name->oid ) )
1629 {
1630 if( name->val.len == cn_len &&
1631 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1632 break;
1633
1634 if( name->val.len > 2 &&
1635 memcmp( name->val.p, "*.", 2 ) == 0 &&
1636 x509_wildcard_verify( cn, &name->val ) )
1637 break;
1638 }
1639
1640 name = name->next;
1641 }
1642
1643 if( name == NULL )
1644 *flags |= BADCERT_CN_MISMATCH;
1645 }
1646 }
1647
1648 /*
1649 * Iterate upwards in the given cert chain, to find our crt parent.
1650 * Ignore any upper cert with CA != TRUE.
1651 */
1652 parent = crt->next;
1653
1654 while( parent != NULL && parent->version != 0 )
1655 {
1656 if( parent->ca_istrue == 0 ||
1657 crt->issuer_raw.len != parent->subject_raw.len ||
1658 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
1659 crt->issuer_raw.len ) != 0 )
1660 {
1661 parent = parent->next;
1662 continue;
1663 }
1664 break;
1665 }
1666
1667 if( parent != NULL )
1668 {
1669 /*
1670 * Part of the chain
1671 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001672 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001673 if( ret != 0 )
1674 return( ret );
1675 }
1676 else
1677 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001678 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679 if( ret != 0 )
1680 return( ret );
1681 }
1682
1683 if( *flags != 0 )
1684 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1685
1686 return( 0 );
1687}
1688
1689/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001690 * Initialize a certificate chain
1691 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001692void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001693{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001694 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001695}
1696
1697/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001698 * Unallocate all certificate data
1699 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001700void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001701{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001702 x509_crt *cert_cur = crt;
1703 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001704 x509_name *name_cur;
1705 x509_name *name_prv;
1706 x509_sequence *seq_cur;
1707 x509_sequence *seq_prv;
1708
1709 if( crt == NULL )
1710 return;
1711
1712 do
1713 {
1714 pk_free( &cert_cur->pk );
1715
1716 name_cur = cert_cur->issuer.next;
1717 while( name_cur != NULL )
1718 {
1719 name_prv = name_cur;
1720 name_cur = name_cur->next;
1721 memset( name_prv, 0, sizeof( x509_name ) );
1722 polarssl_free( name_prv );
1723 }
1724
1725 name_cur = cert_cur->subject.next;
1726 while( name_cur != NULL )
1727 {
1728 name_prv = name_cur;
1729 name_cur = name_cur->next;
1730 memset( name_prv, 0, sizeof( x509_name ) );
1731 polarssl_free( name_prv );
1732 }
1733
1734 seq_cur = cert_cur->ext_key_usage.next;
1735 while( seq_cur != NULL )
1736 {
1737 seq_prv = seq_cur;
1738 seq_cur = seq_cur->next;
1739 memset( seq_prv, 0, sizeof( x509_sequence ) );
1740 polarssl_free( seq_prv );
1741 }
1742
1743 seq_cur = cert_cur->subject_alt_names.next;
1744 while( seq_cur != NULL )
1745 {
1746 seq_prv = seq_cur;
1747 seq_cur = seq_cur->next;
1748 memset( seq_prv, 0, sizeof( x509_sequence ) );
1749 polarssl_free( seq_prv );
1750 }
1751
1752 if( cert_cur->raw.p != NULL )
1753 {
1754 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1755 polarssl_free( cert_cur->raw.p );
1756 }
1757
1758 cert_cur = cert_cur->next;
1759 }
1760 while( cert_cur != NULL );
1761
1762 cert_cur = crt;
1763 do
1764 {
1765 cert_prv = cert_cur;
1766 cert_cur = cert_cur->next;
1767
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001768 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001769 if( cert_prv != crt )
1770 polarssl_free( cert_prv );
1771 }
1772 while( cert_cur != NULL );
1773}
1774
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001775#endif