blob: 025f3e0e17e27039850eef547af27cd0ff2b597f [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
344 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
345 {
346 *p += tag_len;
347 continue;
348 }
349
350 buf = &(cur->buf);
351 buf->tag = tag;
352 buf->p = *p;
353 buf->len = tag_len;
354 *p += buf->len;
355
356 /* Allocate and assign next pointer */
357 if (*p < end)
358 {
359 cur->next = (asn1_sequence *) polarssl_malloc(
360 sizeof( asn1_sequence ) );
361
362 if( cur->next == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200363 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 POLARSSL_ERR_ASN1_MALLOC_FAILED );
365
366 memset( cur->next, 0, sizeof( asn1_sequence ) );
367 cur = cur->next;
368 }
369 }
370
371 /* Set final sequence entry's next pointer to NULL */
372 cur->next = NULL;
373
374 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200375 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
377
378 return( 0 );
379}
380
381/*
382 * X.509 v3 extensions
383 *
384 * TODO: Perform all of the basic constraints tests required by the RFC
385 * TODO: Set values for undetected extensions to a sane default?
386 *
387 */
388static int x509_get_crt_ext( unsigned char **p,
389 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200390 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391{
392 int ret;
393 size_t len;
394 unsigned char *end_ext_data, *end_ext_octet;
395
396 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
397 {
398 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
399 return( 0 );
400
401 return( ret );
402 }
403
404 while( *p < end )
405 {
406 /*
407 * Extension ::= SEQUENCE {
408 * extnID OBJECT IDENTIFIER,
409 * critical BOOLEAN DEFAULT FALSE,
410 * extnValue OCTET STRING }
411 */
412 x509_buf extn_oid = {0, 0, NULL};
413 int is_critical = 0; /* DEFAULT FALSE */
414 int ext_type = 0;
415
416 if( ( ret = asn1_get_tag( p, end, &len,
417 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200418 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419
420 end_ext_data = *p + len;
421
422 /* Get extension ID */
423 extn_oid.tag = **p;
424
425 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200426 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427
428 extn_oid.p = *p;
429 *p += extn_oid.len;
430
431 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200432 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433 POLARSSL_ERR_ASN1_OUT_OF_DATA );
434
435 /* Get optional critical */
436 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
437 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200438 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439
440 /* Data should be octet string type */
441 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
442 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200443 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
445 end_ext_octet = *p + len;
446
447 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200448 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
450
451 /*
452 * Detect supported extensions
453 */
454 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
455
456 if( ret != 0 )
457 {
458 /* No parser found, skip extension */
459 *p = end_ext_octet;
460
461#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
462 if( is_critical )
463 {
464 /* Data is marked as critical: fail */
Paul Bakker51876562013-09-17 14:36:05 +0200465 return ( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
467 }
468#endif
469 continue;
470 }
471
472 crt->ext_types |= ext_type;
473
474 switch( ext_type )
475 {
476 case EXT_BASIC_CONSTRAINTS:
477 /* Parse basic constraints */
478 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
479 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
480 return ( ret );
481 break;
482
483 case EXT_KEY_USAGE:
484 /* Parse key usage */
485 if( ( ret = x509_get_key_usage( p, end_ext_octet,
486 &crt->key_usage ) ) != 0 )
487 return ( ret );
488 break;
489
490 case EXT_EXTENDED_KEY_USAGE:
491 /* Parse extended key usage */
492 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
493 &crt->ext_key_usage ) ) != 0 )
494 return ( ret );
495 break;
496
497 case EXT_SUBJECT_ALT_NAME:
498 /* Parse subject alt name */
499 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
500 &crt->subject_alt_names ) ) != 0 )
501 return ( ret );
502 break;
503
504 case EXT_NS_CERT_TYPE:
505 /* Parse netscape certificate type */
506 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
507 &crt->ns_cert_type ) ) != 0 )
508 return ( ret );
509 break;
510
511 default:
512 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
513 }
514 }
515
516 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200517 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
519
520 return( 0 );
521}
522
523/*
524 * Parse and fill a single X.509 certificate in DER format
525 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200526static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200527 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528{
529 int ret;
530 size_t len;
531 unsigned char *p, *end, *crt_end;
532
533 /*
534 * Check for valid input
535 */
536 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200537 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538
539 p = (unsigned char *) polarssl_malloc( len = buflen );
540
541 if( p == NULL )
542 return( POLARSSL_ERR_X509_MALLOC_FAILED );
543
544 memcpy( p, buf, buflen );
545
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546 crt->raw.p = p;
547 crt->raw.len = len;
548 end = p + len;
549
550 /*
551 * Certificate ::= SEQUENCE {
552 * tbsCertificate TBSCertificate,
553 * signatureAlgorithm AlgorithmIdentifier,
554 * signatureValue BIT STRING }
555 */
556 if( ( ret = asn1_get_tag( &p, end, &len,
557 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
558 {
559 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200560 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561 }
562
563 if( len > (size_t) ( end - p ) )
564 {
565 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200566 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
568 }
569 crt_end = p + len;
570
571 /*
572 * TBSCertificate ::= SEQUENCE {
573 */
574 crt->tbs.p = p;
575
576 if( ( ret = asn1_get_tag( &p, end, &len,
577 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
578 {
579 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200580 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581 }
582
583 end = p + len;
584 crt->tbs.len = end - crt->tbs.p;
585
586 /*
587 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
588 *
589 * CertificateSerialNumber ::= INTEGER
590 *
591 * signature AlgorithmIdentifier
592 */
593 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
594 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +0100595 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200596 {
597 x509_crt_free( crt );
598 return( ret );
599 }
600
601 crt->version++;
602
603 if( crt->version > 3 )
604 {
605 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200606 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200607 }
608
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +0100609 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
610 &crt->sig_pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611 {
612 x509_crt_free( crt );
613 return( ret );
614 }
615
616 /*
617 * issuer Name
618 */
619 crt->issuer_raw.p = p;
620
621 if( ( ret = asn1_get_tag( &p, end, &len,
622 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
623 {
624 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200625 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 }
627
628 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
629 {
630 x509_crt_free( crt );
631 return( ret );
632 }
633
634 crt->issuer_raw.len = p - crt->issuer_raw.p;
635
636 /*
637 * Validity ::= SEQUENCE {
638 * notBefore Time,
639 * notAfter Time }
640 *
641 */
642 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
643 &crt->valid_to ) ) != 0 )
644 {
645 x509_crt_free( crt );
646 return( ret );
647 }
648
649 /*
650 * subject Name
651 */
652 crt->subject_raw.p = p;
653
654 if( ( ret = asn1_get_tag( &p, end, &len,
655 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
656 {
657 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200658 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659 }
660
661 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
662 {
663 x509_crt_free( crt );
664 return( ret );
665 }
666
667 crt->subject_raw.len = p - crt->subject_raw.p;
668
669 /*
670 * SubjectPublicKeyInfo
671 */
Paul Bakkerda771152013-09-16 22:45:03 +0200672 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673 {
674 x509_crt_free( crt );
675 return( ret );
676 }
677
678 /*
679 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
680 * -- If present, version shall be v2 or v3
681 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
682 * -- If present, version shall be v2 or v3
683 * extensions [3] EXPLICIT Extensions OPTIONAL
684 * -- If present, version shall be v3
685 */
686 if( crt->version == 2 || crt->version == 3 )
687 {
688 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
689 if( ret != 0 )
690 {
691 x509_crt_free( crt );
692 return( ret );
693 }
694 }
695
696 if( crt->version == 2 || crt->version == 3 )
697 {
698 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
699 if( ret != 0 )
700 {
701 x509_crt_free( crt );
702 return( ret );
703 }
704 }
705
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200706#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707 if( crt->version == 3 )
708 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200709#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710 ret = x509_get_crt_ext( &p, end, crt);
711 if( ret != 0 )
712 {
713 x509_crt_free( crt );
714 return( ret );
715 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200716#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200718#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719
720 if( p != end )
721 {
722 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200723 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
725 }
726
727 end = crt_end;
728
729 /*
730 * }
731 * -- end of TBSCertificate
732 *
733 * signatureAlgorithm AlgorithmIdentifier,
734 * signatureValue BIT STRING
735 */
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +0100736 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737 {
738 x509_crt_free( crt );
739 return( ret );
740 }
741
742 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +0100743 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200744 {
745 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200746 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200747 }
748
749 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
750 {
751 x509_crt_free( crt );
752 return( ret );
753 }
754
755 if( p != end )
756 {
757 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200758 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200759 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
760 }
761
762 return( 0 );
763}
764
765/*
766 * Parse one X.509 certificate in DER format from a buffer and add them to a
767 * chained list
768 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200769int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200770 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771{
772 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200773 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774
775 /*
776 * Check for valid input
777 */
778 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200779 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780
781 while( crt->version != 0 && crt->next != NULL )
782 {
783 prev = crt;
784 crt = crt->next;
785 }
786
787 /*
788 * Add new certificate on the end of the chain if needed.
789 */
790 if ( crt->version != 0 && crt->next == NULL)
791 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200792 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793
794 if( crt->next == NULL )
795 return( POLARSSL_ERR_X509_MALLOC_FAILED );
796
797 prev = crt;
798 crt = crt->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200799 x509_crt_init( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800 }
801
Paul Bakkerddf26b42013-09-18 13:46:23 +0200802 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803 {
804 if( prev )
805 prev->next = NULL;
806
807 if( crt != chain )
808 polarssl_free( crt );
809
810 return( ret );
811 }
812
813 return( 0 );
814}
815
816/*
817 * Parse one or more PEM certificates from a buffer and add them to the chained list
818 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200819int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820{
821 int success = 0, first_error = 0, total_failed = 0;
822 int buf_format = X509_FORMAT_DER;
823
824 /*
825 * Check for valid input
826 */
827 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200828 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200829
830 /*
831 * Determine buffer content. Buffer contains either one DER certificate or
832 * one or more PEM certificates.
833 */
834#if defined(POLARSSL_PEM_PARSE_C)
835 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
836 buf_format = X509_FORMAT_PEM;
837#endif
838
839 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200840 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200841
842#if defined(POLARSSL_PEM_PARSE_C)
843 if( buf_format == X509_FORMAT_PEM )
844 {
845 int ret;
846 pem_context pem;
847
848 while( buflen > 0 )
849 {
850 size_t use_len;
851 pem_init( &pem );
852
853 ret = pem_read_buffer( &pem,
854 "-----BEGIN CERTIFICATE-----",
855 "-----END CERTIFICATE-----",
856 buf, NULL, 0, &use_len );
857
858 if( ret == 0 )
859 {
860 /*
861 * Was PEM encoded
862 */
863 buflen -= use_len;
864 buf += use_len;
865 }
866 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
867 {
868 return( ret );
869 }
870 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
871 {
872 pem_free( &pem );
873
874 /*
875 * PEM header and footer were found
876 */
877 buflen -= use_len;
878 buf += use_len;
879
880 if( first_error == 0 )
881 first_error = ret;
882
883 continue;
884 }
885 else
886 break;
887
Paul Bakkerddf26b42013-09-18 13:46:23 +0200888 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889
890 pem_free( &pem );
891
892 if( ret != 0 )
893 {
894 /*
895 * Quit parsing on a memory error
896 */
897 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
898 return( ret );
899
900 if( first_error == 0 )
901 first_error = ret;
902
903 total_failed++;
904 continue;
905 }
906
907 success = 1;
908 }
909 }
910#endif
911
912 if( success )
913 return( total_failed );
914 else if( first_error )
915 return( first_error );
916 else
917 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
918}
919
920#if defined(POLARSSL_FS_IO)
921/*
922 * Load one or more certificates and add them to the chained list
923 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200924int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925{
926 int ret;
927 size_t n;
928 unsigned char *buf;
929
930 if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
931 return( ret );
932
Paul Bakkerddf26b42013-09-18 13:46:23 +0200933 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934
935 memset( buf, 0, n + 1 );
936 polarssl_free( buf );
937
938 return( ret );
939}
940
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100941#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100942static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
943#endif
944
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200945int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946{
947 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100948#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949 int w_ret;
950 WCHAR szDir[MAX_PATH];
951 char filename[MAX_PATH];
952 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200953 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
955 WIN32_FIND_DATAW file_data;
956 HANDLE hFind;
957
958 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200959 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960
961 memset( szDir, 0, sizeof(szDir) );
962 memset( filename, 0, MAX_PATH );
963 memcpy( filename, path, len );
964 filename[len++] = '\\';
965 p = filename + len;
966 filename[len++] = '*';
967
Paul Bakker1a56fc92013-12-19 13:51:24 +0100968 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir, MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969
970 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200971 if (hFind == INVALID_HANDLE_VALUE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
973
974 len = MAX_PATH - len;
975 do
976 {
977 memset( p, 0, len );
978
979 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
980 continue;
981
982 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
983 lstrlenW(file_data.cFileName),
984 p, len - 1,
985 NULL, NULL );
986
Paul Bakkerddf26b42013-09-18 13:46:23 +0200987 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988 if( w_ret < 0 )
989 ret++;
990 else
991 ret += w_ret;
992 }
993 while( FindNextFileW( hFind, &file_data ) != 0 );
994
Paul Bakker4aa40d42013-10-11 10:49:24 +0200995 if (GetLastError() != ERROR_NO_MORE_FILES)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
997
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +0200999#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001000 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001002 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003 char entry_name[255];
1004 DIR *dir = opendir( path );
1005
1006 if( dir == NULL)
1007 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1008
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001009#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001010 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1011 return( ret );
1012#endif
1013
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001014 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001016 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001018 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 {
1020 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001021 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1022 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 }
1024
1025 if( !S_ISREG( sb.st_mode ) )
1026 continue;
1027
1028 // Ignore parse errors
1029 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001030 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 if( t_ret < 0 )
1032 ret++;
1033 else
1034 ret += t_ret;
1035 }
1036 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001037
1038cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001039#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001040 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1041 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1042#endif
1043
Paul Bakkerbe089b02013-10-14 15:51:50 +02001044#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045
1046 return( ret );
1047}
1048#endif /* POLARSSL_FS_IO */
1049
Paul Bakker6edcd412013-10-29 15:22:54 +01001050#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1051 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001052#include <stdarg.h>
1053
1054#if !defined vsnprintf
1055#define vsnprintf _vsnprintf
1056#endif // vsnprintf
1057
1058/*
1059 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1060 * Result value is not size of buffer needed, but -1 if no fit is possible.
1061 *
1062 * This fuction tries to 'fix' this by at least suggesting enlarging the
1063 * size by 20.
1064 */
1065static int compat_snprintf(char *str, size_t size, const char *format, ...)
1066{
1067 va_list ap;
1068 int res = -1;
1069
1070 va_start( ap, format );
1071
1072 res = vsnprintf( str, size, format, ap );
1073
1074 va_end( ap );
1075
1076 // No quick fix possible
1077 if ( res < 0 )
1078 return( (int) size + 20 );
1079
1080 return res;
1081}
1082
1083#define snprintf compat_snprintf
1084#endif
1085
1086#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1087
1088#define SAFE_SNPRINTF() \
1089{ \
1090 if( ret == -1 ) \
1091 return( -1 ); \
1092 \
1093 if ( (unsigned int) ret > n ) { \
1094 p[n - 1] = '\0'; \
1095 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1096 } \
1097 \
1098 n -= (unsigned int) ret; \
1099 p += (unsigned int) ret; \
1100}
1101
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001102static int x509_info_cert_type( char **buf, size_t *size,
1103 unsigned char ns_cert_type )
1104{
1105 int ret;
1106 size_t n = *size;
1107 char *p = *buf;
1108
1109 if( ns_cert_type & NS_CERT_TYPE_SSL_CLIENT )
1110 {
1111 ret = snprintf( p, n, " SSL Client" );
1112 SAFE_SNPRINTF();
1113 }
1114 if( ns_cert_type & NS_CERT_TYPE_SSL_SERVER )
1115 {
1116 ret = snprintf( p, n, " SSL Server" );
1117 SAFE_SNPRINTF();
1118 }
1119 if( ns_cert_type & NS_CERT_TYPE_EMAIL )
1120 {
1121 ret = snprintf( p, n, " Email" );
1122 SAFE_SNPRINTF();
1123 }
1124 if( ns_cert_type & NS_CERT_TYPE_OBJECT_SIGNING )
1125 {
1126 ret = snprintf( p, n, " Object Signing" );
1127 SAFE_SNPRINTF();
1128 }
1129 if( ns_cert_type & NS_CERT_TYPE_RESERVED )
1130 {
1131 ret = snprintf( p, n, " Reserved" );
1132 SAFE_SNPRINTF();
1133 }
1134 if( ns_cert_type & NS_CERT_TYPE_SSL_CA )
1135 {
1136 ret = snprintf( p, n, " SSL CA" );
1137 SAFE_SNPRINTF();
1138 }
1139 if( ns_cert_type & NS_CERT_TYPE_EMAIL_CA )
1140 {
1141 ret = snprintf( p, n, " Email CA" );
1142 SAFE_SNPRINTF();
1143 }
1144 if( ns_cert_type & NS_CERT_TYPE_OBJECT_SIGNING_CA )
1145 {
1146 ret = snprintf( p, n, " Object Signing CA" );
1147 SAFE_SNPRINTF();
1148 }
1149
1150 *size = n;
1151 *buf = p;
1152
1153 return( 0 );
1154}
1155
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156/*
1157 * Return an informational string about the certificate.
1158 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001159#define BEFORE_COLON 18
1160#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001161int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001162 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001163{
1164 int ret;
1165 size_t n;
1166 char *p;
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001167 const char *desc = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168 char key_size_str[BEFORE_COLON];
1169
1170 p = buf;
1171 n = size;
1172
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001173 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 prefix, crt->version );
1175 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001176 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001177 prefix );
1178 SAFE_SNPRINTF();
1179
Paul Bakker86d0c192013-09-18 11:11:02 +02001180 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181 SAFE_SNPRINTF();
1182
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001183 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001184 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001185 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186 SAFE_SNPRINTF();
1187
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001188 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001190 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 SAFE_SNPRINTF();
1192
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001193 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1195 crt->valid_from.year, crt->valid_from.mon,
1196 crt->valid_from.day, crt->valid_from.hour,
1197 crt->valid_from.min, crt->valid_from.sec );
1198 SAFE_SNPRINTF();
1199
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001200 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001201 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1202 crt->valid_to.year, crt->valid_to.mon,
1203 crt->valid_to.day, crt->valid_to.hour,
1204 crt->valid_to.min, crt->valid_to.sec );
1205 SAFE_SNPRINTF();
1206
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001207 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001208 SAFE_SNPRINTF();
1209
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001210 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1211 if( ret != 0 )
1212 ret = snprintf( p, n, "???" );
1213 else
1214 ret = snprintf( p, n, "%s", desc );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001215 SAFE_SNPRINTF();
1216
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001217 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001218 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1219 pk_get_name( &crt->pk ) ) ) != 0 )
1220 {
1221 return( ret );
1222 }
1223
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001224 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001225 (int) pk_get_size( &crt->pk ) );
1226 SAFE_SNPRINTF();
1227
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001228 /*
1229 * Optional extensions
1230 */
1231
1232 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1233 {
1234 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1235 crt->ca_istrue ? "true" : "false" );
1236 SAFE_SNPRINTF();
1237
1238 if( crt->max_pathlen > 0 )
1239 {
1240 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1241 SAFE_SNPRINTF();
1242 }
1243 }
1244
1245 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1246 {
1247 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
1248 SAFE_SNPRINTF();
1249 /* TODO */
1250 }
1251
1252 if( crt->ext_types & EXT_NS_CERT_TYPE )
1253 {
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001254 ret = snprintf( p, n, "\n%scert. type :", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001255 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001256
1257 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1258 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001259 }
1260
1261 if( crt->ext_types & EXT_KEY_USAGE )
1262 {
1263 ret = snprintf( p, n, "\n%skey usage : ", prefix );
1264 SAFE_SNPRINTF();
1265 /* TODO */
1266 }
1267
1268 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1269 {
1270 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
1271 SAFE_SNPRINTF();
1272 /* TODO */
1273 }
1274
1275 ret = snprintf( p, n, "\n" );
1276 SAFE_SNPRINTF();
1277
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001278 return( (int) ( size - n ) );
1279}
1280
1281#if defined(POLARSSL_X509_CRL_PARSE_C)
1282/*
1283 * Return 1 if the certificate is revoked, or 0 otherwise.
1284 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001285int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286{
1287 const x509_crl_entry *cur = &crl->entry;
1288
1289 while( cur != NULL && cur->serial.len != 0 )
1290 {
1291 if( crt->serial.len == cur->serial.len &&
1292 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1293 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001294 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001295 return( 1 );
1296 }
1297
1298 cur = cur->next;
1299 }
1300
1301 return( 0 );
1302}
1303
1304/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001305 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001306 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001307static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001308 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001309{
1310 int flags = 0;
1311 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1312 const md_info_t *md_info;
1313
1314 if( ca == NULL )
1315 return( flags );
1316
1317 /*
1318 * TODO: What happens if no CRL is present?
1319 * Suggestion: Revocation state should be unknown if no CRL is present.
1320 * For backwards compatibility this is not yet implemented.
1321 */
1322
1323 while( crl_list != NULL )
1324 {
1325 if( crl_list->version == 0 ||
1326 crl_list->issuer_raw.len != ca->subject_raw.len ||
1327 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1328 crl_list->issuer_raw.len ) != 0 )
1329 {
1330 crl_list = crl_list->next;
1331 continue;
1332 }
1333
1334 /*
1335 * Check if CRL is correctly signed by the trusted CA
1336 */
1337 md_info = md_info_from_type( crl_list->sig_md );
1338 if( md_info == NULL )
1339 {
1340 /*
1341 * Cannot check 'unknown' hash
1342 */
1343 flags |= BADCRL_NOT_TRUSTED;
1344 break;
1345 }
1346
1347 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1348
1349 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1350 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1351 crl_list->sig.p, crl_list->sig.len ) != 0 )
1352 {
1353 flags |= BADCRL_NOT_TRUSTED;
1354 break;
1355 }
1356
1357 /*
1358 * Check for validity of CRL (Do not drop out)
1359 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001360 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361 flags |= BADCRL_EXPIRED;
1362
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001363 if( x509_time_future( &crl_list->this_update ) )
1364 flags |= BADCRL_FUTURE;
1365
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366 /*
1367 * Check if certificate is revoked
1368 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001369 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370 {
1371 flags |= BADCERT_REVOKED;
1372 break;
1373 }
1374
1375 crl_list = crl_list->next;
1376 }
1377 return flags;
1378}
1379#endif /* POLARSSL_X509_CRL_PARSE_C */
1380
1381// Equal == 0, inequal == 1
1382static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1383{
1384 size_t i;
1385 unsigned char diff;
1386 const unsigned char *n1 = s1, *n2 = s2;
1387
1388 for( i = 0; i < len; i++ )
1389 {
1390 diff = n1[i] ^ n2[i];
1391
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001392 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001393 continue;
1394
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001395 if( diff == 32 &&
1396 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1397 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1398 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001399 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001400 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001401
1402 return( 1 );
1403 }
1404
1405 return( 0 );
1406}
1407
1408static int x509_wildcard_verify( const char *cn, x509_buf *name )
1409{
1410 size_t i;
1411 size_t cn_idx = 0;
1412
1413 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1414 return( 0 );
1415
1416 for( i = 0; i < strlen( cn ); ++i )
1417 {
1418 if( cn[i] == '.' )
1419 {
1420 cn_idx = i;
1421 break;
1422 }
1423 }
1424
1425 if( cn_idx == 0 )
1426 return( 0 );
1427
1428 if( strlen( cn ) - cn_idx == name->len - 1 &&
1429 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1430 {
1431 return( 1 );
1432 }
1433
1434 return( 0 );
1435}
1436
Paul Bakkerddf26b42013-09-18 13:46:23 +02001437static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001438 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001440 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441 void *p_vrfy )
1442{
1443 int ret;
1444 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1445 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1446 const md_info_t *md_info;
1447
Paul Bakker86d0c192013-09-18 11:11:02 +02001448 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449 *flags |= BADCERT_EXPIRED;
1450
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001451 if( x509_time_future( &child->valid_from ) )
1452 *flags |= BADCERT_FUTURE;
1453
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454 /*
1455 * Child is the top of the chain. Check against the trust_ca list.
1456 */
1457 *flags |= BADCERT_NOT_TRUSTED;
1458
1459 md_info = md_info_from_type( child->sig_md );
1460 if( md_info == NULL )
1461 {
1462 /*
1463 * Cannot check 'unknown', no need to try any CA
1464 */
1465 trust_ca = NULL;
1466 }
1467 else
1468 md( md_info, child->tbs.p, child->tbs.len, hash );
1469
1470 while( trust_ca != NULL )
1471 {
1472 if( trust_ca->version == 0 ||
1473 child->issuer_raw.len != trust_ca->subject_raw.len ||
1474 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
1475 child->issuer_raw.len ) != 0 )
1476 {
1477 trust_ca = trust_ca->next;
1478 continue;
1479 }
1480
1481 /*
1482 * Reduce path_len to check against if top of the chain is
1483 * the same as the trusted CA
1484 */
1485 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1486 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1487 child->issuer_raw.len ) == 0 )
1488 {
1489 check_path_cnt--;
1490 }
1491
1492 if( trust_ca->max_pathlen > 0 &&
1493 trust_ca->max_pathlen < check_path_cnt )
1494 {
1495 trust_ca = trust_ca->next;
1496 continue;
1497 }
1498
1499 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1500 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1501 child->sig.p, child->sig.len ) != 0 )
1502 {
1503 trust_ca = trust_ca->next;
1504 continue;
1505 }
1506
1507 /*
1508 * Top of chain is signed by a trusted CA
1509 */
1510 *flags &= ~BADCERT_NOT_TRUSTED;
1511 break;
1512 }
1513
1514 /*
1515 * If top of chain is not the same as the trusted CA send a verify request
1516 * to the callback for any issues with validity and CRL presence for the
1517 * trusted CA certificate.
1518 */
1519 if( trust_ca != NULL &&
1520 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1521 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1522 child->issuer_raw.len ) != 0 ) )
1523 {
1524#if defined(POLARSSL_X509_CRL_PARSE_C)
1525 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001526 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001527#else
1528 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529#endif
1530
Paul Bakker86d0c192013-09-18 11:11:02 +02001531 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532 ca_flags |= BADCERT_EXPIRED;
1533
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001534 if( x509_time_future( &trust_ca->valid_from ) )
1535 ca_flags |= BADCERT_FUTURE;
1536
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001537 if( NULL != f_vrfy )
1538 {
1539 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
1540 return( ret );
1541 }
1542 }
1543
1544 /* Call callback on top cert */
1545 if( NULL != f_vrfy )
1546 {
1547 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1548 return( ret );
1549 }
1550
1551 *flags |= ca_flags;
1552
1553 return( 0 );
1554}
1555
Paul Bakkerddf26b42013-09-18 13:46:23 +02001556static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001557 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001559 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560 void *p_vrfy )
1561{
1562 int ret;
1563 int parent_flags = 0;
1564 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001565 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001566 const md_info_t *md_info;
1567
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001568 if( x509_time_future( &child->valid_from ) )
1569 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570
1571 md_info = md_info_from_type( child->sig_md );
1572 if( md_info == NULL )
1573 {
1574 /*
1575 * Cannot check 'unknown' hash
1576 */
1577 *flags |= BADCERT_NOT_TRUSTED;
1578 }
1579 else
1580 {
1581 md( md_info, child->tbs.p, child->tbs.len, hash );
1582
1583 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1584 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1585 child->sig.p, child->sig.len ) != 0 )
1586 {
1587 *flags |= BADCERT_NOT_TRUSTED;
1588 }
1589 }
1590
1591#if defined(POLARSSL_X509_CRL_PARSE_C)
1592 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001593 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594#endif
1595
1596 grandparent = parent->next;
1597
1598 while( grandparent != NULL )
1599 {
1600 if( grandparent->version == 0 ||
1601 grandparent->ca_istrue == 0 ||
1602 parent->issuer_raw.len != grandparent->subject_raw.len ||
1603 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
1604 parent->issuer_raw.len ) != 0 )
1605 {
1606 grandparent = grandparent->next;
1607 continue;
1608 }
1609 break;
1610 }
1611
1612 if( grandparent != NULL )
1613 {
1614 /*
1615 * Part of the chain
1616 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001617 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 +02001618 if( ret != 0 )
1619 return( ret );
1620 }
1621 else
1622 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001623 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 +02001624 if( ret != 0 )
1625 return( ret );
1626 }
1627
1628 /* child is verified to be a child of the parent, call verify callback */
1629 if( NULL != f_vrfy )
1630 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1631 return( ret );
1632
1633 *flags |= parent_flags;
1634
1635 return( 0 );
1636}
1637
1638/*
1639 * Verify the certificate validity
1640 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001641int x509_crt_verify( x509_crt *crt,
1642 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001643 x509_crl *ca_crl,
1644 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001645 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001646 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001647{
1648 size_t cn_len;
1649 int ret;
1650 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001651 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001652 x509_name *name;
1653 x509_sequence *cur = NULL;
1654
1655 *flags = 0;
1656
1657 if( cn != NULL )
1658 {
1659 name = &crt->subject;
1660 cn_len = strlen( cn );
1661
1662 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1663 {
1664 cur = &crt->subject_alt_names;
1665
1666 while( cur != NULL )
1667 {
1668 if( cur->buf.len == cn_len &&
1669 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1670 break;
1671
1672 if( cur->buf.len > 2 &&
1673 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1674 x509_wildcard_verify( cn, &cur->buf ) )
1675 break;
1676
1677 cur = cur->next;
1678 }
1679
1680 if( cur == NULL )
1681 *flags |= BADCERT_CN_MISMATCH;
1682 }
1683 else
1684 {
1685 while( name != NULL )
1686 {
1687 if( OID_CMP( OID_AT_CN, &name->oid ) )
1688 {
1689 if( name->val.len == cn_len &&
1690 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1691 break;
1692
1693 if( name->val.len > 2 &&
1694 memcmp( name->val.p, "*.", 2 ) == 0 &&
1695 x509_wildcard_verify( cn, &name->val ) )
1696 break;
1697 }
1698
1699 name = name->next;
1700 }
1701
1702 if( name == NULL )
1703 *flags |= BADCERT_CN_MISMATCH;
1704 }
1705 }
1706
1707 /*
1708 * Iterate upwards in the given cert chain, to find our crt parent.
1709 * Ignore any upper cert with CA != TRUE.
1710 */
1711 parent = crt->next;
1712
1713 while( parent != NULL && parent->version != 0 )
1714 {
1715 if( parent->ca_istrue == 0 ||
1716 crt->issuer_raw.len != parent->subject_raw.len ||
1717 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
1718 crt->issuer_raw.len ) != 0 )
1719 {
1720 parent = parent->next;
1721 continue;
1722 }
1723 break;
1724 }
1725
1726 if( parent != NULL )
1727 {
1728 /*
1729 * Part of the chain
1730 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001731 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001732 if( ret != 0 )
1733 return( ret );
1734 }
1735 else
1736 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001737 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001738 if( ret != 0 )
1739 return( ret );
1740 }
1741
1742 if( *flags != 0 )
1743 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1744
1745 return( 0 );
1746}
1747
1748/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001749 * Initialize a certificate chain
1750 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001751void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001752{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001753 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001754}
1755
1756/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001757 * Unallocate all certificate data
1758 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001759void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001760{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001761 x509_crt *cert_cur = crt;
1762 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001763 x509_name *name_cur;
1764 x509_name *name_prv;
1765 x509_sequence *seq_cur;
1766 x509_sequence *seq_prv;
1767
1768 if( crt == NULL )
1769 return;
1770
1771 do
1772 {
1773 pk_free( &cert_cur->pk );
1774
1775 name_cur = cert_cur->issuer.next;
1776 while( name_cur != NULL )
1777 {
1778 name_prv = name_cur;
1779 name_cur = name_cur->next;
1780 memset( name_prv, 0, sizeof( x509_name ) );
1781 polarssl_free( name_prv );
1782 }
1783
1784 name_cur = cert_cur->subject.next;
1785 while( name_cur != NULL )
1786 {
1787 name_prv = name_cur;
1788 name_cur = name_cur->next;
1789 memset( name_prv, 0, sizeof( x509_name ) );
1790 polarssl_free( name_prv );
1791 }
1792
1793 seq_cur = cert_cur->ext_key_usage.next;
1794 while( seq_cur != NULL )
1795 {
1796 seq_prv = seq_cur;
1797 seq_cur = seq_cur->next;
1798 memset( seq_prv, 0, sizeof( x509_sequence ) );
1799 polarssl_free( seq_prv );
1800 }
1801
1802 seq_cur = cert_cur->subject_alt_names.next;
1803 while( seq_cur != NULL )
1804 {
1805 seq_prv = seq_cur;
1806 seq_cur = seq_cur->next;
1807 memset( seq_prv, 0, sizeof( x509_sequence ) );
1808 polarssl_free( seq_prv );
1809 }
1810
1811 if( cert_cur->raw.p != NULL )
1812 {
1813 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1814 polarssl_free( cert_cur->raw.p );
1815 }
1816
1817 cert_cur = cert_cur->next;
1818 }
1819 while( cert_cur != NULL );
1820
1821 cert_cur = crt;
1822 do
1823 {
1824 cert_prv = cert_cur;
1825 cert_cur = cert_cur->next;
1826
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001827 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001828 if( cert_prv != crt )
1829 polarssl_free( cert_prv );
1830 }
1831 while( cert_cur != NULL );
1832}
1833
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001834#endif