blob: be2e526cc5611002b4d8f86536747671fed663d6 [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>
Paul Bakker5ff3f912014-04-04 15:08:20 +020072#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073#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;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001110 const char *sep = "";
1111 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001112
1113 while( cur != NULL )
1114 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001115 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001116 {
1117 *p = '\0';
1118 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1119 }
1120
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001121 n -= cur->buf.len + sep_len;
1122 for( i = 0; i < sep_len; i++ )
1123 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001124 for( i = 0; i < cur->buf.len; i++ )
1125 *p++ = cur->buf.p[i];
1126
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001127 sep = ", ";
1128 sep_len = 2;
1129
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001130 cur = cur->next;
1131 }
1132
1133 *p = '\0';
1134
1135 *size = n;
1136 *buf = p;
1137
1138 return( 0 );
1139}
1140
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001141#define PRINT_ITEM(i) \
1142 { \
1143 ret = snprintf( p, n, "%s" i, sep ); \
1144 SAFE_SNPRINTF(); \
1145 sep = ", "; \
1146 }
1147
1148#define CERT_TYPE(type,name) \
1149 if( ns_cert_type & type ) \
1150 PRINT_ITEM( name );
1151
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001152static int x509_info_cert_type( char **buf, size_t *size,
1153 unsigned char ns_cert_type )
1154{
1155 int ret;
1156 size_t n = *size;
1157 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001158 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001159
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001160 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1161 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1162 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1163 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1164 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1165 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1166 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1167 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001168
1169 *size = n;
1170 *buf = p;
1171
1172 return( 0 );
1173}
1174
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001175#define KEY_USAGE(code,name) \
1176 if( key_usage & code ) \
1177 PRINT_ITEM( name );
1178
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001179static int x509_info_key_usage( char **buf, size_t *size,
1180 unsigned char key_usage )
1181{
1182 int ret;
1183 size_t n = *size;
1184 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001185 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001186
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001187 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1188 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1189 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1190 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1191 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1192 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1193 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001194
1195 *size = n;
1196 *buf = p;
1197
1198 return( 0 );
1199}
1200
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001201static int x509_info_ext_key_usage( char **buf, size_t *size,
1202 const x509_sequence *extended_key_usage )
1203{
1204 int ret;
1205 const char *desc;
1206 size_t n = *size;
1207 char *p = *buf;
1208 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001209 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001210
1211 while( cur != NULL )
1212 {
1213 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1214 desc = "???";
1215
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001216 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001217 SAFE_SNPRINTF();
1218
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001219 sep = ", ";
1220
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001221 cur = cur->next;
1222 }
1223
1224 *size = n;
1225 *buf = p;
1226
1227 return( 0 );
1228}
1229
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001230/*
1231 * Return an informational string about the certificate.
1232 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001233#define BEFORE_COLON 18
1234#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001235int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001236 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001237{
1238 int ret;
1239 size_t n;
1240 char *p;
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001241 const char *desc = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242 char key_size_str[BEFORE_COLON];
1243
1244 p = buf;
1245 n = size;
1246
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001247 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001248 prefix, crt->version );
1249 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001250 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001251 prefix );
1252 SAFE_SNPRINTF();
1253
Paul Bakker86d0c192013-09-18 11:11:02 +02001254 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001255 SAFE_SNPRINTF();
1256
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001257 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001258 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001259 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001260 SAFE_SNPRINTF();
1261
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001262 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001263 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001264 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001265 SAFE_SNPRINTF();
1266
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001267 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001268 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1269 crt->valid_from.year, crt->valid_from.mon,
1270 crt->valid_from.day, crt->valid_from.hour,
1271 crt->valid_from.min, crt->valid_from.sec );
1272 SAFE_SNPRINTF();
1273
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001274 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001275 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1276 crt->valid_to.year, crt->valid_to.mon,
1277 crt->valid_to.day, crt->valid_to.hour,
1278 crt->valid_to.min, crt->valid_to.sec );
1279 SAFE_SNPRINTF();
1280
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001281 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282 SAFE_SNPRINTF();
1283
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001284 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1285 if( ret != 0 )
1286 ret = snprintf( p, n, "???" );
1287 else
1288 ret = snprintf( p, n, "%s", desc );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001289 SAFE_SNPRINTF();
1290
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001291 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001292 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1293 pk_get_name( &crt->pk ) ) ) != 0 )
1294 {
1295 return( ret );
1296 }
1297
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001298 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001299 (int) pk_get_size( &crt->pk ) );
1300 SAFE_SNPRINTF();
1301
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001302 /*
1303 * Optional extensions
1304 */
1305
1306 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1307 {
1308 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1309 crt->ca_istrue ? "true" : "false" );
1310 SAFE_SNPRINTF();
1311
1312 if( crt->max_pathlen > 0 )
1313 {
1314 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1315 SAFE_SNPRINTF();
1316 }
1317 }
1318
1319 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1320 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001321 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001322 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001323
1324 if( ( ret = x509_info_subject_alt_name( &p, &n,
1325 &crt->subject_alt_names ) ) != 0 )
1326 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001327 }
1328
1329 if( crt->ext_types & EXT_NS_CERT_TYPE )
1330 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001331 ret = snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001332 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001333
1334 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1335 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001336 }
1337
1338 if( crt->ext_types & EXT_KEY_USAGE )
1339 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001340 ret = snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001341 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001342
1343 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1344 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001345 }
1346
1347 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1348 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001349 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001350 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001351
1352 if( ( ret = x509_info_ext_key_usage( &p, &n,
1353 &crt->ext_key_usage ) ) != 0 )
1354 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001355 }
1356
1357 ret = snprintf( p, n, "\n" );
1358 SAFE_SNPRINTF();
1359
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360 return( (int) ( size - n ) );
1361}
1362
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001363#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1364int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1365{
1366 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1367 ( crt->key_usage & usage ) != usage )
1368 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1369
1370 return( 0 );
1371}
1372#endif
1373
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001374#if defined(POLARSSL_X509_CRL_PARSE_C)
1375/*
1376 * Return 1 if the certificate is revoked, or 0 otherwise.
1377 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001378int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001379{
1380 const x509_crl_entry *cur = &crl->entry;
1381
1382 while( cur != NULL && cur->serial.len != 0 )
1383 {
1384 if( crt->serial.len == cur->serial.len &&
1385 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1386 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001387 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001388 return( 1 );
1389 }
1390
1391 cur = cur->next;
1392 }
1393
1394 return( 0 );
1395}
1396
1397/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001398 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001399 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001400static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001401 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001402{
1403 int flags = 0;
1404 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1405 const md_info_t *md_info;
1406
1407 if( ca == NULL )
1408 return( flags );
1409
1410 /*
1411 * TODO: What happens if no CRL is present?
1412 * Suggestion: Revocation state should be unknown if no CRL is present.
1413 * For backwards compatibility this is not yet implemented.
1414 */
1415
1416 while( crl_list != NULL )
1417 {
1418 if( crl_list->version == 0 ||
1419 crl_list->issuer_raw.len != ca->subject_raw.len ||
1420 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1421 crl_list->issuer_raw.len ) != 0 )
1422 {
1423 crl_list = crl_list->next;
1424 continue;
1425 }
1426
1427 /*
1428 * Check if CRL is correctly signed by the trusted CA
1429 */
1430 md_info = md_info_from_type( crl_list->sig_md );
1431 if( md_info == NULL )
1432 {
1433 /*
1434 * Cannot check 'unknown' hash
1435 */
1436 flags |= BADCRL_NOT_TRUSTED;
1437 break;
1438 }
1439
1440 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1441
1442 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1443 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1444 crl_list->sig.p, crl_list->sig.len ) != 0 )
1445 {
1446 flags |= BADCRL_NOT_TRUSTED;
1447 break;
1448 }
1449
1450 /*
1451 * Check for validity of CRL (Do not drop out)
1452 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001453 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454 flags |= BADCRL_EXPIRED;
1455
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001456 if( x509_time_future( &crl_list->this_update ) )
1457 flags |= BADCRL_FUTURE;
1458
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001459 /*
1460 * Check if certificate is revoked
1461 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001462 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463 {
1464 flags |= BADCERT_REVOKED;
1465 break;
1466 }
1467
1468 crl_list = crl_list->next;
1469 }
1470 return flags;
1471}
1472#endif /* POLARSSL_X509_CRL_PARSE_C */
1473
1474// Equal == 0, inequal == 1
1475static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1476{
1477 size_t i;
1478 unsigned char diff;
1479 const unsigned char *n1 = s1, *n2 = s2;
1480
1481 for( i = 0; i < len; i++ )
1482 {
1483 diff = n1[i] ^ n2[i];
1484
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001485 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486 continue;
1487
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001488 if( diff == 32 &&
1489 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1490 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1491 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001492 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001493 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001494
1495 return( 1 );
1496 }
1497
1498 return( 0 );
1499}
1500
1501static int x509_wildcard_verify( const char *cn, x509_buf *name )
1502{
1503 size_t i;
1504 size_t cn_idx = 0;
1505
1506 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1507 return( 0 );
1508
1509 for( i = 0; i < strlen( cn ); ++i )
1510 {
1511 if( cn[i] == '.' )
1512 {
1513 cn_idx = i;
1514 break;
1515 }
1516 }
1517
1518 if( cn_idx == 0 )
1519 return( 0 );
1520
1521 if( strlen( cn ) - cn_idx == name->len - 1 &&
1522 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1523 {
1524 return( 1 );
1525 }
1526
1527 return( 0 );
1528}
1529
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001530/*
1531 * Iterate upwards in the given cert chain to find our parent.
1532 *
1533 * Ignore any upper cert that can't be used to sign other certificates
1534 * (basic constraints CA=true for now, keyUsage soon).
1535 */
1536static x509_crt *x509_crt_find_parent( x509_crt *crt )
1537{
1538 x509_crt *parent;
1539
1540 for( parent = crt->next; parent != NULL; parent = parent->next )
1541 {
1542 if( parent->version == 0 ||
1543 parent->ca_istrue == 0 ||
1544 crt->issuer_raw.len != parent->subject_raw.len ||
1545 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
1546 crt->issuer_raw.len ) != 0 )
1547 {
1548 continue;
1549 }
1550
1551 /* If we get there, we found a suitable parent */
1552 break;
1553 }
1554
1555 return( parent );
1556}
1557
Paul Bakkerddf26b42013-09-18 13:46:23 +02001558static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001559 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001561 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562 void *p_vrfy )
1563{
1564 int ret;
1565 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1566 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1567 const md_info_t *md_info;
1568
Paul Bakker86d0c192013-09-18 11:11:02 +02001569 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570 *flags |= BADCERT_EXPIRED;
1571
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001572 if( x509_time_future( &child->valid_from ) )
1573 *flags |= BADCERT_FUTURE;
1574
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575 /*
1576 * Child is the top of the chain. Check against the trust_ca list.
1577 */
1578 *flags |= BADCERT_NOT_TRUSTED;
1579
1580 md_info = md_info_from_type( child->sig_md );
1581 if( md_info == NULL )
1582 {
1583 /*
1584 * Cannot check 'unknown', no need to try any CA
1585 */
1586 trust_ca = NULL;
1587 }
1588 else
1589 md( md_info, child->tbs.p, child->tbs.len, hash );
1590
1591 while( trust_ca != NULL )
1592 {
1593 if( trust_ca->version == 0 ||
1594 child->issuer_raw.len != trust_ca->subject_raw.len ||
1595 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
1596 child->issuer_raw.len ) != 0 )
1597 {
1598 trust_ca = trust_ca->next;
1599 continue;
1600 }
1601
1602 /*
1603 * Reduce path_len to check against if top of the chain is
1604 * the same as the trusted CA
1605 */
1606 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1607 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1608 child->issuer_raw.len ) == 0 )
1609 {
1610 check_path_cnt--;
1611 }
1612
1613 if( trust_ca->max_pathlen > 0 &&
1614 trust_ca->max_pathlen < check_path_cnt )
1615 {
1616 trust_ca = trust_ca->next;
1617 continue;
1618 }
1619
1620 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1621 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1622 child->sig.p, child->sig.len ) != 0 )
1623 {
1624 trust_ca = trust_ca->next;
1625 continue;
1626 }
1627
1628 /*
1629 * Top of chain is signed by a trusted CA
1630 */
1631 *flags &= ~BADCERT_NOT_TRUSTED;
1632 break;
1633 }
1634
1635 /*
1636 * If top of chain is not the same as the trusted CA send a verify request
1637 * to the callback for any issues with validity and CRL presence for the
1638 * trusted CA certificate.
1639 */
1640 if( trust_ca != NULL &&
1641 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1642 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1643 child->issuer_raw.len ) != 0 ) )
1644 {
1645#if defined(POLARSSL_X509_CRL_PARSE_C)
1646 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001647 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001648#else
1649 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001650#endif
1651
Paul Bakker86d0c192013-09-18 11:11:02 +02001652 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001653 ca_flags |= BADCERT_EXPIRED;
1654
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001655 if( x509_time_future( &trust_ca->valid_from ) )
1656 ca_flags |= BADCERT_FUTURE;
1657
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001658 if( NULL != f_vrfy )
1659 {
1660 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
1661 return( ret );
1662 }
1663 }
1664
1665 /* Call callback on top cert */
1666 if( NULL != f_vrfy )
1667 {
1668 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1669 return( ret );
1670 }
1671
1672 *flags |= ca_flags;
1673
1674 return( 0 );
1675}
1676
Paul Bakkerddf26b42013-09-18 13:46:23 +02001677static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001678 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001680 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001681 void *p_vrfy )
1682{
1683 int ret;
1684 int parent_flags = 0;
1685 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001686 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001687 const md_info_t *md_info;
1688
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001689 if( x509_time_expired( &child->valid_to ) )
1690 *flags |= BADCERT_EXPIRED;
1691
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001692 if( x509_time_future( &child->valid_from ) )
1693 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001694
1695 md_info = md_info_from_type( child->sig_md );
1696 if( md_info == NULL )
1697 {
1698 /*
1699 * Cannot check 'unknown' hash
1700 */
1701 *flags |= BADCERT_NOT_TRUSTED;
1702 }
1703 else
1704 {
1705 md( md_info, child->tbs.p, child->tbs.len, hash );
1706
1707 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1708 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1709 child->sig.p, child->sig.len ) != 0 )
1710 {
1711 *flags |= BADCERT_NOT_TRUSTED;
1712 }
1713 }
1714
1715#if defined(POLARSSL_X509_CRL_PARSE_C)
1716 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001717 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001718#endif
1719
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001720 if( ( grandparent = x509_crt_find_parent( parent) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721 {
1722 /*
1723 * Part of the chain
1724 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001725 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 +02001726 if( ret != 0 )
1727 return( ret );
1728 }
1729 else
1730 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001731 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 +02001732 if( ret != 0 )
1733 return( ret );
1734 }
1735
1736 /* child is verified to be a child of the parent, call verify callback */
1737 if( NULL != f_vrfy )
1738 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1739 return( ret );
1740
1741 *flags |= parent_flags;
1742
1743 return( 0 );
1744}
1745
1746/*
1747 * Verify the certificate validity
1748 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001749int x509_crt_verify( x509_crt *crt,
1750 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001751 x509_crl *ca_crl,
1752 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001753 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001754 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001755{
1756 size_t cn_len;
1757 int ret;
1758 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001759 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001760 x509_name *name;
1761 x509_sequence *cur = NULL;
1762
1763 *flags = 0;
1764
1765 if( cn != NULL )
1766 {
1767 name = &crt->subject;
1768 cn_len = strlen( cn );
1769
1770 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1771 {
1772 cur = &crt->subject_alt_names;
1773
1774 while( cur != NULL )
1775 {
1776 if( cur->buf.len == cn_len &&
1777 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1778 break;
1779
1780 if( cur->buf.len > 2 &&
1781 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1782 x509_wildcard_verify( cn, &cur->buf ) )
1783 break;
1784
1785 cur = cur->next;
1786 }
1787
1788 if( cur == NULL )
1789 *flags |= BADCERT_CN_MISMATCH;
1790 }
1791 else
1792 {
1793 while( name != NULL )
1794 {
1795 if( OID_CMP( OID_AT_CN, &name->oid ) )
1796 {
1797 if( name->val.len == cn_len &&
1798 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1799 break;
1800
1801 if( name->val.len > 2 &&
1802 memcmp( name->val.p, "*.", 2 ) == 0 &&
1803 x509_wildcard_verify( cn, &name->val ) )
1804 break;
1805 }
1806
1807 name = name->next;
1808 }
1809
1810 if( name == NULL )
1811 *flags |= BADCERT_CN_MISMATCH;
1812 }
1813 }
1814
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001815 if( ( parent = x509_crt_find_parent( crt ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001816 {
1817 /*
1818 * Part of the chain
1819 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001820 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001821 if( ret != 0 )
1822 return( ret );
1823 }
1824 else
1825 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001826 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001827 if( ret != 0 )
1828 return( ret );
1829 }
1830
1831 if( *flags != 0 )
1832 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1833
1834 return( 0 );
1835}
1836
1837/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001838 * Initialize a certificate chain
1839 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001840void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001841{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001842 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001843}
1844
1845/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001846 * Unallocate all certificate data
1847 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001848void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001849{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001850 x509_crt *cert_cur = crt;
1851 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001852 x509_name *name_cur;
1853 x509_name *name_prv;
1854 x509_sequence *seq_cur;
1855 x509_sequence *seq_prv;
1856
1857 if( crt == NULL )
1858 return;
1859
1860 do
1861 {
1862 pk_free( &cert_cur->pk );
1863
1864 name_cur = cert_cur->issuer.next;
1865 while( name_cur != NULL )
1866 {
1867 name_prv = name_cur;
1868 name_cur = name_cur->next;
1869 memset( name_prv, 0, sizeof( x509_name ) );
1870 polarssl_free( name_prv );
1871 }
1872
1873 name_cur = cert_cur->subject.next;
1874 while( name_cur != NULL )
1875 {
1876 name_prv = name_cur;
1877 name_cur = name_cur->next;
1878 memset( name_prv, 0, sizeof( x509_name ) );
1879 polarssl_free( name_prv );
1880 }
1881
1882 seq_cur = cert_cur->ext_key_usage.next;
1883 while( seq_cur != NULL )
1884 {
1885 seq_prv = seq_cur;
1886 seq_cur = seq_cur->next;
1887 memset( seq_prv, 0, sizeof( x509_sequence ) );
1888 polarssl_free( seq_prv );
1889 }
1890
1891 seq_cur = cert_cur->subject_alt_names.next;
1892 while( seq_cur != NULL )
1893 {
1894 seq_prv = seq_cur;
1895 seq_cur = seq_cur->next;
1896 memset( seq_prv, 0, sizeof( x509_sequence ) );
1897 polarssl_free( seq_prv );
1898 }
1899
1900 if( cert_cur->raw.p != NULL )
1901 {
1902 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1903 polarssl_free( cert_cur->raw.p );
1904 }
1905
1906 cert_cur = cert_cur->next;
1907 }
1908 while( cert_cur != NULL );
1909
1910 cert_cur = crt;
1911 do
1912 {
1913 cert_prv = cert_cur;
1914 cert_cur = cert_cur->next;
1915
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001916 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001917 if( cert_prv != crt )
1918 polarssl_free( cert_prv );
1919 }
1920 while( cert_cur != NULL );
1921}
1922
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001923#endif