blob: 525d250fb9418e7cadd0f33d4948f8341eacfe50 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
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 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020028 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
29 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
30 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031 *
32 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
34 */
35
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#else
39#include POLARSSL_CONFIG_FILE
40#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
42#if defined(POLARSSL_X509_CRT_PARSE_C)
43
44#include "polarssl/x509_crt.h"
45#include "polarssl/oid.h"
46#if defined(POLARSSL_PEM_PARSE_C)
47#include "polarssl/pem.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010057#if defined(POLARSSL_THREADING_C)
58#include "polarssl/threading.h"
59#endif
60
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#include <string.h>
62#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010063#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064#include <windows.h>
65#else
66#include <time.h>
67#endif
68
Paul Bakkerfa6a6202013-10-28 18:48:30 +010069#if defined(EFIX64) || defined(EFI32)
70#include <stdio.h>
71#endif
72
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073#if defined(POLARSSL_FS_IO)
74#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020075#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#include <sys/types.h>
77#include <sys/stat.h>
78#include <dirent.h>
79#endif
80#endif
81
Paul Bakker34617722014-06-13 17:20:13 +020082/* Implementation that should never be optimized out by the compiler */
83static void polarssl_zeroize( void *v, size_t n ) {
84 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
85}
86
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087/*
88 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
89 */
90static int x509_get_version( unsigned char **p,
91 const unsigned char *end,
92 int *ver )
93{
94 int ret;
95 size_t len;
96
97 if( ( ret = asn1_get_tag( p, end, &len,
98 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
99 {
100 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
101 {
102 *ver = 0;
103 return( 0 );
104 }
105
106 return( ret );
107 }
108
109 end = *p + len;
110
111 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200112 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113
114 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200115 return( POLARSSL_ERR_X509_INVALID_VERSION +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
117
118 return( 0 );
119}
120
121/*
122 * Validity ::= SEQUENCE {
123 * notBefore Time,
124 * notAfter Time }
125 */
126static int x509_get_dates( unsigned char **p,
127 const unsigned char *end,
128 x509_time *from,
129 x509_time *to )
130{
131 int ret;
132 size_t len;
133
134 if( ( ret = asn1_get_tag( p, end, &len,
135 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200136 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200137
138 end = *p + len;
139
140 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
141 return( ret );
142
143 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
144 return( ret );
145
146 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200147 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200148 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
149
150 return( 0 );
151}
152
153/*
154 * X.509 v2/v3 unique identifier (not parsed)
155 */
156static int x509_get_uid( unsigned char **p,
157 const unsigned char *end,
158 x509_buf *uid, int n )
159{
160 int ret;
161
162 if( *p == end )
163 return( 0 );
164
165 uid->tag = **p;
166
167 if( ( ret = asn1_get_tag( p, end, &uid->len,
168 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
169 {
170 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
171 return( 0 );
172
173 return( ret );
174 }
175
176 uid->p = *p;
177 *p += uid->len;
178
179 return( 0 );
180}
181
182static int x509_get_basic_constraints( unsigned char **p,
183 const unsigned char *end,
184 int *ca_istrue,
185 int *max_pathlen )
186{
187 int ret;
188 size_t len;
189
190 /*
191 * BasicConstraints ::= SEQUENCE {
192 * cA BOOLEAN DEFAULT FALSE,
193 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
194 */
195 *ca_istrue = 0; /* DEFAULT FALSE */
196 *max_pathlen = 0; /* endless */
197
198 if( ( ret = asn1_get_tag( p, end, &len,
199 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200200 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200201
202 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200203 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200204
205 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
206 {
207 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
208 ret = asn1_get_int( p, end, ca_istrue );
209
210 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200211 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200212
213 if( *ca_istrue != 0 )
214 *ca_istrue = 1;
215 }
216
217 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200218 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219
220 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200221 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200222
223 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200224 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200225 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
226
227 (*max_pathlen)++;
228
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200229 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200230}
231
232static int x509_get_ns_cert_type( unsigned char **p,
233 const unsigned char *end,
234 unsigned char *ns_cert_type)
235{
236 int ret;
237 x509_bitstring bs = { 0, 0, NULL };
238
239 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200240 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200241
242 if( bs.len != 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200243 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200244 POLARSSL_ERR_ASN1_INVALID_LENGTH );
245
246 /* Get actual bitstring */
247 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200248 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249}
250
251static int x509_get_key_usage( unsigned char **p,
252 const unsigned char *end,
253 unsigned char *key_usage)
254{
255 int ret;
256 x509_bitstring bs = { 0, 0, NULL };
257
258 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200259 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260
261 if( bs.len < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200262 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200263 POLARSSL_ERR_ASN1_INVALID_LENGTH );
264
265 /* Get actual bitstring */
266 *key_usage = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200267 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268}
269
270/*
271 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
272 *
273 * KeyPurposeId ::= OBJECT IDENTIFIER
274 */
275static int x509_get_ext_key_usage( unsigned char **p,
276 const unsigned char *end,
277 x509_sequence *ext_key_usage)
278{
279 int ret;
280
281 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200282 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283
284 /* Sequence length must be >= 1 */
285 if( ext_key_usage->buf.p == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200286 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287 POLARSSL_ERR_ASN1_INVALID_LENGTH );
288
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200289 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290}
291
292/*
293 * SubjectAltName ::= GeneralNames
294 *
295 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
296 *
297 * GeneralName ::= CHOICE {
298 * otherName [0] OtherName,
299 * rfc822Name [1] IA5String,
300 * dNSName [2] IA5String,
301 * x400Address [3] ORAddress,
302 * directoryName [4] Name,
303 * ediPartyName [5] EDIPartyName,
304 * uniformResourceIdentifier [6] IA5String,
305 * iPAddress [7] OCTET STRING,
306 * registeredID [8] OBJECT IDENTIFIER }
307 *
308 * OtherName ::= SEQUENCE {
309 * type-id OBJECT IDENTIFIER,
310 * value [0] EXPLICIT ANY DEFINED BY type-id }
311 *
312 * EDIPartyName ::= SEQUENCE {
313 * nameAssigner [0] DirectoryString OPTIONAL,
314 * partyName [1] DirectoryString }
315 *
316 * NOTE: PolarSSL only parses and uses dNSName at this point.
317 */
318static int x509_get_subject_alt_name( unsigned char **p,
319 const unsigned char *end,
320 x509_sequence *subject_alt_name )
321{
322 int ret;
323 size_t len, tag_len;
324 asn1_buf *buf;
325 unsigned char tag;
326 asn1_sequence *cur = subject_alt_name;
327
328 /* Get main sequence tag */
329 if( ( ret = asn1_get_tag( p, end, &len,
330 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200331 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200332
333 if( *p + len != end )
Paul Bakker51876562013-09-17 14:36:05 +0200334 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
336
337 while( *p < end )
338 {
339 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200340 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341 POLARSSL_ERR_ASN1_OUT_OF_DATA );
342
343 tag = **p;
344 (*p)++;
345 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200346 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347
348 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
Paul Bakker51876562013-09-17 14:36:05 +0200349 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200350 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
351
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200352 /* Skip everything but DNS name */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
354 {
355 *p += tag_len;
356 continue;
357 }
358
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200360 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100362 if( cur->next != NULL )
363 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS );
364
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365 cur->next = (asn1_sequence *) polarssl_malloc(
366 sizeof( asn1_sequence ) );
367
368 if( cur->next == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200369 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370 POLARSSL_ERR_ASN1_MALLOC_FAILED );
371
372 memset( cur->next, 0, sizeof( asn1_sequence ) );
373 cur = cur->next;
374 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200375
376 buf = &(cur->buf);
377 buf->tag = tag;
378 buf->p = *p;
379 buf->len = tag_len;
380 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381 }
382
383 /* Set final sequence entry's next pointer to NULL */
384 cur->next = NULL;
385
386 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200387 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
389
390 return( 0 );
391}
392
393/*
394 * X.509 v3 extensions
395 *
396 * TODO: Perform all of the basic constraints tests required by the RFC
397 * TODO: Set values for undetected extensions to a sane default?
398 *
399 */
400static int x509_get_crt_ext( unsigned char **p,
401 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200402 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403{
404 int ret;
405 size_t len;
406 unsigned char *end_ext_data, *end_ext_octet;
407
408 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
409 {
410 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
411 return( 0 );
412
413 return( ret );
414 }
415
416 while( *p < end )
417 {
418 /*
419 * Extension ::= SEQUENCE {
420 * extnID OBJECT IDENTIFIER,
421 * critical BOOLEAN DEFAULT FALSE,
422 * extnValue OCTET STRING }
423 */
424 x509_buf extn_oid = {0, 0, NULL};
425 int is_critical = 0; /* DEFAULT FALSE */
426 int ext_type = 0;
427
428 if( ( ret = asn1_get_tag( p, end, &len,
429 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200430 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431
432 end_ext_data = *p + len;
433
434 /* Get extension ID */
435 extn_oid.tag = **p;
436
437 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200438 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439
440 extn_oid.p = *p;
441 *p += extn_oid.len;
442
443 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200444 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445 POLARSSL_ERR_ASN1_OUT_OF_DATA );
446
447 /* Get optional critical */
448 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
449 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200450 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
452 /* Data should be octet string type */
453 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
454 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200455 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456
457 end_ext_octet = *p + len;
458
459 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200460 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
462
463 /*
464 * Detect supported extensions
465 */
466 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
467
468 if( ret != 0 )
469 {
470 /* No parser found, skip extension */
471 *p = end_ext_octet;
472
473#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
474 if( is_critical )
475 {
476 /* Data is marked as critical: fail */
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200477 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
479 }
480#endif
481 continue;
482 }
483
484 crt->ext_types |= ext_type;
485
486 switch( ext_type )
487 {
488 case EXT_BASIC_CONSTRAINTS:
489 /* Parse basic constraints */
490 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
491 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200492 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 break;
494
495 case EXT_KEY_USAGE:
496 /* Parse key usage */
497 if( ( ret = x509_get_key_usage( p, end_ext_octet,
498 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200499 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500 break;
501
502 case EXT_EXTENDED_KEY_USAGE:
503 /* Parse extended key usage */
504 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
505 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200506 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 break;
508
509 case EXT_SUBJECT_ALT_NAME:
510 /* Parse subject alt name */
511 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
512 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200513 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514 break;
515
516 case EXT_NS_CERT_TYPE:
517 /* Parse netscape certificate type */
518 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
519 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200520 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521 break;
522
523 default:
524 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
525 }
526 }
527
528 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200529 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
531
532 return( 0 );
533}
534
535/*
536 * Parse and fill a single X.509 certificate in DER format
537 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200538static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200539 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540{
541 int ret;
542 size_t len;
543 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200544 x509_buf sig_params1, sig_params2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100545
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200546 memset( &sig_params1, 0, sizeof( x509_buf ) );
547 memset( &sig_params2, 0, sizeof( x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548
549 /*
550 * Check for valid input
551 */
552 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200553 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554
555 p = (unsigned char *) polarssl_malloc( len = buflen );
556
557 if( p == NULL )
558 return( POLARSSL_ERR_X509_MALLOC_FAILED );
559
560 memcpy( p, buf, buflen );
561
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562 crt->raw.p = p;
563 crt->raw.len = len;
564 end = p + len;
565
566 /*
567 * Certificate ::= SEQUENCE {
568 * tbsCertificate TBSCertificate,
569 * signatureAlgorithm AlgorithmIdentifier,
570 * signatureValue BIT STRING }
571 */
572 if( ( ret = asn1_get_tag( &p, end, &len,
573 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
574 {
575 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200576 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577 }
578
579 if( len > (size_t) ( end - p ) )
580 {
581 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200582 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
584 }
585 crt_end = p + len;
586
587 /*
588 * TBSCertificate ::= SEQUENCE {
589 */
590 crt->tbs.p = p;
591
592 if( ( ret = asn1_get_tag( &p, end, &len,
593 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
594 {
595 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200596 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597 }
598
599 end = p + len;
600 crt->tbs.len = end - crt->tbs.p;
601
602 /*
603 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
604 *
605 * CertificateSerialNumber ::= INTEGER
606 *
607 * signature AlgorithmIdentifier
608 */
609 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
610 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100611 ( ret = x509_get_alg( &p, end, &crt->sig_oid1,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200612 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200613 {
614 x509_crt_free( crt );
615 return( ret );
616 }
617
618 crt->version++;
619
620 if( crt->version > 3 )
621 {
622 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200623 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624 }
625
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200626 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200627 &crt->sig_md, &crt->sig_pk,
628 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629 {
630 x509_crt_free( crt );
631 return( ret );
632 }
633
634 /*
635 * issuer Name
636 */
637 crt->issuer_raw.p = p;
638
639 if( ( ret = asn1_get_tag( &p, end, &len,
640 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
641 {
642 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200643 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644 }
645
646 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
647 {
648 x509_crt_free( crt );
649 return( ret );
650 }
651
652 crt->issuer_raw.len = p - crt->issuer_raw.p;
653
654 /*
655 * Validity ::= SEQUENCE {
656 * notBefore Time,
657 * notAfter Time }
658 *
659 */
660 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
661 &crt->valid_to ) ) != 0 )
662 {
663 x509_crt_free( crt );
664 return( ret );
665 }
666
667 /*
668 * subject Name
669 */
670 crt->subject_raw.p = p;
671
672 if( ( ret = asn1_get_tag( &p, end, &len,
673 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
674 {
675 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200676 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 }
678
679 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
680 {
681 x509_crt_free( crt );
682 return( ret );
683 }
684
685 crt->subject_raw.len = p - crt->subject_raw.p;
686
687 /*
688 * SubjectPublicKeyInfo
689 */
Paul Bakkerda771152013-09-16 22:45:03 +0200690 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 {
692 x509_crt_free( crt );
693 return( ret );
694 }
695
696 /*
697 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
698 * -- If present, version shall be v2 or v3
699 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
700 * -- If present, version shall be v2 or v3
701 * extensions [3] EXPLICIT Extensions OPTIONAL
702 * -- If present, version shall be v3
703 */
704 if( crt->version == 2 || crt->version == 3 )
705 {
706 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
707 if( ret != 0 )
708 {
709 x509_crt_free( crt );
710 return( ret );
711 }
712 }
713
714 if( crt->version == 2 || crt->version == 3 )
715 {
716 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
717 if( ret != 0 )
718 {
719 x509_crt_free( crt );
720 return( ret );
721 }
722 }
723
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200724#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725 if( crt->version == 3 )
726 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200727#endif
Paul Bakker66d5d072014-06-17 16:39:18 +0200728 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200729 if( ret != 0 )
730 {
731 x509_crt_free( crt );
732 return( ret );
733 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200734#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200736#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737
738 if( p != end )
739 {
740 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200741 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200742 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
743 }
744
745 end = crt_end;
746
747 /*
748 * }
749 * -- end of TBSCertificate
750 *
751 * signatureAlgorithm AlgorithmIdentifier,
752 * signatureValue BIT STRING
753 */
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200754 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 {
756 x509_crt_free( crt );
757 return( ret );
758 }
759
760 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200761 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 ||
762 sig_params1.len != sig_params2.len ||
Paul Bakker66d5d072014-06-17 16:39:18 +0200763 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764 {
765 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200766 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200767 }
768
769 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
770 {
771 x509_crt_free( crt );
772 return( ret );
773 }
774
775 if( p != end )
776 {
777 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200778 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
780 }
781
782 return( 0 );
783}
784
785/*
786 * Parse one X.509 certificate in DER format from a buffer and add them to a
787 * chained list
788 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200789int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200790 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791{
792 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200793 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794
795 /*
796 * Check for valid input
797 */
798 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200799 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800
801 while( crt->version != 0 && crt->next != NULL )
802 {
803 prev = crt;
804 crt = crt->next;
805 }
806
807 /*
808 * Add new certificate on the end of the chain if needed.
809 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200810 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200812 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813
814 if( crt->next == NULL )
815 return( POLARSSL_ERR_X509_MALLOC_FAILED );
816
817 prev = crt;
818 crt = crt->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200819 x509_crt_init( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820 }
821
Paul Bakkerddf26b42013-09-18 13:46:23 +0200822 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823 {
824 if( prev )
825 prev->next = NULL;
826
827 if( crt != chain )
828 polarssl_free( crt );
829
830 return( ret );
831 }
832
833 return( 0 );
834}
835
836/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200837 * Parse one or more PEM certificates from a buffer and add them to the chained
838 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200840int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200841{
842 int success = 0, first_error = 0, total_failed = 0;
843 int buf_format = X509_FORMAT_DER;
844
845 /*
846 * Check for valid input
847 */
848 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200849 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200850
851 /*
852 * Determine buffer content. Buffer contains either one DER certificate or
853 * one or more PEM certificates.
854 */
855#if defined(POLARSSL_PEM_PARSE_C)
856 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
857 buf_format = X509_FORMAT_PEM;
858#endif
859
860 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200861 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200862
863#if defined(POLARSSL_PEM_PARSE_C)
864 if( buf_format == X509_FORMAT_PEM )
865 {
866 int ret;
867 pem_context pem;
868
869 while( buflen > 0 )
870 {
871 size_t use_len;
872 pem_init( &pem );
873
874 ret = pem_read_buffer( &pem,
875 "-----BEGIN CERTIFICATE-----",
876 "-----END CERTIFICATE-----",
877 buf, NULL, 0, &use_len );
878
879 if( ret == 0 )
880 {
881 /*
882 * Was PEM encoded
883 */
884 buflen -= use_len;
885 buf += use_len;
886 }
887 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
888 {
889 return( ret );
890 }
891 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
892 {
893 pem_free( &pem );
894
895 /*
896 * PEM header and footer were found
897 */
898 buflen -= use_len;
899 buf += use_len;
900
901 if( first_error == 0 )
902 first_error = ret;
903
Paul Bakker5a5fa922014-09-26 14:53:04 +0200904 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 continue;
906 }
907 else
908 break;
909
Paul Bakkerddf26b42013-09-18 13:46:23 +0200910 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911
912 pem_free( &pem );
913
914 if( ret != 0 )
915 {
916 /*
917 * Quit parsing on a memory error
918 */
919 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
920 return( ret );
921
922 if( first_error == 0 )
923 first_error = ret;
924
925 total_failed++;
926 continue;
927 }
928
929 success = 1;
930 }
931 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200932#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
934 if( success )
935 return( total_failed );
936 else if( first_error )
937 return( first_error );
938 else
939 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
940}
941
942#if defined(POLARSSL_FS_IO)
943/*
944 * Load one or more certificates and add them to the chained list
945 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200946int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947{
948 int ret;
949 size_t n;
950 unsigned char *buf;
951
Paul Bakker66d5d072014-06-17 16:39:18 +0200952 if( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953 return( ret );
954
Paul Bakkerddf26b42013-09-18 13:46:23 +0200955 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956
Paul Bakker34617722014-06-13 17:20:13 +0200957 polarssl_zeroize( buf, n + 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958 polarssl_free( buf );
959
960 return( ret );
961}
962
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100963#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100964static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
965#endif
966
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200967int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968{
969 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100970#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971 int w_ret;
972 WCHAR szDir[MAX_PATH];
973 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200974 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200975 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976
Paul Bakker9af723c2014-05-01 13:03:14 +0200977 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978 HANDLE hFind;
979
980 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200981 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982
Paul Bakker9af723c2014-05-01 13:03:14 +0200983 memset( szDir, 0, sizeof(szDir) );
984 memset( filename, 0, MAX_PATH );
985 memcpy( filename, path, len );
986 filename[len++] = '\\';
987 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988 filename[len++] = '*';
989
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200990 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
991 MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992
993 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +0200994 if( hFind == INVALID_HANDLE_VALUE )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
996
997 len = MAX_PATH - len;
998 do
999 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001000 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001
1002 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1003 continue;
1004
Paul Bakker9af723c2014-05-01 13:03:14 +02001005 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001006 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001007 p, len - 1,
1008 NULL, NULL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009
Paul Bakkerddf26b42013-09-18 13:46:23 +02001010 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011 if( w_ret < 0 )
1012 ret++;
1013 else
1014 ret += w_ret;
1015 }
1016 while( FindNextFileW( hFind, &file_data ) != 0 );
1017
Paul Bakker66d5d072014-06-17 16:39:18 +02001018 if( GetLastError() != ERROR_NO_MORE_FILES )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1020
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001022#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001023 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001025 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 char entry_name[255];
1027 DIR *dir = opendir( path );
1028
Paul Bakker66d5d072014-06-17 16:39:18 +02001029 if( dir == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1031
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001032#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001033 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1034 return( ret );
1035#endif
1036
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001037 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001039 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001041 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042 {
1043 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001044 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1045 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046 }
1047
1048 if( !S_ISREG( sb.st_mode ) )
1049 continue;
1050
1051 // Ignore parse errors
1052 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001053 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001054 if( t_ret < 0 )
1055 ret++;
1056 else
1057 ret += t_ret;
1058 }
1059 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001060
1061cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001062#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001063 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1064 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1065#endif
1066
Paul Bakkerbe089b02013-10-14 15:51:50 +02001067#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068
1069 return( ret );
1070}
1071#endif /* POLARSSL_FS_IO */
1072
Paul Bakker6edcd412013-10-29 15:22:54 +01001073#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1074 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075#include <stdarg.h>
1076
1077#if !defined vsnprintf
1078#define vsnprintf _vsnprintf
1079#endif // vsnprintf
1080
1081/*
1082 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1083 * Result value is not size of buffer needed, but -1 if no fit is possible.
1084 *
1085 * This fuction tries to 'fix' this by at least suggesting enlarging the
1086 * size by 20.
1087 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001088static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089{
1090 va_list ap;
1091 int res = -1;
1092
1093 va_start( ap, format );
1094
1095 res = vsnprintf( str, size, format, ap );
1096
1097 va_end( ap );
1098
1099 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +02001100 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101 return( (int) size + 20 );
1102
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001103 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104}
1105
1106#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001107#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108
1109#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1110
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001111#define SAFE_SNPRINTF() \
1112{ \
1113 if( ret == -1 ) \
1114 return( -1 ); \
1115 \
Paul Bakker66d5d072014-06-17 16:39:18 +02001116 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001117 p[n - 1] = '\0'; \
1118 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
1119 } \
1120 \
1121 n -= (unsigned int) ret; \
1122 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123}
1124
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001125static int x509_info_subject_alt_name( char **buf, size_t *size,
1126 const x509_sequence *subject_alt_name )
1127{
1128 size_t i;
1129 size_t n = *size;
1130 char *p = *buf;
1131 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001132 const char *sep = "";
1133 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001134
1135 while( cur != NULL )
1136 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001137 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001138 {
1139 *p = '\0';
1140 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1141 }
1142
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001143 n -= cur->buf.len + sep_len;
1144 for( i = 0; i < sep_len; i++ )
1145 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001146 for( i = 0; i < cur->buf.len; i++ )
1147 *p++ = cur->buf.p[i];
1148
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001149 sep = ", ";
1150 sep_len = 2;
1151
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001152 cur = cur->next;
1153 }
1154
1155 *p = '\0';
1156
1157 *size = n;
1158 *buf = p;
1159
1160 return( 0 );
1161}
1162
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001163#define PRINT_ITEM(i) \
1164 { \
1165 ret = snprintf( p, n, "%s" i, sep ); \
1166 SAFE_SNPRINTF(); \
1167 sep = ", "; \
1168 }
1169
1170#define CERT_TYPE(type,name) \
1171 if( ns_cert_type & type ) \
1172 PRINT_ITEM( name );
1173
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001174static int x509_info_cert_type( char **buf, size_t *size,
1175 unsigned char ns_cert_type )
1176{
1177 int ret;
1178 size_t n = *size;
1179 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001180 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001181
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001182 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1183 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1184 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1185 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1186 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1187 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1188 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1189 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001190
1191 *size = n;
1192 *buf = p;
1193
1194 return( 0 );
1195}
1196
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001197#define KEY_USAGE(code,name) \
1198 if( key_usage & code ) \
1199 PRINT_ITEM( name );
1200
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001201static int x509_info_key_usage( char **buf, size_t *size,
1202 unsigned char key_usage )
1203{
1204 int ret;
1205 size_t n = *size;
1206 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001207 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001208
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001209 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1210 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1211 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1212 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1213 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1214 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1215 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001216
1217 *size = n;
1218 *buf = p;
1219
1220 return( 0 );
1221}
1222
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001223static int x509_info_ext_key_usage( char **buf, size_t *size,
1224 const x509_sequence *extended_key_usage )
1225{
1226 int ret;
1227 const char *desc;
1228 size_t n = *size;
1229 char *p = *buf;
1230 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001231 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001232
1233 while( cur != NULL )
1234 {
1235 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1236 desc = "???";
1237
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001238 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001239 SAFE_SNPRINTF();
1240
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001241 sep = ", ";
1242
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001243 cur = cur->next;
1244 }
1245
1246 *size = n;
1247 *buf = p;
1248
1249 return( 0 );
1250}
1251
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001252/*
1253 * Return an informational string about the certificate.
1254 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001255#define BEFORE_COLON 18
1256#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001257int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001258 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001259{
1260 int ret;
1261 size_t n;
1262 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001263 char key_size_str[BEFORE_COLON];
1264
1265 p = buf;
1266 n = size;
1267
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001268 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001269 prefix, crt->version );
1270 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001271 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001272 prefix );
1273 SAFE_SNPRINTF();
1274
Paul Bakker66d5d072014-06-17 16:39:18 +02001275 ret = x509_serial_gets( p, n, &crt->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 SAFE_SNPRINTF();
1277
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001278 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001279 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001280 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 SAFE_SNPRINTF();
1282
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001283 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001284 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001285 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286 SAFE_SNPRINTF();
1287
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001288 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001289 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1290 crt->valid_from.year, crt->valid_from.mon,
1291 crt->valid_from.day, crt->valid_from.hour,
1292 crt->valid_from.min, crt->valid_from.sec );
1293 SAFE_SNPRINTF();
1294
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001295 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001296 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1297 crt->valid_to.year, crt->valid_to.mon,
1298 crt->valid_to.day, crt->valid_to.hour,
1299 crt->valid_to.min, crt->valid_to.sec );
1300 SAFE_SNPRINTF();
1301
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001302 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001303 SAFE_SNPRINTF();
1304
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001305 ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001306 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 SAFE_SNPRINTF();
1308
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001309 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001310 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1311 pk_get_name( &crt->pk ) ) ) != 0 )
1312 {
1313 return( ret );
1314 }
1315
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001316 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317 (int) pk_get_size( &crt->pk ) );
1318 SAFE_SNPRINTF();
1319
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001320 /*
1321 * Optional extensions
1322 */
1323
1324 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1325 {
1326 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1327 crt->ca_istrue ? "true" : "false" );
1328 SAFE_SNPRINTF();
1329
1330 if( crt->max_pathlen > 0 )
1331 {
1332 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1333 SAFE_SNPRINTF();
1334 }
1335 }
1336
1337 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1338 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001339 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001340 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001341
1342 if( ( ret = x509_info_subject_alt_name( &p, &n,
1343 &crt->subject_alt_names ) ) != 0 )
1344 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001345 }
1346
1347 if( crt->ext_types & EXT_NS_CERT_TYPE )
1348 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001349 ret = snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001350 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001351
1352 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1353 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001354 }
1355
1356 if( crt->ext_types & EXT_KEY_USAGE )
1357 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001358 ret = snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001359 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001360
1361 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1362 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001363 }
1364
1365 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1366 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001367 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001368 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001369
1370 if( ( ret = x509_info_ext_key_usage( &p, &n,
1371 &crt->ext_key_usage ) ) != 0 )
1372 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001373 }
1374
1375 ret = snprintf( p, n, "\n" );
1376 SAFE_SNPRINTF();
1377
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378 return( (int) ( size - n ) );
1379}
1380
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001381#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1382int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1383{
1384 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1385 ( crt->key_usage & usage ) != usage )
1386 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1387
1388 return( 0 );
1389}
1390#endif
1391
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001392#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1393int x509_crt_check_extended_key_usage( const x509_crt *crt,
1394 const char *usage_oid,
1395 size_t usage_len )
1396{
1397 const x509_sequence *cur;
1398
1399 /* Extension is not mandatory, absent means no restriction */
1400 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1401 return( 0 );
1402
1403 /*
1404 * Look for the requested usage (or wildcard ANY) in our list
1405 */
1406 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1407 {
1408 const x509_buf *cur_oid = &cur->buf;
1409
1410 if( cur_oid->len == usage_len &&
1411 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1412 {
1413 return( 0 );
1414 }
1415
1416 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1417 return( 0 );
1418 }
1419
1420 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1421}
Paul Bakker9af723c2014-05-01 13:03:14 +02001422#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001423
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001424#if defined(POLARSSL_X509_CRL_PARSE_C)
1425/*
1426 * Return 1 if the certificate is revoked, or 0 otherwise.
1427 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001428int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429{
1430 const x509_crl_entry *cur = &crl->entry;
1431
1432 while( cur != NULL && cur->serial.len != 0 )
1433 {
1434 if( crt->serial.len == cur->serial.len &&
1435 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1436 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001437 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001438 return( 1 );
1439 }
1440
1441 cur = cur->next;
1442 }
1443
1444 return( 0 );
1445}
1446
1447/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001448 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001450static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001451 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452{
1453 int flags = 0;
1454 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1455 const md_info_t *md_info;
1456
1457 if( ca == NULL )
1458 return( flags );
1459
1460 /*
1461 * TODO: What happens if no CRL is present?
1462 * Suggestion: Revocation state should be unknown if no CRL is present.
1463 * For backwards compatibility this is not yet implemented.
1464 */
1465
1466 while( crl_list != NULL )
1467 {
1468 if( crl_list->version == 0 ||
1469 crl_list->issuer_raw.len != ca->subject_raw.len ||
1470 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1471 crl_list->issuer_raw.len ) != 0 )
1472 {
1473 crl_list = crl_list->next;
1474 continue;
1475 }
1476
1477 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001478 * Check if the CA is configured to sign CRLs
1479 */
1480#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1481 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1482 {
1483 flags |= BADCRL_NOT_TRUSTED;
1484 break;
1485 }
1486#endif
1487
1488 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001489 * Check if CRL is correctly signed by the trusted CA
1490 */
1491 md_info = md_info_from_type( crl_list->sig_md );
1492 if( md_info == NULL )
1493 {
1494 /*
1495 * Cannot check 'unknown' hash
1496 */
1497 flags |= BADCRL_NOT_TRUSTED;
1498 break;
1499 }
1500
1501 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1502
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001503 if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1504 crl_list->sig_md, hash, md_info->size,
1505 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506 {
1507 flags |= BADCRL_NOT_TRUSTED;
1508 break;
1509 }
1510
1511 /*
1512 * Check for validity of CRL (Do not drop out)
1513 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001514 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515 flags |= BADCRL_EXPIRED;
1516
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001517 if( x509_time_future( &crl_list->this_update ) )
1518 flags |= BADCRL_FUTURE;
1519
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520 /*
1521 * Check if certificate is revoked
1522 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001523 if( x509_crt_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524 {
1525 flags |= BADCERT_REVOKED;
1526 break;
1527 }
1528
1529 crl_list = crl_list->next;
1530 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001531 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532}
1533#endif /* POLARSSL_X509_CRL_PARSE_C */
1534
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001535/*
1536 * Like memcmp, but case-insensitive and always returns -1 if different
1537 */
1538static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539{
1540 size_t i;
1541 unsigned char diff;
1542 const unsigned char *n1 = s1, *n2 = s2;
1543
1544 for( i = 0; i < len; i++ )
1545 {
1546 diff = n1[i] ^ n2[i];
1547
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001548 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549 continue;
1550
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001551 if( diff == 32 &&
1552 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1553 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1554 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001556 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001558 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559 }
1560
1561 return( 0 );
1562}
1563
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001564/*
1565 * Return 1 if match, 0 if not
1566 * TODO: inverted return value!
1567 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001568static int x509_wildcard_verify( const char *cn, x509_buf *name )
1569{
1570 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001571 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001572
1573 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1574 return( 0 );
1575
Paul Bakker14b16c62014-05-28 11:33:54 +02001576 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577 {
1578 if( cn[i] == '.' )
1579 {
1580 cn_idx = i;
1581 break;
1582 }
1583 }
1584
1585 if( cn_idx == 0 )
1586 return( 0 );
1587
Paul Bakker14b16c62014-05-28 11:33:54 +02001588 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001589 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590 {
1591 return( 1 );
1592 }
1593
1594 return( 0 );
1595}
1596
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001597/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001598 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1599 * variations (but not all).
1600 *
1601 * Return 0 if equal, -1 otherwise.
1602 */
1603static int x509_string_cmp( const x509_buf *a, const x509_buf *b )
1604{
1605 if( a->tag == b->tag &&
1606 a->len == b->len &&
1607 memcmp( a->p, b->p, b->len ) == 0 )
1608 {
1609 return( 0 );
1610 }
1611
1612 if( ( a->tag == ASN1_UTF8_STRING || a->tag == ASN1_PRINTABLE_STRING ) &&
1613 ( b->tag == ASN1_UTF8_STRING || b->tag == ASN1_PRINTABLE_STRING ) &&
1614 a->len == b->len &&
1615 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1616 {
1617 return( 0 );
1618 }
1619
1620 return( -1 );
1621}
1622
1623/*
1624 * Compare two X.509 Names (aka rdnSequence).
1625 *
1626 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1627 * we sometimes return unequal when the full algorithm would return equal,
1628 * but never the other way. (In particular, we don't do Unicode normalisation
1629 * or space folding.)
1630 *
1631 * Return 0 if equal, -1 otherwise.
1632 */
1633static int x509_name_cmp( const x509_name *a, const x509_name *b )
1634{
1635 if( a == NULL && b == NULL )
1636 return( 0 );
1637
1638 if( a == NULL || b == NULL )
1639 return( -1 );
1640
1641 /* type */
1642 if( a->oid.tag != b->oid.tag ||
1643 a->oid.len != b->oid.len ||
1644 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1645 {
1646 return( -1 );
1647 }
1648
1649 /* value */
1650 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1651 return( -1 );
1652
1653 return( x509_name_cmp( a->next, b->next ) );
1654}
1655
1656/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001657 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1658 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001659 *
1660 * top means parent is a locally-trusted certificate
1661 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001662 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001663static int x509_crt_check_parent( const x509_crt *child,
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001664 const x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001665 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001666{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001667 int need_ca_bit;
1668
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001669 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001670 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001671 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001672
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001673 /* Parent must have the basicConstraints CA bit set as a general rule */
1674 need_ca_bit = 1;
1675
1676 /* Exception: v1/v2 certificates that are locally trusted. */
1677 if( top && parent->version < 3 )
1678 need_ca_bit = 0;
1679
1680 /* Exception: self-signed end-entity certs that are locally trusted. */
1681 if( top && bottom &&
1682 child->raw.len == parent->raw.len &&
1683 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1684 {
1685 need_ca_bit = 0;
1686 }
1687
1688 if( need_ca_bit && ! parent->ca_istrue )
1689 return( -1 );
1690
1691#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1692 if( need_ca_bit &&
1693 x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001694 {
1695 return( -1 );
1696 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001697#endif
1698
1699 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001700}
1701
Paul Bakkerddf26b42013-09-18 13:46:23 +02001702static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001703 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001704 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001705 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001706 void *p_vrfy )
1707{
1708 int ret;
1709 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1710 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1711 const md_info_t *md_info;
1712
Paul Bakker86d0c192013-09-18 11:11:02 +02001713 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001714 *flags |= BADCERT_EXPIRED;
1715
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001716 if( x509_time_future( &child->valid_from ) )
1717 *flags |= BADCERT_FUTURE;
1718
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001719 /*
1720 * Child is the top of the chain. Check against the trust_ca list.
1721 */
1722 *flags |= BADCERT_NOT_TRUSTED;
1723
1724 md_info = md_info_from_type( child->sig_md );
1725 if( md_info == NULL )
1726 {
1727 /*
1728 * Cannot check 'unknown', no need to try any CA
1729 */
1730 trust_ca = NULL;
1731 }
1732 else
1733 md( md_info, child->tbs.p, child->tbs.len, hash );
1734
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001735 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001737 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001738 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001739
1740 /*
1741 * Reduce path_len to check against if top of the chain is
1742 * the same as the trusted CA
1743 */
1744 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1745 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1746 child->issuer_raw.len ) == 0 )
1747 {
1748 check_path_cnt--;
1749 }
1750
1751 if( trust_ca->max_pathlen > 0 &&
1752 trust_ca->max_pathlen < check_path_cnt )
1753 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001754 continue;
1755 }
1756
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001757 if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1758 child->sig_md, hash, md_info->size,
1759 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001760 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001761 continue;
1762 }
1763
1764 /*
1765 * Top of chain is signed by a trusted CA
1766 */
1767 *flags &= ~BADCERT_NOT_TRUSTED;
1768 break;
1769 }
1770
1771 /*
1772 * If top of chain is not the same as the trusted CA send a verify request
1773 * to the callback for any issues with validity and CRL presence for the
1774 * trusted CA certificate.
1775 */
1776 if( trust_ca != NULL &&
1777 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1778 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1779 child->issuer_raw.len ) != 0 ) )
1780 {
1781#if defined(POLARSSL_X509_CRL_PARSE_C)
1782 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001783 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001784#else
1785 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001786#endif
1787
Paul Bakker86d0c192013-09-18 11:11:02 +02001788 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001789 ca_flags |= BADCERT_EXPIRED;
1790
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001791 if( x509_time_future( &trust_ca->valid_from ) )
1792 ca_flags |= BADCERT_FUTURE;
1793
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001794 if( NULL != f_vrfy )
1795 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001796 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1797 &ca_flags ) ) != 0 )
1798 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001799 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001800 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001801 }
1802 }
1803
1804 /* Call callback on top cert */
1805 if( NULL != f_vrfy )
1806 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001807 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808 return( ret );
1809 }
1810
1811 *flags |= ca_flags;
1812
1813 return( 0 );
1814}
1815
Paul Bakkerddf26b42013-09-18 13:46:23 +02001816static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001817 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001818 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001819 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001820 void *p_vrfy )
1821{
1822 int ret;
1823 int parent_flags = 0;
1824 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001825 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001826 const md_info_t *md_info;
1827
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001828 if( x509_time_expired( &child->valid_to ) )
1829 *flags |= BADCERT_EXPIRED;
1830
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001831 if( x509_time_future( &child->valid_from ) )
1832 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001833
1834 md_info = md_info_from_type( child->sig_md );
1835 if( md_info == NULL )
1836 {
1837 /*
1838 * Cannot check 'unknown' hash
1839 */
1840 *flags |= BADCERT_NOT_TRUSTED;
1841 }
1842 else
1843 {
1844 md( md_info, child->tbs.p, child->tbs.len, hash );
1845
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001846 if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1847 child->sig_md, hash, md_info->size,
1848 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001849 {
1850 *flags |= BADCERT_NOT_TRUSTED;
1851 }
1852 }
1853
1854#if defined(POLARSSL_X509_CRL_PARSE_C)
1855 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001856 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001857#endif
1858
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001859 /* Look for a grandparent upwards the chain */
1860 for( grandparent = parent->next;
1861 grandparent != NULL;
1862 grandparent = grandparent->next )
1863 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001864 if( x509_crt_check_parent( parent, grandparent,
1865 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001866 break;
1867 }
1868
1869 /* Is our parent part of the chain or at the top? */
1870 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001871 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001872 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1873 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001874 if( ret != 0 )
1875 return( ret );
1876 }
1877 else
1878 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001879 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1880 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001881 if( ret != 0 )
1882 return( ret );
1883 }
1884
1885 /* child is verified to be a child of the parent, call verify callback */
1886 if( NULL != f_vrfy )
1887 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1888 return( ret );
1889
1890 *flags |= parent_flags;
1891
1892 return( 0 );
1893}
1894
1895/*
1896 * Verify the certificate validity
1897 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001898int x509_crt_verify( x509_crt *crt,
1899 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001900 x509_crl *ca_crl,
1901 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001902 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001903 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001904{
1905 size_t cn_len;
1906 int ret;
1907 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001908 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001909 x509_name *name;
1910 x509_sequence *cur = NULL;
1911
1912 *flags = 0;
1913
1914 if( cn != NULL )
1915 {
1916 name = &crt->subject;
1917 cn_len = strlen( cn );
1918
1919 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1920 {
1921 cur = &crt->subject_alt_names;
1922
1923 while( cur != NULL )
1924 {
1925 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001926 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001927 break;
1928
1929 if( cur->buf.len > 2 &&
1930 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1931 x509_wildcard_verify( cn, &cur->buf ) )
1932 break;
1933
1934 cur = cur->next;
1935 }
1936
1937 if( cur == NULL )
1938 *flags |= BADCERT_CN_MISMATCH;
1939 }
1940 else
1941 {
1942 while( name != NULL )
1943 {
1944 if( OID_CMP( OID_AT_CN, &name->oid ) )
1945 {
1946 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001947 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001948 break;
1949
1950 if( name->val.len > 2 &&
1951 memcmp( name->val.p, "*.", 2 ) == 0 &&
1952 x509_wildcard_verify( cn, &name->val ) )
1953 break;
1954 }
1955
1956 name = name->next;
1957 }
1958
1959 if( name == NULL )
1960 *flags |= BADCERT_CN_MISMATCH;
1961 }
1962 }
1963
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001964 /* Look for a parent upwards the chain */
1965 for( parent = crt->next; parent != NULL; parent = parent->next )
1966 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001967 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001968 break;
1969 }
1970
1971 /* Are we part of the chain or at the top? */
1972 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001973 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001974 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1975 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001976 if( ret != 0 )
1977 return( ret );
1978 }
1979 else
1980 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001981 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1982 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001983 if( ret != 0 )
1984 return( ret );
1985 }
1986
1987 if( *flags != 0 )
1988 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1989
1990 return( 0 );
1991}
1992
1993/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001994 * Initialize a certificate chain
1995 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001996void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001997{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001998 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001999}
2000
2001/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002002 * Unallocate all certificate data
2003 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002004void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002005{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002006 x509_crt *cert_cur = crt;
2007 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002008 x509_name *name_cur;
2009 x509_name *name_prv;
2010 x509_sequence *seq_cur;
2011 x509_sequence *seq_prv;
2012
2013 if( crt == NULL )
2014 return;
2015
2016 do
2017 {
2018 pk_free( &cert_cur->pk );
2019
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +02002020#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002021 polarssl_free( cert_cur->sig_opts );
2022#endif
2023
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002024 name_cur = cert_cur->issuer.next;
2025 while( name_cur != NULL )
2026 {
2027 name_prv = name_cur;
2028 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002029 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002030 polarssl_free( name_prv );
2031 }
2032
2033 name_cur = cert_cur->subject.next;
2034 while( name_cur != NULL )
2035 {
2036 name_prv = name_cur;
2037 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002038 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002039 polarssl_free( name_prv );
2040 }
2041
2042 seq_cur = cert_cur->ext_key_usage.next;
2043 while( seq_cur != NULL )
2044 {
2045 seq_prv = seq_cur;
2046 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002047 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002048 polarssl_free( seq_prv );
2049 }
2050
2051 seq_cur = cert_cur->subject_alt_names.next;
2052 while( seq_cur != NULL )
2053 {
2054 seq_prv = seq_cur;
2055 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002056 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002057 polarssl_free( seq_prv );
2058 }
2059
2060 if( cert_cur->raw.p != NULL )
2061 {
Paul Bakker34617722014-06-13 17:20:13 +02002062 polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002063 polarssl_free( cert_cur->raw.p );
2064 }
2065
2066 cert_cur = cert_cur->next;
2067 }
2068 while( cert_cur != NULL );
2069
2070 cert_cur = crt;
2071 do
2072 {
2073 cert_prv = cert_cur;
2074 cert_cur = cert_cur->next;
2075
Paul Bakker34617722014-06-13 17:20:13 +02002076 polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002077 if( cert_prv != crt )
2078 polarssl_free( cert_prv );
2079 }
2080 while( cert_cur != NULL );
2081}
2082
Paul Bakker9af723c2014-05-01 13:03:14 +02002083#endif /* POLARSSL_X509_CRT_PARSE_C */