blob: 8a3f13f22a2c5879578eb7f9f0bb62d24f2d4f40 [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
54#include <string.h>
55#include <stdlib.h>
56#if defined(_WIN32)
57#include <windows.h>
58#else
59#include <time.h>
60#endif
61
62#if defined(POLARSSL_FS_IO)
63#include <stdio.h>
64#if !defined(_WIN32)
65#include <sys/types.h>
66#include <sys/stat.h>
67#include <dirent.h>
68#endif
69#endif
70
71/*
72 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
73 */
74static int x509_get_version( unsigned char **p,
75 const unsigned char *end,
76 int *ver )
77{
78 int ret;
79 size_t len;
80
81 if( ( ret = asn1_get_tag( p, end, &len,
82 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
83 {
84 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
85 {
86 *ver = 0;
87 return( 0 );
88 }
89
90 return( ret );
91 }
92
93 end = *p + len;
94
95 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
96 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
97
98 if( *p != end )
99 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
100 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
101
102 return( 0 );
103}
104
105/*
106 * Validity ::= SEQUENCE {
107 * notBefore Time,
108 * notAfter Time }
109 */
110static int x509_get_dates( unsigned char **p,
111 const unsigned char *end,
112 x509_time *from,
113 x509_time *to )
114{
115 int ret;
116 size_t len;
117
118 if( ( ret = asn1_get_tag( p, end, &len,
119 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
120 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
121
122 end = *p + len;
123
124 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
125 return( ret );
126
127 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
128 return( ret );
129
130 if( *p != end )
131 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
132 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
133
134 return( 0 );
135}
136
137/*
138 * X.509 v2/v3 unique identifier (not parsed)
139 */
140static int x509_get_uid( unsigned char **p,
141 const unsigned char *end,
142 x509_buf *uid, int n )
143{
144 int ret;
145
146 if( *p == end )
147 return( 0 );
148
149 uid->tag = **p;
150
151 if( ( ret = asn1_get_tag( p, end, &uid->len,
152 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
153 {
154 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
155 return( 0 );
156
157 return( ret );
158 }
159
160 uid->p = *p;
161 *p += uid->len;
162
163 return( 0 );
164}
165
166static int x509_get_basic_constraints( unsigned char **p,
167 const unsigned char *end,
168 int *ca_istrue,
169 int *max_pathlen )
170{
171 int ret;
172 size_t len;
173
174 /*
175 * BasicConstraints ::= SEQUENCE {
176 * cA BOOLEAN DEFAULT FALSE,
177 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
178 */
179 *ca_istrue = 0; /* DEFAULT FALSE */
180 *max_pathlen = 0; /* endless */
181
182 if( ( ret = asn1_get_tag( p, end, &len,
183 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
184 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
185
186 if( *p == end )
187 return 0;
188
189 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
190 {
191 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
192 ret = asn1_get_int( p, end, ca_istrue );
193
194 if( ret != 0 )
195 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
196
197 if( *ca_istrue != 0 )
198 *ca_istrue = 1;
199 }
200
201 if( *p == end )
202 return 0;
203
204 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
205 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
206
207 if( *p != end )
208 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
209 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
210
211 (*max_pathlen)++;
212
213 return 0;
214}
215
216static int x509_get_ns_cert_type( unsigned char **p,
217 const unsigned char *end,
218 unsigned char *ns_cert_type)
219{
220 int ret;
221 x509_bitstring bs = { 0, 0, NULL };
222
223 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
224 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
225
226 if( bs.len != 1 )
227 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
228 POLARSSL_ERR_ASN1_INVALID_LENGTH );
229
230 /* Get actual bitstring */
231 *ns_cert_type = *bs.p;
232 return 0;
233}
234
235static int x509_get_key_usage( unsigned char **p,
236 const unsigned char *end,
237 unsigned char *key_usage)
238{
239 int ret;
240 x509_bitstring bs = { 0, 0, NULL };
241
242 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
243 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
244
245 if( bs.len < 1 )
246 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
247 POLARSSL_ERR_ASN1_INVALID_LENGTH );
248
249 /* Get actual bitstring */
250 *key_usage = *bs.p;
251 return 0;
252}
253
254/*
255 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
256 *
257 * KeyPurposeId ::= OBJECT IDENTIFIER
258 */
259static int x509_get_ext_key_usage( unsigned char **p,
260 const unsigned char *end,
261 x509_sequence *ext_key_usage)
262{
263 int ret;
264
265 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
266 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
267
268 /* Sequence length must be >= 1 */
269 if( ext_key_usage->buf.p == NULL )
270 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
271 POLARSSL_ERR_ASN1_INVALID_LENGTH );
272
273 return 0;
274}
275
276/*
277 * SubjectAltName ::= GeneralNames
278 *
279 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
280 *
281 * GeneralName ::= CHOICE {
282 * otherName [0] OtherName,
283 * rfc822Name [1] IA5String,
284 * dNSName [2] IA5String,
285 * x400Address [3] ORAddress,
286 * directoryName [4] Name,
287 * ediPartyName [5] EDIPartyName,
288 * uniformResourceIdentifier [6] IA5String,
289 * iPAddress [7] OCTET STRING,
290 * registeredID [8] OBJECT IDENTIFIER }
291 *
292 * OtherName ::= SEQUENCE {
293 * type-id OBJECT IDENTIFIER,
294 * value [0] EXPLICIT ANY DEFINED BY type-id }
295 *
296 * EDIPartyName ::= SEQUENCE {
297 * nameAssigner [0] DirectoryString OPTIONAL,
298 * partyName [1] DirectoryString }
299 *
300 * NOTE: PolarSSL only parses and uses dNSName at this point.
301 */
302static int x509_get_subject_alt_name( unsigned char **p,
303 const unsigned char *end,
304 x509_sequence *subject_alt_name )
305{
306 int ret;
307 size_t len, tag_len;
308 asn1_buf *buf;
309 unsigned char tag;
310 asn1_sequence *cur = subject_alt_name;
311
312 /* Get main sequence tag */
313 if( ( ret = asn1_get_tag( p, end, &len,
314 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
315 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
316
317 if( *p + len != end )
318 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
319 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
320
321 while( *p < end )
322 {
323 if( ( end - *p ) < 1 )
324 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
325 POLARSSL_ERR_ASN1_OUT_OF_DATA );
326
327 tag = **p;
328 (*p)++;
329 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
330 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
331
332 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
333 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
334 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
335
336 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
337 {
338 *p += tag_len;
339 continue;
340 }
341
342 buf = &(cur->buf);
343 buf->tag = tag;
344 buf->p = *p;
345 buf->len = tag_len;
346 *p += buf->len;
347
348 /* Allocate and assign next pointer */
349 if (*p < end)
350 {
351 cur->next = (asn1_sequence *) polarssl_malloc(
352 sizeof( asn1_sequence ) );
353
354 if( cur->next == NULL )
355 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
356 POLARSSL_ERR_ASN1_MALLOC_FAILED );
357
358 memset( cur->next, 0, sizeof( asn1_sequence ) );
359 cur = cur->next;
360 }
361 }
362
363 /* Set final sequence entry's next pointer to NULL */
364 cur->next = NULL;
365
366 if( *p != end )
367 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
368 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
369
370 return( 0 );
371}
372
373/*
374 * X.509 v3 extensions
375 *
376 * TODO: Perform all of the basic constraints tests required by the RFC
377 * TODO: Set values for undetected extensions to a sane default?
378 *
379 */
380static int x509_get_crt_ext( unsigned char **p,
381 const unsigned char *end,
382 x509_cert *crt )
383{
384 int ret;
385 size_t len;
386 unsigned char *end_ext_data, *end_ext_octet;
387
388 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
389 {
390 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
391 return( 0 );
392
393 return( ret );
394 }
395
396 while( *p < end )
397 {
398 /*
399 * Extension ::= SEQUENCE {
400 * extnID OBJECT IDENTIFIER,
401 * critical BOOLEAN DEFAULT FALSE,
402 * extnValue OCTET STRING }
403 */
404 x509_buf extn_oid = {0, 0, NULL};
405 int is_critical = 0; /* DEFAULT FALSE */
406 int ext_type = 0;
407
408 if( ( ret = asn1_get_tag( p, end, &len,
409 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
410 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
411
412 end_ext_data = *p + len;
413
414 /* Get extension ID */
415 extn_oid.tag = **p;
416
417 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
418 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
419
420 extn_oid.p = *p;
421 *p += extn_oid.len;
422
423 if( ( end - *p ) < 1 )
424 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
425 POLARSSL_ERR_ASN1_OUT_OF_DATA );
426
427 /* Get optional critical */
428 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
429 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
430 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
431
432 /* Data should be octet string type */
433 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
434 ASN1_OCTET_STRING ) ) != 0 )
435 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
436
437 end_ext_octet = *p + len;
438
439 if( end_ext_octet != end_ext_data )
440 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
441 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
442
443 /*
444 * Detect supported extensions
445 */
446 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
447
448 if( ret != 0 )
449 {
450 /* No parser found, skip extension */
451 *p = end_ext_octet;
452
453#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
454 if( is_critical )
455 {
456 /* Data is marked as critical: fail */
457 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
458 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
459 }
460#endif
461 continue;
462 }
463
464 crt->ext_types |= ext_type;
465
466 switch( ext_type )
467 {
468 case EXT_BASIC_CONSTRAINTS:
469 /* Parse basic constraints */
470 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
471 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
472 return ( ret );
473 break;
474
475 case EXT_KEY_USAGE:
476 /* Parse key usage */
477 if( ( ret = x509_get_key_usage( p, end_ext_octet,
478 &crt->key_usage ) ) != 0 )
479 return ( ret );
480 break;
481
482 case EXT_EXTENDED_KEY_USAGE:
483 /* Parse extended key usage */
484 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
485 &crt->ext_key_usage ) ) != 0 )
486 return ( ret );
487 break;
488
489 case EXT_SUBJECT_ALT_NAME:
490 /* Parse subject alt name */
491 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
492 &crt->subject_alt_names ) ) != 0 )
493 return ( ret );
494 break;
495
496 case EXT_NS_CERT_TYPE:
497 /* Parse netscape certificate type */
498 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
499 &crt->ns_cert_type ) ) != 0 )
500 return ( ret );
501 break;
502
503 default:
504 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
505 }
506 }
507
508 if( *p != end )
509 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
510 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
511
512 return( 0 );
513}
514
515/*
516 * Parse and fill a single X.509 certificate in DER format
517 */
518static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
519 size_t buflen )
520{
521 int ret;
522 size_t len;
523 unsigned char *p, *end, *crt_end;
524
525 /*
526 * Check for valid input
527 */
528 if( crt == NULL || buf == NULL )
529 return( POLARSSL_ERR_X509_INVALID_INPUT );
530
531 p = (unsigned char *) polarssl_malloc( len = buflen );
532
533 if( p == NULL )
534 return( POLARSSL_ERR_X509_MALLOC_FAILED );
535
536 memcpy( p, buf, buflen );
537
538 buflen = 0;
539
540 crt->raw.p = p;
541 crt->raw.len = len;
542 end = p + len;
543
544 /*
545 * Certificate ::= SEQUENCE {
546 * tbsCertificate TBSCertificate,
547 * signatureAlgorithm AlgorithmIdentifier,
548 * signatureValue BIT STRING }
549 */
550 if( ( ret = asn1_get_tag( &p, end, &len,
551 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
552 {
553 x509_crt_free( crt );
554 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
555 }
556
557 if( len > (size_t) ( end - p ) )
558 {
559 x509_crt_free( crt );
560 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
561 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
562 }
563 crt_end = p + len;
564
565 /*
566 * TBSCertificate ::= SEQUENCE {
567 */
568 crt->tbs.p = p;
569
570 if( ( ret = asn1_get_tag( &p, end, &len,
571 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
572 {
573 x509_crt_free( crt );
574 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
575 }
576
577 end = p + len;
578 crt->tbs.len = end - crt->tbs.p;
579
580 /*
581 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
582 *
583 * CertificateSerialNumber ::= INTEGER
584 *
585 * signature AlgorithmIdentifier
586 */
587 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
588 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
589 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
590 {
591 x509_crt_free( crt );
592 return( ret );
593 }
594
595 crt->version++;
596
597 if( crt->version > 3 )
598 {
599 x509_crt_free( crt );
600 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
601 }
602
603 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
604 &crt->sig_pk ) ) != 0 )
605 {
606 x509_crt_free( crt );
607 return( ret );
608 }
609
610 /*
611 * issuer Name
612 */
613 crt->issuer_raw.p = p;
614
615 if( ( ret = asn1_get_tag( &p, end, &len,
616 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
617 {
618 x509_crt_free( crt );
619 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
620 }
621
622 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
623 {
624 x509_crt_free( crt );
625 return( ret );
626 }
627
628 crt->issuer_raw.len = p - crt->issuer_raw.p;
629
630 /*
631 * Validity ::= SEQUENCE {
632 * notBefore Time,
633 * notAfter Time }
634 *
635 */
636 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
637 &crt->valid_to ) ) != 0 )
638 {
639 x509_crt_free( crt );
640 return( ret );
641 }
642
643 /*
644 * subject Name
645 */
646 crt->subject_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 );
652 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
653 }
654
655 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
656 {
657 x509_crt_free( crt );
658 return( ret );
659 }
660
661 crt->subject_raw.len = p - crt->subject_raw.p;
662
663 /*
664 * SubjectPublicKeyInfo
665 */
Paul Bakkerda771152013-09-16 22:45:03 +0200666 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200667 {
668 x509_crt_free( crt );
669 return( ret );
670 }
671
672 /*
673 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
674 * -- If present, version shall be v2 or v3
675 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
676 * -- If present, version shall be v2 or v3
677 * extensions [3] EXPLICIT Extensions OPTIONAL
678 * -- If present, version shall be v3
679 */
680 if( crt->version == 2 || crt->version == 3 )
681 {
682 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
683 if( ret != 0 )
684 {
685 x509_crt_free( crt );
686 return( ret );
687 }
688 }
689
690 if( crt->version == 2 || crt->version == 3 )
691 {
692 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
693 if( ret != 0 )
694 {
695 x509_crt_free( crt );
696 return( ret );
697 }
698 }
699
700 if( crt->version == 3 )
701 {
702 ret = x509_get_crt_ext( &p, end, crt);
703 if( ret != 0 )
704 {
705 x509_crt_free( crt );
706 return( ret );
707 }
708 }
709
710 if( p != end )
711 {
712 x509_crt_free( crt );
713 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
714 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
715 }
716
717 end = crt_end;
718
719 /*
720 * }
721 * -- end of TBSCertificate
722 *
723 * signatureAlgorithm AlgorithmIdentifier,
724 * signatureValue BIT STRING
725 */
726 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
727 {
728 x509_crt_free( crt );
729 return( ret );
730 }
731
732 if( crt->sig_oid1.len != crt->sig_oid2.len ||
733 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
734 {
735 x509_crt_free( crt );
736 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
737 }
738
739 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
740 {
741 x509_crt_free( crt );
742 return( ret );
743 }
744
745 if( p != end )
746 {
747 x509_crt_free( crt );
748 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
749 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
750 }
751
752 return( 0 );
753}
754
755/*
756 * Parse one X.509 certificate in DER format from a buffer and add them to a
757 * chained list
758 */
759int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
760{
761 int ret;
762 x509_cert *crt = chain, *prev = NULL;
763
764 /*
765 * Check for valid input
766 */
767 if( crt == NULL || buf == NULL )
768 return( POLARSSL_ERR_X509_INVALID_INPUT );
769
770 while( crt->version != 0 && crt->next != NULL )
771 {
772 prev = crt;
773 crt = crt->next;
774 }
775
776 /*
777 * Add new certificate on the end of the chain if needed.
778 */
779 if ( crt->version != 0 && crt->next == NULL)
780 {
781 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
782
783 if( crt->next == NULL )
784 return( POLARSSL_ERR_X509_MALLOC_FAILED );
785
786 prev = crt;
787 crt = crt->next;
788 memset( crt, 0, sizeof( x509_cert ) );
789 }
790
791 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
792 {
793 if( prev )
794 prev->next = NULL;
795
796 if( crt != chain )
797 polarssl_free( crt );
798
799 return( ret );
800 }
801
802 return( 0 );
803}
804
805/*
806 * Parse one or more PEM certificates from a buffer and add them to the chained list
807 */
808int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
809{
810 int success = 0, first_error = 0, total_failed = 0;
811 int buf_format = X509_FORMAT_DER;
812
813 /*
814 * Check for valid input
815 */
816 if( chain == NULL || buf == NULL )
817 return( POLARSSL_ERR_X509_INVALID_INPUT );
818
819 /*
820 * Determine buffer content. Buffer contains either one DER certificate or
821 * one or more PEM certificates.
822 */
823#if defined(POLARSSL_PEM_PARSE_C)
824 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
825 buf_format = X509_FORMAT_PEM;
826#endif
827
828 if( buf_format == X509_FORMAT_DER )
829 return x509parse_crt_der( chain, buf, buflen );
830
831#if defined(POLARSSL_PEM_PARSE_C)
832 if( buf_format == X509_FORMAT_PEM )
833 {
834 int ret;
835 pem_context pem;
836
837 while( buflen > 0 )
838 {
839 size_t use_len;
840 pem_init( &pem );
841
842 ret = pem_read_buffer( &pem,
843 "-----BEGIN CERTIFICATE-----",
844 "-----END CERTIFICATE-----",
845 buf, NULL, 0, &use_len );
846
847 if( ret == 0 )
848 {
849 /*
850 * Was PEM encoded
851 */
852 buflen -= use_len;
853 buf += use_len;
854 }
855 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
856 {
857 return( ret );
858 }
859 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
860 {
861 pem_free( &pem );
862
863 /*
864 * PEM header and footer were found
865 */
866 buflen -= use_len;
867 buf += use_len;
868
869 if( first_error == 0 )
870 first_error = ret;
871
872 continue;
873 }
874 else
875 break;
876
877 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
878
879 pem_free( &pem );
880
881 if( ret != 0 )
882 {
883 /*
884 * Quit parsing on a memory error
885 */
886 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
887 return( ret );
888
889 if( first_error == 0 )
890 first_error = ret;
891
892 total_failed++;
893 continue;
894 }
895
896 success = 1;
897 }
898 }
899#endif
900
901 if( success )
902 return( total_failed );
903 else if( first_error )
904 return( first_error );
905 else
906 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
907}
908
909#if defined(POLARSSL_FS_IO)
910/*
911 * Load one or more certificates and add them to the chained list
912 */
913int x509parse_crtfile( x509_cert *chain, const char *path )
914{
915 int ret;
916 size_t n;
917 unsigned char *buf;
918
919 if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
920 return( ret );
921
922 ret = x509parse_crt( chain, buf, n );
923
924 memset( buf, 0, n + 1 );
925 polarssl_free( buf );
926
927 return( ret );
928}
929
930int x509parse_crtpath( x509_cert *chain, const char *path )
931{
932 int ret = 0;
933#if defined(_WIN32)
934 int w_ret;
935 WCHAR szDir[MAX_PATH];
936 char filename[MAX_PATH];
937 char *p;
938 int len = strlen( path );
939
940 WIN32_FIND_DATAW file_data;
941 HANDLE hFind;
942
943 if( len > MAX_PATH - 3 )
944 return( POLARSSL_ERR_X509_INVALID_INPUT );
945
946 memset( szDir, 0, sizeof(szDir) );
947 memset( filename, 0, MAX_PATH );
948 memcpy( filename, path, len );
949 filename[len++] = '\\';
950 p = filename + len;
951 filename[len++] = '*';
952
953 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
954
955 hFind = FindFirstFileW( szDir, &file_data );
956 if (hFind == INVALID_HANDLE_VALUE)
957 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
958
959 len = MAX_PATH - len;
960 do
961 {
962 memset( p, 0, len );
963
964 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
965 continue;
966
967 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
968 lstrlenW(file_data.cFileName),
969 p, len - 1,
970 NULL, NULL );
971
972 w_ret = x509parse_crtfile( chain, filename );
973 if( w_ret < 0 )
974 ret++;
975 else
976 ret += w_ret;
977 }
978 while( FindNextFileW( hFind, &file_data ) != 0 );
979
980 if (GetLastError() != ERROR_NO_MORE_FILES)
981 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
982
983cleanup:
984 FindClose( hFind );
985#else
986 int t_ret, i;
987 struct stat sb;
988 struct dirent entry, *result = NULL;
989 char entry_name[255];
990 DIR *dir = opendir( path );
991
992 if( dir == NULL)
993 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
994
995 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
996 {
997 if( result == NULL )
998 break;
999
1000 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1001
1002 i = stat( entry_name, &sb );
1003
1004 if( i == -1 )
1005 {
1006 closedir( dir );
1007 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1008 }
1009
1010 if( !S_ISREG( sb.st_mode ) )
1011 continue;
1012
1013 // Ignore parse errors
1014 //
1015 t_ret = x509parse_crtfile( chain, entry_name );
1016 if( t_ret < 0 )
1017 ret++;
1018 else
1019 ret += t_ret;
1020 }
1021 closedir( dir );
1022#endif
1023
1024 return( ret );
1025}
1026#endif /* POLARSSL_FS_IO */
1027
1028#if defined _MSC_VER && !defined snprintf
1029#include <stdarg.h>
1030
1031#if !defined vsnprintf
1032#define vsnprintf _vsnprintf
1033#endif // vsnprintf
1034
1035/*
1036 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1037 * Result value is not size of buffer needed, but -1 if no fit is possible.
1038 *
1039 * This fuction tries to 'fix' this by at least suggesting enlarging the
1040 * size by 20.
1041 */
1042static int compat_snprintf(char *str, size_t size, const char *format, ...)
1043{
1044 va_list ap;
1045 int res = -1;
1046
1047 va_start( ap, format );
1048
1049 res = vsnprintf( str, size, format, ap );
1050
1051 va_end( ap );
1052
1053 // No quick fix possible
1054 if ( res < 0 )
1055 return( (int) size + 20 );
1056
1057 return res;
1058}
1059
1060#define snprintf compat_snprintf
1061#endif
1062
1063#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1064
1065#define SAFE_SNPRINTF() \
1066{ \
1067 if( ret == -1 ) \
1068 return( -1 ); \
1069 \
1070 if ( (unsigned int) ret > n ) { \
1071 p[n - 1] = '\0'; \
1072 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1073 } \
1074 \
1075 n -= (unsigned int) ret; \
1076 p += (unsigned int) ret; \
1077}
1078
1079/*
1080 * Return an informational string about the certificate.
1081 */
1082#define BEFORE_COLON 14
1083#define BC "14"
1084int x509parse_cert_info( char *buf, size_t size, const char *prefix,
1085 const x509_cert *crt )
1086{
1087 int ret;
1088 size_t n;
1089 char *p;
1090 const char *desc = NULL;
1091 char key_size_str[BEFORE_COLON];
1092
1093 p = buf;
1094 n = size;
1095
1096 ret = snprintf( p, n, "%scert. version : %d\n",
1097 prefix, crt->version );
1098 SAFE_SNPRINTF();
1099 ret = snprintf( p, n, "%sserial number : ",
1100 prefix );
1101 SAFE_SNPRINTF();
1102
1103 ret = x509parse_serial_gets( p, n, &crt->serial);
1104 SAFE_SNPRINTF();
1105
1106 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
1107 SAFE_SNPRINTF();
1108 ret = x509parse_dn_gets( p, n, &crt->issuer );
1109 SAFE_SNPRINTF();
1110
1111 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
1112 SAFE_SNPRINTF();
1113 ret = x509parse_dn_gets( p, n, &crt->subject );
1114 SAFE_SNPRINTF();
1115
1116 ret = snprintf( p, n, "\n%sissued on : " \
1117 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1118 crt->valid_from.year, crt->valid_from.mon,
1119 crt->valid_from.day, crt->valid_from.hour,
1120 crt->valid_from.min, crt->valid_from.sec );
1121 SAFE_SNPRINTF();
1122
1123 ret = snprintf( p, n, "\n%sexpires on : " \
1124 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1125 crt->valid_to.year, crt->valid_to.mon,
1126 crt->valid_to.day, crt->valid_to.hour,
1127 crt->valid_to.min, crt->valid_to.sec );
1128 SAFE_SNPRINTF();
1129
1130 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
1131 SAFE_SNPRINTF();
1132
1133 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1134 if( ret != 0 )
1135 ret = snprintf( p, n, "???" );
1136 else
1137 ret = snprintf( p, n, "%s", desc );
1138 SAFE_SNPRINTF();
1139
1140 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1141 pk_get_name( &crt->pk ) ) ) != 0 )
1142 {
1143 return( ret );
1144 }
1145
1146 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
1147 (int) pk_get_size( &crt->pk ) );
1148 SAFE_SNPRINTF();
1149
1150 return( (int) ( size - n ) );
1151}
1152
1153#if defined(POLARSSL_X509_CRL_PARSE_C)
1154/*
1155 * Return 1 if the certificate is revoked, or 0 otherwise.
1156 */
1157int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
1158{
1159 const x509_crl_entry *cur = &crl->entry;
1160
1161 while( cur != NULL && cur->serial.len != 0 )
1162 {
1163 if( crt->serial.len == cur->serial.len &&
1164 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1165 {
1166 if( x509parse_time_expired( &cur->revocation_date ) )
1167 return( 1 );
1168 }
1169
1170 cur = cur->next;
1171 }
1172
1173 return( 0 );
1174}
1175
1176/*
1177 * Check that the given certificate is valid accoring to the CRL.
1178 */
1179static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
1180 x509_crl *crl_list)
1181{
1182 int flags = 0;
1183 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1184 const md_info_t *md_info;
1185
1186 if( ca == NULL )
1187 return( flags );
1188
1189 /*
1190 * TODO: What happens if no CRL is present?
1191 * Suggestion: Revocation state should be unknown if no CRL is present.
1192 * For backwards compatibility this is not yet implemented.
1193 */
1194
1195 while( crl_list != NULL )
1196 {
1197 if( crl_list->version == 0 ||
1198 crl_list->issuer_raw.len != ca->subject_raw.len ||
1199 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1200 crl_list->issuer_raw.len ) != 0 )
1201 {
1202 crl_list = crl_list->next;
1203 continue;
1204 }
1205
1206 /*
1207 * Check if CRL is correctly signed by the trusted CA
1208 */
1209 md_info = md_info_from_type( crl_list->sig_md );
1210 if( md_info == NULL )
1211 {
1212 /*
1213 * Cannot check 'unknown' hash
1214 */
1215 flags |= BADCRL_NOT_TRUSTED;
1216 break;
1217 }
1218
1219 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1220
1221 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1222 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1223 crl_list->sig.p, crl_list->sig.len ) != 0 )
1224 {
1225 flags |= BADCRL_NOT_TRUSTED;
1226 break;
1227 }
1228
1229 /*
1230 * Check for validity of CRL (Do not drop out)
1231 */
1232 if( x509parse_time_expired( &crl_list->next_update ) )
1233 flags |= BADCRL_EXPIRED;
1234
1235 /*
1236 * Check if certificate is revoked
1237 */
1238 if( x509parse_revoked(crt, crl_list) )
1239 {
1240 flags |= BADCERT_REVOKED;
1241 break;
1242 }
1243
1244 crl_list = crl_list->next;
1245 }
1246 return flags;
1247}
1248#endif /* POLARSSL_X509_CRL_PARSE_C */
1249
1250// Equal == 0, inequal == 1
1251static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1252{
1253 size_t i;
1254 unsigned char diff;
1255 const unsigned char *n1 = s1, *n2 = s2;
1256
1257 for( i = 0; i < len; i++ )
1258 {
1259 diff = n1[i] ^ n2[i];
1260
1261 if( ( n1[i] >= 'a' || n1[i] <= 'z' ) && ( diff == 0 || diff == 32 ) )
1262 continue;
1263
1264 if( ( n1[i] >= 'A' || n1[i] <= 'Z' ) && ( diff == 0 || diff == 32 ) )
1265 continue;
1266
1267 return( 1 );
1268 }
1269
1270 return( 0 );
1271}
1272
1273static int x509_wildcard_verify( const char *cn, x509_buf *name )
1274{
1275 size_t i;
1276 size_t cn_idx = 0;
1277
1278 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1279 return( 0 );
1280
1281 for( i = 0; i < strlen( cn ); ++i )
1282 {
1283 if( cn[i] == '.' )
1284 {
1285 cn_idx = i;
1286 break;
1287 }
1288 }
1289
1290 if( cn_idx == 0 )
1291 return( 0 );
1292
1293 if( strlen( cn ) - cn_idx == name->len - 1 &&
1294 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1295 {
1296 return( 1 );
1297 }
1298
1299 return( 0 );
1300}
1301
1302static int x509parse_verify_top(
1303 x509_cert *child, x509_cert *trust_ca,
1304 x509_crl *ca_crl, int path_cnt, int *flags,
1305 int (*f_vrfy)(void *, x509_cert *, int, int *),
1306 void *p_vrfy )
1307{
1308 int ret;
1309 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1310 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1311 const md_info_t *md_info;
1312
1313 if( x509parse_time_expired( &child->valid_to ) )
1314 *flags |= BADCERT_EXPIRED;
1315
1316 /*
1317 * Child is the top of the chain. Check against the trust_ca list.
1318 */
1319 *flags |= BADCERT_NOT_TRUSTED;
1320
1321 md_info = md_info_from_type( child->sig_md );
1322 if( md_info == NULL )
1323 {
1324 /*
1325 * Cannot check 'unknown', no need to try any CA
1326 */
1327 trust_ca = NULL;
1328 }
1329 else
1330 md( md_info, child->tbs.p, child->tbs.len, hash );
1331
1332 while( trust_ca != NULL )
1333 {
1334 if( trust_ca->version == 0 ||
1335 child->issuer_raw.len != trust_ca->subject_raw.len ||
1336 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
1337 child->issuer_raw.len ) != 0 )
1338 {
1339 trust_ca = trust_ca->next;
1340 continue;
1341 }
1342
1343 /*
1344 * Reduce path_len to check against if top of the chain is
1345 * the same as the trusted CA
1346 */
1347 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1348 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1349 child->issuer_raw.len ) == 0 )
1350 {
1351 check_path_cnt--;
1352 }
1353
1354 if( trust_ca->max_pathlen > 0 &&
1355 trust_ca->max_pathlen < check_path_cnt )
1356 {
1357 trust_ca = trust_ca->next;
1358 continue;
1359 }
1360
1361 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1362 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1363 child->sig.p, child->sig.len ) != 0 )
1364 {
1365 trust_ca = trust_ca->next;
1366 continue;
1367 }
1368
1369 /*
1370 * Top of chain is signed by a trusted CA
1371 */
1372 *flags &= ~BADCERT_NOT_TRUSTED;
1373 break;
1374 }
1375
1376 /*
1377 * If top of chain is not the same as the trusted CA send a verify request
1378 * to the callback for any issues with validity and CRL presence for the
1379 * trusted CA certificate.
1380 */
1381 if( trust_ca != NULL &&
1382 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1383 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1384 child->issuer_raw.len ) != 0 ) )
1385 {
1386#if defined(POLARSSL_X509_CRL_PARSE_C)
1387 /* Check trusted CA's CRL for the chain's top crt */
1388 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
1389#endif
1390
1391 if( x509parse_time_expired( &trust_ca->valid_to ) )
1392 ca_flags |= BADCERT_EXPIRED;
1393
1394 if( NULL != f_vrfy )
1395 {
1396 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
1397 return( ret );
1398 }
1399 }
1400
1401 /* Call callback on top cert */
1402 if( NULL != f_vrfy )
1403 {
1404 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1405 return( ret );
1406 }
1407
1408 *flags |= ca_flags;
1409
1410 return( 0 );
1411}
1412
1413static int x509parse_verify_child(
1414 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
1415 x509_crl *ca_crl, int path_cnt, int *flags,
1416 int (*f_vrfy)(void *, x509_cert *, int, int *),
1417 void *p_vrfy )
1418{
1419 int ret;
1420 int parent_flags = 0;
1421 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1422 x509_cert *grandparent;
1423 const md_info_t *md_info;
1424
1425 if( x509parse_time_expired( &child->valid_to ) )
1426 *flags |= BADCERT_EXPIRED;
1427
1428 md_info = md_info_from_type( child->sig_md );
1429 if( md_info == NULL )
1430 {
1431 /*
1432 * Cannot check 'unknown' hash
1433 */
1434 *flags |= BADCERT_NOT_TRUSTED;
1435 }
1436 else
1437 {
1438 md( md_info, child->tbs.p, child->tbs.len, hash );
1439
1440 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1441 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1442 child->sig.p, child->sig.len ) != 0 )
1443 {
1444 *flags |= BADCERT_NOT_TRUSTED;
1445 }
1446 }
1447
1448#if defined(POLARSSL_X509_CRL_PARSE_C)
1449 /* Check trusted CA's CRL for the given crt */
1450 *flags |= x509parse_verifycrl(child, parent, ca_crl);
1451#endif
1452
1453 grandparent = parent->next;
1454
1455 while( grandparent != NULL )
1456 {
1457 if( grandparent->version == 0 ||
1458 grandparent->ca_istrue == 0 ||
1459 parent->issuer_raw.len != grandparent->subject_raw.len ||
1460 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
1461 parent->issuer_raw.len ) != 0 )
1462 {
1463 grandparent = grandparent->next;
1464 continue;
1465 }
1466 break;
1467 }
1468
1469 if( grandparent != NULL )
1470 {
1471 /*
1472 * Part of the chain
1473 */
1474 ret = x509parse_verify_child( parent, grandparent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
1475 if( ret != 0 )
1476 return( ret );
1477 }
1478 else
1479 {
1480 ret = x509parse_verify_top( parent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
1481 if( ret != 0 )
1482 return( ret );
1483 }
1484
1485 /* child is verified to be a child of the parent, call verify callback */
1486 if( NULL != f_vrfy )
1487 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1488 return( ret );
1489
1490 *flags |= parent_flags;
1491
1492 return( 0 );
1493}
1494
1495/*
1496 * Verify the certificate validity
1497 */
1498int x509parse_verify( x509_cert *crt,
1499 x509_cert *trust_ca,
1500 x509_crl *ca_crl,
1501 const char *cn, int *flags,
1502 int (*f_vrfy)(void *, x509_cert *, int, int *),
1503 void *p_vrfy )
1504{
1505 size_t cn_len;
1506 int ret;
1507 int pathlen = 0;
1508 x509_cert *parent;
1509 x509_name *name;
1510 x509_sequence *cur = NULL;
1511
1512 *flags = 0;
1513
1514 if( cn != NULL )
1515 {
1516 name = &crt->subject;
1517 cn_len = strlen( cn );
1518
1519 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1520 {
1521 cur = &crt->subject_alt_names;
1522
1523 while( cur != NULL )
1524 {
1525 if( cur->buf.len == cn_len &&
1526 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1527 break;
1528
1529 if( cur->buf.len > 2 &&
1530 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1531 x509_wildcard_verify( cn, &cur->buf ) )
1532 break;
1533
1534 cur = cur->next;
1535 }
1536
1537 if( cur == NULL )
1538 *flags |= BADCERT_CN_MISMATCH;
1539 }
1540 else
1541 {
1542 while( name != NULL )
1543 {
1544 if( OID_CMP( OID_AT_CN, &name->oid ) )
1545 {
1546 if( name->val.len == cn_len &&
1547 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1548 break;
1549
1550 if( name->val.len > 2 &&
1551 memcmp( name->val.p, "*.", 2 ) == 0 &&
1552 x509_wildcard_verify( cn, &name->val ) )
1553 break;
1554 }
1555
1556 name = name->next;
1557 }
1558
1559 if( name == NULL )
1560 *flags |= BADCERT_CN_MISMATCH;
1561 }
1562 }
1563
1564 /*
1565 * Iterate upwards in the given cert chain, to find our crt parent.
1566 * Ignore any upper cert with CA != TRUE.
1567 */
1568 parent = crt->next;
1569
1570 while( parent != NULL && parent->version != 0 )
1571 {
1572 if( parent->ca_istrue == 0 ||
1573 crt->issuer_raw.len != parent->subject_raw.len ||
1574 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
1575 crt->issuer_raw.len ) != 0 )
1576 {
1577 parent = parent->next;
1578 continue;
1579 }
1580 break;
1581 }
1582
1583 if( parent != NULL )
1584 {
1585 /*
1586 * Part of the chain
1587 */
1588 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
1589 if( ret != 0 )
1590 return( ret );
1591 }
1592 else
1593 {
1594 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
1595 if( ret != 0 )
1596 return( ret );
1597 }
1598
1599 if( *flags != 0 )
1600 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1601
1602 return( 0 );
1603}
1604
1605/*
1606 * Unallocate all certificate data
1607 */
1608void x509_crt_free( x509_cert *crt )
1609{
1610 x509_cert *cert_cur = crt;
1611 x509_cert *cert_prv;
1612 x509_name *name_cur;
1613 x509_name *name_prv;
1614 x509_sequence *seq_cur;
1615 x509_sequence *seq_prv;
1616
1617 if( crt == NULL )
1618 return;
1619
1620 do
1621 {
1622 pk_free( &cert_cur->pk );
1623
1624 name_cur = cert_cur->issuer.next;
1625 while( name_cur != NULL )
1626 {
1627 name_prv = name_cur;
1628 name_cur = name_cur->next;
1629 memset( name_prv, 0, sizeof( x509_name ) );
1630 polarssl_free( name_prv );
1631 }
1632
1633 name_cur = cert_cur->subject.next;
1634 while( name_cur != NULL )
1635 {
1636 name_prv = name_cur;
1637 name_cur = name_cur->next;
1638 memset( name_prv, 0, sizeof( x509_name ) );
1639 polarssl_free( name_prv );
1640 }
1641
1642 seq_cur = cert_cur->ext_key_usage.next;
1643 while( seq_cur != NULL )
1644 {
1645 seq_prv = seq_cur;
1646 seq_cur = seq_cur->next;
1647 memset( seq_prv, 0, sizeof( x509_sequence ) );
1648 polarssl_free( seq_prv );
1649 }
1650
1651 seq_cur = cert_cur->subject_alt_names.next;
1652 while( seq_cur != NULL )
1653 {
1654 seq_prv = seq_cur;
1655 seq_cur = seq_cur->next;
1656 memset( seq_prv, 0, sizeof( x509_sequence ) );
1657 polarssl_free( seq_prv );
1658 }
1659
1660 if( cert_cur->raw.p != NULL )
1661 {
1662 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1663 polarssl_free( cert_cur->raw.p );
1664 }
1665
1666 cert_cur = cert_cur->next;
1667 }
1668 while( cert_cur != NULL );
1669
1670 cert_cur = crt;
1671 do
1672 {
1673 cert_prv = cert_cur;
1674 cert_cur = cert_cur->next;
1675
1676 memset( cert_prv, 0, sizeof( x509_cert ) );
1677 if( cert_prv != crt )
1678 polarssl_free( cert_prv );
1679 }
1680 while( cert_cur != NULL );
1681}
1682
1683#if defined(POLARSSL_SELF_TEST)
1684
1685#include "polarssl/certs.h"
1686
1687/*
1688 * Checkup routine
1689 */
1690int x509_self_test( int verbose )
1691{
1692#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
1693 int ret;
1694 int flags;
1695 x509_cert cacert;
1696 x509_cert clicert;
1697 pk_context pkey;
1698
1699 if( verbose != 0 )
1700 printf( " X.509 certificate load: " );
1701
1702 memset( &clicert, 0, sizeof( x509_cert ) );
1703
1704 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
1705 strlen( test_cli_crt ) );
1706 if( ret != 0 )
1707 {
1708 if( verbose != 0 )
1709 printf( "failed\n" );
1710
1711 return( ret );
1712 }
1713
1714 memset( &cacert, 0, sizeof( x509_cert ) );
1715
1716 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
1717 strlen( test_ca_crt ) );
1718 if( ret != 0 )
1719 {
1720 if( verbose != 0 )
1721 printf( "failed\n" );
1722
1723 return( ret );
1724 }
1725
1726 if( verbose != 0 )
1727 printf( "passed\n X.509 signature verify: ");
1728
1729 ret = x509parse_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
1730 if( ret != 0 )
1731 {
1732 if( verbose != 0 )
1733 printf( "failed\n" );
1734
1735 printf("ret = %d, &flags = %04x\n", ret, flags);
1736
1737 return( ret );
1738 }
1739
1740 if( verbose != 0 )
1741 printf( "passed\n\n");
1742
1743 x509_crt_free( &cacert );
1744 x509_crt_free( &clicert );
1745 pk_free( &pkey );
1746
1747 return( 0 );
1748#else
1749 ((void) verbose);
1750 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
1751#endif
1752}
1753
1754#endif
1755
1756#endif