blob: 436812d6dba7d384b87d6c632e67d1c57f46872e [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
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
Paul Bakker7dc4c442014-02-01 22:50:26 +010047#if defined(POLARSSL_PLATFORM_C)
48#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049#else
50#define polarssl_malloc malloc
51#define polarssl_free free
52#endif
53
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010054#if defined(POLARSSL_THREADING_C)
55#include "polarssl/threading.h"
56#endif
57
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058#include <string.h>
59#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010060#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#include <windows.h>
62#else
63#include <time.h>
64#endif
65
Paul Bakkerfa6a6202013-10-28 18:48:30 +010066#if defined(EFIX64) || defined(EFI32)
67#include <stdio.h>
68#endif
69
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070#if defined(POLARSSL_FS_IO)
71#include <stdio.h>
72#if !defined(_WIN32)
73#include <sys/types.h>
74#include <sys/stat.h>
75#include <dirent.h>
76#endif
77#endif
78
79/*
80 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
81 */
82static int x509_get_version( unsigned char **p,
83 const unsigned char *end,
84 int *ver )
85{
86 int ret;
87 size_t len;
88
89 if( ( ret = asn1_get_tag( p, end, &len,
90 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
91 {
92 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
93 {
94 *ver = 0;
95 return( 0 );
96 }
97
98 return( ret );
99 }
100
101 end = *p + len;
102
103 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200104 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105
106 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200107 return( POLARSSL_ERR_X509_INVALID_VERSION +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200108 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
109
110 return( 0 );
111}
112
113/*
114 * Validity ::= SEQUENCE {
115 * notBefore Time,
116 * notAfter Time }
117 */
118static int x509_get_dates( unsigned char **p,
119 const unsigned char *end,
120 x509_time *from,
121 x509_time *to )
122{
123 int ret;
124 size_t len;
125
126 if( ( ret = asn1_get_tag( p, end, &len,
127 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200128 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200129
130 end = *p + len;
131
132 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
133 return( ret );
134
135 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
136 return( ret );
137
138 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200139 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200140 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
141
142 return( 0 );
143}
144
145/*
146 * X.509 v2/v3 unique identifier (not parsed)
147 */
148static int x509_get_uid( unsigned char **p,
149 const unsigned char *end,
150 x509_buf *uid, int n )
151{
152 int ret;
153
154 if( *p == end )
155 return( 0 );
156
157 uid->tag = **p;
158
159 if( ( ret = asn1_get_tag( p, end, &uid->len,
160 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
161 {
162 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
163 return( 0 );
164
165 return( ret );
166 }
167
168 uid->p = *p;
169 *p += uid->len;
170
171 return( 0 );
172}
173
174static int x509_get_basic_constraints( unsigned char **p,
175 const unsigned char *end,
176 int *ca_istrue,
177 int *max_pathlen )
178{
179 int ret;
180 size_t len;
181
182 /*
183 * BasicConstraints ::= SEQUENCE {
184 * cA BOOLEAN DEFAULT FALSE,
185 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
186 */
187 *ca_istrue = 0; /* DEFAULT FALSE */
188 *max_pathlen = 0; /* endless */
189
190 if( ( ret = asn1_get_tag( p, end, &len,
191 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200192 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200193
194 if( *p == end )
195 return 0;
196
197 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
198 {
199 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
200 ret = asn1_get_int( p, end, ca_istrue );
201
202 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200203 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200204
205 if( *ca_istrue != 0 )
206 *ca_istrue = 1;
207 }
208
209 if( *p == end )
210 return 0;
211
212 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200213 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200214
215 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200216 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200217 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
218
219 (*max_pathlen)++;
220
221 return 0;
222}
223
224static int x509_get_ns_cert_type( unsigned char **p,
225 const unsigned char *end,
226 unsigned char *ns_cert_type)
227{
228 int ret;
229 x509_bitstring bs = { 0, 0, NULL };
230
231 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200232 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200233
234 if( bs.len != 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200235 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236 POLARSSL_ERR_ASN1_INVALID_LENGTH );
237
238 /* Get actual bitstring */
239 *ns_cert_type = *bs.p;
240 return 0;
241}
242
243static int x509_get_key_usage( unsigned char **p,
244 const unsigned char *end,
245 unsigned char *key_usage)
246{
247 int ret;
248 x509_bitstring bs = { 0, 0, NULL };
249
250 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200251 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200252
253 if( bs.len < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200254 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200255 POLARSSL_ERR_ASN1_INVALID_LENGTH );
256
257 /* Get actual bitstring */
258 *key_usage = *bs.p;
259 return 0;
260}
261
262/*
263 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
264 *
265 * KeyPurposeId ::= OBJECT IDENTIFIER
266 */
267static int x509_get_ext_key_usage( unsigned char **p,
268 const unsigned char *end,
269 x509_sequence *ext_key_usage)
270{
271 int ret;
272
273 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200274 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275
276 /* Sequence length must be >= 1 */
277 if( ext_key_usage->buf.p == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200278 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200279 POLARSSL_ERR_ASN1_INVALID_LENGTH );
280
281 return 0;
282}
283
284/*
285 * SubjectAltName ::= GeneralNames
286 *
287 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
288 *
289 * GeneralName ::= CHOICE {
290 * otherName [0] OtherName,
291 * rfc822Name [1] IA5String,
292 * dNSName [2] IA5String,
293 * x400Address [3] ORAddress,
294 * directoryName [4] Name,
295 * ediPartyName [5] EDIPartyName,
296 * uniformResourceIdentifier [6] IA5String,
297 * iPAddress [7] OCTET STRING,
298 * registeredID [8] OBJECT IDENTIFIER }
299 *
300 * OtherName ::= SEQUENCE {
301 * type-id OBJECT IDENTIFIER,
302 * value [0] EXPLICIT ANY DEFINED BY type-id }
303 *
304 * EDIPartyName ::= SEQUENCE {
305 * nameAssigner [0] DirectoryString OPTIONAL,
306 * partyName [1] DirectoryString }
307 *
308 * NOTE: PolarSSL only parses and uses dNSName at this point.
309 */
310static int x509_get_subject_alt_name( unsigned char **p,
311 const unsigned char *end,
312 x509_sequence *subject_alt_name )
313{
314 int ret;
315 size_t len, tag_len;
316 asn1_buf *buf;
317 unsigned char tag;
318 asn1_sequence *cur = subject_alt_name;
319
320 /* Get main sequence tag */
321 if( ( ret = asn1_get_tag( p, end, &len,
322 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200323 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200324
325 if( *p + len != end )
Paul Bakker51876562013-09-17 14:36:05 +0200326 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
328
329 while( *p < end )
330 {
331 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200332 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333 POLARSSL_ERR_ASN1_OUT_OF_DATA );
334
335 tag = **p;
336 (*p)++;
337 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200338 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339
340 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
Paul Bakker51876562013-09-17 14:36:05 +0200341 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200342 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
343
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200344 /* Skip everything but DNS name */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
346 {
347 *p += tag_len;
348 continue;
349 }
350
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200352 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353 {
354 cur->next = (asn1_sequence *) polarssl_malloc(
355 sizeof( asn1_sequence ) );
356
357 if( cur->next == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200358 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359 POLARSSL_ERR_ASN1_MALLOC_FAILED );
360
361 memset( cur->next, 0, sizeof( asn1_sequence ) );
362 cur = cur->next;
363 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200364
365 buf = &(cur->buf);
366 buf->tag = tag;
367 buf->p = *p;
368 buf->len = tag_len;
369 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370 }
371
372 /* Set final sequence entry's next pointer to NULL */
373 cur->next = NULL;
374
375 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200376 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
378
379 return( 0 );
380}
381
382/*
383 * X.509 v3 extensions
384 *
385 * TODO: Perform all of the basic constraints tests required by the RFC
386 * TODO: Set values for undetected extensions to a sane default?
387 *
388 */
389static int x509_get_crt_ext( unsigned char **p,
390 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200391 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392{
393 int ret;
394 size_t len;
395 unsigned char *end_ext_data, *end_ext_octet;
396
397 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
398 {
399 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
400 return( 0 );
401
402 return( ret );
403 }
404
405 while( *p < end )
406 {
407 /*
408 * Extension ::= SEQUENCE {
409 * extnID OBJECT IDENTIFIER,
410 * critical BOOLEAN DEFAULT FALSE,
411 * extnValue OCTET STRING }
412 */
413 x509_buf extn_oid = {0, 0, NULL};
414 int is_critical = 0; /* DEFAULT FALSE */
415 int ext_type = 0;
416
417 if( ( ret = asn1_get_tag( p, end, &len,
418 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200419 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420
421 end_ext_data = *p + len;
422
423 /* Get extension ID */
424 extn_oid.tag = **p;
425
426 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200427 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
429 extn_oid.p = *p;
430 *p += extn_oid.len;
431
432 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200433 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434 POLARSSL_ERR_ASN1_OUT_OF_DATA );
435
436 /* Get optional critical */
437 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
438 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200439 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440
441 /* Data should be octet string type */
442 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
443 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200444 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
446 end_ext_octet = *p + len;
447
448 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200449 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
451
452 /*
453 * Detect supported extensions
454 */
455 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
456
457 if( ret != 0 )
458 {
459 /* No parser found, skip extension */
460 *p = end_ext_octet;
461
462#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
463 if( is_critical )
464 {
465 /* Data is marked as critical: fail */
Paul Bakker51876562013-09-17 14:36:05 +0200466 return ( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
468 }
469#endif
470 continue;
471 }
472
473 crt->ext_types |= ext_type;
474
475 switch( ext_type )
476 {
477 case EXT_BASIC_CONSTRAINTS:
478 /* Parse basic constraints */
479 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
480 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
481 return ( ret );
482 break;
483
484 case EXT_KEY_USAGE:
485 /* Parse key usage */
486 if( ( ret = x509_get_key_usage( p, end_ext_octet,
487 &crt->key_usage ) ) != 0 )
488 return ( ret );
489 break;
490
491 case EXT_EXTENDED_KEY_USAGE:
492 /* Parse extended key usage */
493 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
494 &crt->ext_key_usage ) ) != 0 )
495 return ( ret );
496 break;
497
498 case EXT_SUBJECT_ALT_NAME:
499 /* Parse subject alt name */
500 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
501 &crt->subject_alt_names ) ) != 0 )
502 return ( ret );
503 break;
504
505 case EXT_NS_CERT_TYPE:
506 /* Parse netscape certificate type */
507 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
508 &crt->ns_cert_type ) ) != 0 )
509 return ( ret );
510 break;
511
512 default:
513 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
514 }
515 }
516
517 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200518 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
520
521 return( 0 );
522}
523
524/*
525 * Parse and fill a single X.509 certificate in DER format
526 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200527static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200528 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529{
530 int ret;
531 size_t len;
532 unsigned char *p, *end, *crt_end;
533
534 /*
535 * Check for valid input
536 */
537 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200538 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539
540 p = (unsigned char *) polarssl_malloc( len = buflen );
541
542 if( p == NULL )
543 return( POLARSSL_ERR_X509_MALLOC_FAILED );
544
545 memcpy( p, buf, buflen );
546
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547 crt->raw.p = p;
548 crt->raw.len = len;
549 end = p + len;
550
551 /*
552 * Certificate ::= SEQUENCE {
553 * tbsCertificate TBSCertificate,
554 * signatureAlgorithm AlgorithmIdentifier,
555 * signatureValue BIT STRING }
556 */
557 if( ( ret = asn1_get_tag( &p, end, &len,
558 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
559 {
560 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200561 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562 }
563
564 if( len > (size_t) ( end - p ) )
565 {
566 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200567 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
569 }
570 crt_end = p + len;
571
572 /*
573 * TBSCertificate ::= SEQUENCE {
574 */
575 crt->tbs.p = p;
576
577 if( ( ret = asn1_get_tag( &p, end, &len,
578 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
579 {
580 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200581 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582 }
583
584 end = p + len;
585 crt->tbs.len = end - crt->tbs.p;
586
587 /*
588 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
589 *
590 * CertificateSerialNumber ::= INTEGER
591 *
592 * signature AlgorithmIdentifier
593 */
594 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
595 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +0100596 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597 {
598 x509_crt_free( crt );
599 return( ret );
600 }
601
602 crt->version++;
603
604 if( crt->version > 3 )
605 {
606 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200607 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608 }
609
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +0100610 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
611 &crt->sig_pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612 {
613 x509_crt_free( crt );
614 return( ret );
615 }
616
617 /*
618 * issuer Name
619 */
620 crt->issuer_raw.p = p;
621
622 if( ( ret = asn1_get_tag( &p, end, &len,
623 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
624 {
625 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200626 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627 }
628
629 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
630 {
631 x509_crt_free( crt );
632 return( ret );
633 }
634
635 crt->issuer_raw.len = p - crt->issuer_raw.p;
636
637 /*
638 * Validity ::= SEQUENCE {
639 * notBefore Time,
640 * notAfter Time }
641 *
642 */
643 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
644 &crt->valid_to ) ) != 0 )
645 {
646 x509_crt_free( crt );
647 return( ret );
648 }
649
650 /*
651 * subject Name
652 */
653 crt->subject_raw.p = p;
654
655 if( ( ret = asn1_get_tag( &p, end, &len,
656 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
657 {
658 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200659 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660 }
661
662 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
663 {
664 x509_crt_free( crt );
665 return( ret );
666 }
667
668 crt->subject_raw.len = p - crt->subject_raw.p;
669
670 /*
671 * SubjectPublicKeyInfo
672 */
Paul Bakkerda771152013-09-16 22:45:03 +0200673 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 {
675 x509_crt_free( crt );
676 return( ret );
677 }
678
679 /*
680 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
681 * -- If present, version shall be v2 or v3
682 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
683 * -- If present, version shall be v2 or v3
684 * extensions [3] EXPLICIT Extensions OPTIONAL
685 * -- If present, version shall be v3
686 */
687 if( crt->version == 2 || crt->version == 3 )
688 {
689 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
690 if( ret != 0 )
691 {
692 x509_crt_free( crt );
693 return( ret );
694 }
695 }
696
697 if( crt->version == 2 || crt->version == 3 )
698 {
699 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
700 if( ret != 0 )
701 {
702 x509_crt_free( crt );
703 return( ret );
704 }
705 }
706
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200707#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 if( crt->version == 3 )
709 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200710#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711 ret = x509_get_crt_ext( &p, end, crt);
712 if( ret != 0 )
713 {
714 x509_crt_free( crt );
715 return( ret );
716 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200717#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200719#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720
721 if( p != end )
722 {
723 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200724 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
726 }
727
728 end = crt_end;
729
730 /*
731 * }
732 * -- end of TBSCertificate
733 *
734 * signatureAlgorithm AlgorithmIdentifier,
735 * signatureValue BIT STRING
736 */
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +0100737 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 {
739 x509_crt_free( crt );
740 return( ret );
741 }
742
743 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +0100744 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745 {
746 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200747 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748 }
749
750 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
751 {
752 x509_crt_free( crt );
753 return( ret );
754 }
755
756 if( p != end )
757 {
758 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200759 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200760 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
761 }
762
763 return( 0 );
764}
765
766/*
767 * Parse one X.509 certificate in DER format from a buffer and add them to a
768 * chained list
769 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200770int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200771 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772{
773 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200774 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775
776 /*
777 * Check for valid input
778 */
779 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200780 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781
782 while( crt->version != 0 && crt->next != NULL )
783 {
784 prev = crt;
785 crt = crt->next;
786 }
787
788 /*
789 * Add new certificate on the end of the chain if needed.
790 */
791 if ( crt->version != 0 && crt->next == NULL)
792 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200793 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794
795 if( crt->next == NULL )
796 return( POLARSSL_ERR_X509_MALLOC_FAILED );
797
798 prev = crt;
799 crt = crt->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200800 x509_crt_init( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801 }
802
Paul Bakkerddf26b42013-09-18 13:46:23 +0200803 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804 {
805 if( prev )
806 prev->next = NULL;
807
808 if( crt != chain )
809 polarssl_free( crt );
810
811 return( ret );
812 }
813
814 return( 0 );
815}
816
817/*
818 * Parse one or more PEM certificates from a buffer and add them to the chained list
819 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200820int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821{
822 int success = 0, first_error = 0, total_failed = 0;
823 int buf_format = X509_FORMAT_DER;
824
825 /*
826 * Check for valid input
827 */
828 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200829 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830
831 /*
832 * Determine buffer content. Buffer contains either one DER certificate or
833 * one or more PEM certificates.
834 */
835#if defined(POLARSSL_PEM_PARSE_C)
836 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
837 buf_format = X509_FORMAT_PEM;
838#endif
839
840 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200841 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200842
843#if defined(POLARSSL_PEM_PARSE_C)
844 if( buf_format == X509_FORMAT_PEM )
845 {
846 int ret;
847 pem_context pem;
848
849 while( buflen > 0 )
850 {
851 size_t use_len;
852 pem_init( &pem );
853
854 ret = pem_read_buffer( &pem,
855 "-----BEGIN CERTIFICATE-----",
856 "-----END CERTIFICATE-----",
857 buf, NULL, 0, &use_len );
858
859 if( ret == 0 )
860 {
861 /*
862 * Was PEM encoded
863 */
864 buflen -= use_len;
865 buf += use_len;
866 }
867 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
868 {
869 return( ret );
870 }
871 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
872 {
873 pem_free( &pem );
874
875 /*
876 * PEM header and footer were found
877 */
878 buflen -= use_len;
879 buf += use_len;
880
881 if( first_error == 0 )
882 first_error = ret;
883
884 continue;
885 }
886 else
887 break;
888
Paul Bakkerddf26b42013-09-18 13:46:23 +0200889 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890
891 pem_free( &pem );
892
893 if( ret != 0 )
894 {
895 /*
896 * Quit parsing on a memory error
897 */
898 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
899 return( ret );
900
901 if( first_error == 0 )
902 first_error = ret;
903
904 total_failed++;
905 continue;
906 }
907
908 success = 1;
909 }
910 }
911#endif
912
913 if( success )
914 return( total_failed );
915 else if( first_error )
916 return( first_error );
917 else
918 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
919}
920
921#if defined(POLARSSL_FS_IO)
922/*
923 * Load one or more certificates and add them to the chained list
924 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200925int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926{
927 int ret;
928 size_t n;
929 unsigned char *buf;
930
931 if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
932 return( ret );
933
Paul Bakkerddf26b42013-09-18 13:46:23 +0200934 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935
936 memset( buf, 0, n + 1 );
937 polarssl_free( buf );
938
939 return( ret );
940}
941
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100942#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100943static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
944#endif
945
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200946int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947{
948 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100949#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950 int w_ret;
951 WCHAR szDir[MAX_PATH];
952 char filename[MAX_PATH];
953 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200954 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955
956 WIN32_FIND_DATAW file_data;
957 HANDLE hFind;
958
959 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200960 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961
962 memset( szDir, 0, sizeof(szDir) );
963 memset( filename, 0, MAX_PATH );
964 memcpy( filename, path, len );
965 filename[len++] = '\\';
966 p = filename + len;
967 filename[len++] = '*';
968
Paul Bakker1a56fc92013-12-19 13:51:24 +0100969 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir, MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970
971 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200972 if (hFind == INVALID_HANDLE_VALUE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
974
975 len = MAX_PATH - len;
976 do
977 {
978 memset( p, 0, len );
979
980 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
981 continue;
982
983 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
984 lstrlenW(file_data.cFileName),
985 p, len - 1,
986 NULL, NULL );
987
Paul Bakkerddf26b42013-09-18 13:46:23 +0200988 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989 if( w_ret < 0 )
990 ret++;
991 else
992 ret += w_ret;
993 }
994 while( FindNextFileW( hFind, &file_data ) != 0 );
995
Paul Bakker4aa40d42013-10-11 10:49:24 +0200996 if (GetLastError() != ERROR_NO_MORE_FILES)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
998
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001000#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001001 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001003 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004 char entry_name[255];
1005 DIR *dir = opendir( path );
1006
1007 if( dir == NULL)
1008 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1009
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001010#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001011 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1012 return( ret );
1013#endif
1014
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001015 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001017 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001019 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 {
1021 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001022 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1023 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024 }
1025
1026 if( !S_ISREG( sb.st_mode ) )
1027 continue;
1028
1029 // Ignore parse errors
1030 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001031 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032 if( t_ret < 0 )
1033 ret++;
1034 else
1035 ret += t_ret;
1036 }
1037 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001038
1039cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001040#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001041 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1042 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1043#endif
1044
Paul Bakkerbe089b02013-10-14 15:51:50 +02001045#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046
1047 return( ret );
1048}
1049#endif /* POLARSSL_FS_IO */
1050
Paul Bakker6edcd412013-10-29 15:22:54 +01001051#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1052 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053#include <stdarg.h>
1054
1055#if !defined vsnprintf
1056#define vsnprintf _vsnprintf
1057#endif // vsnprintf
1058
1059/*
1060 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1061 * Result value is not size of buffer needed, but -1 if no fit is possible.
1062 *
1063 * This fuction tries to 'fix' this by at least suggesting enlarging the
1064 * size by 20.
1065 */
1066static int compat_snprintf(char *str, size_t size, const char *format, ...)
1067{
1068 va_list ap;
1069 int res = -1;
1070
1071 va_start( ap, format );
1072
1073 res = vsnprintf( str, size, format, ap );
1074
1075 va_end( ap );
1076
1077 // No quick fix possible
1078 if ( res < 0 )
1079 return( (int) size + 20 );
1080
1081 return res;
1082}
1083
1084#define snprintf compat_snprintf
1085#endif
1086
1087#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1088
1089#define SAFE_SNPRINTF() \
1090{ \
1091 if( ret == -1 ) \
1092 return( -1 ); \
1093 \
1094 if ( (unsigned int) ret > n ) { \
1095 p[n - 1] = '\0'; \
1096 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1097 } \
1098 \
1099 n -= (unsigned int) ret; \
1100 p += (unsigned int) ret; \
1101}
1102
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001103static int x509_info_subject_alt_name( char **buf, size_t *size,
1104 const x509_sequence *subject_alt_name )
1105{
1106 size_t i;
1107 size_t n = *size;
1108 char *p = *buf;
1109 const x509_sequence *cur = subject_alt_name;
1110
1111 while( cur != NULL )
1112 {
1113 if( cur->buf.len + 1 >= n )
1114 {
1115 *p = '\0';
1116 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1117 }
1118
1119 n -= cur->buf.len + 1;
1120 *p++ = ' ';
1121 for( i = 0; i < cur->buf.len; i++ )
1122 *p++ = cur->buf.p[i];
1123
1124 cur = cur->next;
1125 }
1126
1127 *p = '\0';
1128
1129 *size = n;
1130 *buf = p;
1131
1132 return( 0 );
1133}
1134
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001135static int x509_info_cert_type( char **buf, size_t *size,
1136 unsigned char ns_cert_type )
1137{
1138 int ret;
1139 size_t n = *size;
1140 char *p = *buf;
1141
1142 if( ns_cert_type & NS_CERT_TYPE_SSL_CLIENT )
1143 {
1144 ret = snprintf( p, n, " SSL Client" );
1145 SAFE_SNPRINTF();
1146 }
1147 if( ns_cert_type & NS_CERT_TYPE_SSL_SERVER )
1148 {
1149 ret = snprintf( p, n, " SSL Server" );
1150 SAFE_SNPRINTF();
1151 }
1152 if( ns_cert_type & NS_CERT_TYPE_EMAIL )
1153 {
1154 ret = snprintf( p, n, " Email" );
1155 SAFE_SNPRINTF();
1156 }
1157 if( ns_cert_type & NS_CERT_TYPE_OBJECT_SIGNING )
1158 {
1159 ret = snprintf( p, n, " Object Signing" );
1160 SAFE_SNPRINTF();
1161 }
1162 if( ns_cert_type & NS_CERT_TYPE_RESERVED )
1163 {
1164 ret = snprintf( p, n, " Reserved" );
1165 SAFE_SNPRINTF();
1166 }
1167 if( ns_cert_type & NS_CERT_TYPE_SSL_CA )
1168 {
1169 ret = snprintf( p, n, " SSL CA" );
1170 SAFE_SNPRINTF();
1171 }
1172 if( ns_cert_type & NS_CERT_TYPE_EMAIL_CA )
1173 {
1174 ret = snprintf( p, n, " Email CA" );
1175 SAFE_SNPRINTF();
1176 }
1177 if( ns_cert_type & NS_CERT_TYPE_OBJECT_SIGNING_CA )
1178 {
1179 ret = snprintf( p, n, " Object Signing CA" );
1180 SAFE_SNPRINTF();
1181 }
1182
1183 *size = n;
1184 *buf = p;
1185
1186 return( 0 );
1187}
1188
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189/*
1190 * Return an informational string about the certificate.
1191 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001192#define BEFORE_COLON 18
1193#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001194int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001195 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001196{
1197 int ret;
1198 size_t n;
1199 char *p;
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001200 const char *desc = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001201 char key_size_str[BEFORE_COLON];
1202
1203 p = buf;
1204 n = size;
1205
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001206 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001207 prefix, crt->version );
1208 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001209 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001210 prefix );
1211 SAFE_SNPRINTF();
1212
Paul Bakker86d0c192013-09-18 11:11:02 +02001213 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001214 SAFE_SNPRINTF();
1215
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001216 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001217 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001218 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219 SAFE_SNPRINTF();
1220
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001221 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001222 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001223 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001224 SAFE_SNPRINTF();
1225
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001226 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1228 crt->valid_from.year, crt->valid_from.mon,
1229 crt->valid_from.day, crt->valid_from.hour,
1230 crt->valid_from.min, crt->valid_from.sec );
1231 SAFE_SNPRINTF();
1232
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001233 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001234 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1235 crt->valid_to.year, crt->valid_to.mon,
1236 crt->valid_to.day, crt->valid_to.hour,
1237 crt->valid_to.min, crt->valid_to.sec );
1238 SAFE_SNPRINTF();
1239
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001240 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001241 SAFE_SNPRINTF();
1242
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001243 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1244 if( ret != 0 )
1245 ret = snprintf( p, n, "???" );
1246 else
1247 ret = snprintf( p, n, "%s", desc );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001248 SAFE_SNPRINTF();
1249
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001250 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001251 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1252 pk_get_name( &crt->pk ) ) ) != 0 )
1253 {
1254 return( ret );
1255 }
1256
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001257 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001258 (int) pk_get_size( &crt->pk ) );
1259 SAFE_SNPRINTF();
1260
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001261 /*
1262 * Optional extensions
1263 */
1264
1265 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1266 {
1267 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1268 crt->ca_istrue ? "true" : "false" );
1269 SAFE_SNPRINTF();
1270
1271 if( crt->max_pathlen > 0 )
1272 {
1273 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1274 SAFE_SNPRINTF();
1275 }
1276 }
1277
1278 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1279 {
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001280 ret = snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001281 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001282
1283 if( ( ret = x509_info_subject_alt_name( &p, &n,
1284 &crt->subject_alt_names ) ) != 0 )
1285 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001286 }
1287
1288 if( crt->ext_types & EXT_NS_CERT_TYPE )
1289 {
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001290 ret = snprintf( p, n, "\n%scert. type :", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001291 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001292
1293 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1294 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001295 }
1296
1297 if( crt->ext_types & EXT_KEY_USAGE )
1298 {
1299 ret = snprintf( p, n, "\n%skey usage : ", prefix );
1300 SAFE_SNPRINTF();
1301 /* TODO */
1302 }
1303
1304 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1305 {
1306 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
1307 SAFE_SNPRINTF();
1308 /* TODO */
1309 }
1310
1311 ret = snprintf( p, n, "\n" );
1312 SAFE_SNPRINTF();
1313
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 return( (int) ( size - n ) );
1315}
1316
1317#if defined(POLARSSL_X509_CRL_PARSE_C)
1318/*
1319 * Return 1 if the certificate is revoked, or 0 otherwise.
1320 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001321int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001322{
1323 const x509_crl_entry *cur = &crl->entry;
1324
1325 while( cur != NULL && cur->serial.len != 0 )
1326 {
1327 if( crt->serial.len == cur->serial.len &&
1328 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1329 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001330 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001331 return( 1 );
1332 }
1333
1334 cur = cur->next;
1335 }
1336
1337 return( 0 );
1338}
1339
1340/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001341 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001342 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001343static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001344 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001345{
1346 int flags = 0;
1347 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1348 const md_info_t *md_info;
1349
1350 if( ca == NULL )
1351 return( flags );
1352
1353 /*
1354 * TODO: What happens if no CRL is present?
1355 * Suggestion: Revocation state should be unknown if no CRL is present.
1356 * For backwards compatibility this is not yet implemented.
1357 */
1358
1359 while( crl_list != NULL )
1360 {
1361 if( crl_list->version == 0 ||
1362 crl_list->issuer_raw.len != ca->subject_raw.len ||
1363 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1364 crl_list->issuer_raw.len ) != 0 )
1365 {
1366 crl_list = crl_list->next;
1367 continue;
1368 }
1369
1370 /*
1371 * Check if CRL is correctly signed by the trusted CA
1372 */
1373 md_info = md_info_from_type( crl_list->sig_md );
1374 if( md_info == NULL )
1375 {
1376 /*
1377 * Cannot check 'unknown' hash
1378 */
1379 flags |= BADCRL_NOT_TRUSTED;
1380 break;
1381 }
1382
1383 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1384
1385 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1386 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1387 crl_list->sig.p, crl_list->sig.len ) != 0 )
1388 {
1389 flags |= BADCRL_NOT_TRUSTED;
1390 break;
1391 }
1392
1393 /*
1394 * Check for validity of CRL (Do not drop out)
1395 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001396 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001397 flags |= BADCRL_EXPIRED;
1398
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001399 if( x509_time_future( &crl_list->this_update ) )
1400 flags |= BADCRL_FUTURE;
1401
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001402 /*
1403 * Check if certificate is revoked
1404 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001405 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406 {
1407 flags |= BADCERT_REVOKED;
1408 break;
1409 }
1410
1411 crl_list = crl_list->next;
1412 }
1413 return flags;
1414}
1415#endif /* POLARSSL_X509_CRL_PARSE_C */
1416
1417// Equal == 0, inequal == 1
1418static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1419{
1420 size_t i;
1421 unsigned char diff;
1422 const unsigned char *n1 = s1, *n2 = s2;
1423
1424 for( i = 0; i < len; i++ )
1425 {
1426 diff = n1[i] ^ n2[i];
1427
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001428 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429 continue;
1430
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001431 if( diff == 32 &&
1432 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1433 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1434 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001435 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001436 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001437
1438 return( 1 );
1439 }
1440
1441 return( 0 );
1442}
1443
1444static int x509_wildcard_verify( const char *cn, x509_buf *name )
1445{
1446 size_t i;
1447 size_t cn_idx = 0;
1448
1449 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1450 return( 0 );
1451
1452 for( i = 0; i < strlen( cn ); ++i )
1453 {
1454 if( cn[i] == '.' )
1455 {
1456 cn_idx = i;
1457 break;
1458 }
1459 }
1460
1461 if( cn_idx == 0 )
1462 return( 0 );
1463
1464 if( strlen( cn ) - cn_idx == name->len - 1 &&
1465 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1466 {
1467 return( 1 );
1468 }
1469
1470 return( 0 );
1471}
1472
Paul Bakkerddf26b42013-09-18 13:46:23 +02001473static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001474 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001476 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001477 void *p_vrfy )
1478{
1479 int ret;
1480 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1481 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1482 const md_info_t *md_info;
1483
Paul Bakker86d0c192013-09-18 11:11:02 +02001484 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485 *flags |= BADCERT_EXPIRED;
1486
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001487 if( x509_time_future( &child->valid_from ) )
1488 *flags |= BADCERT_FUTURE;
1489
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490 /*
1491 * Child is the top of the chain. Check against the trust_ca list.
1492 */
1493 *flags |= BADCERT_NOT_TRUSTED;
1494
1495 md_info = md_info_from_type( child->sig_md );
1496 if( md_info == NULL )
1497 {
1498 /*
1499 * Cannot check 'unknown', no need to try any CA
1500 */
1501 trust_ca = NULL;
1502 }
1503 else
1504 md( md_info, child->tbs.p, child->tbs.len, hash );
1505
1506 while( trust_ca != NULL )
1507 {
1508 if( trust_ca->version == 0 ||
1509 child->issuer_raw.len != trust_ca->subject_raw.len ||
1510 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
1511 child->issuer_raw.len ) != 0 )
1512 {
1513 trust_ca = trust_ca->next;
1514 continue;
1515 }
1516
1517 /*
1518 * Reduce path_len to check against if top of the chain is
1519 * the same as the trusted CA
1520 */
1521 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1522 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1523 child->issuer_raw.len ) == 0 )
1524 {
1525 check_path_cnt--;
1526 }
1527
1528 if( trust_ca->max_pathlen > 0 &&
1529 trust_ca->max_pathlen < check_path_cnt )
1530 {
1531 trust_ca = trust_ca->next;
1532 continue;
1533 }
1534
1535 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1536 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1537 child->sig.p, child->sig.len ) != 0 )
1538 {
1539 trust_ca = trust_ca->next;
1540 continue;
1541 }
1542
1543 /*
1544 * Top of chain is signed by a trusted CA
1545 */
1546 *flags &= ~BADCERT_NOT_TRUSTED;
1547 break;
1548 }
1549
1550 /*
1551 * If top of chain is not the same as the trusted CA send a verify request
1552 * to the callback for any issues with validity and CRL presence for the
1553 * trusted CA certificate.
1554 */
1555 if( trust_ca != NULL &&
1556 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1557 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1558 child->issuer_raw.len ) != 0 ) )
1559 {
1560#if defined(POLARSSL_X509_CRL_PARSE_C)
1561 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001562 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001563#else
1564 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001565#endif
1566
Paul Bakker86d0c192013-09-18 11:11:02 +02001567 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001568 ca_flags |= BADCERT_EXPIRED;
1569
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001570 if( x509_time_future( &trust_ca->valid_from ) )
1571 ca_flags |= BADCERT_FUTURE;
1572
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573 if( NULL != f_vrfy )
1574 {
1575 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
1576 return( ret );
1577 }
1578 }
1579
1580 /* Call callback on top cert */
1581 if( NULL != f_vrfy )
1582 {
1583 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1584 return( ret );
1585 }
1586
1587 *flags |= ca_flags;
1588
1589 return( 0 );
1590}
1591
Paul Bakkerddf26b42013-09-18 13:46:23 +02001592static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001593 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001595 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596 void *p_vrfy )
1597{
1598 int ret;
1599 int parent_flags = 0;
1600 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001601 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602 const md_info_t *md_info;
1603
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001604 if( x509_time_future( &child->valid_from ) )
1605 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001606
1607 md_info = md_info_from_type( child->sig_md );
1608 if( md_info == NULL )
1609 {
1610 /*
1611 * Cannot check 'unknown' hash
1612 */
1613 *flags |= BADCERT_NOT_TRUSTED;
1614 }
1615 else
1616 {
1617 md( md_info, child->tbs.p, child->tbs.len, hash );
1618
1619 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1620 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1621 child->sig.p, child->sig.len ) != 0 )
1622 {
1623 *flags |= BADCERT_NOT_TRUSTED;
1624 }
1625 }
1626
1627#if defined(POLARSSL_X509_CRL_PARSE_C)
1628 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001629 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630#endif
1631
1632 grandparent = parent->next;
1633
1634 while( grandparent != NULL )
1635 {
1636 if( grandparent->version == 0 ||
1637 grandparent->ca_istrue == 0 ||
1638 parent->issuer_raw.len != grandparent->subject_raw.len ||
1639 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
1640 parent->issuer_raw.len ) != 0 )
1641 {
1642 grandparent = grandparent->next;
1643 continue;
1644 }
1645 break;
1646 }
1647
1648 if( grandparent != NULL )
1649 {
1650 /*
1651 * Part of the chain
1652 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001653 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 +02001654 if( ret != 0 )
1655 return( ret );
1656 }
1657 else
1658 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001659 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 +02001660 if( ret != 0 )
1661 return( ret );
1662 }
1663
1664 /* child is verified to be a child of the parent, call verify callback */
1665 if( NULL != f_vrfy )
1666 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1667 return( ret );
1668
1669 *flags |= parent_flags;
1670
1671 return( 0 );
1672}
1673
1674/*
1675 * Verify the certificate validity
1676 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001677int x509_crt_verify( x509_crt *crt,
1678 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001679 x509_crl *ca_crl,
1680 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001681 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001682 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001683{
1684 size_t cn_len;
1685 int ret;
1686 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001687 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001688 x509_name *name;
1689 x509_sequence *cur = NULL;
1690
1691 *flags = 0;
1692
1693 if( cn != NULL )
1694 {
1695 name = &crt->subject;
1696 cn_len = strlen( cn );
1697
1698 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1699 {
1700 cur = &crt->subject_alt_names;
1701
1702 while( cur != NULL )
1703 {
1704 if( cur->buf.len == cn_len &&
1705 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1706 break;
1707
1708 if( cur->buf.len > 2 &&
1709 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1710 x509_wildcard_verify( cn, &cur->buf ) )
1711 break;
1712
1713 cur = cur->next;
1714 }
1715
1716 if( cur == NULL )
1717 *flags |= BADCERT_CN_MISMATCH;
1718 }
1719 else
1720 {
1721 while( name != NULL )
1722 {
1723 if( OID_CMP( OID_AT_CN, &name->oid ) )
1724 {
1725 if( name->val.len == cn_len &&
1726 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1727 break;
1728
1729 if( name->val.len > 2 &&
1730 memcmp( name->val.p, "*.", 2 ) == 0 &&
1731 x509_wildcard_verify( cn, &name->val ) )
1732 break;
1733 }
1734
1735 name = name->next;
1736 }
1737
1738 if( name == NULL )
1739 *flags |= BADCERT_CN_MISMATCH;
1740 }
1741 }
1742
1743 /*
1744 * Iterate upwards in the given cert chain, to find our crt parent.
1745 * Ignore any upper cert with CA != TRUE.
1746 */
1747 parent = crt->next;
1748
1749 while( parent != NULL && parent->version != 0 )
1750 {
1751 if( parent->ca_istrue == 0 ||
1752 crt->issuer_raw.len != parent->subject_raw.len ||
1753 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
1754 crt->issuer_raw.len ) != 0 )
1755 {
1756 parent = parent->next;
1757 continue;
1758 }
1759 break;
1760 }
1761
1762 if( parent != NULL )
1763 {
1764 /*
1765 * Part of the chain
1766 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001767 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768 if( ret != 0 )
1769 return( ret );
1770 }
1771 else
1772 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001773 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001774 if( ret != 0 )
1775 return( ret );
1776 }
1777
1778 if( *flags != 0 )
1779 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1780
1781 return( 0 );
1782}
1783
1784/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001785 * Initialize a certificate chain
1786 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001787void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001788{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001789 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001790}
1791
1792/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001793 * Unallocate all certificate data
1794 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001795void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001796{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001797 x509_crt *cert_cur = crt;
1798 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001799 x509_name *name_cur;
1800 x509_name *name_prv;
1801 x509_sequence *seq_cur;
1802 x509_sequence *seq_prv;
1803
1804 if( crt == NULL )
1805 return;
1806
1807 do
1808 {
1809 pk_free( &cert_cur->pk );
1810
1811 name_cur = cert_cur->issuer.next;
1812 while( name_cur != NULL )
1813 {
1814 name_prv = name_cur;
1815 name_cur = name_cur->next;
1816 memset( name_prv, 0, sizeof( x509_name ) );
1817 polarssl_free( name_prv );
1818 }
1819
1820 name_cur = cert_cur->subject.next;
1821 while( name_cur != NULL )
1822 {
1823 name_prv = name_cur;
1824 name_cur = name_cur->next;
1825 memset( name_prv, 0, sizeof( x509_name ) );
1826 polarssl_free( name_prv );
1827 }
1828
1829 seq_cur = cert_cur->ext_key_usage.next;
1830 while( seq_cur != NULL )
1831 {
1832 seq_prv = seq_cur;
1833 seq_cur = seq_cur->next;
1834 memset( seq_prv, 0, sizeof( x509_sequence ) );
1835 polarssl_free( seq_prv );
1836 }
1837
1838 seq_cur = cert_cur->subject_alt_names.next;
1839 while( seq_cur != NULL )
1840 {
1841 seq_prv = seq_cur;
1842 seq_cur = seq_cur->next;
1843 memset( seq_prv, 0, sizeof( x509_sequence ) );
1844 polarssl_free( seq_prv );
1845 }
1846
1847 if( cert_cur->raw.p != NULL )
1848 {
1849 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1850 polarssl_free( cert_cur->raw.p );
1851 }
1852
1853 cert_cur = cert_cur->next;
1854 }
1855 while( cert_cur != NULL );
1856
1857 cert_cur = crt;
1858 do
1859 {
1860 cert_prv = cert_cur;
1861 cert_cur = cert_cur->next;
1862
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001863 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001864 if( cert_prv != crt )
1865 polarssl_free( cert_prv );
1866 }
1867 while( cert_cur != NULL );
1868}
1869
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001870#endif