blob: 70ad356a97ba164d032f863ee768ca2c338ccfac [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
Manuel Pégourié-Gonnard48ed5502017-06-08 17:27:20 +020079#if !defined(POLARSSL_X509_MIN_VERIFY_MD_ALG)
80#define POLARSSL_X509_MIN_VERIFY_MD_ALG POLARSSL_MD_SHA1
81#endif
82
Paul Bakker34617722014-06-13 17:20:13 +020083/* Implementation that should never be optimized out by the compiler */
84static void polarssl_zeroize( void *v, size_t n ) {
85 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
86}
87
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088/*
89 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
90 */
91static int x509_get_version( unsigned char **p,
92 const unsigned char *end,
93 int *ver )
94{
95 int ret;
96 size_t len;
97
98 if( ( ret = asn1_get_tag( p, end, &len,
99 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
100 {
101 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
102 {
103 *ver = 0;
104 return( 0 );
105 }
106
107 return( ret );
108 }
109
110 end = *p + len;
111
112 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200113 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200114
115 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200116 return( POLARSSL_ERR_X509_INVALID_VERSION +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
118
119 return( 0 );
120}
121
122/*
123 * Validity ::= SEQUENCE {
124 * notBefore Time,
125 * notAfter Time }
126 */
127static int x509_get_dates( unsigned char **p,
128 const unsigned char *end,
129 x509_time *from,
130 x509_time *to )
131{
132 int ret;
133 size_t len;
134
135 if( ( ret = asn1_get_tag( p, end, &len,
136 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200137 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138
139 end = *p + len;
140
141 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
142 return( ret );
143
144 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
145 return( ret );
146
147 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200148 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200149 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
150
151 return( 0 );
152}
153
154/*
155 * X.509 v2/v3 unique identifier (not parsed)
156 */
157static int x509_get_uid( unsigned char **p,
158 const unsigned char *end,
159 x509_buf *uid, int n )
160{
161 int ret;
162
163 if( *p == end )
164 return( 0 );
165
166 uid->tag = **p;
167
168 if( ( ret = asn1_get_tag( p, end, &uid->len,
169 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
170 {
171 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
172 return( 0 );
173
174 return( ret );
175 }
176
177 uid->p = *p;
178 *p += uid->len;
179
180 return( 0 );
181}
182
183static int x509_get_basic_constraints( unsigned char **p,
184 const unsigned char *end,
185 int *ca_istrue,
186 int *max_pathlen )
187{
188 int ret;
189 size_t len;
190
191 /*
192 * BasicConstraints ::= SEQUENCE {
193 * cA BOOLEAN DEFAULT FALSE,
194 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
195 */
196 *ca_istrue = 0; /* DEFAULT FALSE */
197 *max_pathlen = 0; /* endless */
198
199 if( ( ret = asn1_get_tag( p, end, &len,
200 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200201 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200202
203 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200204 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200205
206 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
207 {
208 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
209 ret = asn1_get_int( p, end, ca_istrue );
210
211 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200212 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213
214 if( *ca_istrue != 0 )
215 *ca_istrue = 1;
216 }
217
218 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200219 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200220
221 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200222 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200223
224 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200225 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200226 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
227
228 (*max_pathlen)++;
229
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200230 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200231}
232
233static int x509_get_ns_cert_type( unsigned char **p,
234 const unsigned char *end,
235 unsigned char *ns_cert_type)
236{
237 int ret;
238 x509_bitstring bs = { 0, 0, NULL };
239
240 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200241 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242
243 if( bs.len != 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200244 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200245 POLARSSL_ERR_ASN1_INVALID_LENGTH );
246
247 /* Get actual bitstring */
248 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200249 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200250}
251
252static int x509_get_key_usage( unsigned char **p,
253 const unsigned char *end,
254 unsigned char *key_usage)
255{
256 int ret;
257 x509_bitstring bs = { 0, 0, NULL };
258
259 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200260 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261
262 if( bs.len < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200263 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264 POLARSSL_ERR_ASN1_INVALID_LENGTH );
265
266 /* Get actual bitstring */
267 *key_usage = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200268 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269}
270
271/*
272 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
273 *
274 * KeyPurposeId ::= OBJECT IDENTIFIER
275 */
276static int x509_get_ext_key_usage( unsigned char **p,
277 const unsigned char *end,
278 x509_sequence *ext_key_usage)
279{
280 int ret;
281
282 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200283 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284
285 /* Sequence length must be >= 1 */
286 if( ext_key_usage->buf.p == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200287 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 POLARSSL_ERR_ASN1_INVALID_LENGTH );
289
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200290 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291}
292
293/*
294 * SubjectAltName ::= GeneralNames
295 *
296 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
297 *
298 * GeneralName ::= CHOICE {
299 * otherName [0] OtherName,
300 * rfc822Name [1] IA5String,
301 * dNSName [2] IA5String,
302 * x400Address [3] ORAddress,
303 * directoryName [4] Name,
304 * ediPartyName [5] EDIPartyName,
305 * uniformResourceIdentifier [6] IA5String,
306 * iPAddress [7] OCTET STRING,
307 * registeredID [8] OBJECT IDENTIFIER }
308 *
309 * OtherName ::= SEQUENCE {
310 * type-id OBJECT IDENTIFIER,
311 * value [0] EXPLICIT ANY DEFINED BY type-id }
312 *
313 * EDIPartyName ::= SEQUENCE {
314 * nameAssigner [0] DirectoryString OPTIONAL,
315 * partyName [1] DirectoryString }
316 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000317 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200318 */
319static int x509_get_subject_alt_name( unsigned char **p,
320 const unsigned char *end,
321 x509_sequence *subject_alt_name )
322{
323 int ret;
324 size_t len, tag_len;
325 asn1_buf *buf;
326 unsigned char tag;
327 asn1_sequence *cur = subject_alt_name;
328
329 /* Get main sequence tag */
330 if( ( ret = asn1_get_tag( p, end, &len,
331 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200332 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333
334 if( *p + len != end )
Paul Bakker51876562013-09-17 14:36:05 +0200335 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
337
338 while( *p < end )
339 {
340 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200341 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200342 POLARSSL_ERR_ASN1_OUT_OF_DATA );
343
344 tag = **p;
345 (*p)++;
346 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200347 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348
349 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
Paul Bakker51876562013-09-17 14:36:05 +0200350 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
352
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200353 /* Skip everything but DNS name */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
355 {
356 *p += tag_len;
357 continue;
358 }
359
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200361 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100363 if( cur->next != NULL )
364 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS );
365
Mansour Moufid99b92592015-02-15 17:46:32 -0500366 cur->next = polarssl_malloc( sizeof( asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367
368 if( cur->next == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200369 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370 POLARSSL_ERR_ASN1_MALLOC_FAILED );
371
372 memset( cur->next, 0, sizeof( asn1_sequence ) );
373 cur = cur->next;
374 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200375
376 buf = &(cur->buf);
377 buf->tag = tag;
378 buf->p = *p;
379 buf->len = tag_len;
380 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381 }
382
383 /* Set final sequence entry's next pointer to NULL */
384 cur->next = NULL;
385
386 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200387 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
389
390 return( 0 );
391}
392
393/*
394 * X.509 v3 extensions
395 *
396 * TODO: Perform all of the basic constraints tests required by the RFC
397 * TODO: Set values for undetected extensions to a sane default?
398 *
399 */
400static int x509_get_crt_ext( unsigned char **p,
401 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200402 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403{
404 int ret;
405 size_t len;
406 unsigned char *end_ext_data, *end_ext_octet;
407
408 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
409 {
410 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
411 return( 0 );
412
413 return( ret );
414 }
415
416 while( *p < end )
417 {
418 /*
419 * Extension ::= SEQUENCE {
420 * extnID OBJECT IDENTIFIER,
421 * critical BOOLEAN DEFAULT FALSE,
422 * extnValue OCTET STRING }
423 */
424 x509_buf extn_oid = {0, 0, NULL};
425 int is_critical = 0; /* DEFAULT FALSE */
426 int ext_type = 0;
427
428 if( ( ret = asn1_get_tag( p, end, &len,
429 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200430 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431
432 end_ext_data = *p + len;
433
434 /* Get extension ID */
435 extn_oid.tag = **p;
436
437 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200438 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439
440 extn_oid.p = *p;
441 *p += extn_oid.len;
442
443 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200444 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445 POLARSSL_ERR_ASN1_OUT_OF_DATA );
446
447 /* Get optional critical */
448 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
449 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200450 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
452 /* Data should be octet string type */
453 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
454 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200455 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456
457 end_ext_octet = *p + len;
458
459 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200460 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
462
463 /*
464 * Detect supported extensions
465 */
466 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
467
468 if( ret != 0 )
469 {
470 /* No parser found, skip extension */
471 *p = end_ext_octet;
472
473#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
474 if( is_critical )
475 {
476 /* Data is marked as critical: fail */
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200477 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
479 }
480#endif
481 continue;
482 }
483
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100484 /* Forbid repeated extensions */
485 if( ( crt->ext_types & ext_type ) != 0 )
486 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS );
487
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488 crt->ext_types |= ext_type;
489
490 switch( ext_type )
491 {
492 case EXT_BASIC_CONSTRAINTS:
493 /* Parse basic constraints */
494 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
495 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200496 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 break;
498
499 case EXT_KEY_USAGE:
500 /* Parse key usage */
501 if( ( ret = x509_get_key_usage( p, end_ext_octet,
502 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200503 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504 break;
505
506 case EXT_EXTENDED_KEY_USAGE:
507 /* Parse extended key usage */
508 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
509 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200510 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511 break;
512
513 case EXT_SUBJECT_ALT_NAME:
514 /* Parse subject alt name */
515 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
516 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200517 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518 break;
519
520 case EXT_NS_CERT_TYPE:
521 /* Parse netscape certificate type */
522 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
523 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200524 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 break;
526
527 default:
528 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
529 }
530 }
531
532 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200533 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
535
536 return( 0 );
537}
538
539/*
540 * Parse and fill a single X.509 certificate in DER format
541 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200542static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200543 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544{
545 int ret;
546 size_t len;
547 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200548 x509_buf sig_params1, sig_params2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100549
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200550 memset( &sig_params1, 0, sizeof( x509_buf ) );
551 memset( &sig_params2, 0, sizeof( x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552
553 /*
554 * Check for valid input
555 */
556 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200557 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500559 p = polarssl_malloc( len = buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560
561 if( p == NULL )
562 return( POLARSSL_ERR_X509_MALLOC_FAILED );
563
564 memcpy( p, buf, buflen );
565
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566 crt->raw.p = p;
567 crt->raw.len = len;
568 end = p + len;
569
570 /*
571 * Certificate ::= SEQUENCE {
572 * tbsCertificate TBSCertificate,
573 * signatureAlgorithm AlgorithmIdentifier,
574 * signatureValue BIT STRING }
575 */
576 if( ( ret = asn1_get_tag( &p, end, &len,
577 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
578 {
579 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200580 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581 }
582
583 if( len > (size_t) ( end - p ) )
584 {
585 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200586 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
588 }
589 crt_end = p + len;
590
591 /*
592 * TBSCertificate ::= SEQUENCE {
593 */
594 crt->tbs.p = p;
595
596 if( ( ret = asn1_get_tag( &p, end, &len,
597 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
598 {
599 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200600 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200601 }
602
603 end = p + len;
604 crt->tbs.len = end - crt->tbs.p;
605
606 /*
607 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
608 *
609 * CertificateSerialNumber ::= INTEGER
610 *
611 * signature AlgorithmIdentifier
612 */
613 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
614 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100615 ( ret = x509_get_alg( &p, end, &crt->sig_oid1,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200616 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617 {
618 x509_crt_free( crt );
619 return( ret );
620 }
621
Andres AG47f30592017-03-09 16:46:14 +0000622 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623 {
624 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200625 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 }
627
Andres AG47f30592017-03-09 16:46:14 +0000628 crt->version++;
629
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200630 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200631 &crt->sig_md, &crt->sig_pk,
632 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633 {
634 x509_crt_free( crt );
635 return( ret );
636 }
637
638 /*
639 * issuer Name
640 */
641 crt->issuer_raw.p = p;
642
643 if( ( ret = asn1_get_tag( &p, end, &len,
644 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
645 {
646 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200647 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 }
649
650 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
651 {
652 x509_crt_free( crt );
653 return( ret );
654 }
655
656 crt->issuer_raw.len = p - crt->issuer_raw.p;
657
658 /*
659 * Validity ::= SEQUENCE {
660 * notBefore Time,
661 * notAfter Time }
662 *
663 */
664 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
665 &crt->valid_to ) ) != 0 )
666 {
667 x509_crt_free( crt );
668 return( ret );
669 }
670
671 /*
672 * subject Name
673 */
674 crt->subject_raw.p = p;
675
676 if( ( ret = asn1_get_tag( &p, end, &len,
677 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
678 {
679 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200680 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681 }
682
683 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
684 {
685 x509_crt_free( crt );
686 return( ret );
687 }
688
689 crt->subject_raw.len = p - crt->subject_raw.p;
690
691 /*
692 * SubjectPublicKeyInfo
693 */
Paul Bakkerda771152013-09-16 22:45:03 +0200694 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 {
696 x509_crt_free( crt );
697 return( ret );
698 }
699
700 /*
701 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
702 * -- If present, version shall be v2 or v3
703 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
704 * -- If present, version shall be v2 or v3
705 * extensions [3] EXPLICIT Extensions OPTIONAL
706 * -- If present, version shall be v3
707 */
708 if( crt->version == 2 || crt->version == 3 )
709 {
710 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
711 if( ret != 0 )
712 {
713 x509_crt_free( crt );
714 return( ret );
715 }
716 }
717
718 if( crt->version == 2 || crt->version == 3 )
719 {
720 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
721 if( ret != 0 )
722 {
723 x509_crt_free( crt );
724 return( ret );
725 }
726 }
727
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200728#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200729 if( crt->version == 3 )
730 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200731#endif
Paul Bakker66d5d072014-06-17 16:39:18 +0200732 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733 if( ret != 0 )
734 {
735 x509_crt_free( crt );
736 return( ret );
737 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200738#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200740#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741
742 if( p != end )
743 {
744 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200745 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
747 }
748
749 end = crt_end;
750
751 /*
752 * }
753 * -- end of TBSCertificate
754 *
755 * signatureAlgorithm AlgorithmIdentifier,
756 * signatureValue BIT STRING
757 */
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200758 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200759 {
760 x509_crt_free( crt );
761 return( ret );
762 }
763
764 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200765 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 ||
766 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200767 ( sig_params1.len != 0 &&
768 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 {
770 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200771 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772 }
773
774 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
775 {
776 x509_crt_free( crt );
777 return( ret );
778 }
779
780 if( p != end )
781 {
782 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200783 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
785 }
786
787 return( 0 );
788}
789
790/*
791 * Parse one X.509 certificate in DER format from a buffer and add them to a
792 * chained list
793 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200794int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200795 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796{
797 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200798 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799
800 /*
801 * Check for valid input
802 */
803 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200804 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200805
806 while( crt->version != 0 && crt->next != NULL )
807 {
808 prev = crt;
809 crt = crt->next;
810 }
811
812 /*
813 * Add new certificate on the end of the chain if needed.
814 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200815 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500817 crt->next = polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818
819 if( crt->next == NULL )
820 return( POLARSSL_ERR_X509_MALLOC_FAILED );
821
822 prev = crt;
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100823 x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200825 }
826
Paul Bakkerddf26b42013-09-18 13:46:23 +0200827 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200828 {
829 if( prev )
830 prev->next = NULL;
831
832 if( crt != chain )
833 polarssl_free( crt );
834
835 return( ret );
836 }
837
838 return( 0 );
839}
840
841/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200842 * Parse one or more PEM certificates from a buffer and add them to the chained
843 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200844 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200845int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200846{
847 int success = 0, first_error = 0, total_failed = 0;
848 int buf_format = X509_FORMAT_DER;
849
850 /*
851 * Check for valid input
852 */
853 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200854 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200855
856 /*
857 * Determine buffer content. Buffer contains either one DER certificate or
858 * one or more PEM certificates.
859 */
860#if defined(POLARSSL_PEM_PARSE_C)
861 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
862 buf_format = X509_FORMAT_PEM;
863#endif
864
865 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200866 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200867
868#if defined(POLARSSL_PEM_PARSE_C)
869 if( buf_format == X509_FORMAT_PEM )
870 {
871 int ret;
872 pem_context pem;
873
874 while( buflen > 0 )
875 {
876 size_t use_len;
877 pem_init( &pem );
878
879 ret = pem_read_buffer( &pem,
880 "-----BEGIN CERTIFICATE-----",
881 "-----END CERTIFICATE-----",
882 buf, NULL, 0, &use_len );
883
884 if( ret == 0 )
885 {
886 /*
887 * Was PEM encoded
888 */
889 buflen -= use_len;
890 buf += use_len;
891 }
892 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
893 {
894 return( ret );
895 }
896 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
897 {
898 pem_free( &pem );
899
900 /*
901 * PEM header and footer were found
902 */
903 buflen -= use_len;
904 buf += use_len;
905
906 if( first_error == 0 )
907 first_error = ret;
908
Paul Bakker5a5fa922014-09-26 14:53:04 +0200909 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910 continue;
911 }
912 else
913 break;
914
Paul Bakkerddf26b42013-09-18 13:46:23 +0200915 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916
917 pem_free( &pem );
918
919 if( ret != 0 )
920 {
921 /*
922 * Quit parsing on a memory error
923 */
924 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
925 return( ret );
926
927 if( first_error == 0 )
928 first_error = ret;
929
930 total_failed++;
931 continue;
932 }
933
934 success = 1;
935 }
936 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200937#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938
939 if( success )
940 return( total_failed );
941 else if( first_error )
942 return( first_error );
943 else
944 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
945}
946
947#if defined(POLARSSL_FS_IO)
948/*
949 * Load one or more certificates and add them to the chained list
950 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200951int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952{
953 int ret;
954 size_t n;
955 unsigned char *buf;
956
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100957 if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958 return( ret );
959
Paul Bakkerddf26b42013-09-18 13:46:23 +0200960 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961
Paul Bakker34617722014-06-13 17:20:13 +0200962 polarssl_zeroize( buf, n + 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963 polarssl_free( buf );
964
965 return( ret );
966}
967
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100968#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100969static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
970#endif
971
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200972int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973{
974 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100975#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976 int w_ret;
977 WCHAR szDir[MAX_PATH];
978 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200979 char *p;
Manuel Pégourié-Gonnard215a14b2015-10-21 10:16:29 +0200980 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981
Paul Bakker9af723c2014-05-01 13:03:14 +0200982 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983 HANDLE hFind;
984
985 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200986 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987
Paul Bakker9af723c2014-05-01 13:03:14 +0200988 memset( szDir, 0, sizeof(szDir) );
989 memset( filename, 0, MAX_PATH );
990 memcpy( filename, path, len );
991 filename[len++] = '\\';
992 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 filename[len++] = '*';
994
Simon Ba697bf52016-11-10 13:19:42 +0000995 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200996 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +0000997 if( w_ret == 0 )
998 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999
1000 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001001 if( hFind == INVALID_HANDLE_VALUE )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1003
1004 len = MAX_PATH - len;
1005 do
1006 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001007 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008
1009 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1010 continue;
1011
Paul Bakker9af723c2014-05-01 13:03:14 +02001012 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001013 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard215a14b2015-10-21 10:16:29 +02001014 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001015 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001016 if( w_ret == 0 )
Ron Eldor454da1f2017-01-09 15:09:16 +02001017 {
1018 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1019 goto cleanup;
1020 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021
Paul Bakkerddf26b42013-09-18 13:46:23 +02001022 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 if( w_ret < 0 )
1024 ret++;
1025 else
1026 ret += w_ret;
1027 }
1028 while( FindNextFileW( hFind, &file_data ) != 0 );
1029
Paul Bakker66d5d072014-06-17 16:39:18 +02001030 if( GetLastError() != ERROR_NO_MORE_FILES )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1032
Ron Eldor454da1f2017-01-09 15:09:16 +02001033cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001035#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001036 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001038 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039 char entry_name[255];
1040 DIR *dir = opendir( path );
1041
Paul Bakker66d5d072014-06-17 16:39:18 +02001042 if( dir == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1044
Ron Eldor77718242017-01-09 19:27:59 +02001045#if defined(POLARSSL_THREADING_C)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001046 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1047 return( ret );
Ron Eldor77718242017-01-09 19:27:59 +02001048#endif /* POLARSSL_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001049
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001050 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051 {
Rich Evansfac657f2015-01-30 11:00:01 +00001052 polarssl_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001054 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055 {
1056 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001057 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1058 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059 }
1060
1061 if( !S_ISREG( sb.st_mode ) )
1062 continue;
1063
1064 // Ignore parse errors
1065 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001066 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067 if( t_ret < 0 )
1068 ret++;
1069 else
1070 ret += t_ret;
1071 }
1072 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001073
1074cleanup:
Ron Eldor77718242017-01-09 19:27:59 +02001075#if defined(POLARSSL_THREADING_C)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001076 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1077 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
Ron Eldor77718242017-01-09 19:27:59 +02001078#endif /* POLARSSL_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001079
Paul Bakkerbe089b02013-10-14 15:51:50 +02001080#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081
1082 return( ret );
1083}
1084#endif /* POLARSSL_FS_IO */
1085
Paul Bakker6edcd412013-10-29 15:22:54 +01001086#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1087 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088#include <stdarg.h>
1089
1090#if !defined vsnprintf
1091#define vsnprintf _vsnprintf
1092#endif // vsnprintf
1093
1094/*
1095 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1096 * Result value is not size of buffer needed, but -1 if no fit is possible.
1097 *
1098 * This fuction tries to 'fix' this by at least suggesting enlarging the
1099 * size by 20.
1100 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001101static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102{
1103 va_list ap;
1104 int res = -1;
1105
1106 va_start( ap, format );
1107
1108 res = vsnprintf( str, size, format, ap );
1109
1110 va_end( ap );
1111
1112 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +02001113 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114 return( (int) size + 20 );
1115
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001116 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117}
1118
1119#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001120#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121
1122#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1123
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001124#define SAFE_SNPRINTF() \
1125{ \
1126 if( ret == -1 ) \
1127 return( -1 ); \
1128 \
Paul Bakker66d5d072014-06-17 16:39:18 +02001129 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001130 p[n - 1] = '\0'; \
1131 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
1132 } \
1133 \
1134 n -= (unsigned int) ret; \
1135 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001136}
1137
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001138static int x509_info_subject_alt_name( char **buf, size_t *size,
1139 const x509_sequence *subject_alt_name )
1140{
1141 size_t i;
1142 size_t n = *size;
1143 char *p = *buf;
1144 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001145 const char *sep = "";
1146 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001147
1148 while( cur != NULL )
1149 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001150 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001151 {
1152 *p = '\0';
1153 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1154 }
1155
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001156 n -= cur->buf.len + sep_len;
1157 for( i = 0; i < sep_len; i++ )
1158 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001159 for( i = 0; i < cur->buf.len; i++ )
1160 *p++ = cur->buf.p[i];
1161
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001162 sep = ", ";
1163 sep_len = 2;
1164
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001165 cur = cur->next;
1166 }
1167
1168 *p = '\0';
1169
1170 *size = n;
1171 *buf = p;
1172
1173 return( 0 );
1174}
1175
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001176#define PRINT_ITEM(i) \
1177 { \
Rich Evansfac657f2015-01-30 11:00:01 +00001178 ret = polarssl_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001179 SAFE_SNPRINTF(); \
1180 sep = ", "; \
1181 }
1182
1183#define CERT_TYPE(type,name) \
1184 if( ns_cert_type & type ) \
1185 PRINT_ITEM( name );
1186
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001187static int x509_info_cert_type( char **buf, size_t *size,
1188 unsigned char ns_cert_type )
1189{
1190 int ret;
1191 size_t n = *size;
1192 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001193 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001194
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001195 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1196 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1197 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1198 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1199 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1200 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1201 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1202 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001203
1204 *size = n;
1205 *buf = p;
1206
1207 return( 0 );
1208}
1209
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001210#define KEY_USAGE(code,name) \
1211 if( key_usage & code ) \
1212 PRINT_ITEM( name );
1213
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001214static int x509_info_key_usage( char **buf, size_t *size,
1215 unsigned char key_usage )
1216{
1217 int ret;
1218 size_t n = *size;
1219 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001220 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001221
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001222 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1223 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1224 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1225 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1226 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1227 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1228 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001229
1230 *size = n;
1231 *buf = p;
1232
1233 return( 0 );
1234}
1235
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001236static int x509_info_ext_key_usage( char **buf, size_t *size,
1237 const x509_sequence *extended_key_usage )
1238{
1239 int ret;
1240 const char *desc;
1241 size_t n = *size;
1242 char *p = *buf;
1243 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001244 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001245
1246 while( cur != NULL )
1247 {
1248 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1249 desc = "???";
1250
Rich Evansfac657f2015-01-30 11:00:01 +00001251 ret = polarssl_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001252 SAFE_SNPRINTF();
1253
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001254 sep = ", ";
1255
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001256 cur = cur->next;
1257 }
1258
1259 *size = n;
1260 *buf = p;
1261
1262 return( 0 );
1263}
1264
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001265/*
1266 * Return an informational string about the certificate.
1267 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001268#define BEFORE_COLON 18
1269#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001270int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001271 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001272{
1273 int ret;
1274 size_t n;
1275 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 char key_size_str[BEFORE_COLON];
1277
1278 p = buf;
1279 n = size;
1280
Rich Evansfac657f2015-01-30 11:00:01 +00001281 ret = polarssl_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282 prefix, crt->version );
1283 SAFE_SNPRINTF();
Rich Evansfac657f2015-01-30 11:00:01 +00001284 ret = polarssl_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001285 prefix );
1286 SAFE_SNPRINTF();
1287
Paul Bakker66d5d072014-06-17 16:39:18 +02001288 ret = x509_serial_gets( p, n, &crt->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001289 SAFE_SNPRINTF();
1290
Rich Evansfac657f2015-01-30 11:00:01 +00001291 ret = polarssl_snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001292 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001293 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001294 SAFE_SNPRINTF();
1295
Rich Evansfac657f2015-01-30 11:00:01 +00001296 ret = polarssl_snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001297 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001298 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001299 SAFE_SNPRINTF();
1300
Rich Evansfac657f2015-01-30 11:00:01 +00001301 ret = polarssl_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001302 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1303 crt->valid_from.year, crt->valid_from.mon,
1304 crt->valid_from.day, crt->valid_from.hour,
1305 crt->valid_from.min, crt->valid_from.sec );
1306 SAFE_SNPRINTF();
1307
Rich Evansfac657f2015-01-30 11:00:01 +00001308 ret = polarssl_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001309 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1310 crt->valid_to.year, crt->valid_to.mon,
1311 crt->valid_to.day, crt->valid_to.hour,
1312 crt->valid_to.min, crt->valid_to.sec );
1313 SAFE_SNPRINTF();
1314
Rich Evansfac657f2015-01-30 11:00:01 +00001315 ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001316 SAFE_SNPRINTF();
1317
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001318 ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001319 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001320 SAFE_SNPRINTF();
1321
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001322 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001323 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1324 pk_get_name( &crt->pk ) ) ) != 0 )
1325 {
1326 return( ret );
1327 }
1328
Rich Evansfac657f2015-01-30 11:00:01 +00001329 ret = polarssl_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001330 (int) pk_get_size( &crt->pk ) );
1331 SAFE_SNPRINTF();
1332
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001333 /*
1334 * Optional extensions
1335 */
1336
1337 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1338 {
Rich Evansfac657f2015-01-30 11:00:01 +00001339 ret = polarssl_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001340 crt->ca_istrue ? "true" : "false" );
1341 SAFE_SNPRINTF();
1342
1343 if( crt->max_pathlen > 0 )
1344 {
Rich Evansfac657f2015-01-30 11:00:01 +00001345 ret = polarssl_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001346 SAFE_SNPRINTF();
1347 }
1348 }
1349
1350 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1351 {
Rich Evansfac657f2015-01-30 11:00:01 +00001352 ret = polarssl_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001353 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001354
1355 if( ( ret = x509_info_subject_alt_name( &p, &n,
1356 &crt->subject_alt_names ) ) != 0 )
1357 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001358 }
1359
1360 if( crt->ext_types & EXT_NS_CERT_TYPE )
1361 {
Rich Evansfac657f2015-01-30 11:00:01 +00001362 ret = polarssl_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001363 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001364
1365 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1366 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001367 }
1368
1369 if( crt->ext_types & EXT_KEY_USAGE )
1370 {
Rich Evansfac657f2015-01-30 11:00:01 +00001371 ret = polarssl_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001372 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001373
1374 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1375 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001376 }
1377
1378 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1379 {
Rich Evansfac657f2015-01-30 11:00:01 +00001380 ret = polarssl_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001381 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001382
1383 if( ( ret = x509_info_ext_key_usage( &p, &n,
1384 &crt->ext_key_usage ) ) != 0 )
1385 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001386 }
1387
Rich Evansfac657f2015-01-30 11:00:01 +00001388 ret = polarssl_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001389 SAFE_SNPRINTF();
1390
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001391 return( (int) ( size - n ) );
1392}
1393
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +02001394struct x509_crt_verify_string {
1395 int code;
1396 const char *string;
1397};
1398
1399static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
1400 { BADCERT_EXPIRED, "The certificate validity has expired" },
1401 { BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1402 { BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1403 { BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1404 { BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1405 { BADCRL_EXPIRED, "The CRL is expired" },
1406 { BADCERT_MISSING, "Certificate was missing" },
1407 { BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1408 { BADCERT_OTHER, "Other reason (can be used by verify callback)" },
1409 { BADCERT_FUTURE, "The certificate validity starts in the future" },
1410 { BADCRL_FUTURE, "The CRL is from the future" },
1411 { BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1412 { BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1413 { BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Hanno Becker888c2fd2017-05-11 11:12:40 +01001414 { BADCERT_BAD_KEY, "The certificate uses an invalid key (e.g. unsupported elliptic curve)" },
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +02001415 { 0, NULL }
1416};
1417
1418int x509_crt_verify_info( char *buf, size_t size, const char *prefix,
1419 int flags )
1420{
1421 int ret;
1422 const struct x509_crt_verify_string *cur;
1423 char *p = buf;
1424 size_t n = size;
1425
1426 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1427 {
1428 if( ( flags & cur->code ) == 0 )
1429 continue;
1430
1431 ret = polarssl_snprintf( p, n, "%s%s\n", prefix, cur->string );
1432 SAFE_SNPRINTF();
1433 flags ^= cur->code;
1434 }
1435
1436 if( flags != 0 )
1437 {
1438 ret = polarssl_snprintf( p, n, "%sUnknown reason "
1439 "(this should not happen)\n", prefix );
1440 SAFE_SNPRINTF();
1441 }
1442
1443 return( (int) ( size - n ) );
1444}
1445
Ron Eldora9ec0cd2017-02-09 19:29:33 +02001446/*
1447 * Check md_alg against profile
1448 * Return 0 if md_alg acceptable for this profile, -1 otherwise
1449 */
1450static int x509_check_md_alg( md_type_t md_alg )
1451{
Manuel Pégourié-Gonnard48ed5502017-06-08 17:27:20 +02001452 if( md_alg >= POLARSSL_X509_MIN_VERIFY_MD_ALG )
Ron Eldora9ec0cd2017-02-09 19:29:33 +02001453 return( 0 );
1454
1455 return( -1 );
1456}
1457
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001458#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1459int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1460{
1461 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1462 ( crt->key_usage & usage ) != usage )
1463 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1464
1465 return( 0 );
1466}
1467#endif
1468
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001469#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1470int x509_crt_check_extended_key_usage( const x509_crt *crt,
1471 const char *usage_oid,
1472 size_t usage_len )
1473{
1474 const x509_sequence *cur;
1475
1476 /* Extension is not mandatory, absent means no restriction */
1477 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1478 return( 0 );
1479
1480 /*
1481 * Look for the requested usage (or wildcard ANY) in our list
1482 */
1483 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1484 {
1485 const x509_buf *cur_oid = &cur->buf;
1486
1487 if( cur_oid->len == usage_len &&
1488 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1489 {
1490 return( 0 );
1491 }
1492
1493 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1494 return( 0 );
1495 }
1496
1497 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1498}
Paul Bakker9af723c2014-05-01 13:03:14 +02001499#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001500
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001501#if defined(POLARSSL_X509_CRL_PARSE_C)
1502/*
1503 * Return 1 if the certificate is revoked, or 0 otherwise.
1504 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001505int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506{
1507 const x509_crl_entry *cur = &crl->entry;
1508
1509 while( cur != NULL && cur->serial.len != 0 )
1510 {
1511 if( crt->serial.len == cur->serial.len &&
1512 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1513 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001514 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515 return( 1 );
1516 }
1517
1518 cur = cur->next;
1519 }
1520
1521 return( 0 );
1522}
1523
1524/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001525 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001527static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001528 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529{
1530 int flags = 0;
1531 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1532 const md_info_t *md_info;
1533
1534 if( ca == NULL )
1535 return( flags );
1536
1537 /*
1538 * TODO: What happens if no CRL is present?
1539 * Suggestion: Revocation state should be unknown if no CRL is present.
1540 * For backwards compatibility this is not yet implemented.
1541 */
1542
1543 while( crl_list != NULL )
1544 {
1545 if( crl_list->version == 0 ||
1546 crl_list->issuer_raw.len != ca->subject_raw.len ||
1547 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1548 crl_list->issuer_raw.len ) != 0 )
1549 {
1550 crl_list = crl_list->next;
1551 continue;
1552 }
1553
1554 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001555 * Check if the CA is configured to sign CRLs
1556 */
1557#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1558 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1559 {
1560 flags |= BADCRL_NOT_TRUSTED;
1561 break;
1562 }
1563#endif
1564
1565 /*
Ron Eldora9ec0cd2017-02-09 19:29:33 +02001566 * Check if CRL is signed with a valid MD
1567 */
1568 if( x509_check_md_alg( crl_list->sig_md ) != 0 )
1569 {
1570 flags |= BADCRL_NOT_TRUSTED;
1571 break;
1572 }
1573
1574 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575 * Check if CRL is correctly signed by the trusted CA
1576 */
1577 md_info = md_info_from_type( crl_list->sig_md );
1578 if( md_info == NULL )
1579 {
1580 /*
1581 * Cannot check 'unknown' hash
1582 */
1583 flags |= BADCRL_NOT_TRUSTED;
1584 break;
1585 }
1586
1587 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1588
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001589 if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1590 crl_list->sig_md, hash, md_info->size,
1591 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592 {
1593 flags |= BADCRL_NOT_TRUSTED;
1594 break;
1595 }
1596
1597 /*
1598 * Check for validity of CRL (Do not drop out)
1599 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001600 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001601 flags |= BADCRL_EXPIRED;
1602
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001603 if( x509_time_future( &crl_list->this_update ) )
1604 flags |= BADCRL_FUTURE;
1605
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001606 /*
1607 * Check if certificate is revoked
1608 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001609 if( x509_crt_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001610 {
1611 flags |= BADCERT_REVOKED;
1612 break;
1613 }
1614
1615 crl_list = crl_list->next;
1616 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001617 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001618}
1619#endif /* POLARSSL_X509_CRL_PARSE_C */
1620
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001621/*
1622 * Like memcmp, but case-insensitive and always returns -1 if different
1623 */
1624static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625{
1626 size_t i;
1627 unsigned char diff;
1628 const unsigned char *n1 = s1, *n2 = s2;
1629
1630 for( i = 0; i < len; i++ )
1631 {
1632 diff = n1[i] ^ n2[i];
1633
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001634 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635 continue;
1636
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001637 if( diff == 32 &&
1638 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1639 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1640 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001642 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001643
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001644 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001645 }
1646
1647 return( 0 );
1648}
1649
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001650/*
1651 * Return 1 if match, 0 if not
1652 * TODO: inverted return value!
1653 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001654static int x509_wildcard_verify( const char *cn, x509_buf *name )
1655{
1656 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001657 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001658
1659 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1660 return( 0 );
1661
Paul Bakker14b16c62014-05-28 11:33:54 +02001662 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001663 {
1664 if( cn[i] == '.' )
1665 {
1666 cn_idx = i;
1667 break;
1668 }
1669 }
1670
1671 if( cn_idx == 0 )
1672 return( 0 );
1673
Paul Bakker14b16c62014-05-28 11:33:54 +02001674 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001675 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001676 {
1677 return( 1 );
1678 }
1679
1680 return( 0 );
1681}
1682
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001683/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001684 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1685 * variations (but not all).
1686 *
1687 * Return 0 if equal, -1 otherwise.
1688 */
1689static int x509_string_cmp( const x509_buf *a, const x509_buf *b )
1690{
1691 if( a->tag == b->tag &&
1692 a->len == b->len &&
1693 memcmp( a->p, b->p, b->len ) == 0 )
1694 {
1695 return( 0 );
1696 }
1697
1698 if( ( a->tag == ASN1_UTF8_STRING || a->tag == ASN1_PRINTABLE_STRING ) &&
1699 ( b->tag == ASN1_UTF8_STRING || b->tag == ASN1_PRINTABLE_STRING ) &&
1700 a->len == b->len &&
1701 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1702 {
1703 return( 0 );
1704 }
1705
1706 return( -1 );
1707}
1708
1709/*
1710 * Compare two X.509 Names (aka rdnSequence).
1711 *
1712 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1713 * we sometimes return unequal when the full algorithm would return equal,
1714 * but never the other way. (In particular, we don't do Unicode normalisation
1715 * or space folding.)
1716 *
1717 * Return 0 if equal, -1 otherwise.
1718 */
1719static int x509_name_cmp( const x509_name *a, const x509_name *b )
1720{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001721 /* Avoid recursion, it might not be optimised by the compiler */
1722 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001723 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001724 if( a == NULL || b == NULL )
1725 return( -1 );
1726
1727 /* type */
1728 if( a->oid.tag != b->oid.tag ||
1729 a->oid.len != b->oid.len ||
1730 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1731 {
1732 return( -1 );
1733 }
1734
1735 /* value */
1736 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1737 return( -1 );
1738
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001739 /* structure of the list of sets */
1740 if( a->next_merged != b->next_merged )
1741 return( -1 );
1742
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001743 a = a->next;
1744 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001745 }
1746
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001747 /* a == NULL == b */
1748 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001749}
1750
1751/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001752 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1753 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001754 *
1755 * top means parent is a locally-trusted certificate
1756 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001757 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001758static int x509_crt_check_parent( const x509_crt *child,
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001759 const x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001760 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001761{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001762 int need_ca_bit;
1763
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001764 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001765 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001766 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001767
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001768 /* Parent must have the basicConstraints CA bit set as a general rule */
1769 need_ca_bit = 1;
1770
1771 /* Exception: v1/v2 certificates that are locally trusted. */
1772 if( top && parent->version < 3 )
1773 need_ca_bit = 0;
1774
1775 /* Exception: self-signed end-entity certs that are locally trusted. */
1776 if( top && bottom &&
1777 child->raw.len == parent->raw.len &&
1778 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1779 {
1780 need_ca_bit = 0;
1781 }
1782
1783 if( need_ca_bit && ! parent->ca_istrue )
1784 return( -1 );
1785
1786#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1787 if( need_ca_bit &&
1788 x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001789 {
1790 return( -1 );
1791 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001792#endif
1793
1794 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001795}
1796
Paul Bakkerddf26b42013-09-18 13:46:23 +02001797static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001798 x509_crt *child, x509_crt *trust_ca,
Janos Follath92ac0592015-10-12 09:02:20 +02001799 x509_crl *ca_crl,
1800 int path_cnt, int self_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001801 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001802 void *p_vrfy )
1803{
1804 int ret;
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001805 int ca_flags = 0, check_path_cnt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001806 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1807 const md_info_t *md_info;
Andres AG2f3fe702017-01-20 17:07:46 +00001808 x509_crt *future_past_ca = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001809
Paul Bakker86d0c192013-09-18 11:11:02 +02001810 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001811 *flags |= BADCERT_EXPIRED;
1812
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001813 if( x509_time_future( &child->valid_from ) )
1814 *flags |= BADCERT_FUTURE;
1815
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001816 /*
1817 * Child is the top of the chain. Check against the trust_ca list.
1818 */
1819 *flags |= BADCERT_NOT_TRUSTED;
1820
Ron Eldora9ec0cd2017-02-09 19:29:33 +02001821 /*
1822 * Check if certificate is signed with a valid MD
1823 */
1824 if( x509_check_md_alg( child->sig_md ) != 0 )
1825 {
1826 *flags |= BADCERT_NOT_TRUSTED;
1827 /*
1828 * not signed with a valid MD, no need to check trust_ca
1829 */
1830 trust_ca = NULL;
1831 }
1832
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001833 md_info = md_info_from_type( child->sig_md );
1834 if( md_info == NULL )
1835 {
1836 /*
1837 * Cannot check 'unknown', no need to try any CA
1838 */
1839 trust_ca = NULL;
1840 }
1841 else
1842 md( md_info, child->tbs.p, child->tbs.len, hash );
1843
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001844 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001845 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001846 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001847 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001848
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001849 check_path_cnt = path_cnt + 1;
1850
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001851 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001852 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001853 * the same as the trusted CA
1854 */
1855 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1856 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1857 child->issuer_raw.len ) == 0 )
1858 {
1859 check_path_cnt--;
1860 }
1861
Janos Follath92ac0592015-10-12 09:02:20 +02001862 /* Self signed certificates do not count towards the limit */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001863 if( trust_ca->max_pathlen > 0 &&
Janos Follath92ac0592015-10-12 09:02:20 +02001864 trust_ca->max_pathlen < check_path_cnt - self_cnt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001865 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001866 continue;
1867 }
1868
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001869 if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1870 child->sig_md, hash, md_info->size,
1871 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001872 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001873 continue;
1874 }
1875
Andres AG2f3fe702017-01-20 17:07:46 +00001876 if( x509_time_expired( &trust_ca->valid_to ) ||
1877 x509_time_future( &trust_ca->valid_from ) )
1878 {
1879 if( future_past_ca == NULL )
1880 future_past_ca = trust_ca;
1881 continue;
1882 }
1883
1884 break;
1885 }
1886
1887 if( trust_ca != NULL || ( trust_ca = future_past_ca ) != NULL )
1888 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001889 /*
1890 * Top of chain is signed by a trusted CA
1891 */
1892 *flags &= ~BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001893 }
1894
1895 /*
1896 * If top of chain is not the same as the trusted CA send a verify request
1897 * to the callback for any issues with validity and CRL presence for the
1898 * trusted CA certificate.
1899 */
1900 if( trust_ca != NULL &&
1901 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1902 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1903 child->issuer_raw.len ) != 0 ) )
1904 {
1905#if defined(POLARSSL_X509_CRL_PARSE_C)
1906 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001907 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001908#else
1909 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001910#endif
1911
Andres AG2f3fe702017-01-20 17:07:46 +00001912 if( x509_time_expired( &trust_ca->valid_to ) )
1913 ca_flags |= BADCERT_EXPIRED;
1914
1915 if( x509_time_future( &trust_ca->valid_from ) )
1916 ca_flags |= BADCERT_FUTURE;
1917
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001918 if( NULL != f_vrfy )
1919 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001920 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1921 &ca_flags ) ) != 0 )
1922 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001923 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001924 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001925 }
1926 }
1927
1928 /* Call callback on top cert */
1929 if( NULL != f_vrfy )
1930 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001931 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001932 return( ret );
1933 }
1934
1935 *flags |= ca_flags;
1936
1937 return( 0 );
1938}
1939
Paul Bakkerddf26b42013-09-18 13:46:23 +02001940static int x509_crt_verify_child(
Janos Follath92ac0592015-10-12 09:02:20 +02001941 x509_crt *child, x509_crt *parent,
1942 x509_crt *trust_ca, x509_crl *ca_crl,
1943 int path_cnt, int self_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001944 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001945 void *p_vrfy )
1946{
1947 int ret;
1948 int parent_flags = 0;
1949 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001950 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001951 const md_info_t *md_info;
1952
Janos Follath92ac0592015-10-12 09:02:20 +02001953 /* Counting intermediate self signed certificates */
1954 if( ( path_cnt != 0 ) && x509_name_cmp( &child->issuer, &child->subject ) == 0 )
1955 self_cnt++;
1956
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001957 /* path_cnt is 0 for the first intermediate CA */
1958 if( 1 + path_cnt > POLARSSL_X509_MAX_INTERMEDIATE_CA )
1959 {
1960 *flags |= BADCERT_NOT_TRUSTED;
1961 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1962 }
1963
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001964 if( x509_time_expired( &child->valid_to ) )
1965 *flags |= BADCERT_EXPIRED;
1966
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001967 if( x509_time_future( &child->valid_from ) )
1968 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001969
Ron Eldora9ec0cd2017-02-09 19:29:33 +02001970 /*
1971 * Check if certificate is signed with a valid MD
1972 */
1973 if( x509_check_md_alg( child->sig_md ) != 0 )
1974 *flags |= BADCERT_NOT_TRUSTED;
1975
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001976 md_info = md_info_from_type( child->sig_md );
1977 if( md_info == NULL )
1978 {
1979 /*
1980 * Cannot check 'unknown' hash
1981 */
1982 *flags |= BADCERT_NOT_TRUSTED;
1983 }
1984 else
1985 {
1986 md( md_info, child->tbs.p, child->tbs.len, hash );
1987
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001988 if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1989 child->sig_md, hash, md_info->size,
1990 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001991 {
1992 *flags |= BADCERT_NOT_TRUSTED;
1993 }
1994 }
1995
1996#if defined(POLARSSL_X509_CRL_PARSE_C)
1997 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001998 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001999#endif
2000
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002001 /* Look for a grandparent in trusted CAs */
2002 for( grandparent = trust_ca;
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002003 grandparent != NULL;
2004 grandparent = grandparent->next )
2005 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002006 if( x509_crt_check_parent( parent, grandparent,
2007 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002008 break;
2009 }
2010
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002011 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002012 {
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002013 ret = x509_crt_verify_top( parent, grandparent, ca_crl,
Janos Follath92ac0592015-10-12 09:02:20 +02002014 path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002015 if( ret != 0 )
2016 return( ret );
2017 }
2018 else
2019 {
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002020 /* Look for a grandparent upwards the chain */
2021 for( grandparent = parent->next;
2022 grandparent != NULL;
2023 grandparent = grandparent->next )
2024 {
Janos Follath92ac0592015-10-12 09:02:20 +02002025 /* +2 because the current step is not yet accounted for
2026 * and because max_pathlen is one higher than it should be.
2027 * Also self signed certificates do not count to the limit. */
2028 if( grandparent->max_pathlen > 0 &&
2029 grandparent->max_pathlen < 2 + path_cnt - self_cnt )
2030 {
2031 continue;
2032 }
2033
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002034 if( x509_crt_check_parent( parent, grandparent,
2035 0, path_cnt == 0 ) == 0 )
2036 break;
2037 }
2038
2039 /* Is our parent part of the chain or at the top? */
2040 if( grandparent != NULL )
2041 {
2042 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
Janos Follath92ac0592015-10-12 09:02:20 +02002043 path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002044 f_vrfy, p_vrfy );
2045 if( ret != 0 )
2046 return( ret );
2047 }
2048 else
2049 {
2050 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
Janos Follath92ac0592015-10-12 09:02:20 +02002051 path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002052 f_vrfy, p_vrfy );
2053 if( ret != 0 )
2054 return( ret );
2055 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002056 }
2057
2058 /* child is verified to be a child of the parent, call verify callback */
2059 if( NULL != f_vrfy )
2060 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2061 return( ret );
2062
2063 *flags |= parent_flags;
2064
2065 return( 0 );
2066}
2067
2068/*
2069 * Verify the certificate validity
2070 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002071int x509_crt_verify( x509_crt *crt,
2072 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02002073 x509_crl *ca_crl,
2074 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002075 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002076 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002077{
2078 size_t cn_len;
2079 int ret;
2080 int pathlen = 0;
Janos Follath92ac0592015-10-12 09:02:20 +02002081 int selfsigned = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002082 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002083 x509_name *name;
2084 x509_sequence *cur = NULL;
2085
2086 *flags = 0;
2087
2088 if( cn != NULL )
2089 {
2090 name = &crt->subject;
2091 cn_len = strlen( cn );
2092
2093 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
2094 {
2095 cur = &crt->subject_alt_names;
2096
2097 while( cur != NULL )
2098 {
2099 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002100 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002101 break;
2102
2103 if( cur->buf.len > 2 &&
2104 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
2105 x509_wildcard_verify( cn, &cur->buf ) )
2106 break;
2107
2108 cur = cur->next;
2109 }
2110
2111 if( cur == NULL )
2112 *flags |= BADCERT_CN_MISMATCH;
2113 }
2114 else
2115 {
2116 while( name != NULL )
2117 {
2118 if( OID_CMP( OID_AT_CN, &name->oid ) )
2119 {
2120 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002121 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002122 break;
2123
2124 if( name->val.len > 2 &&
2125 memcmp( name->val.p, "*.", 2 ) == 0 &&
2126 x509_wildcard_verify( cn, &name->val ) )
2127 break;
2128 }
2129
2130 name = name->next;
2131 }
2132
2133 if( name == NULL )
2134 *flags |= BADCERT_CN_MISMATCH;
2135 }
2136 }
2137
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002138 /* Look for a parent in trusted CAs */
2139 for( parent = trust_ca; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002140 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002141 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002142 break;
2143 }
2144
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002145 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002146 {
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002147 ret = x509_crt_verify_top( crt, parent, ca_crl,
Janos Follath92ac0592015-10-12 09:02:20 +02002148 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002149 if( ret != 0 )
Manuel Pégourié-Gonnard8af7bfa2017-07-10 11:20:08 +02002150 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002151 }
2152 else
2153 {
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002154 /* Look for a parent upwards the chain */
2155 for( parent = crt->next; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002156 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
2157 break;
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002158
2159 /* Are we part of the chain or at the top? */
2160 if( parent != NULL )
2161 {
2162 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
Janos Follath92ac0592015-10-12 09:02:20 +02002163 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002164 if( ret != 0 )
Manuel Pégourié-Gonnard8af7bfa2017-07-10 11:20:08 +02002165 goto exit;
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002166 }
2167 else
2168 {
2169 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
Janos Follath92ac0592015-10-12 09:02:20 +02002170 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002171 if( ret != 0 )
Manuel Pégourié-Gonnard8af7bfa2017-07-10 11:20:08 +02002172 goto exit;
Manuel Pégourié-Gonnard1c385502015-09-01 16:35:00 +02002173 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002174 }
2175
Manuel Pégourié-Gonnard8af7bfa2017-07-10 11:20:08 +02002176exit:
2177 if( ret != 0 )
2178 {
2179 *flags = (uint32_t) -1;
2180 return( ret );
2181 }
2182
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002183 if( *flags != 0 )
2184 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
2185
2186 return( 0 );
2187}
2188
2189/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002190 * Initialize a certificate chain
2191 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002192void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002193{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002194 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002195}
2196
2197/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002198 * Unallocate all certificate data
2199 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002200void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002201{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002202 x509_crt *cert_cur = crt;
2203 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002204 x509_name *name_cur;
2205 x509_name *name_prv;
2206 x509_sequence *seq_cur;
2207 x509_sequence *seq_prv;
2208
2209 if( crt == NULL )
2210 return;
2211
2212 do
2213 {
2214 pk_free( &cert_cur->pk );
2215
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +02002216#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002217 polarssl_free( cert_cur->sig_opts );
2218#endif
2219
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002220 name_cur = cert_cur->issuer.next;
2221 while( name_cur != NULL )
2222 {
2223 name_prv = name_cur;
2224 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002225 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002226 polarssl_free( name_prv );
2227 }
2228
2229 name_cur = cert_cur->subject.next;
2230 while( name_cur != NULL )
2231 {
2232 name_prv = name_cur;
2233 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002234 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002235 polarssl_free( name_prv );
2236 }
2237
2238 seq_cur = cert_cur->ext_key_usage.next;
2239 while( seq_cur != NULL )
2240 {
2241 seq_prv = seq_cur;
2242 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002243 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002244 polarssl_free( seq_prv );
2245 }
2246
2247 seq_cur = cert_cur->subject_alt_names.next;
2248 while( seq_cur != NULL )
2249 {
2250 seq_prv = seq_cur;
2251 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002252 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002253 polarssl_free( seq_prv );
2254 }
2255
2256 if( cert_cur->raw.p != NULL )
2257 {
Paul Bakker34617722014-06-13 17:20:13 +02002258 polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002259 polarssl_free( cert_cur->raw.p );
2260 }
2261
2262 cert_cur = cert_cur->next;
2263 }
2264 while( cert_cur != NULL );
2265
2266 cert_cur = crt;
2267 do
2268 {
2269 cert_prv = cert_cur;
2270 cert_cur = cert_cur->next;
2271
Paul Bakker34617722014-06-13 17:20:13 +02002272 polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002273 if( cert_prv != crt )
2274 polarssl_free( cert_prv );
2275 }
2276 while( cert_cur != NULL );
2277}
2278
Paul Bakker9af723c2014-05-01 13:03:14 +02002279#endif /* POLARSSL_X509_CRT_PARSE_C */