blob: 9470132baed7c8ad063f4ff7f2b0669100d3ed1c [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 Bakkerfa6a6202013-10-28 18:48:30 +01001042#if defined(_MSC_VER) && !defined snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043#include <stdarg.h>
1044
1045#if !defined vsnprintf
1046#define vsnprintf _vsnprintf
1047#endif // vsnprintf
1048
1049/*
1050 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1051 * Result value is not size of buffer needed, but -1 if no fit is possible.
1052 *
1053 * This fuction tries to 'fix' this by at least suggesting enlarging the
1054 * size by 20.
1055 */
1056static int compat_snprintf(char *str, size_t size, const char *format, ...)
1057{
1058 va_list ap;
1059 int res = -1;
1060
1061 va_start( ap, format );
1062
1063 res = vsnprintf( str, size, format, ap );
1064
1065 va_end( ap );
1066
1067 // No quick fix possible
1068 if ( res < 0 )
1069 return( (int) size + 20 );
1070
1071 return res;
1072}
1073
1074#define snprintf compat_snprintf
1075#endif
1076
1077#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1078
1079#define SAFE_SNPRINTF() \
1080{ \
1081 if( ret == -1 ) \
1082 return( -1 ); \
1083 \
1084 if ( (unsigned int) ret > n ) { \
1085 p[n - 1] = '\0'; \
1086 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1087 } \
1088 \
1089 n -= (unsigned int) ret; \
1090 p += (unsigned int) ret; \
1091}
1092
1093/*
1094 * Return an informational string about the certificate.
1095 */
1096#define BEFORE_COLON 14
1097#define BC "14"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001098int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001099 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100{
1101 int ret;
1102 size_t n;
1103 char *p;
1104 const char *desc = NULL;
1105 char key_size_str[BEFORE_COLON];
1106
1107 p = buf;
1108 n = size;
1109
1110 ret = snprintf( p, n, "%scert. version : %d\n",
1111 prefix, crt->version );
1112 SAFE_SNPRINTF();
1113 ret = snprintf( p, n, "%sserial number : ",
1114 prefix );
1115 SAFE_SNPRINTF();
1116
Paul Bakker86d0c192013-09-18 11:11:02 +02001117 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118 SAFE_SNPRINTF();
1119
1120 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
1121 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001122 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123 SAFE_SNPRINTF();
1124
1125 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
1126 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001127 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128 SAFE_SNPRINTF();
1129
1130 ret = snprintf( p, n, "\n%sissued on : " \
1131 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1132 crt->valid_from.year, crt->valid_from.mon,
1133 crt->valid_from.day, crt->valid_from.hour,
1134 crt->valid_from.min, crt->valid_from.sec );
1135 SAFE_SNPRINTF();
1136
1137 ret = snprintf( p, n, "\n%sexpires on : " \
1138 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1139 crt->valid_to.year, crt->valid_to.mon,
1140 crt->valid_to.day, crt->valid_to.hour,
1141 crt->valid_to.min, crt->valid_to.sec );
1142 SAFE_SNPRINTF();
1143
1144 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
1145 SAFE_SNPRINTF();
1146
1147 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1148 if( ret != 0 )
1149 ret = snprintf( p, n, "???" );
1150 else
1151 ret = snprintf( p, n, "%s", desc );
1152 SAFE_SNPRINTF();
1153
1154 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1155 pk_get_name( &crt->pk ) ) ) != 0 )
1156 {
1157 return( ret );
1158 }
1159
1160 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
1161 (int) pk_get_size( &crt->pk ) );
1162 SAFE_SNPRINTF();
1163
1164 return( (int) ( size - n ) );
1165}
1166
1167#if defined(POLARSSL_X509_CRL_PARSE_C)
1168/*
1169 * Return 1 if the certificate is revoked, or 0 otherwise.
1170 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001171int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172{
1173 const x509_crl_entry *cur = &crl->entry;
1174
1175 while( cur != NULL && cur->serial.len != 0 )
1176 {
1177 if( crt->serial.len == cur->serial.len &&
1178 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1179 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001180 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181 return( 1 );
1182 }
1183
1184 cur = cur->next;
1185 }
1186
1187 return( 0 );
1188}
1189
1190/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001191 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001193static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001194 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001195{
1196 int flags = 0;
1197 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1198 const md_info_t *md_info;
1199
1200 if( ca == NULL )
1201 return( flags );
1202
1203 /*
1204 * TODO: What happens if no CRL is present?
1205 * Suggestion: Revocation state should be unknown if no CRL is present.
1206 * For backwards compatibility this is not yet implemented.
1207 */
1208
1209 while( crl_list != NULL )
1210 {
1211 if( crl_list->version == 0 ||
1212 crl_list->issuer_raw.len != ca->subject_raw.len ||
1213 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1214 crl_list->issuer_raw.len ) != 0 )
1215 {
1216 crl_list = crl_list->next;
1217 continue;
1218 }
1219
1220 /*
1221 * Check if CRL is correctly signed by the trusted CA
1222 */
1223 md_info = md_info_from_type( crl_list->sig_md );
1224 if( md_info == NULL )
1225 {
1226 /*
1227 * Cannot check 'unknown' hash
1228 */
1229 flags |= BADCRL_NOT_TRUSTED;
1230 break;
1231 }
1232
1233 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1234
1235 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1236 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1237 crl_list->sig.p, crl_list->sig.len ) != 0 )
1238 {
1239 flags |= BADCRL_NOT_TRUSTED;
1240 break;
1241 }
1242
1243 /*
1244 * Check for validity of CRL (Do not drop out)
1245 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001246 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001247 flags |= BADCRL_EXPIRED;
1248
1249 /*
1250 * Check if certificate is revoked
1251 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001252 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001253 {
1254 flags |= BADCERT_REVOKED;
1255 break;
1256 }
1257
1258 crl_list = crl_list->next;
1259 }
1260 return flags;
1261}
1262#endif /* POLARSSL_X509_CRL_PARSE_C */
1263
1264// Equal == 0, inequal == 1
1265static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1266{
1267 size_t i;
1268 unsigned char diff;
1269 const unsigned char *n1 = s1, *n2 = s2;
1270
1271 for( i = 0; i < len; i++ )
1272 {
1273 diff = n1[i] ^ n2[i];
1274
1275 if( ( n1[i] >= 'a' || n1[i] <= 'z' ) && ( diff == 0 || diff == 32 ) )
1276 continue;
1277
1278 if( ( n1[i] >= 'A' || n1[i] <= 'Z' ) && ( diff == 0 || diff == 32 ) )
1279 continue;
1280
1281 return( 1 );
1282 }
1283
1284 return( 0 );
1285}
1286
1287static int x509_wildcard_verify( const char *cn, x509_buf *name )
1288{
1289 size_t i;
1290 size_t cn_idx = 0;
1291
1292 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1293 return( 0 );
1294
1295 for( i = 0; i < strlen( cn ); ++i )
1296 {
1297 if( cn[i] == '.' )
1298 {
1299 cn_idx = i;
1300 break;
1301 }
1302 }
1303
1304 if( cn_idx == 0 )
1305 return( 0 );
1306
1307 if( strlen( cn ) - cn_idx == name->len - 1 &&
1308 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1309 {
1310 return( 1 );
1311 }
1312
1313 return( 0 );
1314}
1315
Paul Bakkerddf26b42013-09-18 13:46:23 +02001316static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001317 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001318 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001319 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001320 void *p_vrfy )
1321{
1322 int ret;
1323 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1324 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1325 const md_info_t *md_info;
1326
Paul Bakker86d0c192013-09-18 11:11:02 +02001327 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328 *flags |= BADCERT_EXPIRED;
1329
1330 /*
1331 * Child is the top of the chain. Check against the trust_ca list.
1332 */
1333 *flags |= BADCERT_NOT_TRUSTED;
1334
1335 md_info = md_info_from_type( child->sig_md );
1336 if( md_info == NULL )
1337 {
1338 /*
1339 * Cannot check 'unknown', no need to try any CA
1340 */
1341 trust_ca = NULL;
1342 }
1343 else
1344 md( md_info, child->tbs.p, child->tbs.len, hash );
1345
1346 while( trust_ca != NULL )
1347 {
1348 if( trust_ca->version == 0 ||
1349 child->issuer_raw.len != trust_ca->subject_raw.len ||
1350 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
1351 child->issuer_raw.len ) != 0 )
1352 {
1353 trust_ca = trust_ca->next;
1354 continue;
1355 }
1356
1357 /*
1358 * Reduce path_len to check against if top of the chain is
1359 * the same as the trusted CA
1360 */
1361 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1362 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1363 child->issuer_raw.len ) == 0 )
1364 {
1365 check_path_cnt--;
1366 }
1367
1368 if( trust_ca->max_pathlen > 0 &&
1369 trust_ca->max_pathlen < check_path_cnt )
1370 {
1371 trust_ca = trust_ca->next;
1372 continue;
1373 }
1374
1375 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1376 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1377 child->sig.p, child->sig.len ) != 0 )
1378 {
1379 trust_ca = trust_ca->next;
1380 continue;
1381 }
1382
1383 /*
1384 * Top of chain is signed by a trusted CA
1385 */
1386 *flags &= ~BADCERT_NOT_TRUSTED;
1387 break;
1388 }
1389
1390 /*
1391 * If top of chain is not the same as the trusted CA send a verify request
1392 * to the callback for any issues with validity and CRL presence for the
1393 * trusted CA certificate.
1394 */
1395 if( trust_ca != NULL &&
1396 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1397 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1398 child->issuer_raw.len ) != 0 ) )
1399 {
1400#if defined(POLARSSL_X509_CRL_PARSE_C)
1401 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001402 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001403#else
1404 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001405#endif
1406
Paul Bakker86d0c192013-09-18 11:11:02 +02001407 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001408 ca_flags |= BADCERT_EXPIRED;
1409
1410 if( NULL != f_vrfy )
1411 {
1412 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
1413 return( ret );
1414 }
1415 }
1416
1417 /* Call callback on top cert */
1418 if( NULL != f_vrfy )
1419 {
1420 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1421 return( ret );
1422 }
1423
1424 *flags |= ca_flags;
1425
1426 return( 0 );
1427}
1428
Paul Bakkerddf26b42013-09-18 13:46:23 +02001429static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001430 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001432 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433 void *p_vrfy )
1434{
1435 int ret;
1436 int parent_flags = 0;
1437 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001438 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439 const md_info_t *md_info;
1440
Paul Bakker86d0c192013-09-18 11:11:02 +02001441 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442 *flags |= BADCERT_EXPIRED;
1443
1444 md_info = md_info_from_type( child->sig_md );
1445 if( md_info == NULL )
1446 {
1447 /*
1448 * Cannot check 'unknown' hash
1449 */
1450 *flags |= BADCERT_NOT_TRUSTED;
1451 }
1452 else
1453 {
1454 md( md_info, child->tbs.p, child->tbs.len, hash );
1455
1456 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1457 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1458 child->sig.p, child->sig.len ) != 0 )
1459 {
1460 *flags |= BADCERT_NOT_TRUSTED;
1461 }
1462 }
1463
1464#if defined(POLARSSL_X509_CRL_PARSE_C)
1465 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001466 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467#endif
1468
1469 grandparent = parent->next;
1470
1471 while( grandparent != NULL )
1472 {
1473 if( grandparent->version == 0 ||
1474 grandparent->ca_istrue == 0 ||
1475 parent->issuer_raw.len != grandparent->subject_raw.len ||
1476 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
1477 parent->issuer_raw.len ) != 0 )
1478 {
1479 grandparent = grandparent->next;
1480 continue;
1481 }
1482 break;
1483 }
1484
1485 if( grandparent != NULL )
1486 {
1487 /*
1488 * Part of the chain
1489 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001490 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 +02001491 if( ret != 0 )
1492 return( ret );
1493 }
1494 else
1495 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001496 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 +02001497 if( ret != 0 )
1498 return( ret );
1499 }
1500
1501 /* child is verified to be a child of the parent, call verify callback */
1502 if( NULL != f_vrfy )
1503 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1504 return( ret );
1505
1506 *flags |= parent_flags;
1507
1508 return( 0 );
1509}
1510
1511/*
1512 * Verify the certificate validity
1513 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001514int x509_crt_verify( x509_crt *crt,
1515 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001516 x509_crl *ca_crl,
1517 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001518 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001519 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520{
1521 size_t cn_len;
1522 int ret;
1523 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001524 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001525 x509_name *name;
1526 x509_sequence *cur = NULL;
1527
1528 *flags = 0;
1529
1530 if( cn != NULL )
1531 {
1532 name = &crt->subject;
1533 cn_len = strlen( cn );
1534
1535 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1536 {
1537 cur = &crt->subject_alt_names;
1538
1539 while( cur != NULL )
1540 {
1541 if( cur->buf.len == cn_len &&
1542 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1543 break;
1544
1545 if( cur->buf.len > 2 &&
1546 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1547 x509_wildcard_verify( cn, &cur->buf ) )
1548 break;
1549
1550 cur = cur->next;
1551 }
1552
1553 if( cur == NULL )
1554 *flags |= BADCERT_CN_MISMATCH;
1555 }
1556 else
1557 {
1558 while( name != NULL )
1559 {
1560 if( OID_CMP( OID_AT_CN, &name->oid ) )
1561 {
1562 if( name->val.len == cn_len &&
1563 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1564 break;
1565
1566 if( name->val.len > 2 &&
1567 memcmp( name->val.p, "*.", 2 ) == 0 &&
1568 x509_wildcard_verify( cn, &name->val ) )
1569 break;
1570 }
1571
1572 name = name->next;
1573 }
1574
1575 if( name == NULL )
1576 *flags |= BADCERT_CN_MISMATCH;
1577 }
1578 }
1579
1580 /*
1581 * Iterate upwards in the given cert chain, to find our crt parent.
1582 * Ignore any upper cert with CA != TRUE.
1583 */
1584 parent = crt->next;
1585
1586 while( parent != NULL && parent->version != 0 )
1587 {
1588 if( parent->ca_istrue == 0 ||
1589 crt->issuer_raw.len != parent->subject_raw.len ||
1590 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
1591 crt->issuer_raw.len ) != 0 )
1592 {
1593 parent = parent->next;
1594 continue;
1595 }
1596 break;
1597 }
1598
1599 if( parent != NULL )
1600 {
1601 /*
1602 * Part of the chain
1603 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001604 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001605 if( ret != 0 )
1606 return( ret );
1607 }
1608 else
1609 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001610 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611 if( ret != 0 )
1612 return( ret );
1613 }
1614
1615 if( *flags != 0 )
1616 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1617
1618 return( 0 );
1619}
1620
1621/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001622 * Initialize a certificate chain
1623 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001624void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001625{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001626 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001627}
1628
1629/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630 * Unallocate all certificate data
1631 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001632void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001634 x509_crt *cert_cur = crt;
1635 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636 x509_name *name_cur;
1637 x509_name *name_prv;
1638 x509_sequence *seq_cur;
1639 x509_sequence *seq_prv;
1640
1641 if( crt == NULL )
1642 return;
1643
1644 do
1645 {
1646 pk_free( &cert_cur->pk );
1647
1648 name_cur = cert_cur->issuer.next;
1649 while( name_cur != NULL )
1650 {
1651 name_prv = name_cur;
1652 name_cur = name_cur->next;
1653 memset( name_prv, 0, sizeof( x509_name ) );
1654 polarssl_free( name_prv );
1655 }
1656
1657 name_cur = cert_cur->subject.next;
1658 while( name_cur != NULL )
1659 {
1660 name_prv = name_cur;
1661 name_cur = name_cur->next;
1662 memset( name_prv, 0, sizeof( x509_name ) );
1663 polarssl_free( name_prv );
1664 }
1665
1666 seq_cur = cert_cur->ext_key_usage.next;
1667 while( seq_cur != NULL )
1668 {
1669 seq_prv = seq_cur;
1670 seq_cur = seq_cur->next;
1671 memset( seq_prv, 0, sizeof( x509_sequence ) );
1672 polarssl_free( seq_prv );
1673 }
1674
1675 seq_cur = cert_cur->subject_alt_names.next;
1676 while( seq_cur != NULL )
1677 {
1678 seq_prv = seq_cur;
1679 seq_cur = seq_cur->next;
1680 memset( seq_prv, 0, sizeof( x509_sequence ) );
1681 polarssl_free( seq_prv );
1682 }
1683
1684 if( cert_cur->raw.p != NULL )
1685 {
1686 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1687 polarssl_free( cert_cur->raw.p );
1688 }
1689
1690 cert_cur = cert_cur->next;
1691 }
1692 while( cert_cur != NULL );
1693
1694 cert_cur = crt;
1695 do
1696 {
1697 cert_prv = cert_cur;
1698 cert_cur = cert_cur->next;
1699
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001700 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001701 if( cert_prv != crt )
1702 polarssl_free( cert_prv );
1703 }
1704 while( cert_cur != NULL );
1705}
1706
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001707#endif