blob: 84e4415064809b65e4b56f56788cc4479c1be99a [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/x509_crt.h"
42#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000043
44#include <stdio.h>
45#include <string.h>
46
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047#if defined(POLARSSL_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049#endif
50
Paul Bakker7dc4c442014-02-01 22:50:26 +010051#if defined(POLARSSL_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#else
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <stdlib.h>
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#define polarssl_free free
Rich Evansfac657f2015-01-30 11:00:01 +000056#define polarssl_malloc malloc
57#define polarssl_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058#endif
59
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010060#if defined(POLARSSL_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010062#endif
63
Paul Bakkerfa6a6202013-10-28 18:48:30 +010064#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065#include <windows.h>
66#else
67#include <time.h>
68#endif
69
70#if defined(POLARSSL_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000071#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020072#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073#include <sys/types.h>
74#include <sys/stat.h>
75#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000076#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#endif
78
Paul Bakker34617722014-06-13 17:20:13 +020079/* Implementation that should never be optimized out by the compiler */
80static void polarssl_zeroize( void *v, size_t n ) {
81 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
82}
83
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084/*
85 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
86 */
87static int x509_get_version( unsigned char **p,
88 const unsigned char *end,
89 int *ver )
90{
91 int ret;
92 size_t len;
93
94 if( ( ret = asn1_get_tag( p, end, &len,
95 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
96 {
97 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
98 {
99 *ver = 0;
100 return( 0 );
101 }
102
103 return( ret );
104 }
105
106 end = *p + len;
107
108 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200109 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110
111 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200112 return( POLARSSL_ERR_X509_INVALID_VERSION +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
114
115 return( 0 );
116}
117
118/*
119 * Validity ::= SEQUENCE {
120 * notBefore Time,
121 * notAfter Time }
122 */
123static int x509_get_dates( unsigned char **p,
124 const unsigned char *end,
125 x509_time *from,
126 x509_time *to )
127{
128 int ret;
129 size_t len;
130
131 if( ( ret = asn1_get_tag( p, end, &len,
132 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200133 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200134
135 end = *p + len;
136
137 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
138 return( ret );
139
140 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
141 return( ret );
142
143 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200144 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
146
147 return( 0 );
148}
149
150/*
151 * X.509 v2/v3 unique identifier (not parsed)
152 */
153static int x509_get_uid( unsigned char **p,
154 const unsigned char *end,
155 x509_buf *uid, int n )
156{
157 int ret;
158
159 if( *p == end )
160 return( 0 );
161
162 uid->tag = **p;
163
164 if( ( ret = asn1_get_tag( p, end, &uid->len,
165 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
166 {
167 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
168 return( 0 );
169
170 return( ret );
171 }
172
173 uid->p = *p;
174 *p += uid->len;
175
176 return( 0 );
177}
178
179static int x509_get_basic_constraints( unsigned char **p,
180 const unsigned char *end,
181 int *ca_istrue,
182 int *max_pathlen )
183{
184 int ret;
185 size_t len;
186
187 /*
188 * BasicConstraints ::= SEQUENCE {
189 * cA BOOLEAN DEFAULT FALSE,
190 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
191 */
192 *ca_istrue = 0; /* DEFAULT FALSE */
193 *max_pathlen = 0; /* endless */
194
195 if( ( ret = asn1_get_tag( p, end, &len,
196 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200197 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200198
199 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200200 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200201
202 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
203 {
204 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
205 ret = asn1_get_int( p, end, ca_istrue );
206
207 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200208 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200209
210 if( *ca_istrue != 0 )
211 *ca_istrue = 1;
212 }
213
214 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200215 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216
217 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200218 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219
220 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200221 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200222 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
223
224 (*max_pathlen)++;
225
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200226 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200227}
228
229static int x509_get_ns_cert_type( unsigned char **p,
230 const unsigned char *end,
231 unsigned char *ns_cert_type)
232{
233 int ret;
234 x509_bitstring bs = { 0, 0, NULL };
235
236 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200237 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200238
239 if( bs.len != 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200240 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200241 POLARSSL_ERR_ASN1_INVALID_LENGTH );
242
243 /* Get actual bitstring */
244 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200245 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200246}
247
248static int x509_get_key_usage( unsigned char **p,
249 const unsigned char *end,
250 unsigned char *key_usage)
251{
252 int ret;
253 x509_bitstring bs = { 0, 0, NULL };
254
255 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200256 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200257
258 if( bs.len < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200259 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260 POLARSSL_ERR_ASN1_INVALID_LENGTH );
261
262 /* Get actual bitstring */
263 *key_usage = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200264 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265}
266
267/*
268 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
269 *
270 * KeyPurposeId ::= OBJECT IDENTIFIER
271 */
272static int x509_get_ext_key_usage( unsigned char **p,
273 const unsigned char *end,
274 x509_sequence *ext_key_usage)
275{
276 int ret;
277
278 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200279 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280
281 /* Sequence length must be >= 1 */
282 if( ext_key_usage->buf.p == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200283 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284 POLARSSL_ERR_ASN1_INVALID_LENGTH );
285
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200286 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287}
288
289/*
290 * SubjectAltName ::= GeneralNames
291 *
292 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
293 *
294 * GeneralName ::= CHOICE {
295 * otherName [0] OtherName,
296 * rfc822Name [1] IA5String,
297 * dNSName [2] IA5String,
298 * x400Address [3] ORAddress,
299 * directoryName [4] Name,
300 * ediPartyName [5] EDIPartyName,
301 * uniformResourceIdentifier [6] IA5String,
302 * iPAddress [7] OCTET STRING,
303 * registeredID [8] OBJECT IDENTIFIER }
304 *
305 * OtherName ::= SEQUENCE {
306 * type-id OBJECT IDENTIFIER,
307 * value [0] EXPLICIT ANY DEFINED BY type-id }
308 *
309 * EDIPartyName ::= SEQUENCE {
310 * nameAssigner [0] DirectoryString OPTIONAL,
311 * partyName [1] DirectoryString }
312 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000313 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200314 */
315static int x509_get_subject_alt_name( unsigned char **p,
316 const unsigned char *end,
317 x509_sequence *subject_alt_name )
318{
319 int ret;
320 size_t len, tag_len;
321 asn1_buf *buf;
322 unsigned char tag;
323 asn1_sequence *cur = subject_alt_name;
324
325 /* Get main sequence tag */
326 if( ( ret = asn1_get_tag( p, end, &len,
327 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200328 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329
330 if( *p + len != end )
Paul Bakker51876562013-09-17 14:36:05 +0200331 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200332 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
333
334 while( *p < end )
335 {
336 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200337 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200338 POLARSSL_ERR_ASN1_OUT_OF_DATA );
339
340 tag = **p;
341 (*p)++;
342 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200343 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344
345 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
Paul Bakker51876562013-09-17 14:36:05 +0200346 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
348
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200349 /* Skip everything but DNS name */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200350 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
351 {
352 *p += tag_len;
353 continue;
354 }
355
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200357 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100359 if( cur->next != NULL )
360 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS );
361
Mansour Moufid99b92592015-02-15 17:46:32 -0500362 cur->next = polarssl_malloc( sizeof( asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363
364 if( cur->next == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200365 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366 POLARSSL_ERR_ASN1_MALLOC_FAILED );
367
368 memset( cur->next, 0, sizeof( asn1_sequence ) );
369 cur = cur->next;
370 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200371
372 buf = &(cur->buf);
373 buf->tag = tag;
374 buf->p = *p;
375 buf->len = tag_len;
376 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377 }
378
379 /* Set final sequence entry's next pointer to NULL */
380 cur->next = NULL;
381
382 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200383 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
385
386 return( 0 );
387}
388
389/*
390 * X.509 v3 extensions
391 *
392 * TODO: Perform all of the basic constraints tests required by the RFC
393 * TODO: Set values for undetected extensions to a sane default?
394 *
395 */
396static int x509_get_crt_ext( unsigned char **p,
397 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200398 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399{
400 int ret;
401 size_t len;
402 unsigned char *end_ext_data, *end_ext_octet;
403
404 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
405 {
406 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
407 return( 0 );
408
409 return( ret );
410 }
411
412 while( *p < end )
413 {
414 /*
415 * Extension ::= SEQUENCE {
416 * extnID OBJECT IDENTIFIER,
417 * critical BOOLEAN DEFAULT FALSE,
418 * extnValue OCTET STRING }
419 */
420 x509_buf extn_oid = {0, 0, NULL};
421 int is_critical = 0; /* DEFAULT FALSE */
422 int ext_type = 0;
423
424 if( ( ret = asn1_get_tag( p, end, &len,
425 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200426 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427
428 end_ext_data = *p + len;
429
430 /* Get extension ID */
431 extn_oid.tag = **p;
432
433 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200434 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
436 extn_oid.p = *p;
437 *p += extn_oid.len;
438
439 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200440 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441 POLARSSL_ERR_ASN1_OUT_OF_DATA );
442
443 /* Get optional critical */
444 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
445 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200446 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447
448 /* Data should be octet string type */
449 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
450 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200451 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452
453 end_ext_octet = *p + len;
454
455 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200456 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
458
459 /*
460 * Detect supported extensions
461 */
462 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
463
464 if( ret != 0 )
465 {
466 /* No parser found, skip extension */
467 *p = end_ext_octet;
468
469#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
470 if( is_critical )
471 {
472 /* Data is marked as critical: fail */
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200473 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
475 }
476#endif
477 continue;
478 }
479
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100480 /* Forbid repeated extensions */
481 if( ( crt->ext_types & ext_type ) != 0 )
482 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS );
483
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484 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
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500555 p = polarssl_malloc( len = buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556
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 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500812 crt->next = 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;
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100818 x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200819 crt = crt->next;
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
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100952 if( ( ret = pk_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 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +0000992 if( w_ret == 0 )
993 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994
995 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +0200996 if( hFind == INVALID_HANDLE_VALUE )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
998
999 len = MAX_PATH - len;
1000 do
1001 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001002 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003
1004 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1005 continue;
1006
Paul Bakker9af723c2014-05-01 13:03:14 +02001007 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001008 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001009 p, len - 1,
1010 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001011 if( w_ret == 0 )
1012 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013
Paul Bakkerddf26b42013-09-18 13:46:23 +02001014 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015 if( w_ret < 0 )
1016 ret++;
1017 else
1018 ret += w_ret;
1019 }
1020 while( FindNextFileW( hFind, &file_data ) != 0 );
1021
Paul Bakker66d5d072014-06-17 16:39:18 +02001022 if( GetLastError() != ERROR_NO_MORE_FILES )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1024
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001026#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001027 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001029 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 char entry_name[255];
1031 DIR *dir = opendir( path );
1032
Paul Bakker66d5d072014-06-17 16:39:18 +02001033 if( dir == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1035
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001036#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001037 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1038 return( ret );
1039#endif
1040
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001041 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042 {
Rich Evansfac657f2015-01-30 11:00:01 +00001043 polarssl_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001045 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046 {
1047 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001048 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1049 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050 }
1051
1052 if( !S_ISREG( sb.st_mode ) )
1053 continue;
1054
1055 // Ignore parse errors
1056 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001057 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001058 if( t_ret < 0 )
1059 ret++;
1060 else
1061 ret += t_ret;
1062 }
1063 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001064
1065cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001066#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001067 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1068 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1069#endif
1070
Paul Bakkerbe089b02013-10-14 15:51:50 +02001071#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072
1073 return( ret );
1074}
1075#endif /* POLARSSL_FS_IO */
1076
Paul Bakker6edcd412013-10-29 15:22:54 +01001077#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1078 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079#include <stdarg.h>
1080
1081#if !defined vsnprintf
1082#define vsnprintf _vsnprintf
1083#endif // vsnprintf
1084
1085/*
1086 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1087 * Result value is not size of buffer needed, but -1 if no fit is possible.
1088 *
1089 * This fuction tries to 'fix' this by at least suggesting enlarging the
1090 * size by 20.
1091 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001092static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093{
1094 va_list ap;
1095 int res = -1;
1096
1097 va_start( ap, format );
1098
1099 res = vsnprintf( str, size, format, ap );
1100
1101 va_end( ap );
1102
1103 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +02001104 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105 return( (int) size + 20 );
1106
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001107 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108}
1109
1110#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001111#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112
1113#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1114
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001115#define SAFE_SNPRINTF() \
1116{ \
1117 if( ret == -1 ) \
1118 return( -1 ); \
1119 \
Paul Bakker66d5d072014-06-17 16:39:18 +02001120 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001121 p[n - 1] = '\0'; \
1122 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
1123 } \
1124 \
1125 n -= (unsigned int) ret; \
1126 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127}
1128
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001129static int x509_info_subject_alt_name( char **buf, size_t *size,
1130 const x509_sequence *subject_alt_name )
1131{
1132 size_t i;
1133 size_t n = *size;
1134 char *p = *buf;
1135 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001136 const char *sep = "";
1137 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001138
1139 while( cur != NULL )
1140 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001141 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001142 {
1143 *p = '\0';
1144 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1145 }
1146
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001147 n -= cur->buf.len + sep_len;
1148 for( i = 0; i < sep_len; i++ )
1149 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001150 for( i = 0; i < cur->buf.len; i++ )
1151 *p++ = cur->buf.p[i];
1152
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001153 sep = ", ";
1154 sep_len = 2;
1155
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001156 cur = cur->next;
1157 }
1158
1159 *p = '\0';
1160
1161 *size = n;
1162 *buf = p;
1163
1164 return( 0 );
1165}
1166
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001167#define PRINT_ITEM(i) \
1168 { \
Rich Evansfac657f2015-01-30 11:00:01 +00001169 ret = polarssl_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001170 SAFE_SNPRINTF(); \
1171 sep = ", "; \
1172 }
1173
1174#define CERT_TYPE(type,name) \
1175 if( ns_cert_type & type ) \
1176 PRINT_ITEM( name );
1177
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001178static int x509_info_cert_type( char **buf, size_t *size,
1179 unsigned char ns_cert_type )
1180{
1181 int ret;
1182 size_t n = *size;
1183 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001184 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001185
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001186 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1187 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1188 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1189 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1190 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1191 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1192 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1193 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001194
1195 *size = n;
1196 *buf = p;
1197
1198 return( 0 );
1199}
1200
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001201#define KEY_USAGE(code,name) \
1202 if( key_usage & code ) \
1203 PRINT_ITEM( name );
1204
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001205static int x509_info_key_usage( char **buf, size_t *size,
1206 unsigned char key_usage )
1207{
1208 int ret;
1209 size_t n = *size;
1210 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001211 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001212
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001213 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1214 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1215 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1216 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1217 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1218 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1219 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001220
1221 *size = n;
1222 *buf = p;
1223
1224 return( 0 );
1225}
1226
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001227static int x509_info_ext_key_usage( char **buf, size_t *size,
1228 const x509_sequence *extended_key_usage )
1229{
1230 int ret;
1231 const char *desc;
1232 size_t n = *size;
1233 char *p = *buf;
1234 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001235 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001236
1237 while( cur != NULL )
1238 {
1239 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1240 desc = "???";
1241
Rich Evansfac657f2015-01-30 11:00:01 +00001242 ret = polarssl_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001243 SAFE_SNPRINTF();
1244
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001245 sep = ", ";
1246
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001247 cur = cur->next;
1248 }
1249
1250 *size = n;
1251 *buf = p;
1252
1253 return( 0 );
1254}
1255
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001256/*
1257 * Return an informational string about the certificate.
1258 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001259#define BEFORE_COLON 18
1260#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001261int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001262 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001263{
1264 int ret;
1265 size_t n;
1266 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001267 char key_size_str[BEFORE_COLON];
1268
1269 p = buf;
1270 n = size;
1271
Rich Evansfac657f2015-01-30 11:00:01 +00001272 ret = polarssl_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273 prefix, crt->version );
1274 SAFE_SNPRINTF();
Rich Evansfac657f2015-01-30 11:00:01 +00001275 ret = polarssl_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 prefix );
1277 SAFE_SNPRINTF();
1278
Paul Bakker66d5d072014-06-17 16:39:18 +02001279 ret = x509_serial_gets( p, n, &crt->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001280 SAFE_SNPRINTF();
1281
Rich Evansfac657f2015-01-30 11:00:01 +00001282 ret = polarssl_snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001283 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001284 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001285 SAFE_SNPRINTF();
1286
Rich Evansfac657f2015-01-30 11:00:01 +00001287 ret = polarssl_snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001288 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001289 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001290 SAFE_SNPRINTF();
1291
Rich Evansfac657f2015-01-30 11:00:01 +00001292 ret = polarssl_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001293 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1294 crt->valid_from.year, crt->valid_from.mon,
1295 crt->valid_from.day, crt->valid_from.hour,
1296 crt->valid_from.min, crt->valid_from.sec );
1297 SAFE_SNPRINTF();
1298
Rich Evansfac657f2015-01-30 11:00:01 +00001299 ret = polarssl_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001300 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1301 crt->valid_to.year, crt->valid_to.mon,
1302 crt->valid_to.day, crt->valid_to.hour,
1303 crt->valid_to.min, crt->valid_to.sec );
1304 SAFE_SNPRINTF();
1305
Rich Evansfac657f2015-01-30 11:00:01 +00001306 ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 SAFE_SNPRINTF();
1308
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001309 ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001310 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001311 SAFE_SNPRINTF();
1312
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001313 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1315 pk_get_name( &crt->pk ) ) ) != 0 )
1316 {
1317 return( ret );
1318 }
1319
Rich Evansfac657f2015-01-30 11:00:01 +00001320 ret = polarssl_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001321 (int) pk_get_size( &crt->pk ) );
1322 SAFE_SNPRINTF();
1323
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001324 /*
1325 * Optional extensions
1326 */
1327
1328 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1329 {
Rich Evansfac657f2015-01-30 11:00:01 +00001330 ret = polarssl_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001331 crt->ca_istrue ? "true" : "false" );
1332 SAFE_SNPRINTF();
1333
1334 if( crt->max_pathlen > 0 )
1335 {
Rich Evansfac657f2015-01-30 11:00:01 +00001336 ret = polarssl_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001337 SAFE_SNPRINTF();
1338 }
1339 }
1340
1341 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1342 {
Rich Evansfac657f2015-01-30 11:00:01 +00001343 ret = polarssl_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001344 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001345
1346 if( ( ret = x509_info_subject_alt_name( &p, &n,
1347 &crt->subject_alt_names ) ) != 0 )
1348 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001349 }
1350
1351 if( crt->ext_types & EXT_NS_CERT_TYPE )
1352 {
Rich Evansfac657f2015-01-30 11:00:01 +00001353 ret = polarssl_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001354 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001355
1356 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1357 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001358 }
1359
1360 if( crt->ext_types & EXT_KEY_USAGE )
1361 {
Rich Evansfac657f2015-01-30 11:00:01 +00001362 ret = polarssl_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001363 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001364
1365 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1366 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001367 }
1368
1369 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1370 {
Rich Evansfac657f2015-01-30 11:00:01 +00001371 ret = polarssl_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001372 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001373
1374 if( ( ret = x509_info_ext_key_usage( &p, &n,
1375 &crt->ext_key_usage ) ) != 0 )
1376 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001377 }
1378
Rich Evansfac657f2015-01-30 11:00:01 +00001379 ret = polarssl_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001380 SAFE_SNPRINTF();
1381
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382 return( (int) ( size - n ) );
1383}
1384
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001385#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1386int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1387{
1388 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1389 ( crt->key_usage & usage ) != usage )
1390 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1391
1392 return( 0 );
1393}
1394#endif
1395
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001396#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1397int x509_crt_check_extended_key_usage( const x509_crt *crt,
1398 const char *usage_oid,
1399 size_t usage_len )
1400{
1401 const x509_sequence *cur;
1402
1403 /* Extension is not mandatory, absent means no restriction */
1404 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1405 return( 0 );
1406
1407 /*
1408 * Look for the requested usage (or wildcard ANY) in our list
1409 */
1410 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1411 {
1412 const x509_buf *cur_oid = &cur->buf;
1413
1414 if( cur_oid->len == usage_len &&
1415 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1416 {
1417 return( 0 );
1418 }
1419
1420 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1421 return( 0 );
1422 }
1423
1424 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1425}
Paul Bakker9af723c2014-05-01 13:03:14 +02001426#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001427
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001428#if defined(POLARSSL_X509_CRL_PARSE_C)
1429/*
1430 * Return 1 if the certificate is revoked, or 0 otherwise.
1431 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001432int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433{
1434 const x509_crl_entry *cur = &crl->entry;
1435
1436 while( cur != NULL && cur->serial.len != 0 )
1437 {
1438 if( crt->serial.len == cur->serial.len &&
1439 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1440 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001441 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442 return( 1 );
1443 }
1444
1445 cur = cur->next;
1446 }
1447
1448 return( 0 );
1449}
1450
1451/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001452 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001454static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001455 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001456{
1457 int flags = 0;
1458 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1459 const md_info_t *md_info;
1460
1461 if( ca == NULL )
1462 return( flags );
1463
1464 /*
1465 * TODO: What happens if no CRL is present?
1466 * Suggestion: Revocation state should be unknown if no CRL is present.
1467 * For backwards compatibility this is not yet implemented.
1468 */
1469
1470 while( crl_list != NULL )
1471 {
1472 if( crl_list->version == 0 ||
1473 crl_list->issuer_raw.len != ca->subject_raw.len ||
1474 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1475 crl_list->issuer_raw.len ) != 0 )
1476 {
1477 crl_list = crl_list->next;
1478 continue;
1479 }
1480
1481 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001482 * Check if the CA is configured to sign CRLs
1483 */
1484#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1485 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1486 {
1487 flags |= BADCRL_NOT_TRUSTED;
1488 break;
1489 }
1490#endif
1491
1492 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493 * Check if CRL is correctly signed by the trusted CA
1494 */
1495 md_info = md_info_from_type( crl_list->sig_md );
1496 if( md_info == NULL )
1497 {
1498 /*
1499 * Cannot check 'unknown' hash
1500 */
1501 flags |= BADCRL_NOT_TRUSTED;
1502 break;
1503 }
1504
1505 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1506
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001507 if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1508 crl_list->sig_md, hash, md_info->size,
1509 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510 {
1511 flags |= BADCRL_NOT_TRUSTED;
1512 break;
1513 }
1514
1515 /*
1516 * Check for validity of CRL (Do not drop out)
1517 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001518 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001519 flags |= BADCRL_EXPIRED;
1520
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001521 if( x509_time_future( &crl_list->this_update ) )
1522 flags |= BADCRL_FUTURE;
1523
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524 /*
1525 * Check if certificate is revoked
1526 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001527 if( x509_crt_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001528 {
1529 flags |= BADCERT_REVOKED;
1530 break;
1531 }
1532
1533 crl_list = crl_list->next;
1534 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001535 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536}
1537#endif /* POLARSSL_X509_CRL_PARSE_C */
1538
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001539/*
1540 * Like memcmp, but case-insensitive and always returns -1 if different
1541 */
1542static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543{
1544 size_t i;
1545 unsigned char diff;
1546 const unsigned char *n1 = s1, *n2 = s2;
1547
1548 for( i = 0; i < len; i++ )
1549 {
1550 diff = n1[i] ^ n2[i];
1551
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001552 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553 continue;
1554
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001555 if( diff == 32 &&
1556 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1557 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1558 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001560 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001562 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001563 }
1564
1565 return( 0 );
1566}
1567
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001568/*
1569 * Return 1 if match, 0 if not
1570 * TODO: inverted return value!
1571 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001572static int x509_wildcard_verify( const char *cn, x509_buf *name )
1573{
1574 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001575 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001576
1577 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1578 return( 0 );
1579
Paul Bakker14b16c62014-05-28 11:33:54 +02001580 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581 {
1582 if( cn[i] == '.' )
1583 {
1584 cn_idx = i;
1585 break;
1586 }
1587 }
1588
1589 if( cn_idx == 0 )
1590 return( 0 );
1591
Paul Bakker14b16c62014-05-28 11:33:54 +02001592 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001593 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594 {
1595 return( 1 );
1596 }
1597
1598 return( 0 );
1599}
1600
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001601/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001602 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1603 * variations (but not all).
1604 *
1605 * Return 0 if equal, -1 otherwise.
1606 */
1607static int x509_string_cmp( const x509_buf *a, const x509_buf *b )
1608{
1609 if( a->tag == b->tag &&
1610 a->len == b->len &&
1611 memcmp( a->p, b->p, b->len ) == 0 )
1612 {
1613 return( 0 );
1614 }
1615
1616 if( ( a->tag == ASN1_UTF8_STRING || a->tag == ASN1_PRINTABLE_STRING ) &&
1617 ( b->tag == ASN1_UTF8_STRING || b->tag == ASN1_PRINTABLE_STRING ) &&
1618 a->len == b->len &&
1619 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1620 {
1621 return( 0 );
1622 }
1623
1624 return( -1 );
1625}
1626
1627/*
1628 * Compare two X.509 Names (aka rdnSequence).
1629 *
1630 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1631 * we sometimes return unequal when the full algorithm would return equal,
1632 * but never the other way. (In particular, we don't do Unicode normalisation
1633 * or space folding.)
1634 *
1635 * Return 0 if equal, -1 otherwise.
1636 */
1637static int x509_name_cmp( const x509_name *a, const x509_name *b )
1638{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001639 /* Avoid recursion, it might not be optimised by the compiler */
1640 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001641 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001642 if( a == NULL || b == NULL )
1643 return( -1 );
1644
1645 /* type */
1646 if( a->oid.tag != b->oid.tag ||
1647 a->oid.len != b->oid.len ||
1648 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1649 {
1650 return( -1 );
1651 }
1652
1653 /* value */
1654 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1655 return( -1 );
1656
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001657 /* structure of the list of sets */
1658 if( a->next_merged != b->next_merged )
1659 return( -1 );
1660
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001661 a = a->next;
1662 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001663 }
1664
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001665 /* a == NULL == b */
1666 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001667}
1668
1669/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001670 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1671 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001672 *
1673 * top means parent is a locally-trusted certificate
1674 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001675 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001676static int x509_crt_check_parent( const x509_crt *child,
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001677 const x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001678 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001679{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001680 int need_ca_bit;
1681
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001682 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001683 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001684 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001685
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001686 /* Parent must have the basicConstraints CA bit set as a general rule */
1687 need_ca_bit = 1;
1688
1689 /* Exception: v1/v2 certificates that are locally trusted. */
1690 if( top && parent->version < 3 )
1691 need_ca_bit = 0;
1692
1693 /* Exception: self-signed end-entity certs that are locally trusted. */
1694 if( top && bottom &&
1695 child->raw.len == parent->raw.len &&
1696 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1697 {
1698 need_ca_bit = 0;
1699 }
1700
1701 if( need_ca_bit && ! parent->ca_istrue )
1702 return( -1 );
1703
1704#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1705 if( need_ca_bit &&
1706 x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001707 {
1708 return( -1 );
1709 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001710#endif
1711
1712 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001713}
1714
Paul Bakkerddf26b42013-09-18 13:46:23 +02001715static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001716 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001717 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001718 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001719 void *p_vrfy )
1720{
1721 int ret;
1722 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1723 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1724 const md_info_t *md_info;
1725
Paul Bakker86d0c192013-09-18 11:11:02 +02001726 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001727 *flags |= BADCERT_EXPIRED;
1728
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001729 if( x509_time_future( &child->valid_from ) )
1730 *flags |= BADCERT_FUTURE;
1731
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001732 /*
1733 * Child is the top of the chain. Check against the trust_ca list.
1734 */
1735 *flags |= BADCERT_NOT_TRUSTED;
1736
1737 md_info = md_info_from_type( child->sig_md );
1738 if( md_info == NULL )
1739 {
1740 /*
1741 * Cannot check 'unknown', no need to try any CA
1742 */
1743 trust_ca = NULL;
1744 }
1745 else
1746 md( md_info, child->tbs.p, child->tbs.len, hash );
1747
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001748 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001749 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001750 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001751 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001752
1753 /*
1754 * Reduce path_len to check against if top of the chain is
1755 * the same as the trusted CA
1756 */
1757 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1758 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1759 child->issuer_raw.len ) == 0 )
1760 {
1761 check_path_cnt--;
1762 }
1763
1764 if( trust_ca->max_pathlen > 0 &&
1765 trust_ca->max_pathlen < check_path_cnt )
1766 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001767 continue;
1768 }
1769
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001770 if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1771 child->sig_md, hash, md_info->size,
1772 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001773 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001774 continue;
1775 }
1776
1777 /*
1778 * Top of chain is signed by a trusted CA
1779 */
1780 *flags &= ~BADCERT_NOT_TRUSTED;
1781 break;
1782 }
1783
1784 /*
1785 * If top of chain is not the same as the trusted CA send a verify request
1786 * to the callback for any issues with validity and CRL presence for the
1787 * trusted CA certificate.
1788 */
1789 if( trust_ca != NULL &&
1790 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1791 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1792 child->issuer_raw.len ) != 0 ) )
1793 {
1794#if defined(POLARSSL_X509_CRL_PARSE_C)
1795 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001796 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001797#else
1798 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001799#endif
1800
Paul Bakker86d0c192013-09-18 11:11:02 +02001801 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001802 ca_flags |= BADCERT_EXPIRED;
1803
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001804 if( x509_time_future( &trust_ca->valid_from ) )
1805 ca_flags |= BADCERT_FUTURE;
1806
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001807 if( NULL != f_vrfy )
1808 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001809 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1810 &ca_flags ) ) != 0 )
1811 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001812 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001813 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001814 }
1815 }
1816
1817 /* Call callback on top cert */
1818 if( NULL != f_vrfy )
1819 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001820 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001821 return( ret );
1822 }
1823
1824 *flags |= ca_flags;
1825
1826 return( 0 );
1827}
1828
Paul Bakkerddf26b42013-09-18 13:46:23 +02001829static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001830 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001831 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001832 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001833 void *p_vrfy )
1834{
1835 int ret;
1836 int parent_flags = 0;
1837 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001838 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001839 const md_info_t *md_info;
1840
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001841 /* path_cnt is 0 for the first intermediate CA */
1842 if( 1 + path_cnt > POLARSSL_X509_MAX_INTERMEDIATE_CA )
1843 {
1844 *flags |= BADCERT_NOT_TRUSTED;
1845 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1846 }
1847
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001848 if( x509_time_expired( &child->valid_to ) )
1849 *flags |= BADCERT_EXPIRED;
1850
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001851 if( x509_time_future( &child->valid_from ) )
1852 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001853
1854 md_info = md_info_from_type( child->sig_md );
1855 if( md_info == NULL )
1856 {
1857 /*
1858 * Cannot check 'unknown' hash
1859 */
1860 *flags |= BADCERT_NOT_TRUSTED;
1861 }
1862 else
1863 {
1864 md( md_info, child->tbs.p, child->tbs.len, hash );
1865
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001866 if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1867 child->sig_md, hash, md_info->size,
1868 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001869 {
1870 *flags |= BADCERT_NOT_TRUSTED;
1871 }
1872 }
1873
1874#if defined(POLARSSL_X509_CRL_PARSE_C)
1875 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001876 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001877#endif
1878
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001879 /* Look for a grandparent upwards the chain */
1880 for( grandparent = parent->next;
1881 grandparent != NULL;
1882 grandparent = grandparent->next )
1883 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001884 if( x509_crt_check_parent( parent, grandparent,
1885 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001886 break;
1887 }
1888
1889 /* Is our parent part of the chain or at the top? */
1890 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001891 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001892 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1893 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001894 if( ret != 0 )
1895 return( ret );
1896 }
1897 else
1898 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001899 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1900 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001901 if( ret != 0 )
1902 return( ret );
1903 }
1904
1905 /* child is verified to be a child of the parent, call verify callback */
1906 if( NULL != f_vrfy )
1907 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1908 return( ret );
1909
1910 *flags |= parent_flags;
1911
1912 return( 0 );
1913}
1914
1915/*
1916 * Verify the certificate validity
1917 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001918int x509_crt_verify( x509_crt *crt,
1919 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001920 x509_crl *ca_crl,
1921 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001922 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001923 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001924{
1925 size_t cn_len;
1926 int ret;
1927 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001928 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001929 x509_name *name;
1930 x509_sequence *cur = NULL;
1931
1932 *flags = 0;
1933
1934 if( cn != NULL )
1935 {
1936 name = &crt->subject;
1937 cn_len = strlen( cn );
1938
1939 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1940 {
1941 cur = &crt->subject_alt_names;
1942
1943 while( cur != NULL )
1944 {
1945 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001946 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001947 break;
1948
1949 if( cur->buf.len > 2 &&
1950 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1951 x509_wildcard_verify( cn, &cur->buf ) )
1952 break;
1953
1954 cur = cur->next;
1955 }
1956
1957 if( cur == NULL )
1958 *flags |= BADCERT_CN_MISMATCH;
1959 }
1960 else
1961 {
1962 while( name != NULL )
1963 {
1964 if( OID_CMP( OID_AT_CN, &name->oid ) )
1965 {
1966 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001967 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001968 break;
1969
1970 if( name->val.len > 2 &&
1971 memcmp( name->val.p, "*.", 2 ) == 0 &&
1972 x509_wildcard_verify( cn, &name->val ) )
1973 break;
1974 }
1975
1976 name = name->next;
1977 }
1978
1979 if( name == NULL )
1980 *flags |= BADCERT_CN_MISMATCH;
1981 }
1982 }
1983
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001984 /* Look for a parent upwards the chain */
1985 for( parent = crt->next; parent != NULL; parent = parent->next )
1986 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001987 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001988 break;
1989 }
1990
1991 /* Are we part of the chain or at the top? */
1992 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001993 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001994 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1995 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001996 if( ret != 0 )
1997 return( ret );
1998 }
1999 else
2000 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002001 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
2002 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002003 if( ret != 0 )
2004 return( ret );
2005 }
2006
2007 if( *flags != 0 )
2008 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
2009
2010 return( 0 );
2011}
2012
2013/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002014 * Initialize a certificate chain
2015 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002016void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002017{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002018 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002019}
2020
2021/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002022 * Unallocate all certificate data
2023 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002024void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002025{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002026 x509_crt *cert_cur = crt;
2027 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002028 x509_name *name_cur;
2029 x509_name *name_prv;
2030 x509_sequence *seq_cur;
2031 x509_sequence *seq_prv;
2032
2033 if( crt == NULL )
2034 return;
2035
2036 do
2037 {
2038 pk_free( &cert_cur->pk );
2039
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +02002040#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002041 polarssl_free( cert_cur->sig_opts );
2042#endif
2043
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002044 name_cur = cert_cur->issuer.next;
2045 while( name_cur != NULL )
2046 {
2047 name_prv = name_cur;
2048 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002049 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002050 polarssl_free( name_prv );
2051 }
2052
2053 name_cur = cert_cur->subject.next;
2054 while( name_cur != NULL )
2055 {
2056 name_prv = name_cur;
2057 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002058 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002059 polarssl_free( name_prv );
2060 }
2061
2062 seq_cur = cert_cur->ext_key_usage.next;
2063 while( seq_cur != NULL )
2064 {
2065 seq_prv = seq_cur;
2066 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002067 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002068 polarssl_free( seq_prv );
2069 }
2070
2071 seq_cur = cert_cur->subject_alt_names.next;
2072 while( seq_cur != NULL )
2073 {
2074 seq_prv = seq_cur;
2075 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002076 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002077 polarssl_free( seq_prv );
2078 }
2079
2080 if( cert_cur->raw.p != NULL )
2081 {
Paul Bakker34617722014-06-13 17:20:13 +02002082 polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002083 polarssl_free( cert_cur->raw.p );
2084 }
2085
2086 cert_cur = cert_cur->next;
2087 }
2088 while( cert_cur != NULL );
2089
2090 cert_cur = crt;
2091 do
2092 {
2093 cert_prv = cert_cur;
2094 cert_cur = cert_cur->next;
2095
Paul Bakker34617722014-06-13 17:20:13 +02002096 polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002097 if( cert_prv != crt )
2098 polarssl_free( cert_prv );
2099 }
2100 while( cert_cur != NULL );
2101}
2102
Paul Bakker9af723c2014-05-01 13:03:14 +02002103#endif /* POLARSSL_X509_CRT_PARSE_C */