blob: e114c0f321c5bc88582a5609294b97e0376092c8 [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 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The ITU-T X.509 standard defines a certificate format for PKI.
24 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020025 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
26 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
27 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028 *
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
30 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
31 */
32
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
36#include POLARSSL_CONFIG_FILE
37#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038
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#include <stdio.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010067
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#if defined(POLARSSL_FS_IO)
Paul Bakker5ff3f912014-04-04 15:08:20 +020069#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070#include <sys/types.h>
71#include <sys/stat.h>
72#include <dirent.h>
73#endif
74#endif
75
Paul Bakker34617722014-06-13 17:20:13 +020076/* Implementation that should never be optimized out by the compiler */
77static void polarssl_zeroize( void *v, size_t n ) {
78 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
79}
80
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081/*
82 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
83 */
84static int x509_get_version( unsigned char **p,
85 const unsigned char *end,
86 int *ver )
87{
88 int ret;
89 size_t len;
90
91 if( ( ret = asn1_get_tag( p, end, &len,
92 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
93 {
94 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
95 {
96 *ver = 0;
97 return( 0 );
98 }
99
100 return( ret );
101 }
102
103 end = *p + len;
104
105 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200106 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107
108 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200109 return( POLARSSL_ERR_X509_INVALID_VERSION +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
111
112 return( 0 );
113}
114
115/*
116 * Validity ::= SEQUENCE {
117 * notBefore Time,
118 * notAfter Time }
119 */
120static int x509_get_dates( unsigned char **p,
121 const unsigned char *end,
122 x509_time *from,
123 x509_time *to )
124{
125 int ret;
126 size_t len;
127
128 if( ( ret = asn1_get_tag( p, end, &len,
129 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200130 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200131
132 end = *p + len;
133
134 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
135 return( ret );
136
137 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
138 return( ret );
139
140 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200141 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200142 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
143
144 return( 0 );
145}
146
147/*
148 * X.509 v2/v3 unique identifier (not parsed)
149 */
150static int x509_get_uid( unsigned char **p,
151 const unsigned char *end,
152 x509_buf *uid, int n )
153{
154 int ret;
155
156 if( *p == end )
157 return( 0 );
158
159 uid->tag = **p;
160
161 if( ( ret = asn1_get_tag( p, end, &uid->len,
162 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
163 {
164 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
165 return( 0 );
166
167 return( ret );
168 }
169
170 uid->p = *p;
171 *p += uid->len;
172
173 return( 0 );
174}
175
176static int x509_get_basic_constraints( unsigned char **p,
177 const unsigned char *end,
178 int *ca_istrue,
179 int *max_pathlen )
180{
181 int ret;
182 size_t len;
183
184 /*
185 * BasicConstraints ::= SEQUENCE {
186 * cA BOOLEAN DEFAULT FALSE,
187 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
188 */
189 *ca_istrue = 0; /* DEFAULT FALSE */
190 *max_pathlen = 0; /* endless */
191
192 if( ( ret = asn1_get_tag( p, end, &len,
193 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200194 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200195
196 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200197 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200198
199 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
200 {
201 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
202 ret = asn1_get_int( p, end, ca_istrue );
203
204 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200205 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206
207 if( *ca_istrue != 0 )
208 *ca_istrue = 1;
209 }
210
211 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200212 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213
214 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200215 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216
217 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200218 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
220
221 (*max_pathlen)++;
222
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200223 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200224}
225
226static int x509_get_ns_cert_type( unsigned char **p,
227 const unsigned char *end,
228 unsigned char *ns_cert_type)
229{
230 int ret;
231 x509_bitstring bs = { 0, 0, NULL };
232
233 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200234 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200235
236 if( bs.len != 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200237 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200238 POLARSSL_ERR_ASN1_INVALID_LENGTH );
239
240 /* Get actual bitstring */
241 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200242 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200243}
244
245static int x509_get_key_usage( unsigned char **p,
246 const unsigned char *end,
247 unsigned char *key_usage)
248{
249 int ret;
250 x509_bitstring bs = { 0, 0, NULL };
251
252 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200253 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254
255 if( bs.len < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200256 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200257 POLARSSL_ERR_ASN1_INVALID_LENGTH );
258
259 /* Get actual bitstring */
260 *key_usage = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200261 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200262}
263
264/*
265 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
266 *
267 * KeyPurposeId ::= OBJECT IDENTIFIER
268 */
269static int x509_get_ext_key_usage( unsigned char **p,
270 const unsigned char *end,
271 x509_sequence *ext_key_usage)
272{
273 int ret;
274
275 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200276 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200277
278 /* Sequence length must be >= 1 */
279 if( ext_key_usage->buf.p == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200280 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281 POLARSSL_ERR_ASN1_INVALID_LENGTH );
282
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200283 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284}
285
286/*
287 * SubjectAltName ::= GeneralNames
288 *
289 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
290 *
291 * GeneralName ::= CHOICE {
292 * otherName [0] OtherName,
293 * rfc822Name [1] IA5String,
294 * dNSName [2] IA5String,
295 * x400Address [3] ORAddress,
296 * directoryName [4] Name,
297 * ediPartyName [5] EDIPartyName,
298 * uniformResourceIdentifier [6] IA5String,
299 * iPAddress [7] OCTET STRING,
300 * registeredID [8] OBJECT IDENTIFIER }
301 *
302 * OtherName ::= SEQUENCE {
303 * type-id OBJECT IDENTIFIER,
304 * value [0] EXPLICIT ANY DEFINED BY type-id }
305 *
306 * EDIPartyName ::= SEQUENCE {
307 * nameAssigner [0] DirectoryString OPTIONAL,
308 * partyName [1] DirectoryString }
309 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000310 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311 */
312static int x509_get_subject_alt_name( unsigned char **p,
313 const unsigned char *end,
314 x509_sequence *subject_alt_name )
315{
316 int ret;
317 size_t len, tag_len;
318 asn1_buf *buf;
319 unsigned char tag;
320 asn1_sequence *cur = subject_alt_name;
321
322 /* Get main sequence tag */
323 if( ( ret = asn1_get_tag( p, end, &len,
324 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200325 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200326
327 if( *p + len != end )
Paul Bakker51876562013-09-17 14:36:05 +0200328 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
330
331 while( *p < end )
332 {
333 if( ( end - *p ) < 1 )
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_OUT_OF_DATA );
336
337 tag = **p;
338 (*p)++;
339 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200340 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341
342 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
Paul Bakker51876562013-09-17 14:36:05 +0200343 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
345
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200346 /* Skip everything but DNS name */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
348 {
349 *p += tag_len;
350 continue;
351 }
352
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200354 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100356 if( cur->next != NULL )
357 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS );
358
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359 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 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200369
370 buf = &(cur->buf);
371 buf->tag = tag;
372 buf->p = *p;
373 buf->len = tag_len;
374 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375 }
376
377 /* Set final sequence entry's next pointer to NULL */
378 cur->next = NULL;
379
380 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200381 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
383
384 return( 0 );
385}
386
387/*
388 * X.509 v3 extensions
389 *
390 * TODO: Perform all of the basic constraints tests required by the RFC
391 * TODO: Set values for undetected extensions to a sane default?
392 *
393 */
394static int x509_get_crt_ext( unsigned char **p,
395 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200396 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397{
398 int ret;
399 size_t len;
400 unsigned char *end_ext_data, *end_ext_octet;
401
402 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
403 {
404 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
405 return( 0 );
406
407 return( ret );
408 }
409
410 while( *p < end )
411 {
412 /*
413 * Extension ::= SEQUENCE {
414 * extnID OBJECT IDENTIFIER,
415 * critical BOOLEAN DEFAULT FALSE,
416 * extnValue OCTET STRING }
417 */
418 x509_buf extn_oid = {0, 0, NULL};
419 int is_critical = 0; /* DEFAULT FALSE */
420 int ext_type = 0;
421
422 if( ( ret = asn1_get_tag( p, end, &len,
423 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200424 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
426 end_ext_data = *p + len;
427
428 /* Get extension ID */
429 extn_oid.tag = **p;
430
431 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200432 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433
434 extn_oid.p = *p;
435 *p += extn_oid.len;
436
437 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200438 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439 POLARSSL_ERR_ASN1_OUT_OF_DATA );
440
441 /* Get optional critical */
442 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
443 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200444 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
446 /* Data should be octet string type */
447 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
448 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200449 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450
451 end_ext_octet = *p + len;
452
453 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200454 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
456
457 /*
458 * Detect supported extensions
459 */
460 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
461
462 if( ret != 0 )
463 {
464 /* No parser found, skip extension */
465 *p = end_ext_octet;
466
467#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
468 if( is_critical )
469 {
470 /* Data is marked as critical: fail */
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200471 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
473 }
474#endif
475 continue;
476 }
477
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100478 /* Forbid repeated extensions */
479 if( ( crt->ext_types & ext_type ) != 0 )
480 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS );
481
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482 crt->ext_types |= ext_type;
483
484 switch( ext_type )
485 {
486 case EXT_BASIC_CONSTRAINTS:
487 /* Parse basic constraints */
488 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
489 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200490 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 break;
492
493 case EXT_KEY_USAGE:
494 /* Parse key usage */
495 if( ( ret = x509_get_key_usage( p, end_ext_octet,
496 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200497 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498 break;
499
500 case EXT_EXTENDED_KEY_USAGE:
501 /* Parse extended key usage */
502 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
503 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200504 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505 break;
506
507 case EXT_SUBJECT_ALT_NAME:
508 /* Parse subject alt name */
509 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
510 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200511 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512 break;
513
514 case EXT_NS_CERT_TYPE:
515 /* Parse netscape certificate type */
516 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
517 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200518 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519 break;
520
521 default:
522 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
523 }
524 }
525
526 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200527 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
529
530 return( 0 );
531}
532
533/*
534 * Parse and fill a single X.509 certificate in DER format
535 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200536static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200537 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538{
539 int ret;
540 size_t len;
541 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200542 x509_buf sig_params1, sig_params2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100543
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200544 memset( &sig_params1, 0, sizeof( x509_buf ) );
545 memset( &sig_params2, 0, sizeof( x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546
547 /*
548 * Check for valid input
549 */
550 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200551 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552
553 p = (unsigned char *) polarssl_malloc( len = buflen );
554
555 if( p == NULL )
556 return( POLARSSL_ERR_X509_MALLOC_FAILED );
557
558 memcpy( p, buf, buflen );
559
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560 crt->raw.p = p;
561 crt->raw.len = len;
562 end = p + len;
563
564 /*
565 * Certificate ::= SEQUENCE {
566 * tbsCertificate TBSCertificate,
567 * signatureAlgorithm AlgorithmIdentifier,
568 * signatureValue BIT STRING }
569 */
570 if( ( ret = asn1_get_tag( &p, end, &len,
571 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
572 {
573 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200574 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575 }
576
577 if( len > (size_t) ( end - p ) )
578 {
579 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200580 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
582 }
583 crt_end = p + len;
584
585 /*
586 * TBSCertificate ::= SEQUENCE {
587 */
588 crt->tbs.p = p;
589
590 if( ( ret = asn1_get_tag( &p, end, &len,
591 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
592 {
593 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200594 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595 }
596
597 end = p + len;
598 crt->tbs.len = end - crt->tbs.p;
599
600 /*
601 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
602 *
603 * CertificateSerialNumber ::= INTEGER
604 *
605 * signature AlgorithmIdentifier
606 */
607 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
608 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100609 ( ret = x509_get_alg( &p, end, &crt->sig_oid1,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200610 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611 {
612 x509_crt_free( crt );
613 return( ret );
614 }
615
616 crt->version++;
617
618 if( crt->version > 3 )
619 {
620 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200621 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622 }
623
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200624 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200625 &crt->sig_md, &crt->sig_pk,
626 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627 {
628 x509_crt_free( crt );
629 return( ret );
630 }
631
632 /*
633 * issuer Name
634 */
635 crt->issuer_raw.p = p;
636
637 if( ( ret = asn1_get_tag( &p, end, &len,
638 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
639 {
640 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200641 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 }
643
644 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
645 {
646 x509_crt_free( crt );
647 return( ret );
648 }
649
650 crt->issuer_raw.len = p - crt->issuer_raw.p;
651
652 /*
653 * Validity ::= SEQUENCE {
654 * notBefore Time,
655 * notAfter Time }
656 *
657 */
658 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
659 &crt->valid_to ) ) != 0 )
660 {
661 x509_crt_free( crt );
662 return( ret );
663 }
664
665 /*
666 * subject Name
667 */
668 crt->subject_raw.p = p;
669
670 if( ( ret = asn1_get_tag( &p, end, &len,
671 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
672 {
673 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200674 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675 }
676
677 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
678 {
679 x509_crt_free( crt );
680 return( ret );
681 }
682
683 crt->subject_raw.len = p - crt->subject_raw.p;
684
685 /*
686 * SubjectPublicKeyInfo
687 */
Paul Bakkerda771152013-09-16 22:45:03 +0200688 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689 {
690 x509_crt_free( crt );
691 return( ret );
692 }
693
694 /*
695 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
696 * -- If present, version shall be v2 or v3
697 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
698 * -- If present, version shall be v2 or v3
699 * extensions [3] EXPLICIT Extensions OPTIONAL
700 * -- If present, version shall be v3
701 */
702 if( crt->version == 2 || crt->version == 3 )
703 {
704 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
705 if( ret != 0 )
706 {
707 x509_crt_free( crt );
708 return( ret );
709 }
710 }
711
712 if( crt->version == 2 || crt->version == 3 )
713 {
714 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
715 if( ret != 0 )
716 {
717 x509_crt_free( crt );
718 return( ret );
719 }
720 }
721
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200722#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200723 if( crt->version == 3 )
724 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200725#endif
Paul Bakker66d5d072014-06-17 16:39:18 +0200726 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727 if( ret != 0 )
728 {
729 x509_crt_free( crt );
730 return( ret );
731 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200732#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200734#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735
736 if( p != end )
737 {
738 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200739 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
741 }
742
743 end = crt_end;
744
745 /*
746 * }
747 * -- end of TBSCertificate
748 *
749 * signatureAlgorithm AlgorithmIdentifier,
750 * signatureValue BIT STRING
751 */
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200752 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753 {
754 x509_crt_free( crt );
755 return( ret );
756 }
757
758 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200759 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 ||
760 sig_params1.len != sig_params2.len ||
Paul Bakker66d5d072014-06-17 16:39:18 +0200761 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762 {
763 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200764 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765 }
766
767 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
768 {
769 x509_crt_free( crt );
770 return( ret );
771 }
772
773 if( p != end )
774 {
775 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200776 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
778 }
779
780 return( 0 );
781}
782
783/*
784 * Parse one X.509 certificate in DER format from a buffer and add them to a
785 * chained list
786 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200787int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200788 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789{
790 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200791 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792
793 /*
794 * Check for valid input
795 */
796 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200797 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798
799 while( crt->version != 0 && crt->next != NULL )
800 {
801 prev = crt;
802 crt = crt->next;
803 }
804
805 /*
806 * Add new certificate on the end of the chain if needed.
807 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200808 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200809 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200810 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811
812 if( crt->next == NULL )
813 return( POLARSSL_ERR_X509_MALLOC_FAILED );
814
815 prev = crt;
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100816 x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200817 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818 }
819
Paul Bakkerddf26b42013-09-18 13:46:23 +0200820 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821 {
822 if( prev )
823 prev->next = NULL;
824
825 if( crt != chain )
826 polarssl_free( crt );
827
828 return( ret );
829 }
830
831 return( 0 );
832}
833
834/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200835 * Parse one or more PEM certificates from a buffer and add them to the chained
836 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200838int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839{
840 int success = 0, first_error = 0, total_failed = 0;
841 int buf_format = X509_FORMAT_DER;
842
843 /*
844 * Check for valid input
845 */
846 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200847 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200848
849 /*
850 * Determine buffer content. Buffer contains either one DER certificate or
851 * one or more PEM certificates.
852 */
853#if defined(POLARSSL_PEM_PARSE_C)
854 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
855 buf_format = X509_FORMAT_PEM;
856#endif
857
858 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200859 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200860
861#if defined(POLARSSL_PEM_PARSE_C)
862 if( buf_format == X509_FORMAT_PEM )
863 {
864 int ret;
865 pem_context pem;
866
867 while( buflen > 0 )
868 {
869 size_t use_len;
870 pem_init( &pem );
871
872 ret = pem_read_buffer( &pem,
873 "-----BEGIN CERTIFICATE-----",
874 "-----END CERTIFICATE-----",
875 buf, NULL, 0, &use_len );
876
877 if( ret == 0 )
878 {
879 /*
880 * Was PEM encoded
881 */
882 buflen -= use_len;
883 buf += use_len;
884 }
885 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
886 {
887 return( ret );
888 }
889 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
890 {
891 pem_free( &pem );
892
893 /*
894 * PEM header and footer were found
895 */
896 buflen -= use_len;
897 buf += use_len;
898
899 if( first_error == 0 )
900 first_error = ret;
901
Paul Bakker5a5fa922014-09-26 14:53:04 +0200902 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 continue;
904 }
905 else
906 break;
907
Paul Bakkerddf26b42013-09-18 13:46:23 +0200908 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909
910 pem_free( &pem );
911
912 if( ret != 0 )
913 {
914 /*
915 * Quit parsing on a memory error
916 */
917 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
918 return( ret );
919
920 if( first_error == 0 )
921 first_error = ret;
922
923 total_failed++;
924 continue;
925 }
926
927 success = 1;
928 }
929 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200930#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931
932 if( success )
933 return( total_failed );
934 else if( first_error )
935 return( first_error );
936 else
937 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
938}
939
940#if defined(POLARSSL_FS_IO)
941/*
942 * Load one or more certificates and add them to the chained list
943 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200944int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945{
946 int ret;
947 size_t n;
948 unsigned char *buf;
949
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100950 if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 return( ret );
952
Paul Bakkerddf26b42013-09-18 13:46:23 +0200953 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
Paul Bakker34617722014-06-13 17:20:13 +0200955 polarssl_zeroize( buf, n + 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956 polarssl_free( buf );
957
958 return( ret );
959}
960
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100961#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100962static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
963#endif
964
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200965int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966{
967 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100968#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969 int w_ret;
970 WCHAR szDir[MAX_PATH];
971 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200972 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200973 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974
Paul Bakker9af723c2014-05-01 13:03:14 +0200975 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976 HANDLE hFind;
977
978 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200979 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980
Paul Bakker9af723c2014-05-01 13:03:14 +0200981 memset( szDir, 0, sizeof(szDir) );
982 memset( filename, 0, MAX_PATH );
983 memcpy( filename, path, len );
984 filename[len++] = '\\';
985 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 filename[len++] = '*';
987
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200988 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
989 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +0000990 if( w_ret == 0 )
991 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
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 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001009 if( w_ret == 0 )
1010 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011
Paul Bakkerddf26b42013-09-18 13:46:23 +02001012 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 if( w_ret < 0 )
1014 ret++;
1015 else
1016 ret += w_ret;
1017 }
1018 while( FindNextFileW( hFind, &file_data ) != 0 );
1019
Paul Bakker66d5d072014-06-17 16:39:18 +02001020 if( GetLastError() != ERROR_NO_MORE_FILES )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1022
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001024#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001025 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001027 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 char entry_name[255];
1029 DIR *dir = opendir( path );
1030
Paul Bakker66d5d072014-06-17 16:39:18 +02001031 if( dir == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1033
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001034#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001035 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1036 return( ret );
1037#endif
1038
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001039 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001041 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001043 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 {
1045 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001046 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1047 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 }
1049
1050 if( !S_ISREG( sb.st_mode ) )
1051 continue;
1052
1053 // Ignore parse errors
1054 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001055 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056 if( t_ret < 0 )
1057 ret++;
1058 else
1059 ret += t_ret;
1060 }
1061 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001062
1063cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001064#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001065 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1066 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1067#endif
1068
Paul Bakkerbe089b02013-10-14 15:51:50 +02001069#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070
1071 return( ret );
1072}
1073#endif /* POLARSSL_FS_IO */
1074
Paul Bakker6edcd412013-10-29 15:22:54 +01001075#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1076 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077#include <stdarg.h>
1078
1079#if !defined vsnprintf
1080#define vsnprintf _vsnprintf
1081#endif // vsnprintf
1082
1083/*
1084 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1085 * Result value is not size of buffer needed, but -1 if no fit is possible.
1086 *
1087 * This fuction tries to 'fix' this by at least suggesting enlarging the
1088 * size by 20.
1089 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001090static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091{
1092 va_list ap;
1093 int res = -1;
1094
1095 va_start( ap, format );
1096
1097 res = vsnprintf( str, size, format, ap );
1098
1099 va_end( ap );
1100
1101 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +02001102 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103 return( (int) size + 20 );
1104
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001105 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106}
1107
1108#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001109#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110
1111#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1112
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001113#define SAFE_SNPRINTF() \
1114{ \
1115 if( ret == -1 ) \
1116 return( -1 ); \
1117 \
Paul Bakker66d5d072014-06-17 16:39:18 +02001118 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001119 p[n - 1] = '\0'; \
1120 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
1121 } \
1122 \
1123 n -= (unsigned int) ret; \
1124 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001125}
1126
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001127static int x509_info_subject_alt_name( char **buf, size_t *size,
1128 const x509_sequence *subject_alt_name )
1129{
1130 size_t i;
1131 size_t n = *size;
1132 char *p = *buf;
1133 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001134 const char *sep = "";
1135 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001136
1137 while( cur != NULL )
1138 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001139 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001140 {
1141 *p = '\0';
1142 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1143 }
1144
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001145 n -= cur->buf.len + sep_len;
1146 for( i = 0; i < sep_len; i++ )
1147 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001148 for( i = 0; i < cur->buf.len; i++ )
1149 *p++ = cur->buf.p[i];
1150
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001151 sep = ", ";
1152 sep_len = 2;
1153
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001154 cur = cur->next;
1155 }
1156
1157 *p = '\0';
1158
1159 *size = n;
1160 *buf = p;
1161
1162 return( 0 );
1163}
1164
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001165#define PRINT_ITEM(i) \
1166 { \
1167 ret = snprintf( p, n, "%s" i, sep ); \
1168 SAFE_SNPRINTF(); \
1169 sep = ", "; \
1170 }
1171
1172#define CERT_TYPE(type,name) \
1173 if( ns_cert_type & type ) \
1174 PRINT_ITEM( name );
1175
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001176static int x509_info_cert_type( char **buf, size_t *size,
1177 unsigned char ns_cert_type )
1178{
1179 int ret;
1180 size_t n = *size;
1181 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001182 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001183
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001184 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1185 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1186 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1187 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1188 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1189 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1190 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1191 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001192
1193 *size = n;
1194 *buf = p;
1195
1196 return( 0 );
1197}
1198
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001199#define KEY_USAGE(code,name) \
1200 if( key_usage & code ) \
1201 PRINT_ITEM( name );
1202
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001203static int x509_info_key_usage( char **buf, size_t *size,
1204 unsigned char key_usage )
1205{
1206 int ret;
1207 size_t n = *size;
1208 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001209 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001210
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001211 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1212 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1213 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1214 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1215 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1216 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1217 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001218
1219 *size = n;
1220 *buf = p;
1221
1222 return( 0 );
1223}
1224
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001225static int x509_info_ext_key_usage( char **buf, size_t *size,
1226 const x509_sequence *extended_key_usage )
1227{
1228 int ret;
1229 const char *desc;
1230 size_t n = *size;
1231 char *p = *buf;
1232 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001233 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001234
1235 while( cur != NULL )
1236 {
1237 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1238 desc = "???";
1239
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001240 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001241 SAFE_SNPRINTF();
1242
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001243 sep = ", ";
1244
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001245 cur = cur->next;
1246 }
1247
1248 *size = n;
1249 *buf = p;
1250
1251 return( 0 );
1252}
1253
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001254/*
1255 * Return an informational string about the certificate.
1256 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001257#define BEFORE_COLON 18
1258#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001259int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001260 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001261{
1262 int ret;
1263 size_t n;
1264 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001265 char key_size_str[BEFORE_COLON];
1266
1267 p = buf;
1268 n = size;
1269
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001270 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001271 prefix, crt->version );
1272 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001273 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001274 prefix );
1275 SAFE_SNPRINTF();
1276
Paul Bakker66d5d072014-06-17 16:39:18 +02001277 ret = x509_serial_gets( p, n, &crt->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001278 SAFE_SNPRINTF();
1279
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001280 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001282 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001283 SAFE_SNPRINTF();
1284
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001285 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001287 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001288 SAFE_SNPRINTF();
1289
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001290 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001291 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1292 crt->valid_from.year, crt->valid_from.mon,
1293 crt->valid_from.day, crt->valid_from.hour,
1294 crt->valid_from.min, crt->valid_from.sec );
1295 SAFE_SNPRINTF();
1296
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001297 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001298 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1299 crt->valid_to.year, crt->valid_to.mon,
1300 crt->valid_to.day, crt->valid_to.hour,
1301 crt->valid_to.min, crt->valid_to.sec );
1302 SAFE_SNPRINTF();
1303
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001304 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001305 SAFE_SNPRINTF();
1306
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001307 ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001308 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001309 SAFE_SNPRINTF();
1310
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001311 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001312 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1313 pk_get_name( &crt->pk ) ) ) != 0 )
1314 {
1315 return( ret );
1316 }
1317
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001318 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319 (int) pk_get_size( &crt->pk ) );
1320 SAFE_SNPRINTF();
1321
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001322 /*
1323 * Optional extensions
1324 */
1325
1326 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1327 {
1328 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1329 crt->ca_istrue ? "true" : "false" );
1330 SAFE_SNPRINTF();
1331
1332 if( crt->max_pathlen > 0 )
1333 {
1334 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1335 SAFE_SNPRINTF();
1336 }
1337 }
1338
1339 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1340 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001341 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001342 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001343
1344 if( ( ret = x509_info_subject_alt_name( &p, &n,
1345 &crt->subject_alt_names ) ) != 0 )
1346 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001347 }
1348
1349 if( crt->ext_types & EXT_NS_CERT_TYPE )
1350 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001351 ret = snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001352 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001353
1354 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1355 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001356 }
1357
1358 if( crt->ext_types & EXT_KEY_USAGE )
1359 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001360 ret = snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001361 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001362
1363 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1364 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001365 }
1366
1367 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1368 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001369 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001370 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001371
1372 if( ( ret = x509_info_ext_key_usage( &p, &n,
1373 &crt->ext_key_usage ) ) != 0 )
1374 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001375 }
1376
1377 ret = snprintf( p, n, "\n" );
1378 SAFE_SNPRINTF();
1379
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380 return( (int) ( size - n ) );
1381}
1382
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001383#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1384int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1385{
1386 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1387 ( crt->key_usage & usage ) != usage )
1388 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1389
1390 return( 0 );
1391}
1392#endif
1393
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001394#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1395int x509_crt_check_extended_key_usage( const x509_crt *crt,
1396 const char *usage_oid,
1397 size_t usage_len )
1398{
1399 const x509_sequence *cur;
1400
1401 /* Extension is not mandatory, absent means no restriction */
1402 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1403 return( 0 );
1404
1405 /*
1406 * Look for the requested usage (or wildcard ANY) in our list
1407 */
1408 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1409 {
1410 const x509_buf *cur_oid = &cur->buf;
1411
1412 if( cur_oid->len == usage_len &&
1413 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1414 {
1415 return( 0 );
1416 }
1417
1418 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1419 return( 0 );
1420 }
1421
1422 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1423}
Paul Bakker9af723c2014-05-01 13:03:14 +02001424#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001425
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001426#if defined(POLARSSL_X509_CRL_PARSE_C)
1427/*
1428 * Return 1 if the certificate is revoked, or 0 otherwise.
1429 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001430int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431{
1432 const x509_crl_entry *cur = &crl->entry;
1433
1434 while( cur != NULL && cur->serial.len != 0 )
1435 {
1436 if( crt->serial.len == cur->serial.len &&
1437 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1438 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001439 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440 return( 1 );
1441 }
1442
1443 cur = cur->next;
1444 }
1445
1446 return( 0 );
1447}
1448
1449/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001450 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001452static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001453 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454{
1455 int flags = 0;
1456 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1457 const md_info_t *md_info;
1458
1459 if( ca == NULL )
1460 return( flags );
1461
1462 /*
1463 * TODO: What happens if no CRL is present?
1464 * Suggestion: Revocation state should be unknown if no CRL is present.
1465 * For backwards compatibility this is not yet implemented.
1466 */
1467
1468 while( crl_list != NULL )
1469 {
1470 if( crl_list->version == 0 ||
1471 crl_list->issuer_raw.len != ca->subject_raw.len ||
1472 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1473 crl_list->issuer_raw.len ) != 0 )
1474 {
1475 crl_list = crl_list->next;
1476 continue;
1477 }
1478
1479 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001480 * Check if the CA is configured to sign CRLs
1481 */
1482#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1483 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1484 {
1485 flags |= BADCRL_NOT_TRUSTED;
1486 break;
1487 }
1488#endif
1489
1490 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491 * Check if CRL is correctly signed by the trusted CA
1492 */
1493 md_info = md_info_from_type( crl_list->sig_md );
1494 if( md_info == NULL )
1495 {
1496 /*
1497 * Cannot check 'unknown' hash
1498 */
1499 flags |= BADCRL_NOT_TRUSTED;
1500 break;
1501 }
1502
1503 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1504
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001505 if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1506 crl_list->sig_md, hash, md_info->size,
1507 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508 {
1509 flags |= BADCRL_NOT_TRUSTED;
1510 break;
1511 }
1512
1513 /*
1514 * Check for validity of CRL (Do not drop out)
1515 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001516 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517 flags |= BADCRL_EXPIRED;
1518
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001519 if( x509_time_future( &crl_list->this_update ) )
1520 flags |= BADCRL_FUTURE;
1521
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522 /*
1523 * Check if certificate is revoked
1524 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001525 if( x509_crt_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526 {
1527 flags |= BADCERT_REVOKED;
1528 break;
1529 }
1530
1531 crl_list = crl_list->next;
1532 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001533 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534}
1535#endif /* POLARSSL_X509_CRL_PARSE_C */
1536
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001537/*
1538 * Like memcmp, but case-insensitive and always returns -1 if different
1539 */
1540static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541{
1542 size_t i;
1543 unsigned char diff;
1544 const unsigned char *n1 = s1, *n2 = s2;
1545
1546 for( i = 0; i < len; i++ )
1547 {
1548 diff = n1[i] ^ n2[i];
1549
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001550 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551 continue;
1552
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001553 if( diff == 32 &&
1554 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1555 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1556 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001558 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001560 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561 }
1562
1563 return( 0 );
1564}
1565
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001566/*
1567 * Return 1 if match, 0 if not
1568 * TODO: inverted return value!
1569 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570static int x509_wildcard_verify( const char *cn, x509_buf *name )
1571{
1572 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001573 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001574
1575 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1576 return( 0 );
1577
Paul Bakker14b16c62014-05-28 11:33:54 +02001578 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579 {
1580 if( cn[i] == '.' )
1581 {
1582 cn_idx = i;
1583 break;
1584 }
1585 }
1586
1587 if( cn_idx == 0 )
1588 return( 0 );
1589
Paul Bakker14b16c62014-05-28 11:33:54 +02001590 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001591 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592 {
1593 return( 1 );
1594 }
1595
1596 return( 0 );
1597}
1598
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001599/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001600 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1601 * variations (but not all).
1602 *
1603 * Return 0 if equal, -1 otherwise.
1604 */
1605static int x509_string_cmp( const x509_buf *a, const x509_buf *b )
1606{
1607 if( a->tag == b->tag &&
1608 a->len == b->len &&
1609 memcmp( a->p, b->p, b->len ) == 0 )
1610 {
1611 return( 0 );
1612 }
1613
1614 if( ( a->tag == ASN1_UTF8_STRING || a->tag == ASN1_PRINTABLE_STRING ) &&
1615 ( b->tag == ASN1_UTF8_STRING || b->tag == ASN1_PRINTABLE_STRING ) &&
1616 a->len == b->len &&
1617 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1618 {
1619 return( 0 );
1620 }
1621
1622 return( -1 );
1623}
1624
1625/*
1626 * Compare two X.509 Names (aka rdnSequence).
1627 *
1628 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1629 * we sometimes return unequal when the full algorithm would return equal,
1630 * but never the other way. (In particular, we don't do Unicode normalisation
1631 * or space folding.)
1632 *
1633 * Return 0 if equal, -1 otherwise.
1634 */
1635static int x509_name_cmp( const x509_name *a, const x509_name *b )
1636{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001637 /* Avoid recursion, it might not be optimised by the compiler */
1638 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001639 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001640 if( a == NULL || b == NULL )
1641 return( -1 );
1642
1643 /* type */
1644 if( a->oid.tag != b->oid.tag ||
1645 a->oid.len != b->oid.len ||
1646 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1647 {
1648 return( -1 );
1649 }
1650
1651 /* value */
1652 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1653 return( -1 );
1654
1655 a = a->next;
1656 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001657 }
1658
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001659 /* a == NULL == b */
1660 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001661}
1662
1663/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001664 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1665 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001666 *
1667 * top means parent is a locally-trusted certificate
1668 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001669 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001670static int x509_crt_check_parent( const x509_crt *child,
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001671 const x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001672 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001673{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001674 int need_ca_bit;
1675
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001676 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001677 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001678 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001679
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001680 /* Parent must have the basicConstraints CA bit set as a general rule */
1681 need_ca_bit = 1;
1682
1683 /* Exception: v1/v2 certificates that are locally trusted. */
1684 if( top && parent->version < 3 )
1685 need_ca_bit = 0;
1686
1687 /* Exception: self-signed end-entity certs that are locally trusted. */
1688 if( top && bottom &&
1689 child->raw.len == parent->raw.len &&
1690 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1691 {
1692 need_ca_bit = 0;
1693 }
1694
1695 if( need_ca_bit && ! parent->ca_istrue )
1696 return( -1 );
1697
1698#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1699 if( need_ca_bit &&
1700 x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001701 {
1702 return( -1 );
1703 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001704#endif
1705
1706 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001707}
1708
Paul Bakkerddf26b42013-09-18 13:46:23 +02001709static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001710 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001711 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001712 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001713 void *p_vrfy )
1714{
1715 int ret;
1716 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1717 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1718 const md_info_t *md_info;
1719
Paul Bakker86d0c192013-09-18 11:11:02 +02001720 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721 *flags |= BADCERT_EXPIRED;
1722
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001723 if( x509_time_future( &child->valid_from ) )
1724 *flags |= BADCERT_FUTURE;
1725
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001726 /*
1727 * Child is the top of the chain. Check against the trust_ca list.
1728 */
1729 *flags |= BADCERT_NOT_TRUSTED;
1730
1731 md_info = md_info_from_type( child->sig_md );
1732 if( md_info == NULL )
1733 {
1734 /*
1735 * Cannot check 'unknown', no need to try any CA
1736 */
1737 trust_ca = NULL;
1738 }
1739 else
1740 md( md_info, child->tbs.p, child->tbs.len, hash );
1741
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001742 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001743 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001744 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001746
1747 /*
1748 * Reduce path_len to check against if top of the chain is
1749 * the same as the trusted CA
1750 */
1751 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1752 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1753 child->issuer_raw.len ) == 0 )
1754 {
1755 check_path_cnt--;
1756 }
1757
1758 if( trust_ca->max_pathlen > 0 &&
1759 trust_ca->max_pathlen < check_path_cnt )
1760 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001761 continue;
1762 }
1763
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001764 if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1765 child->sig_md, hash, md_info->size,
1766 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001767 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768 continue;
1769 }
1770
1771 /*
1772 * Top of chain is signed by a trusted CA
1773 */
1774 *flags &= ~BADCERT_NOT_TRUSTED;
1775 break;
1776 }
1777
1778 /*
1779 * If top of chain is not the same as the trusted CA send a verify request
1780 * to the callback for any issues with validity and CRL presence for the
1781 * trusted CA certificate.
1782 */
1783 if( trust_ca != NULL &&
1784 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1785 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1786 child->issuer_raw.len ) != 0 ) )
1787 {
1788#if defined(POLARSSL_X509_CRL_PARSE_C)
1789 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001790 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001791#else
1792 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001793#endif
1794
Paul Bakker86d0c192013-09-18 11:11:02 +02001795 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001796 ca_flags |= BADCERT_EXPIRED;
1797
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001798 if( x509_time_future( &trust_ca->valid_from ) )
1799 ca_flags |= BADCERT_FUTURE;
1800
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001801 if( NULL != f_vrfy )
1802 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001803 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1804 &ca_flags ) ) != 0 )
1805 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001806 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001807 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808 }
1809 }
1810
1811 /* Call callback on top cert */
1812 if( NULL != f_vrfy )
1813 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001814 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001815 return( ret );
1816 }
1817
1818 *flags |= ca_flags;
1819
1820 return( 0 );
1821}
1822
Paul Bakkerddf26b42013-09-18 13:46:23 +02001823static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001824 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001825 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001826 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001827 void *p_vrfy )
1828{
1829 int ret;
1830 int parent_flags = 0;
1831 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001832 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001833 const md_info_t *md_info;
1834
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001835 /* path_cnt is 0 for the first intermediate CA */
1836 if( 1 + path_cnt > POLARSSL_X509_MAX_INTERMEDIATE_CA )
1837 {
1838 *flags |= BADCERT_NOT_TRUSTED;
1839 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1840 }
1841
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001842 if( x509_time_expired( &child->valid_to ) )
1843 *flags |= BADCERT_EXPIRED;
1844
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001845 if( x509_time_future( &child->valid_from ) )
1846 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001847
1848 md_info = md_info_from_type( child->sig_md );
1849 if( md_info == NULL )
1850 {
1851 /*
1852 * Cannot check 'unknown' hash
1853 */
1854 *flags |= BADCERT_NOT_TRUSTED;
1855 }
1856 else
1857 {
1858 md( md_info, child->tbs.p, child->tbs.len, hash );
1859
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001860 if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1861 child->sig_md, hash, md_info->size,
1862 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001863 {
1864 *flags |= BADCERT_NOT_TRUSTED;
1865 }
1866 }
1867
1868#if defined(POLARSSL_X509_CRL_PARSE_C)
1869 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001870 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001871#endif
1872
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001873 /* Look for a grandparent upwards the chain */
1874 for( grandparent = parent->next;
1875 grandparent != NULL;
1876 grandparent = grandparent->next )
1877 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001878 if( x509_crt_check_parent( parent, grandparent,
1879 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001880 break;
1881 }
1882
1883 /* Is our parent part of the chain or at the top? */
1884 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001885 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001886 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1887 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001888 if( ret != 0 )
1889 return( ret );
1890 }
1891 else
1892 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001893 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1894 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001895 if( ret != 0 )
1896 return( ret );
1897 }
1898
1899 /* child is verified to be a child of the parent, call verify callback */
1900 if( NULL != f_vrfy )
1901 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1902 return( ret );
1903
1904 *flags |= parent_flags;
1905
1906 return( 0 );
1907}
1908
1909/*
1910 * Verify the certificate validity
1911 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001912int x509_crt_verify( x509_crt *crt,
1913 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001914 x509_crl *ca_crl,
1915 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001916 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001917 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001918{
1919 size_t cn_len;
1920 int ret;
1921 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001922 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001923 x509_name *name;
1924 x509_sequence *cur = NULL;
1925
1926 *flags = 0;
1927
1928 if( cn != NULL )
1929 {
1930 name = &crt->subject;
1931 cn_len = strlen( cn );
1932
1933 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1934 {
1935 cur = &crt->subject_alt_names;
1936
1937 while( cur != NULL )
1938 {
1939 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001940 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001941 break;
1942
1943 if( cur->buf.len > 2 &&
1944 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1945 x509_wildcard_verify( cn, &cur->buf ) )
1946 break;
1947
1948 cur = cur->next;
1949 }
1950
1951 if( cur == NULL )
1952 *flags |= BADCERT_CN_MISMATCH;
1953 }
1954 else
1955 {
1956 while( name != NULL )
1957 {
1958 if( OID_CMP( OID_AT_CN, &name->oid ) )
1959 {
1960 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001961 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001962 break;
1963
1964 if( name->val.len > 2 &&
1965 memcmp( name->val.p, "*.", 2 ) == 0 &&
1966 x509_wildcard_verify( cn, &name->val ) )
1967 break;
1968 }
1969
1970 name = name->next;
1971 }
1972
1973 if( name == NULL )
1974 *flags |= BADCERT_CN_MISMATCH;
1975 }
1976 }
1977
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001978 /* Look for a parent upwards the chain */
1979 for( parent = crt->next; parent != NULL; parent = parent->next )
1980 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001981 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001982 break;
1983 }
1984
1985 /* Are we part of the chain or at the top? */
1986 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001987 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001988 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1989 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001990 if( ret != 0 )
1991 return( ret );
1992 }
1993 else
1994 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001995 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1996 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001997 if( ret != 0 )
1998 return( ret );
1999 }
2000
2001 if( *flags != 0 )
2002 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
2003
2004 return( 0 );
2005}
2006
2007/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002008 * Initialize a certificate chain
2009 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002010void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002011{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002012 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002013}
2014
2015/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002016 * Unallocate all certificate data
2017 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002018void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002019{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002020 x509_crt *cert_cur = crt;
2021 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002022 x509_name *name_cur;
2023 x509_name *name_prv;
2024 x509_sequence *seq_cur;
2025 x509_sequence *seq_prv;
2026
2027 if( crt == NULL )
2028 return;
2029
2030 do
2031 {
2032 pk_free( &cert_cur->pk );
2033
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +02002034#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002035 polarssl_free( cert_cur->sig_opts );
2036#endif
2037
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002038 name_cur = cert_cur->issuer.next;
2039 while( name_cur != NULL )
2040 {
2041 name_prv = name_cur;
2042 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002043 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002044 polarssl_free( name_prv );
2045 }
2046
2047 name_cur = cert_cur->subject.next;
2048 while( name_cur != NULL )
2049 {
2050 name_prv = name_cur;
2051 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002052 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002053 polarssl_free( name_prv );
2054 }
2055
2056 seq_cur = cert_cur->ext_key_usage.next;
2057 while( seq_cur != NULL )
2058 {
2059 seq_prv = seq_cur;
2060 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002061 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002062 polarssl_free( seq_prv );
2063 }
2064
2065 seq_cur = cert_cur->subject_alt_names.next;
2066 while( seq_cur != NULL )
2067 {
2068 seq_prv = seq_cur;
2069 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002070 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002071 polarssl_free( seq_prv );
2072 }
2073
2074 if( cert_cur->raw.p != NULL )
2075 {
Paul Bakker34617722014-06-13 17:20:13 +02002076 polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002077 polarssl_free( cert_cur->raw.p );
2078 }
2079
2080 cert_cur = cert_cur->next;
2081 }
2082 while( cert_cur != NULL );
2083
2084 cert_cur = crt;
2085 do
2086 {
2087 cert_prv = cert_cur;
2088 cert_cur = cert_cur->next;
2089
Paul Bakker34617722014-06-13 17:20:13 +02002090 polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002091 if( cert_prv != crt )
2092 polarssl_free( cert_prv );
2093 }
2094 while( cert_cur != NULL );
2095}
2096
Paul Bakker9af723c2014-05-01 13:03:14 +02002097#endif /* POLARSSL_X509_CRT_PARSE_C */