blob: 4e4d806a9c40319cae09672e55606bd0620ab735 [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 ||
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é-Gonnard39a183a2015-04-17 16:14:32 +02001385struct x509_crt_verify_string {
1386 int code;
1387 const char *string;
1388};
1389
1390static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
1391 { BADCERT_EXPIRED, "The certificate validity has expired" },
1392 { BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1393 { BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1394 { BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1395 { BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1396 { BADCRL_EXPIRED, "The CRL is expired" },
1397 { BADCERT_MISSING, "Certificate was missing" },
1398 { BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1399 { BADCERT_OTHER, "Other reason (can be used by verify callback)" },
1400 { BADCERT_FUTURE, "The certificate validity starts in the future" },
1401 { BADCRL_FUTURE, "The CRL is from the future" },
1402 { BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1403 { BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1404 { BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
1405 { 0, NULL }
1406};
1407
1408int x509_crt_verify_info( char *buf, size_t size, const char *prefix,
1409 int flags )
1410{
1411 int ret;
1412 const struct x509_crt_verify_string *cur;
1413 char *p = buf;
1414 size_t n = size;
1415
1416 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1417 {
1418 if( ( flags & cur->code ) == 0 )
1419 continue;
1420
1421 ret = polarssl_snprintf( p, n, "%s%s\n", prefix, cur->string );
1422 SAFE_SNPRINTF();
1423 flags ^= cur->code;
1424 }
1425
1426 if( flags != 0 )
1427 {
1428 ret = polarssl_snprintf( p, n, "%sUnknown reason "
1429 "(this should not happen)\n", prefix );
1430 SAFE_SNPRINTF();
1431 }
1432
1433 return( (int) ( size - n ) );
1434}
1435
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001436#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1437int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1438{
1439 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1440 ( crt->key_usage & usage ) != usage )
1441 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1442
1443 return( 0 );
1444}
1445#endif
1446
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001447#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1448int x509_crt_check_extended_key_usage( const x509_crt *crt,
1449 const char *usage_oid,
1450 size_t usage_len )
1451{
1452 const x509_sequence *cur;
1453
1454 /* Extension is not mandatory, absent means no restriction */
1455 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1456 return( 0 );
1457
1458 /*
1459 * Look for the requested usage (or wildcard ANY) in our list
1460 */
1461 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1462 {
1463 const x509_buf *cur_oid = &cur->buf;
1464
1465 if( cur_oid->len == usage_len &&
1466 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1467 {
1468 return( 0 );
1469 }
1470
1471 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1472 return( 0 );
1473 }
1474
1475 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1476}
Paul Bakker9af723c2014-05-01 13:03:14 +02001477#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001478
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001479#if defined(POLARSSL_X509_CRL_PARSE_C)
1480/*
1481 * Return 1 if the certificate is revoked, or 0 otherwise.
1482 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001483int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001484{
1485 const x509_crl_entry *cur = &crl->entry;
1486
1487 while( cur != NULL && cur->serial.len != 0 )
1488 {
1489 if( crt->serial.len == cur->serial.len &&
1490 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1491 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001492 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493 return( 1 );
1494 }
1495
1496 cur = cur->next;
1497 }
1498
1499 return( 0 );
1500}
1501
1502/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001503 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001505static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001506 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507{
1508 int flags = 0;
1509 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1510 const md_info_t *md_info;
1511
1512 if( ca == NULL )
1513 return( flags );
1514
1515 /*
1516 * TODO: What happens if no CRL is present?
1517 * Suggestion: Revocation state should be unknown if no CRL is present.
1518 * For backwards compatibility this is not yet implemented.
1519 */
1520
1521 while( crl_list != NULL )
1522 {
1523 if( crl_list->version == 0 ||
1524 crl_list->issuer_raw.len != ca->subject_raw.len ||
1525 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1526 crl_list->issuer_raw.len ) != 0 )
1527 {
1528 crl_list = crl_list->next;
1529 continue;
1530 }
1531
1532 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001533 * Check if the CA is configured to sign CRLs
1534 */
1535#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1536 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1537 {
1538 flags |= BADCRL_NOT_TRUSTED;
1539 break;
1540 }
1541#endif
1542
1543 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544 * Check if CRL is correctly signed by the trusted CA
1545 */
1546 md_info = md_info_from_type( crl_list->sig_md );
1547 if( md_info == NULL )
1548 {
1549 /*
1550 * Cannot check 'unknown' hash
1551 */
1552 flags |= BADCRL_NOT_TRUSTED;
1553 break;
1554 }
1555
1556 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1557
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001558 if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1559 crl_list->sig_md, hash, md_info->size,
1560 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561 {
1562 flags |= BADCRL_NOT_TRUSTED;
1563 break;
1564 }
1565
1566 /*
1567 * Check for validity of CRL (Do not drop out)
1568 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001569 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570 flags |= BADCRL_EXPIRED;
1571
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001572 if( x509_time_future( &crl_list->this_update ) )
1573 flags |= BADCRL_FUTURE;
1574
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575 /*
1576 * Check if certificate is revoked
1577 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001578 if( x509_crt_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579 {
1580 flags |= BADCERT_REVOKED;
1581 break;
1582 }
1583
1584 crl_list = crl_list->next;
1585 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001586 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587}
1588#endif /* POLARSSL_X509_CRL_PARSE_C */
1589
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001590/*
1591 * Like memcmp, but case-insensitive and always returns -1 if different
1592 */
1593static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594{
1595 size_t i;
1596 unsigned char diff;
1597 const unsigned char *n1 = s1, *n2 = s2;
1598
1599 for( i = 0; i < len; i++ )
1600 {
1601 diff = n1[i] ^ n2[i];
1602
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001603 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001604 continue;
1605
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001606 if( diff == 32 &&
1607 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1608 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1609 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001610 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001611 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001613 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614 }
1615
1616 return( 0 );
1617}
1618
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001619/*
1620 * Return 1 if match, 0 if not
1621 * TODO: inverted return value!
1622 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001623static int x509_wildcard_verify( const char *cn, x509_buf *name )
1624{
1625 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001626 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001627
1628 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1629 return( 0 );
1630
Paul Bakker14b16c62014-05-28 11:33:54 +02001631 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001632 {
1633 if( cn[i] == '.' )
1634 {
1635 cn_idx = i;
1636 break;
1637 }
1638 }
1639
1640 if( cn_idx == 0 )
1641 return( 0 );
1642
Paul Bakker14b16c62014-05-28 11:33:54 +02001643 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001644 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001645 {
1646 return( 1 );
1647 }
1648
1649 return( 0 );
1650}
1651
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001652/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001653 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1654 * variations (but not all).
1655 *
1656 * Return 0 if equal, -1 otherwise.
1657 */
1658static int x509_string_cmp( const x509_buf *a, const x509_buf *b )
1659{
1660 if( a->tag == b->tag &&
1661 a->len == b->len &&
1662 memcmp( a->p, b->p, b->len ) == 0 )
1663 {
1664 return( 0 );
1665 }
1666
1667 if( ( a->tag == ASN1_UTF8_STRING || a->tag == ASN1_PRINTABLE_STRING ) &&
1668 ( b->tag == ASN1_UTF8_STRING || b->tag == ASN1_PRINTABLE_STRING ) &&
1669 a->len == b->len &&
1670 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1671 {
1672 return( 0 );
1673 }
1674
1675 return( -1 );
1676}
1677
1678/*
1679 * Compare two X.509 Names (aka rdnSequence).
1680 *
1681 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1682 * we sometimes return unequal when the full algorithm would return equal,
1683 * but never the other way. (In particular, we don't do Unicode normalisation
1684 * or space folding.)
1685 *
1686 * Return 0 if equal, -1 otherwise.
1687 */
1688static int x509_name_cmp( const x509_name *a, const x509_name *b )
1689{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001690 /* Avoid recursion, it might not be optimised by the compiler */
1691 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001692 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001693 if( a == NULL || b == NULL )
1694 return( -1 );
1695
1696 /* type */
1697 if( a->oid.tag != b->oid.tag ||
1698 a->oid.len != b->oid.len ||
1699 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1700 {
1701 return( -1 );
1702 }
1703
1704 /* value */
1705 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1706 return( -1 );
1707
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001708 /* structure of the list of sets */
1709 if( a->next_merged != b->next_merged )
1710 return( -1 );
1711
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001712 a = a->next;
1713 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001714 }
1715
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001716 /* a == NULL == b */
1717 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001718}
1719
1720/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001721 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1722 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001723 *
1724 * top means parent is a locally-trusted certificate
1725 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001726 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001727static int x509_crt_check_parent( const x509_crt *child,
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001728 const x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001729 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001730{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001731 int need_ca_bit;
1732
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001733 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001734 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001735 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001736
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001737 /* Parent must have the basicConstraints CA bit set as a general rule */
1738 need_ca_bit = 1;
1739
1740 /* Exception: v1/v2 certificates that are locally trusted. */
1741 if( top && parent->version < 3 )
1742 need_ca_bit = 0;
1743
1744 /* Exception: self-signed end-entity certs that are locally trusted. */
1745 if( top && bottom &&
1746 child->raw.len == parent->raw.len &&
1747 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1748 {
1749 need_ca_bit = 0;
1750 }
1751
1752 if( need_ca_bit && ! parent->ca_istrue )
1753 return( -1 );
1754
1755#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1756 if( need_ca_bit &&
1757 x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001758 {
1759 return( -1 );
1760 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001761#endif
1762
1763 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001764}
1765
Paul Bakkerddf26b42013-09-18 13:46:23 +02001766static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001767 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001769 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001770 void *p_vrfy )
1771{
1772 int ret;
1773 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1774 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1775 const md_info_t *md_info;
1776
Paul Bakker86d0c192013-09-18 11:11:02 +02001777 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001778 *flags |= BADCERT_EXPIRED;
1779
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001780 if( x509_time_future( &child->valid_from ) )
1781 *flags |= BADCERT_FUTURE;
1782
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001783 /*
1784 * Child is the top of the chain. Check against the trust_ca list.
1785 */
1786 *flags |= BADCERT_NOT_TRUSTED;
1787
1788 md_info = md_info_from_type( child->sig_md );
1789 if( md_info == NULL )
1790 {
1791 /*
1792 * Cannot check 'unknown', no need to try any CA
1793 */
1794 trust_ca = NULL;
1795 }
1796 else
1797 md( md_info, child->tbs.p, child->tbs.len, hash );
1798
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001799 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001800 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001801 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001802 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001803
1804 /*
1805 * Reduce path_len to check against if top of the chain is
1806 * the same as the trusted CA
1807 */
1808 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1809 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1810 child->issuer_raw.len ) == 0 )
1811 {
1812 check_path_cnt--;
1813 }
1814
1815 if( trust_ca->max_pathlen > 0 &&
1816 trust_ca->max_pathlen < check_path_cnt )
1817 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001818 continue;
1819 }
1820
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001821 if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1822 child->sig_md, hash, md_info->size,
1823 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001824 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001825 continue;
1826 }
1827
1828 /*
1829 * Top of chain is signed by a trusted CA
1830 */
1831 *flags &= ~BADCERT_NOT_TRUSTED;
1832 break;
1833 }
1834
1835 /*
1836 * If top of chain is not the same as the trusted CA send a verify request
1837 * to the callback for any issues with validity and CRL presence for the
1838 * trusted CA certificate.
1839 */
1840 if( trust_ca != NULL &&
1841 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1842 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1843 child->issuer_raw.len ) != 0 ) )
1844 {
1845#if defined(POLARSSL_X509_CRL_PARSE_C)
1846 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001847 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001848#else
1849 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001850#endif
1851
Paul Bakker86d0c192013-09-18 11:11:02 +02001852 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001853 ca_flags |= BADCERT_EXPIRED;
1854
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001855 if( x509_time_future( &trust_ca->valid_from ) )
1856 ca_flags |= BADCERT_FUTURE;
1857
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001858 if( NULL != f_vrfy )
1859 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001860 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1861 &ca_flags ) ) != 0 )
1862 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001863 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001864 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001865 }
1866 }
1867
1868 /* Call callback on top cert */
1869 if( NULL != f_vrfy )
1870 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001871 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001872 return( ret );
1873 }
1874
1875 *flags |= ca_flags;
1876
1877 return( 0 );
1878}
1879
Paul Bakkerddf26b42013-09-18 13:46:23 +02001880static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001881 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001882 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001883 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001884 void *p_vrfy )
1885{
1886 int ret;
1887 int parent_flags = 0;
1888 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001889 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001890 const md_info_t *md_info;
1891
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001892 /* path_cnt is 0 for the first intermediate CA */
1893 if( 1 + path_cnt > POLARSSL_X509_MAX_INTERMEDIATE_CA )
1894 {
1895 *flags |= BADCERT_NOT_TRUSTED;
1896 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1897 }
1898
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001899 if( x509_time_expired( &child->valid_to ) )
1900 *flags |= BADCERT_EXPIRED;
1901
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001902 if( x509_time_future( &child->valid_from ) )
1903 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001904
1905 md_info = md_info_from_type( child->sig_md );
1906 if( md_info == NULL )
1907 {
1908 /*
1909 * Cannot check 'unknown' hash
1910 */
1911 *flags |= BADCERT_NOT_TRUSTED;
1912 }
1913 else
1914 {
1915 md( md_info, child->tbs.p, child->tbs.len, hash );
1916
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001917 if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1918 child->sig_md, hash, md_info->size,
1919 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001920 {
1921 *flags |= BADCERT_NOT_TRUSTED;
1922 }
1923 }
1924
1925#if defined(POLARSSL_X509_CRL_PARSE_C)
1926 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001927 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001928#endif
1929
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001930 /* Look for a grandparent upwards the chain */
1931 for( grandparent = parent->next;
1932 grandparent != NULL;
1933 grandparent = grandparent->next )
1934 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001935 if( x509_crt_check_parent( parent, grandparent,
1936 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001937 break;
1938 }
1939
1940 /* Is our parent part of the chain or at the top? */
1941 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001942 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001943 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1944 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001945 if( ret != 0 )
1946 return( ret );
1947 }
1948 else
1949 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001950 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1951 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001952 if( ret != 0 )
1953 return( ret );
1954 }
1955
1956 /* child is verified to be a child of the parent, call verify callback */
1957 if( NULL != f_vrfy )
1958 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1959 return( ret );
1960
1961 *flags |= parent_flags;
1962
1963 return( 0 );
1964}
1965
1966/*
1967 * Verify the certificate validity
1968 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001969int x509_crt_verify( x509_crt *crt,
1970 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001971 x509_crl *ca_crl,
1972 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001973 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001974 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001975{
1976 size_t cn_len;
1977 int ret;
1978 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001979 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001980 x509_name *name;
1981 x509_sequence *cur = NULL;
1982
1983 *flags = 0;
1984
1985 if( cn != NULL )
1986 {
1987 name = &crt->subject;
1988 cn_len = strlen( cn );
1989
1990 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1991 {
1992 cur = &crt->subject_alt_names;
1993
1994 while( cur != NULL )
1995 {
1996 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001997 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001998 break;
1999
2000 if( cur->buf.len > 2 &&
2001 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
2002 x509_wildcard_verify( cn, &cur->buf ) )
2003 break;
2004
2005 cur = cur->next;
2006 }
2007
2008 if( cur == NULL )
2009 *flags |= BADCERT_CN_MISMATCH;
2010 }
2011 else
2012 {
2013 while( name != NULL )
2014 {
2015 if( OID_CMP( OID_AT_CN, &name->oid ) )
2016 {
2017 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002018 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002019 break;
2020
2021 if( name->val.len > 2 &&
2022 memcmp( name->val.p, "*.", 2 ) == 0 &&
2023 x509_wildcard_verify( cn, &name->val ) )
2024 break;
2025 }
2026
2027 name = name->next;
2028 }
2029
2030 if( name == NULL )
2031 *flags |= BADCERT_CN_MISMATCH;
2032 }
2033 }
2034
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002035 /* Look for a parent upwards the chain */
2036 for( parent = crt->next; parent != NULL; parent = parent->next )
2037 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002038 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002039 break;
2040 }
2041
2042 /* Are we part of the chain or at the top? */
2043 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002044 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002045 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
2046 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002047 if( ret != 0 )
2048 return( ret );
2049 }
2050 else
2051 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002052 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
2053 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002054 if( ret != 0 )
2055 return( ret );
2056 }
2057
2058 if( *flags != 0 )
2059 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
2060
2061 return( 0 );
2062}
2063
2064/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002065 * Initialize a certificate chain
2066 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002067void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002068{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002069 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002070}
2071
2072/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002073 * Unallocate all certificate data
2074 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002075void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002076{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002077 x509_crt *cert_cur = crt;
2078 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002079 x509_name *name_cur;
2080 x509_name *name_prv;
2081 x509_sequence *seq_cur;
2082 x509_sequence *seq_prv;
2083
2084 if( crt == NULL )
2085 return;
2086
2087 do
2088 {
2089 pk_free( &cert_cur->pk );
2090
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +02002091#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002092 polarssl_free( cert_cur->sig_opts );
2093#endif
2094
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002095 name_cur = cert_cur->issuer.next;
2096 while( name_cur != NULL )
2097 {
2098 name_prv = name_cur;
2099 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002100 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002101 polarssl_free( name_prv );
2102 }
2103
2104 name_cur = cert_cur->subject.next;
2105 while( name_cur != NULL )
2106 {
2107 name_prv = name_cur;
2108 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002109 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002110 polarssl_free( name_prv );
2111 }
2112
2113 seq_cur = cert_cur->ext_key_usage.next;
2114 while( seq_cur != NULL )
2115 {
2116 seq_prv = seq_cur;
2117 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002118 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002119 polarssl_free( seq_prv );
2120 }
2121
2122 seq_cur = cert_cur->subject_alt_names.next;
2123 while( seq_cur != NULL )
2124 {
2125 seq_prv = seq_cur;
2126 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002127 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002128 polarssl_free( seq_prv );
2129 }
2130
2131 if( cert_cur->raw.p != NULL )
2132 {
Paul Bakker34617722014-06-13 17:20:13 +02002133 polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002134 polarssl_free( cert_cur->raw.p );
2135 }
2136
2137 cert_cur = cert_cur->next;
2138 }
2139 while( cert_cur != NULL );
2140
2141 cert_cur = crt;
2142 do
2143 {
2144 cert_prv = cert_cur;
2145 cert_cur = cert_cur->next;
2146
Paul Bakker34617722014-06-13 17:20:13 +02002147 polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002148 if( cert_prv != crt )
2149 polarssl_free( cert_prv );
2150 }
2151 while( cert_cur != NULL );
2152}
2153
Paul Bakker9af723c2014-05-01 13:03:14 +02002154#endif /* POLARSSL_X509_CRT_PARSE_C */