blob: 5330bf17039988226021259fc09e2fcc0a9aa6a6 [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 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +0100994 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +0100996 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 char entry_name[255];
998 DIR *dir = opendir( path );
999
1000 if( dir == NULL)
1001 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1002
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001003 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001005 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001007 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008 {
1009 closedir( dir );
1010 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1011 }
1012
1013 if( !S_ISREG( sb.st_mode ) )
1014 continue;
1015
1016 // Ignore parse errors
1017 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001018 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 if( t_ret < 0 )
1020 ret++;
1021 else
1022 ret += t_ret;
1023 }
1024 closedir( dir );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001025#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026
1027 return( ret );
1028}
1029#endif /* POLARSSL_FS_IO */
1030
Paul Bakker6edcd412013-10-29 15:22:54 +01001031#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1032 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033#include <stdarg.h>
1034
1035#if !defined vsnprintf
1036#define vsnprintf _vsnprintf
1037#endif // vsnprintf
1038
1039/*
1040 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1041 * Result value is not size of buffer needed, but -1 if no fit is possible.
1042 *
1043 * This fuction tries to 'fix' this by at least suggesting enlarging the
1044 * size by 20.
1045 */
1046static int compat_snprintf(char *str, size_t size, const char *format, ...)
1047{
1048 va_list ap;
1049 int res = -1;
1050
1051 va_start( ap, format );
1052
1053 res = vsnprintf( str, size, format, ap );
1054
1055 va_end( ap );
1056
1057 // No quick fix possible
1058 if ( res < 0 )
1059 return( (int) size + 20 );
1060
1061 return res;
1062}
1063
1064#define snprintf compat_snprintf
1065#endif
1066
1067#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1068
1069#define SAFE_SNPRINTF() \
1070{ \
1071 if( ret == -1 ) \
1072 return( -1 ); \
1073 \
1074 if ( (unsigned int) ret > n ) { \
1075 p[n - 1] = '\0'; \
1076 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1077 } \
1078 \
1079 n -= (unsigned int) ret; \
1080 p += (unsigned int) ret; \
1081}
1082
1083/*
1084 * Return an informational string about the certificate.
1085 */
1086#define BEFORE_COLON 14
1087#define BC "14"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001088int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001089 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090{
1091 int ret;
1092 size_t n;
1093 char *p;
1094 const char *desc = NULL;
1095 char key_size_str[BEFORE_COLON];
1096
1097 p = buf;
1098 n = size;
1099
1100 ret = snprintf( p, n, "%scert. version : %d\n",
1101 prefix, crt->version );
1102 SAFE_SNPRINTF();
1103 ret = snprintf( p, n, "%sserial number : ",
1104 prefix );
1105 SAFE_SNPRINTF();
1106
Paul Bakker86d0c192013-09-18 11:11:02 +02001107 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108 SAFE_SNPRINTF();
1109
1110 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
1111 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001112 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113 SAFE_SNPRINTF();
1114
1115 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
1116 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001117 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118 SAFE_SNPRINTF();
1119
1120 ret = snprintf( p, n, "\n%sissued on : " \
1121 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1122 crt->valid_from.year, crt->valid_from.mon,
1123 crt->valid_from.day, crt->valid_from.hour,
1124 crt->valid_from.min, crt->valid_from.sec );
1125 SAFE_SNPRINTF();
1126
1127 ret = snprintf( p, n, "\n%sexpires on : " \
1128 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1129 crt->valid_to.year, crt->valid_to.mon,
1130 crt->valid_to.day, crt->valid_to.hour,
1131 crt->valid_to.min, crt->valid_to.sec );
1132 SAFE_SNPRINTF();
1133
1134 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
1135 SAFE_SNPRINTF();
1136
1137 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1138 if( ret != 0 )
1139 ret = snprintf( p, n, "???" );
1140 else
1141 ret = snprintf( p, n, "%s", desc );
1142 SAFE_SNPRINTF();
1143
1144 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1145 pk_get_name( &crt->pk ) ) ) != 0 )
1146 {
1147 return( ret );
1148 }
1149
1150 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
1151 (int) pk_get_size( &crt->pk ) );
1152 SAFE_SNPRINTF();
1153
1154 return( (int) ( size - n ) );
1155}
1156
1157#if defined(POLARSSL_X509_CRL_PARSE_C)
1158/*
1159 * Return 1 if the certificate is revoked, or 0 otherwise.
1160 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001161int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162{
1163 const x509_crl_entry *cur = &crl->entry;
1164
1165 while( cur != NULL && cur->serial.len != 0 )
1166 {
1167 if( crt->serial.len == cur->serial.len &&
1168 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1169 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001170 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 return( 1 );
1172 }
1173
1174 cur = cur->next;
1175 }
1176
1177 return( 0 );
1178}
1179
1180/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001181 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001183static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001184 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001185{
1186 int flags = 0;
1187 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1188 const md_info_t *md_info;
1189
1190 if( ca == NULL )
1191 return( flags );
1192
1193 /*
1194 * TODO: What happens if no CRL is present?
1195 * Suggestion: Revocation state should be unknown if no CRL is present.
1196 * For backwards compatibility this is not yet implemented.
1197 */
1198
1199 while( crl_list != NULL )
1200 {
1201 if( crl_list->version == 0 ||
1202 crl_list->issuer_raw.len != ca->subject_raw.len ||
1203 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1204 crl_list->issuer_raw.len ) != 0 )
1205 {
1206 crl_list = crl_list->next;
1207 continue;
1208 }
1209
1210 /*
1211 * Check if CRL is correctly signed by the trusted CA
1212 */
1213 md_info = md_info_from_type( crl_list->sig_md );
1214 if( md_info == NULL )
1215 {
1216 /*
1217 * Cannot check 'unknown' hash
1218 */
1219 flags |= BADCRL_NOT_TRUSTED;
1220 break;
1221 }
1222
1223 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1224
1225 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1226 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1227 crl_list->sig.p, crl_list->sig.len ) != 0 )
1228 {
1229 flags |= BADCRL_NOT_TRUSTED;
1230 break;
1231 }
1232
1233 /*
1234 * Check for validity of CRL (Do not drop out)
1235 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001236 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001237 flags |= BADCRL_EXPIRED;
1238
1239 /*
1240 * Check if certificate is revoked
1241 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001242 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001243 {
1244 flags |= BADCERT_REVOKED;
1245 break;
1246 }
1247
1248 crl_list = crl_list->next;
1249 }
1250 return flags;
1251}
1252#endif /* POLARSSL_X509_CRL_PARSE_C */
1253
1254// Equal == 0, inequal == 1
1255static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1256{
1257 size_t i;
1258 unsigned char diff;
1259 const unsigned char *n1 = s1, *n2 = s2;
1260
1261 for( i = 0; i < len; i++ )
1262 {
1263 diff = n1[i] ^ n2[i];
1264
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001265 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001266 continue;
1267
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001268 if( diff == 32 &&
1269 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1270 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1271 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001272 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001273 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001274
1275 return( 1 );
1276 }
1277
1278 return( 0 );
1279}
1280
1281static int x509_wildcard_verify( const char *cn, x509_buf *name )
1282{
1283 size_t i;
1284 size_t cn_idx = 0;
1285
1286 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1287 return( 0 );
1288
1289 for( i = 0; i < strlen( cn ); ++i )
1290 {
1291 if( cn[i] == '.' )
1292 {
1293 cn_idx = i;
1294 break;
1295 }
1296 }
1297
1298 if( cn_idx == 0 )
1299 return( 0 );
1300
1301 if( strlen( cn ) - cn_idx == name->len - 1 &&
1302 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1303 {
1304 return( 1 );
1305 }
1306
1307 return( 0 );
1308}
1309
Paul Bakkerddf26b42013-09-18 13:46:23 +02001310static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001311 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001312 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001313 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 void *p_vrfy )
1315{
1316 int ret;
1317 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1318 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1319 const md_info_t *md_info;
1320
Paul Bakker86d0c192013-09-18 11:11:02 +02001321 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001322 *flags |= BADCERT_EXPIRED;
1323
1324 /*
1325 * Child is the top of the chain. Check against the trust_ca list.
1326 */
1327 *flags |= BADCERT_NOT_TRUSTED;
1328
1329 md_info = md_info_from_type( child->sig_md );
1330 if( md_info == NULL )
1331 {
1332 /*
1333 * Cannot check 'unknown', no need to try any CA
1334 */
1335 trust_ca = NULL;
1336 }
1337 else
1338 md( md_info, child->tbs.p, child->tbs.len, hash );
1339
1340 while( trust_ca != NULL )
1341 {
1342 if( trust_ca->version == 0 ||
1343 child->issuer_raw.len != trust_ca->subject_raw.len ||
1344 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
1345 child->issuer_raw.len ) != 0 )
1346 {
1347 trust_ca = trust_ca->next;
1348 continue;
1349 }
1350
1351 /*
1352 * Reduce path_len to check against if top of the chain is
1353 * the same as the trusted CA
1354 */
1355 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1356 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1357 child->issuer_raw.len ) == 0 )
1358 {
1359 check_path_cnt--;
1360 }
1361
1362 if( trust_ca->max_pathlen > 0 &&
1363 trust_ca->max_pathlen < check_path_cnt )
1364 {
1365 trust_ca = trust_ca->next;
1366 continue;
1367 }
1368
1369 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1370 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1371 child->sig.p, child->sig.len ) != 0 )
1372 {
1373 trust_ca = trust_ca->next;
1374 continue;
1375 }
1376
1377 /*
1378 * Top of chain is signed by a trusted CA
1379 */
1380 *flags &= ~BADCERT_NOT_TRUSTED;
1381 break;
1382 }
1383
1384 /*
1385 * If top of chain is not the same as the trusted CA send a verify request
1386 * to the callback for any issues with validity and CRL presence for the
1387 * trusted CA certificate.
1388 */
1389 if( trust_ca != NULL &&
1390 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1391 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1392 child->issuer_raw.len ) != 0 ) )
1393 {
1394#if defined(POLARSSL_X509_CRL_PARSE_C)
1395 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001396 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001397#else
1398 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001399#endif
1400
Paul Bakker86d0c192013-09-18 11:11:02 +02001401 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001402 ca_flags |= BADCERT_EXPIRED;
1403
1404 if( NULL != f_vrfy )
1405 {
1406 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
1407 return( ret );
1408 }
1409 }
1410
1411 /* Call callback on top cert */
1412 if( NULL != f_vrfy )
1413 {
1414 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1415 return( ret );
1416 }
1417
1418 *flags |= ca_flags;
1419
1420 return( 0 );
1421}
1422
Paul Bakkerddf26b42013-09-18 13:46:23 +02001423static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001424 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001426 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427 void *p_vrfy )
1428{
1429 int ret;
1430 int parent_flags = 0;
1431 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001432 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433 const md_info_t *md_info;
1434
Paul Bakker86d0c192013-09-18 11:11:02 +02001435 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436 *flags |= BADCERT_EXPIRED;
1437
1438 md_info = md_info_from_type( child->sig_md );
1439 if( md_info == NULL )
1440 {
1441 /*
1442 * Cannot check 'unknown' hash
1443 */
1444 *flags |= BADCERT_NOT_TRUSTED;
1445 }
1446 else
1447 {
1448 md( md_info, child->tbs.p, child->tbs.len, hash );
1449
1450 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1451 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1452 child->sig.p, child->sig.len ) != 0 )
1453 {
1454 *flags |= BADCERT_NOT_TRUSTED;
1455 }
1456 }
1457
1458#if defined(POLARSSL_X509_CRL_PARSE_C)
1459 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001460 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001461#endif
1462
1463 grandparent = parent->next;
1464
1465 while( grandparent != NULL )
1466 {
1467 if( grandparent->version == 0 ||
1468 grandparent->ca_istrue == 0 ||
1469 parent->issuer_raw.len != grandparent->subject_raw.len ||
1470 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
1471 parent->issuer_raw.len ) != 0 )
1472 {
1473 grandparent = grandparent->next;
1474 continue;
1475 }
1476 break;
1477 }
1478
1479 if( grandparent != NULL )
1480 {
1481 /*
1482 * Part of the chain
1483 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001484 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 +02001485 if( ret != 0 )
1486 return( ret );
1487 }
1488 else
1489 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001490 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 +02001491 if( ret != 0 )
1492 return( ret );
1493 }
1494
1495 /* child is verified to be a child of the parent, call verify callback */
1496 if( NULL != f_vrfy )
1497 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1498 return( ret );
1499
1500 *flags |= parent_flags;
1501
1502 return( 0 );
1503}
1504
1505/*
1506 * Verify the certificate validity
1507 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001508int x509_crt_verify( x509_crt *crt,
1509 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001510 x509_crl *ca_crl,
1511 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001512 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001513 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514{
1515 size_t cn_len;
1516 int ret;
1517 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001518 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001519 x509_name *name;
1520 x509_sequence *cur = NULL;
1521
1522 *flags = 0;
1523
1524 if( cn != NULL )
1525 {
1526 name = &crt->subject;
1527 cn_len = strlen( cn );
1528
1529 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1530 {
1531 cur = &crt->subject_alt_names;
1532
1533 while( cur != NULL )
1534 {
1535 if( cur->buf.len == cn_len &&
1536 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1537 break;
1538
1539 if( cur->buf.len > 2 &&
1540 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1541 x509_wildcard_verify( cn, &cur->buf ) )
1542 break;
1543
1544 cur = cur->next;
1545 }
1546
1547 if( cur == NULL )
1548 *flags |= BADCERT_CN_MISMATCH;
1549 }
1550 else
1551 {
1552 while( name != NULL )
1553 {
1554 if( OID_CMP( OID_AT_CN, &name->oid ) )
1555 {
1556 if( name->val.len == cn_len &&
1557 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1558 break;
1559
1560 if( name->val.len > 2 &&
1561 memcmp( name->val.p, "*.", 2 ) == 0 &&
1562 x509_wildcard_verify( cn, &name->val ) )
1563 break;
1564 }
1565
1566 name = name->next;
1567 }
1568
1569 if( name == NULL )
1570 *flags |= BADCERT_CN_MISMATCH;
1571 }
1572 }
1573
1574 /*
1575 * Iterate upwards in the given cert chain, to find our crt parent.
1576 * Ignore any upper cert with CA != TRUE.
1577 */
1578 parent = crt->next;
1579
1580 while( parent != NULL && parent->version != 0 )
1581 {
1582 if( parent->ca_istrue == 0 ||
1583 crt->issuer_raw.len != parent->subject_raw.len ||
1584 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
1585 crt->issuer_raw.len ) != 0 )
1586 {
1587 parent = parent->next;
1588 continue;
1589 }
1590 break;
1591 }
1592
1593 if( parent != NULL )
1594 {
1595 /*
1596 * Part of the chain
1597 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001598 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599 if( ret != 0 )
1600 return( ret );
1601 }
1602 else
1603 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001604 ret = x509_crt_verify_top( crt, 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
1609 if( *flags != 0 )
1610 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1611
1612 return( 0 );
1613}
1614
1615/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001616 * Initialize a certificate chain
1617 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001618void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001619{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001620 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001621}
1622
1623/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624 * Unallocate all certificate data
1625 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001626void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001627{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001628 x509_crt *cert_cur = crt;
1629 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630 x509_name *name_cur;
1631 x509_name *name_prv;
1632 x509_sequence *seq_cur;
1633 x509_sequence *seq_prv;
1634
1635 if( crt == NULL )
1636 return;
1637
1638 do
1639 {
1640 pk_free( &cert_cur->pk );
1641
1642 name_cur = cert_cur->issuer.next;
1643 while( name_cur != NULL )
1644 {
1645 name_prv = name_cur;
1646 name_cur = name_cur->next;
1647 memset( name_prv, 0, sizeof( x509_name ) );
1648 polarssl_free( name_prv );
1649 }
1650
1651 name_cur = cert_cur->subject.next;
1652 while( name_cur != NULL )
1653 {
1654 name_prv = name_cur;
1655 name_cur = name_cur->next;
1656 memset( name_prv, 0, sizeof( x509_name ) );
1657 polarssl_free( name_prv );
1658 }
1659
1660 seq_cur = cert_cur->ext_key_usage.next;
1661 while( seq_cur != NULL )
1662 {
1663 seq_prv = seq_cur;
1664 seq_cur = seq_cur->next;
1665 memset( seq_prv, 0, sizeof( x509_sequence ) );
1666 polarssl_free( seq_prv );
1667 }
1668
1669 seq_cur = cert_cur->subject_alt_names.next;
1670 while( seq_cur != NULL )
1671 {
1672 seq_prv = seq_cur;
1673 seq_cur = seq_cur->next;
1674 memset( seq_prv, 0, sizeof( x509_sequence ) );
1675 polarssl_free( seq_prv );
1676 }
1677
1678 if( cert_cur->raw.p != NULL )
1679 {
1680 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1681 polarssl_free( cert_cur->raw.p );
1682 }
1683
1684 cert_cur = cert_cur->next;
1685 }
1686 while( cert_cur != NULL );
1687
1688 cert_cur = crt;
1689 do
1690 {
1691 cert_prv = cert_cur;
1692 cert_cur = cert_cur->next;
1693
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001694 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001695 if( cert_prv != crt )
1696 polarssl_free( cert_prv );
1697 }
1698 while( cert_cur != NULL );
1699}
1700
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001701#endif