blob: cb63b13d56b24486ae97fd754de1364e2fa34406 [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)
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"
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)
48#include "polarssl/pem.h"
49#endif
50
Paul Bakker7dc4c442014-02-01 22:50:26 +010051#if defined(POLARSSL_PLATFORM_C)
52#include "polarssl/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)
61#include "polarssl/threading.h"
62#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 ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200763 ( sig_params1.len != 0 &&
764 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765 {
766 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200767 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 }
769
770 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
771 {
772 x509_crt_free( crt );
773 return( ret );
774 }
775
776 if( p != end )
777 {
778 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200779 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
781 }
782
783 return( 0 );
784}
785
786/*
787 * Parse one X.509 certificate in DER format from a buffer and add them to a
788 * chained list
789 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200790int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200791 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792{
793 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200794 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200795
796 /*
797 * Check for valid input
798 */
799 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200800 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801
802 while( crt->version != 0 && crt->next != NULL )
803 {
804 prev = crt;
805 crt = crt->next;
806 }
807
808 /*
809 * Add new certificate on the end of the chain if needed.
810 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200811 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200812 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500813 crt->next = polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200814
815 if( crt->next == NULL )
816 return( POLARSSL_ERR_X509_MALLOC_FAILED );
817
818 prev = crt;
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100819 x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821 }
822
Paul Bakkerddf26b42013-09-18 13:46:23 +0200823 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824 {
825 if( prev )
826 prev->next = NULL;
827
828 if( crt != chain )
829 polarssl_free( crt );
830
831 return( ret );
832 }
833
834 return( 0 );
835}
836
837/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200838 * Parse one or more PEM certificates from a buffer and add them to the chained
839 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200840 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200841int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200842{
843 int success = 0, first_error = 0, total_failed = 0;
844 int buf_format = X509_FORMAT_DER;
845
846 /*
847 * Check for valid input
848 */
849 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200850 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851
852 /*
853 * Determine buffer content. Buffer contains either one DER certificate or
854 * one or more PEM certificates.
855 */
856#if defined(POLARSSL_PEM_PARSE_C)
857 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
858 buf_format = X509_FORMAT_PEM;
859#endif
860
861 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200862 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200863
864#if defined(POLARSSL_PEM_PARSE_C)
865 if( buf_format == X509_FORMAT_PEM )
866 {
867 int ret;
868 pem_context pem;
869
870 while( buflen > 0 )
871 {
872 size_t use_len;
873 pem_init( &pem );
874
875 ret = pem_read_buffer( &pem,
876 "-----BEGIN CERTIFICATE-----",
877 "-----END CERTIFICATE-----",
878 buf, NULL, 0, &use_len );
879
880 if( ret == 0 )
881 {
882 /*
883 * Was PEM encoded
884 */
885 buflen -= use_len;
886 buf += use_len;
887 }
888 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
889 {
890 return( ret );
891 }
892 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
893 {
894 pem_free( &pem );
895
896 /*
897 * PEM header and footer were found
898 */
899 buflen -= use_len;
900 buf += use_len;
901
902 if( first_error == 0 )
903 first_error = ret;
904
Paul Bakker5a5fa922014-09-26 14:53:04 +0200905 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906 continue;
907 }
908 else
909 break;
910
Paul Bakkerddf26b42013-09-18 13:46:23 +0200911 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912
913 pem_free( &pem );
914
915 if( ret != 0 )
916 {
917 /*
918 * Quit parsing on a memory error
919 */
920 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
921 return( ret );
922
923 if( first_error == 0 )
924 first_error = ret;
925
926 total_failed++;
927 continue;
928 }
929
930 success = 1;
931 }
932 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200933#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934
935 if( success )
936 return( total_failed );
937 else if( first_error )
938 return( first_error );
939 else
940 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
941}
942
943#if defined(POLARSSL_FS_IO)
944/*
945 * Load one or more certificates and add them to the chained list
946 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200947int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948{
949 int ret;
950 size_t n;
951 unsigned char *buf;
952
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100953 if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954 return( ret );
955
Paul Bakkerddf26b42013-09-18 13:46:23 +0200956 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957
Paul Bakker34617722014-06-13 17:20:13 +0200958 polarssl_zeroize( buf, n + 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 polarssl_free( buf );
960
961 return( ret );
962}
963
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100964#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100965static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
966#endif
967
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200968int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969{
970 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100971#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972 int w_ret;
973 WCHAR szDir[MAX_PATH];
974 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200975 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200976 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977
Paul Bakker9af723c2014-05-01 13:03:14 +0200978 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979 HANDLE hFind;
980
981 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200982 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983
Paul Bakker9af723c2014-05-01 13:03:14 +0200984 memset( szDir, 0, sizeof(szDir) );
985 memset( filename, 0, MAX_PATH );
986 memcpy( filename, path, len );
987 filename[len++] = '\\';
988 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989 filename[len++] = '*';
990
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200991 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
992 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +0000993 if( w_ret == 0 )
994 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995
996 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +0200997 if( hFind == INVALID_HANDLE_VALUE )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
999
1000 len = MAX_PATH - len;
1001 do
1002 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001003 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004
1005 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1006 continue;
1007
Paul Bakker9af723c2014-05-01 13:03:14 +02001008 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001009 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001010 p, len - 1,
1011 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001012 if( w_ret == 0 )
1013 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014
Paul Bakkerddf26b42013-09-18 13:46:23 +02001015 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 if( w_ret < 0 )
1017 ret++;
1018 else
1019 ret += w_ret;
1020 }
1021 while( FindNextFileW( hFind, &file_data ) != 0 );
1022
Paul Bakker66d5d072014-06-17 16:39:18 +02001023 if( GetLastError() != ERROR_NO_MORE_FILES )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1025
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001027#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001028 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001030 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 char entry_name[255];
1032 DIR *dir = opendir( path );
1033
Paul Bakker66d5d072014-06-17 16:39:18 +02001034 if( dir == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1036
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001037#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001038 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1039 return( ret );
1040#endif
1041
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001042 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 {
Rich Evansfac657f2015-01-30 11:00:01 +00001044 polarssl_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001046 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001047 {
1048 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001049 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1050 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051 }
1052
1053 if( !S_ISREG( sb.st_mode ) )
1054 continue;
1055
1056 // Ignore parse errors
1057 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001058 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059 if( t_ret < 0 )
1060 ret++;
1061 else
1062 ret += t_ret;
1063 }
1064 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001065
1066cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001067#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001068 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1069 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1070#endif
1071
Paul Bakkerbe089b02013-10-14 15:51:50 +02001072#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001073
1074 return( ret );
1075}
1076#endif /* POLARSSL_FS_IO */
1077
Paul Bakker6edcd412013-10-29 15:22:54 +01001078#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1079 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080#include <stdarg.h>
1081
1082#if !defined vsnprintf
1083#define vsnprintf _vsnprintf
1084#endif // vsnprintf
1085
1086/*
1087 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1088 * Result value is not size of buffer needed, but -1 if no fit is possible.
1089 *
1090 * This fuction tries to 'fix' this by at least suggesting enlarging the
1091 * size by 20.
1092 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001093static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094{
1095 va_list ap;
1096 int res = -1;
1097
1098 va_start( ap, format );
1099
1100 res = vsnprintf( str, size, format, ap );
1101
1102 va_end( ap );
1103
1104 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +02001105 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106 return( (int) size + 20 );
1107
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001108 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109}
1110
1111#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001112#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113
1114#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1115
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001116#define SAFE_SNPRINTF() \
1117{ \
1118 if( ret == -1 ) \
1119 return( -1 ); \
1120 \
Paul Bakker66d5d072014-06-17 16:39:18 +02001121 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001122 p[n - 1] = '\0'; \
1123 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
1124 } \
1125 \
1126 n -= (unsigned int) ret; \
1127 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128}
1129
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001130static int x509_info_subject_alt_name( char **buf, size_t *size,
1131 const x509_sequence *subject_alt_name )
1132{
1133 size_t i;
1134 size_t n = *size;
1135 char *p = *buf;
1136 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001137 const char *sep = "";
1138 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001139
1140 while( cur != NULL )
1141 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001142 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001143 {
1144 *p = '\0';
1145 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1146 }
1147
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001148 n -= cur->buf.len + sep_len;
1149 for( i = 0; i < sep_len; i++ )
1150 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001151 for( i = 0; i < cur->buf.len; i++ )
1152 *p++ = cur->buf.p[i];
1153
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001154 sep = ", ";
1155 sep_len = 2;
1156
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001157 cur = cur->next;
1158 }
1159
1160 *p = '\0';
1161
1162 *size = n;
1163 *buf = p;
1164
1165 return( 0 );
1166}
1167
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001168#define PRINT_ITEM(i) \
1169 { \
Rich Evansfac657f2015-01-30 11:00:01 +00001170 ret = polarssl_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001171 SAFE_SNPRINTF(); \
1172 sep = ", "; \
1173 }
1174
1175#define CERT_TYPE(type,name) \
1176 if( ns_cert_type & type ) \
1177 PRINT_ITEM( name );
1178
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001179static int x509_info_cert_type( char **buf, size_t *size,
1180 unsigned char ns_cert_type )
1181{
1182 int ret;
1183 size_t n = *size;
1184 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001185 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001186
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001187 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1188 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1189 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1190 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1191 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1192 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1193 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1194 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001195
1196 *size = n;
1197 *buf = p;
1198
1199 return( 0 );
1200}
1201
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001202#define KEY_USAGE(code,name) \
1203 if( key_usage & code ) \
1204 PRINT_ITEM( name );
1205
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001206static int x509_info_key_usage( char **buf, size_t *size,
1207 unsigned char key_usage )
1208{
1209 int ret;
1210 size_t n = *size;
1211 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001212 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001213
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001214 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1215 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1216 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1217 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1218 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1219 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1220 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001221
1222 *size = n;
1223 *buf = p;
1224
1225 return( 0 );
1226}
1227
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001228static int x509_info_ext_key_usage( char **buf, size_t *size,
1229 const x509_sequence *extended_key_usage )
1230{
1231 int ret;
1232 const char *desc;
1233 size_t n = *size;
1234 char *p = *buf;
1235 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001236 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001237
1238 while( cur != NULL )
1239 {
1240 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1241 desc = "???";
1242
Rich Evansfac657f2015-01-30 11:00:01 +00001243 ret = polarssl_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001244 SAFE_SNPRINTF();
1245
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001246 sep = ", ";
1247
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001248 cur = cur->next;
1249 }
1250
1251 *size = n;
1252 *buf = p;
1253
1254 return( 0 );
1255}
1256
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001257/*
1258 * Return an informational string about the certificate.
1259 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001260#define BEFORE_COLON 18
1261#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001262int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001263 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001264{
1265 int ret;
1266 size_t n;
1267 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001268 char key_size_str[BEFORE_COLON];
1269
1270 p = buf;
1271 n = size;
1272
Rich Evansfac657f2015-01-30 11:00:01 +00001273 ret = polarssl_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001274 prefix, crt->version );
1275 SAFE_SNPRINTF();
Rich Evansfac657f2015-01-30 11:00:01 +00001276 ret = polarssl_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001277 prefix );
1278 SAFE_SNPRINTF();
1279
Paul Bakker66d5d072014-06-17 16:39:18 +02001280 ret = x509_serial_gets( p, n, &crt->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 SAFE_SNPRINTF();
1282
Rich Evansfac657f2015-01-30 11:00:01 +00001283 ret = polarssl_snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001284 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001285 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286 SAFE_SNPRINTF();
1287
Rich Evansfac657f2015-01-30 11:00:01 +00001288 ret = polarssl_snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001289 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001290 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001291 SAFE_SNPRINTF();
1292
Rich Evansfac657f2015-01-30 11:00:01 +00001293 ret = polarssl_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001294 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1295 crt->valid_from.year, crt->valid_from.mon,
1296 crt->valid_from.day, crt->valid_from.hour,
1297 crt->valid_from.min, crt->valid_from.sec );
1298 SAFE_SNPRINTF();
1299
Rich Evansfac657f2015-01-30 11:00:01 +00001300 ret = polarssl_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001301 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1302 crt->valid_to.year, crt->valid_to.mon,
1303 crt->valid_to.day, crt->valid_to.hour,
1304 crt->valid_to.min, crt->valid_to.sec );
1305 SAFE_SNPRINTF();
1306
Rich Evansfac657f2015-01-30 11:00:01 +00001307 ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001308 SAFE_SNPRINTF();
1309
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001310 ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001311 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001312 SAFE_SNPRINTF();
1313
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001314 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001315 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1316 pk_get_name( &crt->pk ) ) ) != 0 )
1317 {
1318 return( ret );
1319 }
1320
Rich Evansfac657f2015-01-30 11:00:01 +00001321 ret = polarssl_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001322 (int) pk_get_size( &crt->pk ) );
1323 SAFE_SNPRINTF();
1324
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001325 /*
1326 * Optional extensions
1327 */
1328
1329 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1330 {
Rich Evansfac657f2015-01-30 11:00:01 +00001331 ret = polarssl_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001332 crt->ca_istrue ? "true" : "false" );
1333 SAFE_SNPRINTF();
1334
1335 if( crt->max_pathlen > 0 )
1336 {
Rich Evansfac657f2015-01-30 11:00:01 +00001337 ret = polarssl_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001338 SAFE_SNPRINTF();
1339 }
1340 }
1341
1342 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1343 {
Rich Evansfac657f2015-01-30 11:00:01 +00001344 ret = polarssl_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001345 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001346
1347 if( ( ret = x509_info_subject_alt_name( &p, &n,
1348 &crt->subject_alt_names ) ) != 0 )
1349 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001350 }
1351
1352 if( crt->ext_types & EXT_NS_CERT_TYPE )
1353 {
Rich Evansfac657f2015-01-30 11:00:01 +00001354 ret = polarssl_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001355 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001356
1357 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1358 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001359 }
1360
1361 if( crt->ext_types & EXT_KEY_USAGE )
1362 {
Rich Evansfac657f2015-01-30 11:00:01 +00001363 ret = polarssl_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001364 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001365
1366 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1367 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001368 }
1369
1370 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1371 {
Rich Evansfac657f2015-01-30 11:00:01 +00001372 ret = polarssl_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001373 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001374
1375 if( ( ret = x509_info_ext_key_usage( &p, &n,
1376 &crt->ext_key_usage ) ) != 0 )
1377 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001378 }
1379
Rich Evansfac657f2015-01-30 11:00:01 +00001380 ret = polarssl_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001381 SAFE_SNPRINTF();
1382
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383 return( (int) ( size - n ) );
1384}
1385
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +02001386struct x509_crt_verify_string {
1387 int code;
1388 const char *string;
1389};
1390
1391static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
1392 { BADCERT_EXPIRED, "The certificate validity has expired" },
1393 { BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1394 { BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1395 { BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1396 { BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1397 { BADCRL_EXPIRED, "The CRL is expired" },
1398 { BADCERT_MISSING, "Certificate was missing" },
1399 { BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1400 { BADCERT_OTHER, "Other reason (can be used by verify callback)" },
1401 { BADCERT_FUTURE, "The certificate validity starts in the future" },
1402 { BADCRL_FUTURE, "The CRL is from the future" },
1403 { BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1404 { BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1405 { BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
1406 { 0, NULL }
1407};
1408
1409int x509_crt_verify_info( char *buf, size_t size, const char *prefix,
1410 int flags )
1411{
1412 int ret;
1413 const struct x509_crt_verify_string *cur;
1414 char *p = buf;
1415 size_t n = size;
1416
1417 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1418 {
1419 if( ( flags & cur->code ) == 0 )
1420 continue;
1421
1422 ret = polarssl_snprintf( p, n, "%s%s\n", prefix, cur->string );
1423 SAFE_SNPRINTF();
1424 flags ^= cur->code;
1425 }
1426
1427 if( flags != 0 )
1428 {
1429 ret = polarssl_snprintf( p, n, "%sUnknown reason "
1430 "(this should not happen)\n", prefix );
1431 SAFE_SNPRINTF();
1432 }
1433
1434 return( (int) ( size - n ) );
1435}
1436
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001437#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1438int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1439{
1440 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1441 ( crt->key_usage & usage ) != usage )
1442 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1443
1444 return( 0 );
1445}
1446#endif
1447
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001448#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1449int x509_crt_check_extended_key_usage( const x509_crt *crt,
1450 const char *usage_oid,
1451 size_t usage_len )
1452{
1453 const x509_sequence *cur;
1454
1455 /* Extension is not mandatory, absent means no restriction */
1456 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1457 return( 0 );
1458
1459 /*
1460 * Look for the requested usage (or wildcard ANY) in our list
1461 */
1462 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1463 {
1464 const x509_buf *cur_oid = &cur->buf;
1465
1466 if( cur_oid->len == usage_len &&
1467 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1468 {
1469 return( 0 );
1470 }
1471
1472 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1473 return( 0 );
1474 }
1475
1476 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1477}
Paul Bakker9af723c2014-05-01 13:03:14 +02001478#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001479
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480#if defined(POLARSSL_X509_CRL_PARSE_C)
1481/*
1482 * Return 1 if the certificate is revoked, or 0 otherwise.
1483 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001484int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485{
1486 const x509_crl_entry *cur = &crl->entry;
1487
1488 while( cur != NULL && cur->serial.len != 0 )
1489 {
1490 if( crt->serial.len == cur->serial.len &&
1491 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1492 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001493 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001494 return( 1 );
1495 }
1496
1497 cur = cur->next;
1498 }
1499
1500 return( 0 );
1501}
1502
1503/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001504 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001505 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001506static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001507 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508{
1509 int flags = 0;
1510 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1511 const md_info_t *md_info;
1512
1513 if( ca == NULL )
1514 return( flags );
1515
1516 /*
1517 * TODO: What happens if no CRL is present?
1518 * Suggestion: Revocation state should be unknown if no CRL is present.
1519 * For backwards compatibility this is not yet implemented.
1520 */
1521
1522 while( crl_list != NULL )
1523 {
1524 if( crl_list->version == 0 ||
1525 crl_list->issuer_raw.len != ca->subject_raw.len ||
1526 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1527 crl_list->issuer_raw.len ) != 0 )
1528 {
1529 crl_list = crl_list->next;
1530 continue;
1531 }
1532
1533 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001534 * Check if the CA is configured to sign CRLs
1535 */
1536#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1537 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1538 {
1539 flags |= BADCRL_NOT_TRUSTED;
1540 break;
1541 }
1542#endif
1543
1544 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545 * Check if CRL is correctly signed by the trusted CA
1546 */
1547 md_info = md_info_from_type( crl_list->sig_md );
1548 if( md_info == NULL )
1549 {
1550 /*
1551 * Cannot check 'unknown' hash
1552 */
1553 flags |= BADCRL_NOT_TRUSTED;
1554 break;
1555 }
1556
1557 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1558
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001559 if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1560 crl_list->sig_md, hash, md_info->size,
1561 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562 {
1563 flags |= BADCRL_NOT_TRUSTED;
1564 break;
1565 }
1566
1567 /*
1568 * Check for validity of CRL (Do not drop out)
1569 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001570 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571 flags |= BADCRL_EXPIRED;
1572
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001573 if( x509_time_future( &crl_list->this_update ) )
1574 flags |= BADCRL_FUTURE;
1575
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001576 /*
1577 * Check if certificate is revoked
1578 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001579 if( x509_crt_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580 {
1581 flags |= BADCERT_REVOKED;
1582 break;
1583 }
1584
1585 crl_list = crl_list->next;
1586 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001587 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001588}
1589#endif /* POLARSSL_X509_CRL_PARSE_C */
1590
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001591/*
1592 * Like memcmp, but case-insensitive and always returns -1 if different
1593 */
1594static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001595{
1596 size_t i;
1597 unsigned char diff;
1598 const unsigned char *n1 = s1, *n2 = s2;
1599
1600 for( i = 0; i < len; i++ )
1601 {
1602 diff = n1[i] ^ n2[i];
1603
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001604 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001605 continue;
1606
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001607 if( diff == 32 &&
1608 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1609 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1610 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001612 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001613
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001614 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615 }
1616
1617 return( 0 );
1618}
1619
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001620/*
1621 * Return 1 if match, 0 if not
1622 * TODO: inverted return value!
1623 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624static int x509_wildcard_verify( const char *cn, x509_buf *name )
1625{
1626 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001627 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628
1629 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1630 return( 0 );
1631
Paul Bakker14b16c62014-05-28 11:33:54 +02001632 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633 {
1634 if( cn[i] == '.' )
1635 {
1636 cn_idx = i;
1637 break;
1638 }
1639 }
1640
1641 if( cn_idx == 0 )
1642 return( 0 );
1643
Paul Bakker14b16c62014-05-28 11:33:54 +02001644 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001645 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646 {
1647 return( 1 );
1648 }
1649
1650 return( 0 );
1651}
1652
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001653/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001654 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1655 * variations (but not all).
1656 *
1657 * Return 0 if equal, -1 otherwise.
1658 */
1659static int x509_string_cmp( const x509_buf *a, const x509_buf *b )
1660{
1661 if( a->tag == b->tag &&
1662 a->len == b->len &&
1663 memcmp( a->p, b->p, b->len ) == 0 )
1664 {
1665 return( 0 );
1666 }
1667
1668 if( ( a->tag == ASN1_UTF8_STRING || a->tag == ASN1_PRINTABLE_STRING ) &&
1669 ( b->tag == ASN1_UTF8_STRING || b->tag == ASN1_PRINTABLE_STRING ) &&
1670 a->len == b->len &&
1671 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1672 {
1673 return( 0 );
1674 }
1675
1676 return( -1 );
1677}
1678
1679/*
1680 * Compare two X.509 Names (aka rdnSequence).
1681 *
1682 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1683 * we sometimes return unequal when the full algorithm would return equal,
1684 * but never the other way. (In particular, we don't do Unicode normalisation
1685 * or space folding.)
1686 *
1687 * Return 0 if equal, -1 otherwise.
1688 */
1689static int x509_name_cmp( const x509_name *a, const x509_name *b )
1690{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001691 /* Avoid recursion, it might not be optimised by the compiler */
1692 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001693 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001694 if( a == NULL || b == NULL )
1695 return( -1 );
1696
1697 /* type */
1698 if( a->oid.tag != b->oid.tag ||
1699 a->oid.len != b->oid.len ||
1700 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1701 {
1702 return( -1 );
1703 }
1704
1705 /* value */
1706 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1707 return( -1 );
1708
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001709 /* structure of the list of sets */
1710 if( a->next_merged != b->next_merged )
1711 return( -1 );
1712
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001713 a = a->next;
1714 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001715 }
1716
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001717 /* a == NULL == b */
1718 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001719}
1720
1721/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001722 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1723 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001724 *
1725 * top means parent is a locally-trusted certificate
1726 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001727 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001728static int x509_crt_check_parent( const x509_crt *child,
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001729 const x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001730 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001731{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001732 int need_ca_bit;
1733
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001734 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001735 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001736 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001737
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001738 /* Parent must have the basicConstraints CA bit set as a general rule */
1739 need_ca_bit = 1;
1740
1741 /* Exception: v1/v2 certificates that are locally trusted. */
1742 if( top && parent->version < 3 )
1743 need_ca_bit = 0;
1744
1745 /* Exception: self-signed end-entity certs that are locally trusted. */
1746 if( top && bottom &&
1747 child->raw.len == parent->raw.len &&
1748 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1749 {
1750 need_ca_bit = 0;
1751 }
1752
1753 if( need_ca_bit && ! parent->ca_istrue )
1754 return( -1 );
1755
1756#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1757 if( need_ca_bit &&
1758 x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001759 {
1760 return( -1 );
1761 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001762#endif
1763
1764 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001765}
1766
Paul Bakkerddf26b42013-09-18 13:46:23 +02001767static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001768 x509_crt *child, x509_crt *trust_ca,
Janos Follath92ac0592015-10-12 09:02:20 +02001769 x509_crl *ca_crl,
1770 int path_cnt, int self_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001771 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001772 void *p_vrfy )
1773{
1774 int ret;
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001775 int ca_flags = 0, check_path_cnt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001776 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1777 const md_info_t *md_info;
1778
Paul Bakker86d0c192013-09-18 11:11:02 +02001779 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001780 *flags |= BADCERT_EXPIRED;
1781
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001782 if( x509_time_future( &child->valid_from ) )
1783 *flags |= BADCERT_FUTURE;
1784
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001785 /*
1786 * Child is the top of the chain. Check against the trust_ca list.
1787 */
1788 *flags |= BADCERT_NOT_TRUSTED;
1789
1790 md_info = md_info_from_type( child->sig_md );
1791 if( md_info == NULL )
1792 {
1793 /*
1794 * Cannot check 'unknown', no need to try any CA
1795 */
1796 trust_ca = NULL;
1797 }
1798 else
1799 md( md_info, child->tbs.p, child->tbs.len, hash );
1800
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001801 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001802 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001803 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001804 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001805
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001806 check_path_cnt = path_cnt + 1;
1807
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001809 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001810 * the same as the trusted CA
1811 */
1812 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1813 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1814 child->issuer_raw.len ) == 0 )
1815 {
1816 check_path_cnt--;
1817 }
1818
Janos Follath92ac0592015-10-12 09:02:20 +02001819 /* Self signed certificates do not count towards the limit */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001820 if( trust_ca->max_pathlen > 0 &&
Janos Follath92ac0592015-10-12 09:02:20 +02001821 trust_ca->max_pathlen < check_path_cnt - self_cnt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001822 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001823 continue;
1824 }
1825
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001826 if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1827 child->sig_md, hash, md_info->size,
1828 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001829 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001830 continue;
1831 }
1832
1833 /*
1834 * Top of chain is signed by a trusted CA
1835 */
1836 *flags &= ~BADCERT_NOT_TRUSTED;
1837 break;
1838 }
1839
1840 /*
1841 * If top of chain is not the same as the trusted CA send a verify request
1842 * to the callback for any issues with validity and CRL presence for the
1843 * trusted CA certificate.
1844 */
1845 if( trust_ca != NULL &&
1846 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1847 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1848 child->issuer_raw.len ) != 0 ) )
1849 {
1850#if defined(POLARSSL_X509_CRL_PARSE_C)
1851 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001852 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001853#else
1854 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001855#endif
1856
Paul Bakker86d0c192013-09-18 11:11:02 +02001857 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001858 ca_flags |= BADCERT_EXPIRED;
1859
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001860 if( x509_time_future( &trust_ca->valid_from ) )
1861 ca_flags |= BADCERT_FUTURE;
1862
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001863 if( NULL != f_vrfy )
1864 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001865 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1866 &ca_flags ) ) != 0 )
1867 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001868 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001869 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001870 }
1871 }
1872
1873 /* Call callback on top cert */
1874 if( NULL != f_vrfy )
1875 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001876 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001877 return( ret );
1878 }
1879
1880 *flags |= ca_flags;
1881
1882 return( 0 );
1883}
1884
Paul Bakkerddf26b42013-09-18 13:46:23 +02001885static int x509_crt_verify_child(
Janos Follath92ac0592015-10-12 09:02:20 +02001886 x509_crt *child, x509_crt *parent,
1887 x509_crt *trust_ca, x509_crl *ca_crl,
1888 int path_cnt, int self_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001889 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001890 void *p_vrfy )
1891{
1892 int ret;
1893 int parent_flags = 0;
1894 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001895 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001896 const md_info_t *md_info;
1897
Janos Follath92ac0592015-10-12 09:02:20 +02001898 /* Counting intermediate self signed certificates */
1899 if( ( path_cnt != 0 ) && x509_name_cmp( &child->issuer, &child->subject ) == 0 )
1900 self_cnt++;
1901
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001902 /* path_cnt is 0 for the first intermediate CA */
1903 if( 1 + path_cnt > POLARSSL_X509_MAX_INTERMEDIATE_CA )
1904 {
1905 *flags |= BADCERT_NOT_TRUSTED;
1906 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1907 }
1908
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001909 if( x509_time_expired( &child->valid_to ) )
1910 *flags |= BADCERT_EXPIRED;
1911
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001912 if( x509_time_future( &child->valid_from ) )
1913 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001914
1915 md_info = md_info_from_type( child->sig_md );
1916 if( md_info == NULL )
1917 {
1918 /*
1919 * Cannot check 'unknown' hash
1920 */
1921 *flags |= BADCERT_NOT_TRUSTED;
1922 }
1923 else
1924 {
1925 md( md_info, child->tbs.p, child->tbs.len, hash );
1926
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001927 if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1928 child->sig_md, hash, md_info->size,
1929 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001930 {
1931 *flags |= BADCERT_NOT_TRUSTED;
1932 }
1933 }
1934
1935#if defined(POLARSSL_X509_CRL_PARSE_C)
1936 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001937 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001938#endif
1939
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02001940 /* Look for a grandparent in trusted CAs */
1941 for( grandparent = trust_ca;
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001942 grandparent != NULL;
1943 grandparent = grandparent->next )
1944 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001945 if( x509_crt_check_parent( parent, grandparent,
1946 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001947 break;
1948 }
1949
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001950 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001951 {
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02001952 ret = x509_crt_verify_top( parent, grandparent, ca_crl,
Janos Follath92ac0592015-10-12 09:02:20 +02001953 path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001954 if( ret != 0 )
1955 return( ret );
1956 }
1957 else
1958 {
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02001959 /* Look for a grandparent upwards the chain */
1960 for( grandparent = parent->next;
1961 grandparent != NULL;
1962 grandparent = grandparent->next )
1963 {
Janos Follath92ac0592015-10-12 09:02:20 +02001964 /* +2 because the current step is not yet accounted for
1965 * and because max_pathlen is one higher than it should be.
1966 * Also self signed certificates do not count to the limit. */
1967 if( grandparent->max_pathlen > 0 &&
1968 grandparent->max_pathlen < 2 + path_cnt - self_cnt )
1969 {
1970 continue;
1971 }
1972
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02001973 if( x509_crt_check_parent( parent, grandparent,
1974 0, path_cnt == 0 ) == 0 )
1975 break;
1976 }
1977
1978 /* Is our parent part of the chain or at the top? */
1979 if( grandparent != NULL )
1980 {
1981 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
Janos Follath92ac0592015-10-12 09:02:20 +02001982 path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02001983 f_vrfy, p_vrfy );
1984 if( ret != 0 )
1985 return( ret );
1986 }
1987 else
1988 {
1989 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
Janos Follath92ac0592015-10-12 09:02:20 +02001990 path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02001991 f_vrfy, p_vrfy );
1992 if( ret != 0 )
1993 return( ret );
1994 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001995 }
1996
1997 /* child is verified to be a child of the parent, call verify callback */
1998 if( NULL != f_vrfy )
1999 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2000 return( ret );
2001
2002 *flags |= parent_flags;
2003
2004 return( 0 );
2005}
2006
2007/*
2008 * Verify the certificate validity
2009 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002010int x509_crt_verify( x509_crt *crt,
2011 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02002012 x509_crl *ca_crl,
2013 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002014 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002015 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002016{
2017 size_t cn_len;
2018 int ret;
2019 int pathlen = 0;
Janos Follath92ac0592015-10-12 09:02:20 +02002020 int selfsigned = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002021 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002022 x509_name *name;
2023 x509_sequence *cur = NULL;
2024
2025 *flags = 0;
2026
2027 if( cn != NULL )
2028 {
2029 name = &crt->subject;
2030 cn_len = strlen( cn );
2031
2032 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
2033 {
2034 cur = &crt->subject_alt_names;
2035
2036 while( cur != NULL )
2037 {
2038 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002039 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002040 break;
2041
2042 if( cur->buf.len > 2 &&
2043 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
2044 x509_wildcard_verify( cn, &cur->buf ) )
2045 break;
2046
2047 cur = cur->next;
2048 }
2049
2050 if( cur == NULL )
2051 *flags |= BADCERT_CN_MISMATCH;
2052 }
2053 else
2054 {
2055 while( name != NULL )
2056 {
2057 if( OID_CMP( OID_AT_CN, &name->oid ) )
2058 {
2059 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002060 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002061 break;
2062
2063 if( name->val.len > 2 &&
2064 memcmp( name->val.p, "*.", 2 ) == 0 &&
2065 x509_wildcard_verify( cn, &name->val ) )
2066 break;
2067 }
2068
2069 name = name->next;
2070 }
2071
2072 if( name == NULL )
2073 *flags |= BADCERT_CN_MISMATCH;
2074 }
2075 }
2076
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002077 /* Look for a parent in trusted CAs */
2078 for( parent = trust_ca; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002079 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002080 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002081 break;
2082 }
2083
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002084 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002085 {
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002086 ret = x509_crt_verify_top( crt, parent, ca_crl,
Janos Follath92ac0592015-10-12 09:02:20 +02002087 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002088 if( ret != 0 )
2089 return( ret );
2090 }
2091 else
2092 {
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002093 /* Look for a parent upwards the chain */
2094 for( parent = crt->next; parent != NULL; parent = parent->next )
2095 {
Janos Follath92ac0592015-10-12 09:02:20 +02002096 /* +2 because the current step is not yet accounted for
2097 * and because max_pathlen is one higher than it should be */
2098 if( parent->max_pathlen > 0 &&
2099 parent->max_pathlen < 2 + pathlen )
2100 {
2101 continue;
2102 }
2103
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002104 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
2105 break;
2106 }
2107
2108 /* Are we part of the chain or at the top? */
2109 if( parent != NULL )
2110 {
2111 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
Janos Follath92ac0592015-10-12 09:02:20 +02002112 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002113 if( ret != 0 )
2114 return( ret );
2115 }
2116 else
2117 {
2118 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
Janos Follath92ac0592015-10-12 09:02:20 +02002119 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002120 if( ret != 0 )
2121 return( ret );
2122 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002123 }
2124
2125 if( *flags != 0 )
2126 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
2127
2128 return( 0 );
2129}
2130
2131/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002132 * Initialize a certificate chain
2133 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002134void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002135{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002136 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002137}
2138
2139/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002140 * Unallocate all certificate data
2141 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002142void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002143{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002144 x509_crt *cert_cur = crt;
2145 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002146 x509_name *name_cur;
2147 x509_name *name_prv;
2148 x509_sequence *seq_cur;
2149 x509_sequence *seq_prv;
2150
2151 if( crt == NULL )
2152 return;
2153
2154 do
2155 {
2156 pk_free( &cert_cur->pk );
2157
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +02002158#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002159 polarssl_free( cert_cur->sig_opts );
2160#endif
2161
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002162 name_cur = cert_cur->issuer.next;
2163 while( name_cur != NULL )
2164 {
2165 name_prv = name_cur;
2166 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002167 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002168 polarssl_free( name_prv );
2169 }
2170
2171 name_cur = cert_cur->subject.next;
2172 while( name_cur != NULL )
2173 {
2174 name_prv = name_cur;
2175 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002176 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002177 polarssl_free( name_prv );
2178 }
2179
2180 seq_cur = cert_cur->ext_key_usage.next;
2181 while( seq_cur != NULL )
2182 {
2183 seq_prv = seq_cur;
2184 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002185 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002186 polarssl_free( seq_prv );
2187 }
2188
2189 seq_cur = cert_cur->subject_alt_names.next;
2190 while( seq_cur != NULL )
2191 {
2192 seq_prv = seq_cur;
2193 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002194 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002195 polarssl_free( seq_prv );
2196 }
2197
2198 if( cert_cur->raw.p != NULL )
2199 {
Paul Bakker34617722014-06-13 17:20:13 +02002200 polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002201 polarssl_free( cert_cur->raw.p );
2202 }
2203
2204 cert_cur = cert_cur->next;
2205 }
2206 while( cert_cur != NULL );
2207
2208 cert_cur = crt;
2209 do
2210 {
2211 cert_prv = cert_cur;
2212 cert_cur = cert_cur->next;
2213
Paul Bakker34617722014-06-13 17:20:13 +02002214 polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002215 if( cert_prv != crt )
2216 polarssl_free( cert_prv );
2217 }
2218 while( cert_cur != NULL );
2219}
2220
Paul Bakker9af723c2014-05-01 13:03:14 +02002221#endif /* POLARSSL_X509_CRT_PARSE_C */