blob: e88bd4611d644504777cf12c3a6e9c6a669d183d [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
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001189static int x509_info_key_usage( char **buf, size_t *size,
1190 unsigned char key_usage )
1191{
1192 int ret;
1193 size_t n = *size;
1194 char *p = *buf;
1195
1196 if( key_usage & KU_DIGITAL_SIGNATURE )
1197 {
1198 ret = snprintf( p, n, " digitalSignature" );
1199 SAFE_SNPRINTF();
1200 }
1201 if( key_usage & KU_NON_REPUDIATION )
1202 {
1203 ret = snprintf( p, n, " nonRepudiation" );
1204 SAFE_SNPRINTF();
1205 }
1206 if( key_usage & KU_KEY_ENCIPHERMENT )
1207 {
1208 ret = snprintf( p, n, " keyEncipherment" );
1209 SAFE_SNPRINTF();
1210 }
1211 if( key_usage & KU_DATA_ENCIPHERMENT )
1212 {
1213 ret = snprintf( p, n, " dataEncipherment" );
1214 SAFE_SNPRINTF();
1215 }
1216 if( key_usage & KU_KEY_AGREEMENT )
1217 {
1218 ret = snprintf( p, n, " keyAgreement" );
1219 SAFE_SNPRINTF();
1220 }
1221 if( key_usage & KU_KEY_CERT_SIGN )
1222 {
1223 ret = snprintf( p, n, " keyCertSign" );
1224 SAFE_SNPRINTF();
1225 }
1226 if( key_usage & KU_CRL_SIGN )
1227 {
1228 ret = snprintf( p, n, " cRLSign" );
1229 SAFE_SNPRINTF();
1230 }
1231
1232 *size = n;
1233 *buf = p;
1234
1235 return( 0 );
1236}
1237
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001238/*
1239 * Return an informational string about the certificate.
1240 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001241#define BEFORE_COLON 18
1242#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001243int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001244 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245{
1246 int ret;
1247 size_t n;
1248 char *p;
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001249 const char *desc = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001250 char key_size_str[BEFORE_COLON];
1251
1252 p = buf;
1253 n = size;
1254
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001255 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001256 prefix, crt->version );
1257 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001258 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001259 prefix );
1260 SAFE_SNPRINTF();
1261
Paul Bakker86d0c192013-09-18 11:11:02 +02001262 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001263 SAFE_SNPRINTF();
1264
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001265 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001266 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001267 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001268 SAFE_SNPRINTF();
1269
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001270 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001271 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001272 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273 SAFE_SNPRINTF();
1274
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001275 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1277 crt->valid_from.year, crt->valid_from.mon,
1278 crt->valid_from.day, crt->valid_from.hour,
1279 crt->valid_from.min, crt->valid_from.sec );
1280 SAFE_SNPRINTF();
1281
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001282 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001283 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1284 crt->valid_to.year, crt->valid_to.mon,
1285 crt->valid_to.day, crt->valid_to.hour,
1286 crt->valid_to.min, crt->valid_to.sec );
1287 SAFE_SNPRINTF();
1288
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001289 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001290 SAFE_SNPRINTF();
1291
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001292 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1293 if( ret != 0 )
1294 ret = snprintf( p, n, "???" );
1295 else
1296 ret = snprintf( p, n, "%s", desc );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001297 SAFE_SNPRINTF();
1298
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001299 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001300 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1301 pk_get_name( &crt->pk ) ) ) != 0 )
1302 {
1303 return( ret );
1304 }
1305
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001306 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 (int) pk_get_size( &crt->pk ) );
1308 SAFE_SNPRINTF();
1309
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001310 /*
1311 * Optional extensions
1312 */
1313
1314 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1315 {
1316 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1317 crt->ca_istrue ? "true" : "false" );
1318 SAFE_SNPRINTF();
1319
1320 if( crt->max_pathlen > 0 )
1321 {
1322 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1323 SAFE_SNPRINTF();
1324 }
1325 }
1326
1327 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1328 {
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001329 ret = snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001330 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001331
1332 if( ( ret = x509_info_subject_alt_name( &p, &n,
1333 &crt->subject_alt_names ) ) != 0 )
1334 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001335 }
1336
1337 if( crt->ext_types & EXT_NS_CERT_TYPE )
1338 {
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001339 ret = snprintf( p, n, "\n%scert. type :", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001340 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001341
1342 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1343 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001344 }
1345
1346 if( crt->ext_types & EXT_KEY_USAGE )
1347 {
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001348 ret = snprintf( p, n, "\n%skey usage :", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001349 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001350
1351 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1352 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001353 }
1354
1355 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1356 {
1357 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
1358 SAFE_SNPRINTF();
1359 /* TODO */
1360 }
1361
1362 ret = snprintf( p, n, "\n" );
1363 SAFE_SNPRINTF();
1364
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365 return( (int) ( size - n ) );
1366}
1367
1368#if defined(POLARSSL_X509_CRL_PARSE_C)
1369/*
1370 * Return 1 if the certificate is revoked, or 0 otherwise.
1371 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001372int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373{
1374 const x509_crl_entry *cur = &crl->entry;
1375
1376 while( cur != NULL && cur->serial.len != 0 )
1377 {
1378 if( crt->serial.len == cur->serial.len &&
1379 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1380 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001381 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382 return( 1 );
1383 }
1384
1385 cur = cur->next;
1386 }
1387
1388 return( 0 );
1389}
1390
1391/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001392 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001393 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001394static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001395 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001396{
1397 int flags = 0;
1398 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1399 const md_info_t *md_info;
1400
1401 if( ca == NULL )
1402 return( flags );
1403
1404 /*
1405 * TODO: What happens if no CRL is present?
1406 * Suggestion: Revocation state should be unknown if no CRL is present.
1407 * For backwards compatibility this is not yet implemented.
1408 */
1409
1410 while( crl_list != NULL )
1411 {
1412 if( crl_list->version == 0 ||
1413 crl_list->issuer_raw.len != ca->subject_raw.len ||
1414 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1415 crl_list->issuer_raw.len ) != 0 )
1416 {
1417 crl_list = crl_list->next;
1418 continue;
1419 }
1420
1421 /*
1422 * Check if CRL is correctly signed by the trusted CA
1423 */
1424 md_info = md_info_from_type( crl_list->sig_md );
1425 if( md_info == NULL )
1426 {
1427 /*
1428 * Cannot check 'unknown' hash
1429 */
1430 flags |= BADCRL_NOT_TRUSTED;
1431 break;
1432 }
1433
1434 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1435
1436 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1437 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1438 crl_list->sig.p, crl_list->sig.len ) != 0 )
1439 {
1440 flags |= BADCRL_NOT_TRUSTED;
1441 break;
1442 }
1443
1444 /*
1445 * Check for validity of CRL (Do not drop out)
1446 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001447 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001448 flags |= BADCRL_EXPIRED;
1449
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001450 if( x509_time_future( &crl_list->this_update ) )
1451 flags |= BADCRL_FUTURE;
1452
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453 /*
1454 * Check if certificate is revoked
1455 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001456 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457 {
1458 flags |= BADCERT_REVOKED;
1459 break;
1460 }
1461
1462 crl_list = crl_list->next;
1463 }
1464 return flags;
1465}
1466#endif /* POLARSSL_X509_CRL_PARSE_C */
1467
1468// Equal == 0, inequal == 1
1469static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1470{
1471 size_t i;
1472 unsigned char diff;
1473 const unsigned char *n1 = s1, *n2 = s2;
1474
1475 for( i = 0; i < len; i++ )
1476 {
1477 diff = n1[i] ^ n2[i];
1478
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001479 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480 continue;
1481
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001482 if( diff == 32 &&
1483 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1484 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1485 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001487 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488
1489 return( 1 );
1490 }
1491
1492 return( 0 );
1493}
1494
1495static int x509_wildcard_verify( const char *cn, x509_buf *name )
1496{
1497 size_t i;
1498 size_t cn_idx = 0;
1499
1500 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1501 return( 0 );
1502
1503 for( i = 0; i < strlen( cn ); ++i )
1504 {
1505 if( cn[i] == '.' )
1506 {
1507 cn_idx = i;
1508 break;
1509 }
1510 }
1511
1512 if( cn_idx == 0 )
1513 return( 0 );
1514
1515 if( strlen( cn ) - cn_idx == name->len - 1 &&
1516 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1517 {
1518 return( 1 );
1519 }
1520
1521 return( 0 );
1522}
1523
Paul Bakkerddf26b42013-09-18 13:46:23 +02001524static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001525 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001527 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001528 void *p_vrfy )
1529{
1530 int ret;
1531 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1532 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1533 const md_info_t *md_info;
1534
Paul Bakker86d0c192013-09-18 11:11:02 +02001535 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536 *flags |= BADCERT_EXPIRED;
1537
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001538 if( x509_time_future( &child->valid_from ) )
1539 *flags |= BADCERT_FUTURE;
1540
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541 /*
1542 * Child is the top of the chain. Check against the trust_ca list.
1543 */
1544 *flags |= BADCERT_NOT_TRUSTED;
1545
1546 md_info = md_info_from_type( child->sig_md );
1547 if( md_info == NULL )
1548 {
1549 /*
1550 * Cannot check 'unknown', no need to try any CA
1551 */
1552 trust_ca = NULL;
1553 }
1554 else
1555 md( md_info, child->tbs.p, child->tbs.len, hash );
1556
1557 while( trust_ca != NULL )
1558 {
1559 if( trust_ca->version == 0 ||
1560 child->issuer_raw.len != trust_ca->subject_raw.len ||
1561 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
1562 child->issuer_raw.len ) != 0 )
1563 {
1564 trust_ca = trust_ca->next;
1565 continue;
1566 }
1567
1568 /*
1569 * Reduce path_len to check against if top of the chain is
1570 * the same as the trusted CA
1571 */
1572 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1573 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1574 child->issuer_raw.len ) == 0 )
1575 {
1576 check_path_cnt--;
1577 }
1578
1579 if( trust_ca->max_pathlen > 0 &&
1580 trust_ca->max_pathlen < check_path_cnt )
1581 {
1582 trust_ca = trust_ca->next;
1583 continue;
1584 }
1585
1586 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1587 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1588 child->sig.p, child->sig.len ) != 0 )
1589 {
1590 trust_ca = trust_ca->next;
1591 continue;
1592 }
1593
1594 /*
1595 * Top of chain is signed by a trusted CA
1596 */
1597 *flags &= ~BADCERT_NOT_TRUSTED;
1598 break;
1599 }
1600
1601 /*
1602 * If top of chain is not the same as the trusted CA send a verify request
1603 * to the callback for any issues with validity and CRL presence for the
1604 * trusted CA certificate.
1605 */
1606 if( trust_ca != NULL &&
1607 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1608 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1609 child->issuer_raw.len ) != 0 ) )
1610 {
1611#if defined(POLARSSL_X509_CRL_PARSE_C)
1612 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001613 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001614#else
1615 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616#endif
1617
Paul Bakker86d0c192013-09-18 11:11:02 +02001618 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619 ca_flags |= BADCERT_EXPIRED;
1620
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001621 if( x509_time_future( &trust_ca->valid_from ) )
1622 ca_flags |= BADCERT_FUTURE;
1623
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624 if( NULL != f_vrfy )
1625 {
1626 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
1627 return( ret );
1628 }
1629 }
1630
1631 /* Call callback on top cert */
1632 if( NULL != f_vrfy )
1633 {
1634 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1635 return( ret );
1636 }
1637
1638 *flags |= ca_flags;
1639
1640 return( 0 );
1641}
1642
Paul Bakkerddf26b42013-09-18 13:46:23 +02001643static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001644 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001645 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001646 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001647 void *p_vrfy )
1648{
1649 int ret;
1650 int parent_flags = 0;
1651 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001652 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001653 const md_info_t *md_info;
1654
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001655 if( x509_time_future( &child->valid_from ) )
1656 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657
1658 md_info = md_info_from_type( child->sig_md );
1659 if( md_info == NULL )
1660 {
1661 /*
1662 * Cannot check 'unknown' hash
1663 */
1664 *flags |= BADCERT_NOT_TRUSTED;
1665 }
1666 else
1667 {
1668 md( md_info, child->tbs.p, child->tbs.len, hash );
1669
1670 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1671 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1672 child->sig.p, child->sig.len ) != 0 )
1673 {
1674 *flags |= BADCERT_NOT_TRUSTED;
1675 }
1676 }
1677
1678#if defined(POLARSSL_X509_CRL_PARSE_C)
1679 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001680 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001681#endif
1682
1683 grandparent = parent->next;
1684
1685 while( grandparent != NULL )
1686 {
1687 if( grandparent->version == 0 ||
1688 grandparent->ca_istrue == 0 ||
1689 parent->issuer_raw.len != grandparent->subject_raw.len ||
1690 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
1691 parent->issuer_raw.len ) != 0 )
1692 {
1693 grandparent = grandparent->next;
1694 continue;
1695 }
1696 break;
1697 }
1698
1699 if( grandparent != NULL )
1700 {
1701 /*
1702 * Part of the chain
1703 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001704 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 +02001705 if( ret != 0 )
1706 return( ret );
1707 }
1708 else
1709 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001710 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 +02001711 if( ret != 0 )
1712 return( ret );
1713 }
1714
1715 /* child is verified to be a child of the parent, call verify callback */
1716 if( NULL != f_vrfy )
1717 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1718 return( ret );
1719
1720 *flags |= parent_flags;
1721
1722 return( 0 );
1723}
1724
1725/*
1726 * Verify the certificate validity
1727 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001728int x509_crt_verify( x509_crt *crt,
1729 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001730 x509_crl *ca_crl,
1731 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001732 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001733 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001734{
1735 size_t cn_len;
1736 int ret;
1737 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001738 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001739 x509_name *name;
1740 x509_sequence *cur = NULL;
1741
1742 *flags = 0;
1743
1744 if( cn != NULL )
1745 {
1746 name = &crt->subject;
1747 cn_len = strlen( cn );
1748
1749 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1750 {
1751 cur = &crt->subject_alt_names;
1752
1753 while( cur != NULL )
1754 {
1755 if( cur->buf.len == cn_len &&
1756 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1757 break;
1758
1759 if( cur->buf.len > 2 &&
1760 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1761 x509_wildcard_verify( cn, &cur->buf ) )
1762 break;
1763
1764 cur = cur->next;
1765 }
1766
1767 if( cur == NULL )
1768 *flags |= BADCERT_CN_MISMATCH;
1769 }
1770 else
1771 {
1772 while( name != NULL )
1773 {
1774 if( OID_CMP( OID_AT_CN, &name->oid ) )
1775 {
1776 if( name->val.len == cn_len &&
1777 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1778 break;
1779
1780 if( name->val.len > 2 &&
1781 memcmp( name->val.p, "*.", 2 ) == 0 &&
1782 x509_wildcard_verify( cn, &name->val ) )
1783 break;
1784 }
1785
1786 name = name->next;
1787 }
1788
1789 if( name == NULL )
1790 *flags |= BADCERT_CN_MISMATCH;
1791 }
1792 }
1793
1794 /*
1795 * Iterate upwards in the given cert chain, to find our crt parent.
1796 * Ignore any upper cert with CA != TRUE.
1797 */
1798 parent = crt->next;
1799
1800 while( parent != NULL && parent->version != 0 )
1801 {
1802 if( parent->ca_istrue == 0 ||
1803 crt->issuer_raw.len != parent->subject_raw.len ||
1804 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
1805 crt->issuer_raw.len ) != 0 )
1806 {
1807 parent = parent->next;
1808 continue;
1809 }
1810 break;
1811 }
1812
1813 if( parent != NULL )
1814 {
1815 /*
1816 * Part of the chain
1817 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001818 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001819 if( ret != 0 )
1820 return( ret );
1821 }
1822 else
1823 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001824 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001825 if( ret != 0 )
1826 return( ret );
1827 }
1828
1829 if( *flags != 0 )
1830 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1831
1832 return( 0 );
1833}
1834
1835/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001836 * Initialize a certificate chain
1837 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001838void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001839{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001840 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001841}
1842
1843/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001844 * Unallocate all certificate data
1845 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001846void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001847{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001848 x509_crt *cert_cur = crt;
1849 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001850 x509_name *name_cur;
1851 x509_name *name_prv;
1852 x509_sequence *seq_cur;
1853 x509_sequence *seq_prv;
1854
1855 if( crt == NULL )
1856 return;
1857
1858 do
1859 {
1860 pk_free( &cert_cur->pk );
1861
1862 name_cur = cert_cur->issuer.next;
1863 while( name_cur != NULL )
1864 {
1865 name_prv = name_cur;
1866 name_cur = name_cur->next;
1867 memset( name_prv, 0, sizeof( x509_name ) );
1868 polarssl_free( name_prv );
1869 }
1870
1871 name_cur = cert_cur->subject.next;
1872 while( name_cur != NULL )
1873 {
1874 name_prv = name_cur;
1875 name_cur = name_cur->next;
1876 memset( name_prv, 0, sizeof( x509_name ) );
1877 polarssl_free( name_prv );
1878 }
1879
1880 seq_cur = cert_cur->ext_key_usage.next;
1881 while( seq_cur != NULL )
1882 {
1883 seq_prv = seq_cur;
1884 seq_cur = seq_cur->next;
1885 memset( seq_prv, 0, sizeof( x509_sequence ) );
1886 polarssl_free( seq_prv );
1887 }
1888
1889 seq_cur = cert_cur->subject_alt_names.next;
1890 while( seq_cur != NULL )
1891 {
1892 seq_prv = seq_cur;
1893 seq_cur = seq_cur->next;
1894 memset( seq_prv, 0, sizeof( x509_sequence ) );
1895 polarssl_free( seq_prv );
1896 }
1897
1898 if( cert_cur->raw.p != NULL )
1899 {
1900 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1901 polarssl_free( cert_cur->raw.p );
1902 }
1903
1904 cert_cur = cert_cur->next;
1905 }
1906 while( cert_cur != NULL );
1907
1908 cert_cur = crt;
1909 do
1910 {
1911 cert_prv = cert_cur;
1912 cert_cur = cert_cur->next;
1913
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001914 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001915 if( cert_prv != crt )
1916 polarssl_free( cert_prv );
1917 }
1918 while( cert_cur != NULL );
1919}
1920
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001921#endif