blob: 6a127b267258054c9237112af622426a719bc6d1 [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>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010056#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057#include <windows.h>
58#else
59#include <time.h>
60#endif
61
Paul Bakkerfa6a6202013-10-28 18:48:30 +010062#if defined(EFIX64) || defined(EFI32)
63#include <stdio.h>
64#endif
65
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066#if defined(POLARSSL_FS_IO)
67#include <stdio.h>
68#if !defined(_WIN32)
69#include <sys/types.h>
70#include <sys/stat.h>
71#include <dirent.h>
72#endif
73#endif
74
75/*
76 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
77 */
78static int x509_get_version( unsigned char **p,
79 const unsigned char *end,
80 int *ver )
81{
82 int ret;
83 size_t len;
84
85 if( ( ret = asn1_get_tag( p, end, &len,
86 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
87 {
88 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
89 {
90 *ver = 0;
91 return( 0 );
92 }
93
94 return( ret );
95 }
96
97 end = *p + len;
98
99 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200100 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101
102 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200103 return( POLARSSL_ERR_X509_INVALID_VERSION +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
105
106 return( 0 );
107}
108
109/*
110 * Validity ::= SEQUENCE {
111 * notBefore Time,
112 * notAfter Time }
113 */
114static int x509_get_dates( unsigned char **p,
115 const unsigned char *end,
116 x509_time *from,
117 x509_time *to )
118{
119 int ret;
120 size_t len;
121
122 if( ( ret = asn1_get_tag( p, end, &len,
123 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200124 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200125
126 end = *p + len;
127
128 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
129 return( ret );
130
131 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
132 return( ret );
133
134 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200135 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200136 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
137
138 return( 0 );
139}
140
141/*
142 * X.509 v2/v3 unique identifier (not parsed)
143 */
144static int x509_get_uid( unsigned char **p,
145 const unsigned char *end,
146 x509_buf *uid, int n )
147{
148 int ret;
149
150 if( *p == end )
151 return( 0 );
152
153 uid->tag = **p;
154
155 if( ( ret = asn1_get_tag( p, end, &uid->len,
156 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
157 {
158 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
159 return( 0 );
160
161 return( ret );
162 }
163
164 uid->p = *p;
165 *p += uid->len;
166
167 return( 0 );
168}
169
170static int x509_get_basic_constraints( unsigned char **p,
171 const unsigned char *end,
172 int *ca_istrue,
173 int *max_pathlen )
174{
175 int ret;
176 size_t len;
177
178 /*
179 * BasicConstraints ::= SEQUENCE {
180 * cA BOOLEAN DEFAULT FALSE,
181 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
182 */
183 *ca_istrue = 0; /* DEFAULT FALSE */
184 *max_pathlen = 0; /* endless */
185
186 if( ( ret = asn1_get_tag( p, end, &len,
187 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200188 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200189
190 if( *p == end )
191 return 0;
192
193 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
194 {
195 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
196 ret = asn1_get_int( p, end, ca_istrue );
197
198 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200199 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200200
201 if( *ca_istrue != 0 )
202 *ca_istrue = 1;
203 }
204
205 if( *p == end )
206 return 0;
207
208 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200209 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200210
211 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200212 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
214
215 (*max_pathlen)++;
216
217 return 0;
218}
219
220static int x509_get_ns_cert_type( unsigned char **p,
221 const unsigned char *end,
222 unsigned char *ns_cert_type)
223{
224 int ret;
225 x509_bitstring bs = { 0, 0, NULL };
226
227 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200228 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229
230 if( bs.len != 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200231 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200232 POLARSSL_ERR_ASN1_INVALID_LENGTH );
233
234 /* Get actual bitstring */
235 *ns_cert_type = *bs.p;
236 return 0;
237}
238
239static int x509_get_key_usage( unsigned char **p,
240 const unsigned char *end,
241 unsigned char *key_usage)
242{
243 int ret;
244 x509_bitstring bs = { 0, 0, NULL };
245
246 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200247 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200248
249 if( bs.len < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200250 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200251 POLARSSL_ERR_ASN1_INVALID_LENGTH );
252
253 /* Get actual bitstring */
254 *key_usage = *bs.p;
255 return 0;
256}
257
258/*
259 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
260 *
261 * KeyPurposeId ::= OBJECT IDENTIFIER
262 */
263static int x509_get_ext_key_usage( unsigned char **p,
264 const unsigned char *end,
265 x509_sequence *ext_key_usage)
266{
267 int ret;
268
269 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200270 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271
272 /* Sequence length must be >= 1 */
273 if( ext_key_usage->buf.p == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200274 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275 POLARSSL_ERR_ASN1_INVALID_LENGTH );
276
277 return 0;
278}
279
280/*
281 * SubjectAltName ::= GeneralNames
282 *
283 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
284 *
285 * GeneralName ::= CHOICE {
286 * otherName [0] OtherName,
287 * rfc822Name [1] IA5String,
288 * dNSName [2] IA5String,
289 * x400Address [3] ORAddress,
290 * directoryName [4] Name,
291 * ediPartyName [5] EDIPartyName,
292 * uniformResourceIdentifier [6] IA5String,
293 * iPAddress [7] OCTET STRING,
294 * registeredID [8] OBJECT IDENTIFIER }
295 *
296 * OtherName ::= SEQUENCE {
297 * type-id OBJECT IDENTIFIER,
298 * value [0] EXPLICIT ANY DEFINED BY type-id }
299 *
300 * EDIPartyName ::= SEQUENCE {
301 * nameAssigner [0] DirectoryString OPTIONAL,
302 * partyName [1] DirectoryString }
303 *
304 * NOTE: PolarSSL only parses and uses dNSName at this point.
305 */
306static int x509_get_subject_alt_name( unsigned char **p,
307 const unsigned char *end,
308 x509_sequence *subject_alt_name )
309{
310 int ret;
311 size_t len, tag_len;
312 asn1_buf *buf;
313 unsigned char tag;
314 asn1_sequence *cur = subject_alt_name;
315
316 /* Get main sequence tag */
317 if( ( ret = asn1_get_tag( p, end, &len,
318 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200319 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200320
321 if( *p + len != end )
Paul Bakker51876562013-09-17 14:36:05 +0200322 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
324
325 while( *p < end )
326 {
327 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200328 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 POLARSSL_ERR_ASN1_OUT_OF_DATA );
330
331 tag = **p;
332 (*p)++;
333 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200334 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335
336 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
Paul Bakker51876562013-09-17 14:36:05 +0200337 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200338 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
339
340 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
341 {
342 *p += tag_len;
343 continue;
344 }
345
346 buf = &(cur->buf);
347 buf->tag = tag;
348 buf->p = *p;
349 buf->len = tag_len;
350 *p += buf->len;
351
352 /* Allocate and assign next pointer */
353 if (*p < end)
354 {
355 cur->next = (asn1_sequence *) polarssl_malloc(
356 sizeof( asn1_sequence ) );
357
358 if( cur->next == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200359 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360 POLARSSL_ERR_ASN1_MALLOC_FAILED );
361
362 memset( cur->next, 0, sizeof( asn1_sequence ) );
363 cur = cur->next;
364 }
365 }
366
367 /* Set final sequence entry's next pointer to NULL */
368 cur->next = NULL;
369
370 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200371 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
373
374 return( 0 );
375}
376
377/*
378 * X.509 v3 extensions
379 *
380 * TODO: Perform all of the basic constraints tests required by the RFC
381 * TODO: Set values for undetected extensions to a sane default?
382 *
383 */
384static int x509_get_crt_ext( unsigned char **p,
385 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200386 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387{
388 int ret;
389 size_t len;
390 unsigned char *end_ext_data, *end_ext_octet;
391
392 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
393 {
394 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
395 return( 0 );
396
397 return( ret );
398 }
399
400 while( *p < end )
401 {
402 /*
403 * Extension ::= SEQUENCE {
404 * extnID OBJECT IDENTIFIER,
405 * critical BOOLEAN DEFAULT FALSE,
406 * extnValue OCTET STRING }
407 */
408 x509_buf extn_oid = {0, 0, NULL};
409 int is_critical = 0; /* DEFAULT FALSE */
410 int ext_type = 0;
411
412 if( ( ret = asn1_get_tag( p, end, &len,
413 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200414 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415
416 end_ext_data = *p + len;
417
418 /* Get extension ID */
419 extn_oid.tag = **p;
420
421 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200422 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423
424 extn_oid.p = *p;
425 *p += extn_oid.len;
426
427 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200428 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429 POLARSSL_ERR_ASN1_OUT_OF_DATA );
430
431 /* Get optional critical */
432 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
433 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200434 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
436 /* Data should be octet string type */
437 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
438 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200439 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440
441 end_ext_octet = *p + len;
442
443 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200444 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
446
447 /*
448 * Detect supported extensions
449 */
450 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
451
452 if( ret != 0 )
453 {
454 /* No parser found, skip extension */
455 *p = end_ext_octet;
456
457#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
458 if( is_critical )
459 {
460 /* Data is marked as critical: fail */
Paul Bakker51876562013-09-17 14:36:05 +0200461 return ( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
463 }
464#endif
465 continue;
466 }
467
468 crt->ext_types |= ext_type;
469
470 switch( ext_type )
471 {
472 case EXT_BASIC_CONSTRAINTS:
473 /* Parse basic constraints */
474 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
475 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
476 return ( ret );
477 break;
478
479 case EXT_KEY_USAGE:
480 /* Parse key usage */
481 if( ( ret = x509_get_key_usage( p, end_ext_octet,
482 &crt->key_usage ) ) != 0 )
483 return ( ret );
484 break;
485
486 case EXT_EXTENDED_KEY_USAGE:
487 /* Parse extended key usage */
488 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
489 &crt->ext_key_usage ) ) != 0 )
490 return ( ret );
491 break;
492
493 case EXT_SUBJECT_ALT_NAME:
494 /* Parse subject alt name */
495 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
496 &crt->subject_alt_names ) ) != 0 )
497 return ( ret );
498 break;
499
500 case EXT_NS_CERT_TYPE:
501 /* Parse netscape certificate type */
502 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
503 &crt->ns_cert_type ) ) != 0 )
504 return ( ret );
505 break;
506
507 default:
508 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
509 }
510 }
511
512 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200513 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
515
516 return( 0 );
517}
518
519/*
520 * Parse and fill a single X.509 certificate in DER format
521 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200522static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200523 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524{
525 int ret;
526 size_t len;
527 unsigned char *p, *end, *crt_end;
528
529 /*
530 * Check for valid input
531 */
532 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200533 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534
535 p = (unsigned char *) polarssl_malloc( len = buflen );
536
537 if( p == NULL )
538 return( POLARSSL_ERR_X509_MALLOC_FAILED );
539
540 memcpy( p, buf, buflen );
541
542 buflen = 0;
543
544 crt->raw.p = p;
545 crt->raw.len = len;
546 end = p + len;
547
548 /*
549 * Certificate ::= SEQUENCE {
550 * tbsCertificate TBSCertificate,
551 * signatureAlgorithm AlgorithmIdentifier,
552 * signatureValue BIT STRING }
553 */
554 if( ( ret = asn1_get_tag( &p, end, &len,
555 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
556 {
557 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200558 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559 }
560
561 if( len > (size_t) ( end - p ) )
562 {
563 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200564 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
566 }
567 crt_end = p + len;
568
569 /*
570 * TBSCertificate ::= SEQUENCE {
571 */
572 crt->tbs.p = p;
573
574 if( ( ret = asn1_get_tag( &p, end, &len,
575 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
576 {
577 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200578 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579 }
580
581 end = p + len;
582 crt->tbs.len = end - crt->tbs.p;
583
584 /*
585 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
586 *
587 * CertificateSerialNumber ::= INTEGER
588 *
589 * signature AlgorithmIdentifier
590 */
591 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
592 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
593 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
594 {
595 x509_crt_free( crt );
596 return( ret );
597 }
598
599 crt->version++;
600
601 if( crt->version > 3 )
602 {
603 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200604 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605 }
606
607 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
608 &crt->sig_pk ) ) != 0 )
609 {
610 x509_crt_free( crt );
611 return( ret );
612 }
613
614 /*
615 * issuer Name
616 */
617 crt->issuer_raw.p = p;
618
619 if( ( ret = asn1_get_tag( &p, end, &len,
620 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
621 {
622 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200623 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624 }
625
626 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
627 {
628 x509_crt_free( crt );
629 return( ret );
630 }
631
632 crt->issuer_raw.len = p - crt->issuer_raw.p;
633
634 /*
635 * Validity ::= SEQUENCE {
636 * notBefore Time,
637 * notAfter Time }
638 *
639 */
640 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
641 &crt->valid_to ) ) != 0 )
642 {
643 x509_crt_free( crt );
644 return( ret );
645 }
646
647 /*
648 * subject Name
649 */
650 crt->subject_raw.p = p;
651
652 if( ( ret = asn1_get_tag( &p, end, &len,
653 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
654 {
655 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200656 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657 }
658
659 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
660 {
661 x509_crt_free( crt );
662 return( ret );
663 }
664
665 crt->subject_raw.len = p - crt->subject_raw.p;
666
667 /*
668 * SubjectPublicKeyInfo
669 */
Paul Bakkerda771152013-09-16 22:45:03 +0200670 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671 {
672 x509_crt_free( crt );
673 return( ret );
674 }
675
676 /*
677 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
678 * -- If present, version shall be v2 or v3
679 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
680 * -- If present, version shall be v2 or v3
681 * extensions [3] EXPLICIT Extensions OPTIONAL
682 * -- If present, version shall be v3
683 */
684 if( crt->version == 2 || crt->version == 3 )
685 {
686 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
687 if( ret != 0 )
688 {
689 x509_crt_free( crt );
690 return( ret );
691 }
692 }
693
694 if( crt->version == 2 || crt->version == 3 )
695 {
696 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
697 if( ret != 0 )
698 {
699 x509_crt_free( crt );
700 return( ret );
701 }
702 }
703
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200704#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705 if( crt->version == 3 )
706 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200707#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 ret = x509_get_crt_ext( &p, end, crt);
709 if( ret != 0 )
710 {
711 x509_crt_free( crt );
712 return( ret );
713 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200714#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200716#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717
718 if( p != end )
719 {
720 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200721 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
723 }
724
725 end = crt_end;
726
727 /*
728 * }
729 * -- end of TBSCertificate
730 *
731 * signatureAlgorithm AlgorithmIdentifier,
732 * signatureValue BIT STRING
733 */
734 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
735 {
736 x509_crt_free( crt );
737 return( ret );
738 }
739
740 if( crt->sig_oid1.len != crt->sig_oid2.len ||
741 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
742 {
743 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200744 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745 }
746
747 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
748 {
749 x509_crt_free( crt );
750 return( ret );
751 }
752
753 if( p != end )
754 {
755 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200756 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
758 }
759
760 return( 0 );
761}
762
763/*
764 * Parse one X.509 certificate in DER format from a buffer and add them to a
765 * chained list
766 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200767int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200768 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769{
770 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200771 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772
773 /*
774 * Check for valid input
775 */
776 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200777 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200778
779 while( crt->version != 0 && crt->next != NULL )
780 {
781 prev = crt;
782 crt = crt->next;
783 }
784
785 /*
786 * Add new certificate on the end of the chain if needed.
787 */
788 if ( crt->version != 0 && crt->next == NULL)
789 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200790 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791
792 if( crt->next == NULL )
793 return( POLARSSL_ERR_X509_MALLOC_FAILED );
794
795 prev = crt;
796 crt = crt->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200797 x509_crt_init( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798 }
799
Paul Bakkerddf26b42013-09-18 13:46:23 +0200800 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801 {
802 if( prev )
803 prev->next = NULL;
804
805 if( crt != chain )
806 polarssl_free( crt );
807
808 return( ret );
809 }
810
811 return( 0 );
812}
813
814/*
815 * Parse one or more PEM certificates from a buffer and add them to the chained list
816 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200817int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818{
819 int success = 0, first_error = 0, total_failed = 0;
820 int buf_format = X509_FORMAT_DER;
821
822 /*
823 * Check for valid input
824 */
825 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200826 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827
828 /*
829 * Determine buffer content. Buffer contains either one DER certificate or
830 * one or more PEM certificates.
831 */
832#if defined(POLARSSL_PEM_PARSE_C)
833 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
834 buf_format = X509_FORMAT_PEM;
835#endif
836
837 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200838 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839
840#if defined(POLARSSL_PEM_PARSE_C)
841 if( buf_format == X509_FORMAT_PEM )
842 {
843 int ret;
844 pem_context pem;
845
846 while( buflen > 0 )
847 {
848 size_t use_len;
849 pem_init( &pem );
850
851 ret = pem_read_buffer( &pem,
852 "-----BEGIN CERTIFICATE-----",
853 "-----END CERTIFICATE-----",
854 buf, NULL, 0, &use_len );
855
856 if( ret == 0 )
857 {
858 /*
859 * Was PEM encoded
860 */
861 buflen -= use_len;
862 buf += use_len;
863 }
864 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
865 {
866 return( ret );
867 }
868 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
869 {
870 pem_free( &pem );
871
872 /*
873 * PEM header and footer were found
874 */
875 buflen -= use_len;
876 buf += use_len;
877
878 if( first_error == 0 )
879 first_error = ret;
880
881 continue;
882 }
883 else
884 break;
885
Paul Bakkerddf26b42013-09-18 13:46:23 +0200886 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887
888 pem_free( &pem );
889
890 if( ret != 0 )
891 {
892 /*
893 * Quit parsing on a memory error
894 */
895 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
896 return( ret );
897
898 if( first_error == 0 )
899 first_error = ret;
900
901 total_failed++;
902 continue;
903 }
904
905 success = 1;
906 }
907 }
908#endif
909
910 if( success )
911 return( total_failed );
912 else if( first_error )
913 return( first_error );
914 else
915 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
916}
917
918#if defined(POLARSSL_FS_IO)
919/*
920 * Load one or more certificates and add them to the chained list
921 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200922int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923{
924 int ret;
925 size_t n;
926 unsigned char *buf;
927
928 if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
929 return( ret );
930
Paul Bakkerddf26b42013-09-18 13:46:23 +0200931 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932
933 memset( buf, 0, n + 1 );
934 polarssl_free( buf );
935
936 return( ret );
937}
938
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200939int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940{
941 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100942#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943 int w_ret;
944 WCHAR szDir[MAX_PATH];
945 char filename[MAX_PATH];
946 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200947 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948
949 WIN32_FIND_DATAW file_data;
950 HANDLE hFind;
951
952 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200953 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
955 memset( szDir, 0, sizeof(szDir) );
956 memset( filename, 0, MAX_PATH );
957 memcpy( filename, path, len );
958 filename[len++] = '\\';
959 p = filename + len;
960 filename[len++] = '*';
961
962 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
963
964 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200965 if (hFind == INVALID_HANDLE_VALUE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
967
968 len = MAX_PATH - len;
969 do
970 {
971 memset( p, 0, len );
972
973 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
974 continue;
975
976 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
977 lstrlenW(file_data.cFileName),
978 p, len - 1,
979 NULL, NULL );
980
Paul Bakkerddf26b42013-09-18 13:46:23 +0200981 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 if( w_ret < 0 )
983 ret++;
984 else
985 ret += w_ret;
986 }
987 while( FindNextFileW( hFind, &file_data ) != 0 );
988
Paul Bakker4aa40d42013-10-11 10:49:24 +0200989 if (GetLastError() != ERROR_NO_MORE_FILES)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
991
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +0200993#else /* _WIN32 */
994#if defined(POLARSSL_HAVE_READDIR_R)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995 int t_ret, i;
996 struct stat sb;
997 struct dirent entry, *result = NULL;
998 char entry_name[255];
999 DIR *dir = opendir( path );
1000
1001 if( dir == NULL)
1002 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1003
1004 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
1005 {
1006 if( result == NULL )
1007 break;
1008
1009 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1010
1011 i = stat( entry_name, &sb );
1012
1013 if( i == -1 )
1014 {
1015 closedir( dir );
1016 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1017 }
1018
1019 if( !S_ISREG( sb.st_mode ) )
1020 continue;
1021
1022 // Ignore parse errors
1023 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001024 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025 if( t_ret < 0 )
1026 ret++;
1027 else
1028 ret += t_ret;
1029 }
1030 closedir( dir );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001031#else /* POLARSSL_HAVE_READDIR_R */
1032 ((void) chain);
1033 ((void) path);
1034 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
1035#endif /* POLARSSL_HAVE_READDIR_R */
1036#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037
1038 return( ret );
1039}
1040#endif /* POLARSSL_FS_IO */
1041
Paul Bakker6edcd412013-10-29 15:22:54 +01001042#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1043 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044#include <stdarg.h>
1045
1046#if !defined vsnprintf
1047#define vsnprintf _vsnprintf
1048#endif // vsnprintf
1049
1050/*
1051 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1052 * Result value is not size of buffer needed, but -1 if no fit is possible.
1053 *
1054 * This fuction tries to 'fix' this by at least suggesting enlarging the
1055 * size by 20.
1056 */
1057static int compat_snprintf(char *str, size_t size, const char *format, ...)
1058{
1059 va_list ap;
1060 int res = -1;
1061
1062 va_start( ap, format );
1063
1064 res = vsnprintf( str, size, format, ap );
1065
1066 va_end( ap );
1067
1068 // No quick fix possible
1069 if ( res < 0 )
1070 return( (int) size + 20 );
1071
1072 return res;
1073}
1074
1075#define snprintf compat_snprintf
1076#endif
1077
1078#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1079
1080#define SAFE_SNPRINTF() \
1081{ \
1082 if( ret == -1 ) \
1083 return( -1 ); \
1084 \
1085 if ( (unsigned int) ret > n ) { \
1086 p[n - 1] = '\0'; \
1087 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1088 } \
1089 \
1090 n -= (unsigned int) ret; \
1091 p += (unsigned int) ret; \
1092}
1093
1094/*
1095 * Return an informational string about the certificate.
1096 */
1097#define BEFORE_COLON 14
1098#define BC "14"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001099int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001100 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101{
1102 int ret;
1103 size_t n;
1104 char *p;
1105 const char *desc = NULL;
1106 char key_size_str[BEFORE_COLON];
1107
1108 p = buf;
1109 n = size;
1110
1111 ret = snprintf( p, n, "%scert. version : %d\n",
1112 prefix, crt->version );
1113 SAFE_SNPRINTF();
1114 ret = snprintf( p, n, "%sserial number : ",
1115 prefix );
1116 SAFE_SNPRINTF();
1117
Paul Bakker86d0c192013-09-18 11:11:02 +02001118 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119 SAFE_SNPRINTF();
1120
1121 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
1122 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001123 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124 SAFE_SNPRINTF();
1125
1126 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
1127 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001128 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129 SAFE_SNPRINTF();
1130
1131 ret = snprintf( p, n, "\n%sissued on : " \
1132 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1133 crt->valid_from.year, crt->valid_from.mon,
1134 crt->valid_from.day, crt->valid_from.hour,
1135 crt->valid_from.min, crt->valid_from.sec );
1136 SAFE_SNPRINTF();
1137
1138 ret = snprintf( p, n, "\n%sexpires on : " \
1139 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1140 crt->valid_to.year, crt->valid_to.mon,
1141 crt->valid_to.day, crt->valid_to.hour,
1142 crt->valid_to.min, crt->valid_to.sec );
1143 SAFE_SNPRINTF();
1144
1145 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
1146 SAFE_SNPRINTF();
1147
1148 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1149 if( ret != 0 )
1150 ret = snprintf( p, n, "???" );
1151 else
1152 ret = snprintf( p, n, "%s", desc );
1153 SAFE_SNPRINTF();
1154
1155 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1156 pk_get_name( &crt->pk ) ) ) != 0 )
1157 {
1158 return( ret );
1159 }
1160
1161 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
1162 (int) pk_get_size( &crt->pk ) );
1163 SAFE_SNPRINTF();
1164
1165 return( (int) ( size - n ) );
1166}
1167
1168#if defined(POLARSSL_X509_CRL_PARSE_C)
1169/*
1170 * Return 1 if the certificate is revoked, or 0 otherwise.
1171 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001172int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173{
1174 const x509_crl_entry *cur = &crl->entry;
1175
1176 while( cur != NULL && cur->serial.len != 0 )
1177 {
1178 if( crt->serial.len == cur->serial.len &&
1179 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1180 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001181 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 return( 1 );
1183 }
1184
1185 cur = cur->next;
1186 }
1187
1188 return( 0 );
1189}
1190
1191/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001192 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001193 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001194static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001195 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001196{
1197 int flags = 0;
1198 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1199 const md_info_t *md_info;
1200
1201 if( ca == NULL )
1202 return( flags );
1203
1204 /*
1205 * TODO: What happens if no CRL is present?
1206 * Suggestion: Revocation state should be unknown if no CRL is present.
1207 * For backwards compatibility this is not yet implemented.
1208 */
1209
1210 while( crl_list != NULL )
1211 {
1212 if( crl_list->version == 0 ||
1213 crl_list->issuer_raw.len != ca->subject_raw.len ||
1214 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1215 crl_list->issuer_raw.len ) != 0 )
1216 {
1217 crl_list = crl_list->next;
1218 continue;
1219 }
1220
1221 /*
1222 * Check if CRL is correctly signed by the trusted CA
1223 */
1224 md_info = md_info_from_type( crl_list->sig_md );
1225 if( md_info == NULL )
1226 {
1227 /*
1228 * Cannot check 'unknown' hash
1229 */
1230 flags |= BADCRL_NOT_TRUSTED;
1231 break;
1232 }
1233
1234 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1235
1236 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1237 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1238 crl_list->sig.p, crl_list->sig.len ) != 0 )
1239 {
1240 flags |= BADCRL_NOT_TRUSTED;
1241 break;
1242 }
1243
1244 /*
1245 * Check for validity of CRL (Do not drop out)
1246 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001247 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001248 flags |= BADCRL_EXPIRED;
1249
1250 /*
1251 * Check if certificate is revoked
1252 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001253 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001254 {
1255 flags |= BADCERT_REVOKED;
1256 break;
1257 }
1258
1259 crl_list = crl_list->next;
1260 }
1261 return flags;
1262}
1263#endif /* POLARSSL_X509_CRL_PARSE_C */
1264
1265// Equal == 0, inequal == 1
1266static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1267{
1268 size_t i;
1269 unsigned char diff;
1270 const unsigned char *n1 = s1, *n2 = s2;
1271
1272 for( i = 0; i < len; i++ )
1273 {
1274 diff = n1[i] ^ n2[i];
1275
1276 if( ( n1[i] >= 'a' || n1[i] <= 'z' ) && ( diff == 0 || diff == 32 ) )
1277 continue;
1278
1279 if( ( n1[i] >= 'A' || n1[i] <= 'Z' ) && ( diff == 0 || diff == 32 ) )
1280 continue;
1281
1282 return( 1 );
1283 }
1284
1285 return( 0 );
1286}
1287
1288static int x509_wildcard_verify( const char *cn, x509_buf *name )
1289{
1290 size_t i;
1291 size_t cn_idx = 0;
1292
1293 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1294 return( 0 );
1295
1296 for( i = 0; i < strlen( cn ); ++i )
1297 {
1298 if( cn[i] == '.' )
1299 {
1300 cn_idx = i;
1301 break;
1302 }
1303 }
1304
1305 if( cn_idx == 0 )
1306 return( 0 );
1307
1308 if( strlen( cn ) - cn_idx == name->len - 1 &&
1309 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1310 {
1311 return( 1 );
1312 }
1313
1314 return( 0 );
1315}
1316
Paul Bakkerddf26b42013-09-18 13:46:23 +02001317static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001318 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001320 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001321 void *p_vrfy )
1322{
1323 int ret;
1324 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1325 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1326 const md_info_t *md_info;
1327
Paul Bakker86d0c192013-09-18 11:11:02 +02001328 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001329 *flags |= BADCERT_EXPIRED;
1330
1331 /*
1332 * Child is the top of the chain. Check against the trust_ca list.
1333 */
1334 *flags |= BADCERT_NOT_TRUSTED;
1335
1336 md_info = md_info_from_type( child->sig_md );
1337 if( md_info == NULL )
1338 {
1339 /*
1340 * Cannot check 'unknown', no need to try any CA
1341 */
1342 trust_ca = NULL;
1343 }
1344 else
1345 md( md_info, child->tbs.p, child->tbs.len, hash );
1346
1347 while( trust_ca != NULL )
1348 {
1349 if( trust_ca->version == 0 ||
1350 child->issuer_raw.len != trust_ca->subject_raw.len ||
1351 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
1352 child->issuer_raw.len ) != 0 )
1353 {
1354 trust_ca = trust_ca->next;
1355 continue;
1356 }
1357
1358 /*
1359 * Reduce path_len to check against if top of the chain is
1360 * the same as the trusted CA
1361 */
1362 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1363 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1364 child->issuer_raw.len ) == 0 )
1365 {
1366 check_path_cnt--;
1367 }
1368
1369 if( trust_ca->max_pathlen > 0 &&
1370 trust_ca->max_pathlen < check_path_cnt )
1371 {
1372 trust_ca = trust_ca->next;
1373 continue;
1374 }
1375
1376 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1377 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1378 child->sig.p, child->sig.len ) != 0 )
1379 {
1380 trust_ca = trust_ca->next;
1381 continue;
1382 }
1383
1384 /*
1385 * Top of chain is signed by a trusted CA
1386 */
1387 *flags &= ~BADCERT_NOT_TRUSTED;
1388 break;
1389 }
1390
1391 /*
1392 * If top of chain is not the same as the trusted CA send a verify request
1393 * to the callback for any issues with validity and CRL presence for the
1394 * trusted CA certificate.
1395 */
1396 if( trust_ca != NULL &&
1397 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1398 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1399 child->issuer_raw.len ) != 0 ) )
1400 {
1401#if defined(POLARSSL_X509_CRL_PARSE_C)
1402 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001403 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001404#else
1405 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406#endif
1407
Paul Bakker86d0c192013-09-18 11:11:02 +02001408 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001409 ca_flags |= BADCERT_EXPIRED;
1410
1411 if( NULL != f_vrfy )
1412 {
1413 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
1414 return( ret );
1415 }
1416 }
1417
1418 /* Call callback on top cert */
1419 if( NULL != f_vrfy )
1420 {
1421 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1422 return( ret );
1423 }
1424
1425 *flags |= ca_flags;
1426
1427 return( 0 );
1428}
1429
Paul Bakkerddf26b42013-09-18 13:46:23 +02001430static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001431 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001432 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001433 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001434 void *p_vrfy )
1435{
1436 int ret;
1437 int parent_flags = 0;
1438 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001439 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440 const md_info_t *md_info;
1441
Paul Bakker86d0c192013-09-18 11:11:02 +02001442 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443 *flags |= BADCERT_EXPIRED;
1444
1445 md_info = md_info_from_type( child->sig_md );
1446 if( md_info == NULL )
1447 {
1448 /*
1449 * Cannot check 'unknown' hash
1450 */
1451 *flags |= BADCERT_NOT_TRUSTED;
1452 }
1453 else
1454 {
1455 md( md_info, child->tbs.p, child->tbs.len, hash );
1456
1457 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1458 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1459 child->sig.p, child->sig.len ) != 0 )
1460 {
1461 *flags |= BADCERT_NOT_TRUSTED;
1462 }
1463 }
1464
1465#if defined(POLARSSL_X509_CRL_PARSE_C)
1466 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001467 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468#endif
1469
1470 grandparent = parent->next;
1471
1472 while( grandparent != NULL )
1473 {
1474 if( grandparent->version == 0 ||
1475 grandparent->ca_istrue == 0 ||
1476 parent->issuer_raw.len != grandparent->subject_raw.len ||
1477 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
1478 parent->issuer_raw.len ) != 0 )
1479 {
1480 grandparent = grandparent->next;
1481 continue;
1482 }
1483 break;
1484 }
1485
1486 if( grandparent != NULL )
1487 {
1488 /*
1489 * Part of the chain
1490 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001491 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 +02001492 if( ret != 0 )
1493 return( ret );
1494 }
1495 else
1496 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001497 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 +02001498 if( ret != 0 )
1499 return( ret );
1500 }
1501
1502 /* child is verified to be a child of the parent, call verify callback */
1503 if( NULL != f_vrfy )
1504 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1505 return( ret );
1506
1507 *flags |= parent_flags;
1508
1509 return( 0 );
1510}
1511
1512/*
1513 * Verify the certificate validity
1514 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001515int x509_crt_verify( x509_crt *crt,
1516 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001517 x509_crl *ca_crl,
1518 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001519 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001520 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001521{
1522 size_t cn_len;
1523 int ret;
1524 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001525 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526 x509_name *name;
1527 x509_sequence *cur = NULL;
1528
1529 *flags = 0;
1530
1531 if( cn != NULL )
1532 {
1533 name = &crt->subject;
1534 cn_len = strlen( cn );
1535
1536 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1537 {
1538 cur = &crt->subject_alt_names;
1539
1540 while( cur != NULL )
1541 {
1542 if( cur->buf.len == cn_len &&
1543 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1544 break;
1545
1546 if( cur->buf.len > 2 &&
1547 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1548 x509_wildcard_verify( cn, &cur->buf ) )
1549 break;
1550
1551 cur = cur->next;
1552 }
1553
1554 if( cur == NULL )
1555 *flags |= BADCERT_CN_MISMATCH;
1556 }
1557 else
1558 {
1559 while( name != NULL )
1560 {
1561 if( OID_CMP( OID_AT_CN, &name->oid ) )
1562 {
1563 if( name->val.len == cn_len &&
1564 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1565 break;
1566
1567 if( name->val.len > 2 &&
1568 memcmp( name->val.p, "*.", 2 ) == 0 &&
1569 x509_wildcard_verify( cn, &name->val ) )
1570 break;
1571 }
1572
1573 name = name->next;
1574 }
1575
1576 if( name == NULL )
1577 *flags |= BADCERT_CN_MISMATCH;
1578 }
1579 }
1580
1581 /*
1582 * Iterate upwards in the given cert chain, to find our crt parent.
1583 * Ignore any upper cert with CA != TRUE.
1584 */
1585 parent = crt->next;
1586
1587 while( parent != NULL && parent->version != 0 )
1588 {
1589 if( parent->ca_istrue == 0 ||
1590 crt->issuer_raw.len != parent->subject_raw.len ||
1591 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
1592 crt->issuer_raw.len ) != 0 )
1593 {
1594 parent = parent->next;
1595 continue;
1596 }
1597 break;
1598 }
1599
1600 if( parent != NULL )
1601 {
1602 /*
1603 * Part of the chain
1604 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001605 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001606 if( ret != 0 )
1607 return( ret );
1608 }
1609 else
1610 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001611 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612 if( ret != 0 )
1613 return( ret );
1614 }
1615
1616 if( *flags != 0 )
1617 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1618
1619 return( 0 );
1620}
1621
1622/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001623 * Initialize a certificate chain
1624 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001625void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001626{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001627 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001628}
1629
1630/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631 * Unallocate all certificate data
1632 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001633void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001635 x509_crt *cert_cur = crt;
1636 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637 x509_name *name_cur;
1638 x509_name *name_prv;
1639 x509_sequence *seq_cur;
1640 x509_sequence *seq_prv;
1641
1642 if( crt == NULL )
1643 return;
1644
1645 do
1646 {
1647 pk_free( &cert_cur->pk );
1648
1649 name_cur = cert_cur->issuer.next;
1650 while( name_cur != NULL )
1651 {
1652 name_prv = name_cur;
1653 name_cur = name_cur->next;
1654 memset( name_prv, 0, sizeof( x509_name ) );
1655 polarssl_free( name_prv );
1656 }
1657
1658 name_cur = cert_cur->subject.next;
1659 while( name_cur != NULL )
1660 {
1661 name_prv = name_cur;
1662 name_cur = name_cur->next;
1663 memset( name_prv, 0, sizeof( x509_name ) );
1664 polarssl_free( name_prv );
1665 }
1666
1667 seq_cur = cert_cur->ext_key_usage.next;
1668 while( seq_cur != NULL )
1669 {
1670 seq_prv = seq_cur;
1671 seq_cur = seq_cur->next;
1672 memset( seq_prv, 0, sizeof( x509_sequence ) );
1673 polarssl_free( seq_prv );
1674 }
1675
1676 seq_cur = cert_cur->subject_alt_names.next;
1677 while( seq_cur != NULL )
1678 {
1679 seq_prv = seq_cur;
1680 seq_cur = seq_cur->next;
1681 memset( seq_prv, 0, sizeof( x509_sequence ) );
1682 polarssl_free( seq_prv );
1683 }
1684
1685 if( cert_cur->raw.p != NULL )
1686 {
1687 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1688 polarssl_free( cert_cur->raw.p );
1689 }
1690
1691 cert_cur = cert_cur->next;
1692 }
1693 while( cert_cur != NULL );
1694
1695 cert_cur = crt;
1696 do
1697 {
1698 cert_prv = cert_cur;
1699 cert_cur = cert_cur->next;
1700
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001701 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702 if( cert_prv != crt )
1703 polarssl_free( cert_prv );
1704 }
1705 while( cert_cur != NULL );
1706}
1707
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001708#endif