blob: d6164a865863a5922eaf5a246fd17bf703988cbf [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The ITU-T X.509 standard defines a certificate format for PKI.
27 *
28 * http://www.ietf.org/rfc/rfc3279.txt
29 * http://www.ietf.org/rfc/rfc3280.txt
30 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020039#else
40#include POLARSSL_CONFIG_FILE
41#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042
43#if defined(POLARSSL_X509_CRT_PARSE_C)
44
45#include "polarssl/x509_crt.h"
46#include "polarssl/oid.h"
47#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
54#define polarssl_malloc malloc
55#define polarssl_free free
56#endif
57
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010058#if defined(POLARSSL_THREADING_C)
59#include "polarssl/threading.h"
60#endif
61
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#include <string.h>
63#include <stdlib.h>
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
Paul Bakkerfa6a6202013-10-28 18:48:30 +010070#if defined(EFIX64) || defined(EFI32)
71#include <stdio.h>
72#endif
73
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#if defined(POLARSSL_FS_IO)
75#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020076#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#include <sys/types.h>
78#include <sys/stat.h>
79#include <dirent.h>
80#endif
81#endif
82
83/*
84 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
85 */
86static int x509_get_version( unsigned char **p,
87 const unsigned char *end,
88 int *ver )
89{
90 int ret;
91 size_t len;
92
93 if( ( ret = asn1_get_tag( p, end, &len,
94 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
95 {
96 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
97 {
98 *ver = 0;
99 return( 0 );
100 }
101
102 return( ret );
103 }
104
105 end = *p + len;
106
107 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200108 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109
110 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200111 return( POLARSSL_ERR_X509_INVALID_VERSION +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
113
114 return( 0 );
115}
116
117/*
118 * Validity ::= SEQUENCE {
119 * notBefore Time,
120 * notAfter Time }
121 */
122static int x509_get_dates( unsigned char **p,
123 const unsigned char *end,
124 x509_time *from,
125 x509_time *to )
126{
127 int ret;
128 size_t len;
129
130 if( ( ret = asn1_get_tag( p, end, &len,
131 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200132 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200133
134 end = *p + len;
135
136 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
137 return( ret );
138
139 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
140 return( ret );
141
142 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200143 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200144 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
145
146 return( 0 );
147}
148
149/*
150 * X.509 v2/v3 unique identifier (not parsed)
151 */
152static int x509_get_uid( unsigned char **p,
153 const unsigned char *end,
154 x509_buf *uid, int n )
155{
156 int ret;
157
158 if( *p == end )
159 return( 0 );
160
161 uid->tag = **p;
162
163 if( ( ret = asn1_get_tag( p, end, &uid->len,
164 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
165 {
166 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
167 return( 0 );
168
169 return( ret );
170 }
171
172 uid->p = *p;
173 *p += uid->len;
174
175 return( 0 );
176}
177
178static int x509_get_basic_constraints( unsigned char **p,
179 const unsigned char *end,
180 int *ca_istrue,
181 int *max_pathlen )
182{
183 int ret;
184 size_t len;
185
186 /*
187 * BasicConstraints ::= SEQUENCE {
188 * cA BOOLEAN DEFAULT FALSE,
189 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
190 */
191 *ca_istrue = 0; /* DEFAULT FALSE */
192 *max_pathlen = 0; /* endless */
193
194 if( ( ret = asn1_get_tag( p, end, &len,
195 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200196 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200197
198 if( *p == end )
199 return 0;
200
201 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
202 {
203 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
204 ret = asn1_get_int( p, end, ca_istrue );
205
206 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200207 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200208
209 if( *ca_istrue != 0 )
210 *ca_istrue = 1;
211 }
212
213 if( *p == end )
214 return 0;
215
216 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200217 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200218
219 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200220 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
222
223 (*max_pathlen)++;
224
225 return 0;
226}
227
228static int x509_get_ns_cert_type( unsigned char **p,
229 const unsigned char *end,
230 unsigned char *ns_cert_type)
231{
232 int ret;
233 x509_bitstring bs = { 0, 0, NULL };
234
235 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200236 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200237
238 if( bs.len != 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200239 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200240 POLARSSL_ERR_ASN1_INVALID_LENGTH );
241
242 /* Get actual bitstring */
243 *ns_cert_type = *bs.p;
244 return 0;
245}
246
247static int x509_get_key_usage( unsigned char **p,
248 const unsigned char *end,
249 unsigned char *key_usage)
250{
251 int ret;
252 x509_bitstring bs = { 0, 0, NULL };
253
254 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200255 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256
257 if( bs.len < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200258 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200259 POLARSSL_ERR_ASN1_INVALID_LENGTH );
260
261 /* Get actual bitstring */
262 *key_usage = *bs.p;
263 return 0;
264}
265
266/*
267 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
268 *
269 * KeyPurposeId ::= OBJECT IDENTIFIER
270 */
271static int x509_get_ext_key_usage( unsigned char **p,
272 const unsigned char *end,
273 x509_sequence *ext_key_usage)
274{
275 int ret;
276
277 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200278 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200279
280 /* Sequence length must be >= 1 */
281 if( ext_key_usage->buf.p == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200282 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283 POLARSSL_ERR_ASN1_INVALID_LENGTH );
284
285 return 0;
286}
287
288/*
289 * SubjectAltName ::= GeneralNames
290 *
291 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
292 *
293 * GeneralName ::= CHOICE {
294 * otherName [0] OtherName,
295 * rfc822Name [1] IA5String,
296 * dNSName [2] IA5String,
297 * x400Address [3] ORAddress,
298 * directoryName [4] Name,
299 * ediPartyName [5] EDIPartyName,
300 * uniformResourceIdentifier [6] IA5String,
301 * iPAddress [7] OCTET STRING,
302 * registeredID [8] OBJECT IDENTIFIER }
303 *
304 * OtherName ::= SEQUENCE {
305 * type-id OBJECT IDENTIFIER,
306 * value [0] EXPLICIT ANY DEFINED BY type-id }
307 *
308 * EDIPartyName ::= SEQUENCE {
309 * nameAssigner [0] DirectoryString OPTIONAL,
310 * partyName [1] DirectoryString }
311 *
312 * NOTE: PolarSSL only parses and uses dNSName at this point.
313 */
314static int x509_get_subject_alt_name( unsigned char **p,
315 const unsigned char *end,
316 x509_sequence *subject_alt_name )
317{
318 int ret;
319 size_t len, tag_len;
320 asn1_buf *buf;
321 unsigned char tag;
322 asn1_sequence *cur = subject_alt_name;
323
324 /* Get main sequence tag */
325 if( ( ret = asn1_get_tag( p, end, &len,
326 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200327 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328
329 if( *p + len != end )
Paul Bakker51876562013-09-17 14:36:05 +0200330 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200331 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
332
333 while( *p < end )
334 {
335 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200336 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337 POLARSSL_ERR_ASN1_OUT_OF_DATA );
338
339 tag = **p;
340 (*p)++;
341 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200342 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343
344 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
Paul Bakker51876562013-09-17 14:36:05 +0200345 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200346 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
347
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200348 /* Skip everything but DNS name */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
350 {
351 *p += tag_len;
352 continue;
353 }
354
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200356 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357 {
358 cur->next = (asn1_sequence *) polarssl_malloc(
359 sizeof( asn1_sequence ) );
360
361 if( cur->next == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200362 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363 POLARSSL_ERR_ASN1_MALLOC_FAILED );
364
365 memset( cur->next, 0, sizeof( asn1_sequence ) );
366 cur = cur->next;
367 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200368
369 buf = &(cur->buf);
370 buf->tag = tag;
371 buf->p = *p;
372 buf->len = tag_len;
373 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374 }
375
376 /* Set final sequence entry's next pointer to NULL */
377 cur->next = NULL;
378
379 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200380 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
382
383 return( 0 );
384}
385
386/*
387 * X.509 v3 extensions
388 *
389 * TODO: Perform all of the basic constraints tests required by the RFC
390 * TODO: Set values for undetected extensions to a sane default?
391 *
392 */
393static int x509_get_crt_ext( unsigned char **p,
394 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200395 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396{
397 int ret;
398 size_t len;
399 unsigned char *end_ext_data, *end_ext_octet;
400
401 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
402 {
403 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
404 return( 0 );
405
406 return( ret );
407 }
408
409 while( *p < end )
410 {
411 /*
412 * Extension ::= SEQUENCE {
413 * extnID OBJECT IDENTIFIER,
414 * critical BOOLEAN DEFAULT FALSE,
415 * extnValue OCTET STRING }
416 */
417 x509_buf extn_oid = {0, 0, NULL};
418 int is_critical = 0; /* DEFAULT FALSE */
419 int ext_type = 0;
420
421 if( ( ret = asn1_get_tag( p, end, &len,
422 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200423 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424
425 end_ext_data = *p + len;
426
427 /* Get extension ID */
428 extn_oid.tag = **p;
429
430 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200431 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433 extn_oid.p = *p;
434 *p += extn_oid.len;
435
436 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200437 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438 POLARSSL_ERR_ASN1_OUT_OF_DATA );
439
440 /* Get optional critical */
441 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
442 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200443 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
445 /* Data should be octet string type */
446 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
447 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200448 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449
450 end_ext_octet = *p + len;
451
452 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200453 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
455
456 /*
457 * Detect supported extensions
458 */
459 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
460
461 if( ret != 0 )
462 {
463 /* No parser found, skip extension */
464 *p = end_ext_octet;
465
466#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
467 if( is_critical )
468 {
469 /* Data is marked as critical: fail */
Paul Bakker51876562013-09-17 14:36:05 +0200470 return ( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
472 }
473#endif
474 continue;
475 }
476
477 crt->ext_types |= ext_type;
478
479 switch( ext_type )
480 {
481 case EXT_BASIC_CONSTRAINTS:
482 /* Parse basic constraints */
483 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
484 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
485 return ( ret );
486 break;
487
488 case EXT_KEY_USAGE:
489 /* Parse key usage */
490 if( ( ret = x509_get_key_usage( p, end_ext_octet,
491 &crt->key_usage ) ) != 0 )
492 return ( ret );
493 break;
494
495 case EXT_EXTENDED_KEY_USAGE:
496 /* Parse extended key usage */
497 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
498 &crt->ext_key_usage ) ) != 0 )
499 return ( ret );
500 break;
501
502 case EXT_SUBJECT_ALT_NAME:
503 /* Parse subject alt name */
504 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
505 &crt->subject_alt_names ) ) != 0 )
506 return ( ret );
507 break;
508
509 case EXT_NS_CERT_TYPE:
510 /* Parse netscape certificate type */
511 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
512 &crt->ns_cert_type ) ) != 0 )
513 return ( ret );
514 break;
515
516 default:
517 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
518 }
519 }
520
521 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200522 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
524
525 return( 0 );
526}
527
528/*
529 * Parse and fill a single X.509 certificate in DER format
530 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200531static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200532 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533{
534 int ret;
535 size_t len;
536 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200537 x509_buf sig_params1, sig_params2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100538
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200539 memset( &sig_params1, 0, sizeof( x509_buf ) );
540 memset( &sig_params2, 0, sizeof( x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541
542 /*
543 * Check for valid input
544 */
545 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200546 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547
548 p = (unsigned char *) polarssl_malloc( len = buflen );
549
550 if( p == NULL )
551 return( POLARSSL_ERR_X509_MALLOC_FAILED );
552
553 memcpy( p, buf, buflen );
554
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555 crt->raw.p = p;
556 crt->raw.len = len;
557 end = p + len;
558
559 /*
560 * Certificate ::= SEQUENCE {
561 * tbsCertificate TBSCertificate,
562 * signatureAlgorithm AlgorithmIdentifier,
563 * signatureValue BIT STRING }
564 */
565 if( ( ret = asn1_get_tag( &p, end, &len,
566 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
567 {
568 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200569 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200570 }
571
572 if( len > (size_t) ( end - p ) )
573 {
574 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200575 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
577 }
578 crt_end = p + len;
579
580 /*
581 * TBSCertificate ::= SEQUENCE {
582 */
583 crt->tbs.p = p;
584
585 if( ( ret = asn1_get_tag( &p, end, &len,
586 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
587 {
588 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200589 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590 }
591
592 end = p + len;
593 crt->tbs.len = end - crt->tbs.p;
594
595 /*
596 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
597 *
598 * CertificateSerialNumber ::= INTEGER
599 *
600 * signature AlgorithmIdentifier
601 */
602 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
603 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100604 ( ret = x509_get_alg( &p, end, &crt->sig_oid1,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200605 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200606 {
607 x509_crt_free( crt );
608 return( ret );
609 }
610
611 crt->version++;
612
613 if( crt->version > 3 )
614 {
615 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200616 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617 }
618
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200619 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200620 &crt->sig_md, &crt->sig_pk,
621 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622 {
623 x509_crt_free( crt );
624 return( ret );
625 }
626
627 /*
628 * issuer Name
629 */
630 crt->issuer_raw.p = p;
631
632 if( ( ret = asn1_get_tag( &p, end, &len,
633 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
634 {
635 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200636 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637 }
638
639 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
640 {
641 x509_crt_free( crt );
642 return( ret );
643 }
644
645 crt->issuer_raw.len = p - crt->issuer_raw.p;
646
647 /*
648 * Validity ::= SEQUENCE {
649 * notBefore Time,
650 * notAfter Time }
651 *
652 */
653 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
654 &crt->valid_to ) ) != 0 )
655 {
656 x509_crt_free( crt );
657 return( ret );
658 }
659
660 /*
661 * subject Name
662 */
663 crt->subject_raw.p = p;
664
665 if( ( ret = asn1_get_tag( &p, end, &len,
666 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
667 {
668 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200669 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670 }
671
672 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
673 {
674 x509_crt_free( crt );
675 return( ret );
676 }
677
678 crt->subject_raw.len = p - crt->subject_raw.p;
679
680 /*
681 * SubjectPublicKeyInfo
682 */
Paul Bakkerda771152013-09-16 22:45:03 +0200683 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684 {
685 x509_crt_free( crt );
686 return( ret );
687 }
688
689 /*
690 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
691 * -- If present, version shall be v2 or v3
692 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
693 * -- If present, version shall be v2 or v3
694 * extensions [3] EXPLICIT Extensions OPTIONAL
695 * -- If present, version shall be v3
696 */
697 if( crt->version == 2 || crt->version == 3 )
698 {
699 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
700 if( ret != 0 )
701 {
702 x509_crt_free( crt );
703 return( ret );
704 }
705 }
706
707 if( crt->version == 2 || crt->version == 3 )
708 {
709 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
710 if( ret != 0 )
711 {
712 x509_crt_free( crt );
713 return( ret );
714 }
715 }
716
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200717#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718 if( crt->version == 3 )
719 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200720#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721 ret = x509_get_crt_ext( &p, end, crt);
722 if( ret != 0 )
723 {
724 x509_crt_free( crt );
725 return( ret );
726 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200727#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200729#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730
731 if( p != end )
732 {
733 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200734 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
736 }
737
738 end = crt_end;
739
740 /*
741 * }
742 * -- end of TBSCertificate
743 *
744 * signatureAlgorithm AlgorithmIdentifier,
745 * signatureValue BIT STRING
746 */
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200747 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748 {
749 x509_crt_free( crt );
750 return( ret );
751 }
752
753 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200754 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 ||
755 sig_params1.len != sig_params2.len ||
756 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757 {
758 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200759 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200760 }
761
762 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
763 {
764 x509_crt_free( crt );
765 return( ret );
766 }
767
768 if( p != end )
769 {
770 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200771 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
773 }
774
775 return( 0 );
776}
777
778/*
779 * Parse one X.509 certificate in DER format from a buffer and add them to a
780 * chained list
781 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200782int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200783 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784{
785 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200786 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200787
788 /*
789 * Check for valid input
790 */
791 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200792 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793
794 while( crt->version != 0 && crt->next != NULL )
795 {
796 prev = crt;
797 crt = crt->next;
798 }
799
800 /*
801 * Add new certificate on the end of the chain if needed.
802 */
803 if ( crt->version != 0 && crt->next == NULL)
804 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200805 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200806
807 if( crt->next == NULL )
808 return( POLARSSL_ERR_X509_MALLOC_FAILED );
809
810 prev = crt;
811 crt = crt->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200812 x509_crt_init( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813 }
814
Paul Bakkerddf26b42013-09-18 13:46:23 +0200815 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 {
817 if( prev )
818 prev->next = NULL;
819
820 if( crt != chain )
821 polarssl_free( crt );
822
823 return( ret );
824 }
825
826 return( 0 );
827}
828
829/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200830 * Parse one or more PEM certificates from a buffer and add them to the chained
831 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200833int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200834{
835 int success = 0, first_error = 0, total_failed = 0;
836 int buf_format = X509_FORMAT_DER;
837
838 /*
839 * Check for valid input
840 */
841 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200842 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200843
844 /*
845 * Determine buffer content. Buffer contains either one DER certificate or
846 * one or more PEM certificates.
847 */
848#if defined(POLARSSL_PEM_PARSE_C)
849 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
850 buf_format = X509_FORMAT_PEM;
851#endif
852
853 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200854 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200855
856#if defined(POLARSSL_PEM_PARSE_C)
857 if( buf_format == X509_FORMAT_PEM )
858 {
859 int ret;
860 pem_context pem;
861
862 while( buflen > 0 )
863 {
864 size_t use_len;
865 pem_init( &pem );
866
867 ret = pem_read_buffer( &pem,
868 "-----BEGIN CERTIFICATE-----",
869 "-----END CERTIFICATE-----",
870 buf, NULL, 0, &use_len );
871
872 if( ret == 0 )
873 {
874 /*
875 * Was PEM encoded
876 */
877 buflen -= use_len;
878 buf += use_len;
879 }
880 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
881 {
882 return( ret );
883 }
884 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
885 {
886 pem_free( &pem );
887
888 /*
889 * PEM header and footer were found
890 */
891 buflen -= use_len;
892 buf += use_len;
893
894 if( first_error == 0 )
895 first_error = ret;
896
897 continue;
898 }
899 else
900 break;
901
Paul Bakkerddf26b42013-09-18 13:46:23 +0200902 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903
904 pem_free( &pem );
905
906 if( ret != 0 )
907 {
908 /*
909 * Quit parsing on a memory error
910 */
911 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
912 return( ret );
913
914 if( first_error == 0 )
915 first_error = ret;
916
917 total_failed++;
918 continue;
919 }
920
921 success = 1;
922 }
923 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200924#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925
926 if( success )
927 return( total_failed );
928 else if( first_error )
929 return( first_error );
930 else
931 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
932}
933
934#if defined(POLARSSL_FS_IO)
935/*
936 * Load one or more certificates and add them to the chained list
937 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200938int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939{
940 int ret;
941 size_t n;
942 unsigned char *buf;
943
944 if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
945 return( ret );
946
Paul Bakkerddf26b42013-09-18 13:46:23 +0200947 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948
949 memset( buf, 0, n + 1 );
950 polarssl_free( buf );
951
952 return( ret );
953}
954
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100955#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100956static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
957#endif
958
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200959int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960{
961 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100962#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963 int w_ret;
964 WCHAR szDir[MAX_PATH];
965 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200966 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200967 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968
Paul Bakker9af723c2014-05-01 13:03:14 +0200969 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 HANDLE hFind;
971
972 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200973 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974
Paul Bakker9af723c2014-05-01 13:03:14 +0200975 memset( szDir, 0, sizeof(szDir) );
976 memset( filename, 0, MAX_PATH );
977 memcpy( filename, path, len );
978 filename[len++] = '\\';
979 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980 filename[len++] = '*';
981
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200982 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
983 MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984
985 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200986 if (hFind == INVALID_HANDLE_VALUE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
988
989 len = MAX_PATH - len;
990 do
991 {
Paul Bakker9af723c2014-05-01 13:03:14 +0200992 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993
994 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
995 continue;
996
Paul Bakker9af723c2014-05-01 13:03:14 +0200997 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
998 lstrlenW(file_data.cFileName),
999 p, len - 1,
1000 NULL, NULL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001
Paul Bakkerddf26b42013-09-18 13:46:23 +02001002 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003 if( w_ret < 0 )
1004 ret++;
1005 else
1006 ret += w_ret;
1007 }
1008 while( FindNextFileW( hFind, &file_data ) != 0 );
1009
Paul Bakker4aa40d42013-10-11 10:49:24 +02001010 if (GetLastError() != ERROR_NO_MORE_FILES)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1012
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001014#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001015 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001017 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018 char entry_name[255];
1019 DIR *dir = opendir( path );
1020
1021 if( dir == NULL)
1022 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1023
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001024#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001025 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1026 return( ret );
1027#endif
1028
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001029 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001031 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001033 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 {
1035 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001036 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1037 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038 }
1039
1040 if( !S_ISREG( sb.st_mode ) )
1041 continue;
1042
1043 // Ignore parse errors
1044 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001045 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046 if( t_ret < 0 )
1047 ret++;
1048 else
1049 ret += t_ret;
1050 }
1051 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001052
1053cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001054#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001055 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1056 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1057#endif
1058
Paul Bakkerbe089b02013-10-14 15:51:50 +02001059#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060
1061 return( ret );
1062}
1063#endif /* POLARSSL_FS_IO */
1064
Paul Bakker6edcd412013-10-29 15:22:54 +01001065#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1066 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067#include <stdarg.h>
1068
1069#if !defined vsnprintf
1070#define vsnprintf _vsnprintf
1071#endif // vsnprintf
1072
1073/*
1074 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1075 * Result value is not size of buffer needed, but -1 if no fit is possible.
1076 *
1077 * This fuction tries to 'fix' this by at least suggesting enlarging the
1078 * size by 20.
1079 */
1080static int compat_snprintf(char *str, size_t size, const char *format, ...)
1081{
1082 va_list ap;
1083 int res = -1;
1084
1085 va_start( ap, format );
1086
1087 res = vsnprintf( str, size, format, ap );
1088
1089 va_end( ap );
1090
1091 // No quick fix possible
1092 if ( res < 0 )
1093 return( (int) size + 20 );
1094
1095 return res;
1096}
1097
1098#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001099#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100
1101#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1102
1103#define SAFE_SNPRINTF() \
1104{ \
1105 if( ret == -1 ) \
1106 return( -1 ); \
1107 \
1108 if ( (unsigned int) ret > n ) { \
1109 p[n - 1] = '\0'; \
1110 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1111 } \
1112 \
1113 n -= (unsigned int) ret; \
1114 p += (unsigned int) ret; \
1115}
1116
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001117static int x509_info_subject_alt_name( char **buf, size_t *size,
1118 const x509_sequence *subject_alt_name )
1119{
1120 size_t i;
1121 size_t n = *size;
1122 char *p = *buf;
1123 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001124 const char *sep = "";
1125 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001126
1127 while( cur != NULL )
1128 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001129 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001130 {
1131 *p = '\0';
1132 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1133 }
1134
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001135 n -= cur->buf.len + sep_len;
1136 for( i = 0; i < sep_len; i++ )
1137 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001138 for( i = 0; i < cur->buf.len; i++ )
1139 *p++ = cur->buf.p[i];
1140
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001141 sep = ", ";
1142 sep_len = 2;
1143
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001144 cur = cur->next;
1145 }
1146
1147 *p = '\0';
1148
1149 *size = n;
1150 *buf = p;
1151
1152 return( 0 );
1153}
1154
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001155#define PRINT_ITEM(i) \
1156 { \
1157 ret = snprintf( p, n, "%s" i, sep ); \
1158 SAFE_SNPRINTF(); \
1159 sep = ", "; \
1160 }
1161
1162#define CERT_TYPE(type,name) \
1163 if( ns_cert_type & type ) \
1164 PRINT_ITEM( name );
1165
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001166static int x509_info_cert_type( char **buf, size_t *size,
1167 unsigned char ns_cert_type )
1168{
1169 int ret;
1170 size_t n = *size;
1171 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001172 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001173
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001174 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1175 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1176 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1177 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1178 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1179 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1180 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1181 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001182
1183 *size = n;
1184 *buf = p;
1185
1186 return( 0 );
1187}
1188
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001189#define KEY_USAGE(code,name) \
1190 if( key_usage & code ) \
1191 PRINT_ITEM( name );
1192
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001193static int x509_info_key_usage( char **buf, size_t *size,
1194 unsigned char key_usage )
1195{
1196 int ret;
1197 size_t n = *size;
1198 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001199 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001200
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001201 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1202 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1203 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1204 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1205 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1206 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1207 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001208
1209 *size = n;
1210 *buf = p;
1211
1212 return( 0 );
1213}
1214
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001215static int x509_info_ext_key_usage( char **buf, size_t *size,
1216 const x509_sequence *extended_key_usage )
1217{
1218 int ret;
1219 const char *desc;
1220 size_t n = *size;
1221 char *p = *buf;
1222 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001223 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001224
1225 while( cur != NULL )
1226 {
1227 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1228 desc = "???";
1229
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001230 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001231 SAFE_SNPRINTF();
1232
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001233 sep = ", ";
1234
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001235 cur = cur->next;
1236 }
1237
1238 *size = n;
1239 *buf = p;
1240
1241 return( 0 );
1242}
1243
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001244/*
1245 * Return an informational string about the certificate.
1246 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001247#define BEFORE_COLON 18
1248#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001249int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001250 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001251{
1252 int ret;
1253 size_t n;
1254 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001255 char key_size_str[BEFORE_COLON];
1256
1257 p = buf;
1258 n = size;
1259
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001260 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001261 prefix, crt->version );
1262 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001263 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001264 prefix );
1265 SAFE_SNPRINTF();
1266
Paul Bakker86d0c192013-09-18 11:11:02 +02001267 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001268 SAFE_SNPRINTF();
1269
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001270 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001271 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001272 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273 SAFE_SNPRINTF();
1274
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001275 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001277 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001278 SAFE_SNPRINTF();
1279
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001280 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1282 crt->valid_from.year, crt->valid_from.mon,
1283 crt->valid_from.day, crt->valid_from.hour,
1284 crt->valid_from.min, crt->valid_from.sec );
1285 SAFE_SNPRINTF();
1286
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001287 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001288 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1289 crt->valid_to.year, crt->valid_to.mon,
1290 crt->valid_to.day, crt->valid_to.hour,
1291 crt->valid_to.min, crt->valid_to.sec );
1292 SAFE_SNPRINTF();
1293
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001294 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001295 SAFE_SNPRINTF();
1296
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001297 ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001298 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001299 SAFE_SNPRINTF();
1300
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001301 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001302 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1303 pk_get_name( &crt->pk ) ) ) != 0 )
1304 {
1305 return( ret );
1306 }
1307
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001308 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001309 (int) pk_get_size( &crt->pk ) );
1310 SAFE_SNPRINTF();
1311
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001312 /*
1313 * Optional extensions
1314 */
1315
1316 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1317 {
1318 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1319 crt->ca_istrue ? "true" : "false" );
1320 SAFE_SNPRINTF();
1321
1322 if( crt->max_pathlen > 0 )
1323 {
1324 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1325 SAFE_SNPRINTF();
1326 }
1327 }
1328
1329 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1330 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001331 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001332 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001333
1334 if( ( ret = x509_info_subject_alt_name( &p, &n,
1335 &crt->subject_alt_names ) ) != 0 )
1336 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001337 }
1338
1339 if( crt->ext_types & EXT_NS_CERT_TYPE )
1340 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001341 ret = snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001342 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001343
1344 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1345 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001346 }
1347
1348 if( crt->ext_types & EXT_KEY_USAGE )
1349 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001350 ret = snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001351 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001352
1353 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1354 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001355 }
1356
1357 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1358 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001359 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001360 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001361
1362 if( ( ret = x509_info_ext_key_usage( &p, &n,
1363 &crt->ext_key_usage ) ) != 0 )
1364 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001365 }
1366
1367 ret = snprintf( p, n, "\n" );
1368 SAFE_SNPRINTF();
1369
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370 return( (int) ( size - n ) );
1371}
1372
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001373#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1374int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1375{
1376 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1377 ( crt->key_usage & usage ) != usage )
1378 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1379
1380 return( 0 );
1381}
1382#endif
1383
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001384#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1385int x509_crt_check_extended_key_usage( const x509_crt *crt,
1386 const char *usage_oid,
1387 size_t usage_len )
1388{
1389 const x509_sequence *cur;
1390
1391 /* Extension is not mandatory, absent means no restriction */
1392 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1393 return( 0 );
1394
1395 /*
1396 * Look for the requested usage (or wildcard ANY) in our list
1397 */
1398 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1399 {
1400 const x509_buf *cur_oid = &cur->buf;
1401
1402 if( cur_oid->len == usage_len &&
1403 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1404 {
1405 return( 0 );
1406 }
1407
1408 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1409 return( 0 );
1410 }
1411
1412 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1413}
Paul Bakker9af723c2014-05-01 13:03:14 +02001414#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001415
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001416#if defined(POLARSSL_X509_CRL_PARSE_C)
1417/*
1418 * Return 1 if the certificate is revoked, or 0 otherwise.
1419 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001420int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421{
1422 const x509_crl_entry *cur = &crl->entry;
1423
1424 while( cur != NULL && cur->serial.len != 0 )
1425 {
1426 if( crt->serial.len == cur->serial.len &&
1427 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1428 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001429 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001430 return( 1 );
1431 }
1432
1433 cur = cur->next;
1434 }
1435
1436 return( 0 );
1437}
1438
1439/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001440 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001442static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001443 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001444{
1445 int flags = 0;
1446 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1447 const md_info_t *md_info;
1448
1449 if( ca == NULL )
1450 return( flags );
1451
1452 /*
1453 * TODO: What happens if no CRL is present?
1454 * Suggestion: Revocation state should be unknown if no CRL is present.
1455 * For backwards compatibility this is not yet implemented.
1456 */
1457
1458 while( crl_list != NULL )
1459 {
1460 if( crl_list->version == 0 ||
1461 crl_list->issuer_raw.len != ca->subject_raw.len ||
1462 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1463 crl_list->issuer_raw.len ) != 0 )
1464 {
1465 crl_list = crl_list->next;
1466 continue;
1467 }
1468
1469 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001470 * Check if the CA is configured to sign CRLs
1471 */
1472#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1473 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1474 {
1475 flags |= BADCRL_NOT_TRUSTED;
1476 break;
1477 }
1478#endif
1479
1480 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001481 * Check if CRL is correctly signed by the trusted CA
1482 */
1483 md_info = md_info_from_type( crl_list->sig_md );
1484 if( md_info == NULL )
1485 {
1486 /*
1487 * Cannot check 'unknown' hash
1488 */
1489 flags |= BADCRL_NOT_TRUSTED;
1490 break;
1491 }
1492
1493 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1494
1495 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1496 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1497 crl_list->sig.p, crl_list->sig.len ) != 0 )
1498 {
1499 flags |= BADCRL_NOT_TRUSTED;
1500 break;
1501 }
1502
1503 /*
1504 * Check for validity of CRL (Do not drop out)
1505 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001506 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507 flags |= BADCRL_EXPIRED;
1508
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001509 if( x509_time_future( &crl_list->this_update ) )
1510 flags |= BADCRL_FUTURE;
1511
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512 /*
1513 * Check if certificate is revoked
1514 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001515 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516 {
1517 flags |= BADCERT_REVOKED;
1518 break;
1519 }
1520
1521 crl_list = crl_list->next;
1522 }
1523 return flags;
1524}
1525#endif /* POLARSSL_X509_CRL_PARSE_C */
1526
1527// Equal == 0, inequal == 1
1528static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1529{
1530 size_t i;
1531 unsigned char diff;
1532 const unsigned char *n1 = s1, *n2 = s2;
1533
1534 for( i = 0; i < len; i++ )
1535 {
1536 diff = n1[i] ^ n2[i];
1537
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001538 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539 continue;
1540
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001541 if( diff == 32 &&
1542 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1543 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1544 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001546 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547
1548 return( 1 );
1549 }
1550
1551 return( 0 );
1552}
1553
1554static int x509_wildcard_verify( const char *cn, x509_buf *name )
1555{
1556 size_t i;
1557 size_t cn_idx = 0;
1558
1559 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1560 return( 0 );
1561
1562 for( i = 0; i < strlen( cn ); ++i )
1563 {
1564 if( cn[i] == '.' )
1565 {
1566 cn_idx = i;
1567 break;
1568 }
1569 }
1570
1571 if( cn_idx == 0 )
1572 return( 0 );
1573
1574 if( strlen( cn ) - cn_idx == name->len - 1 &&
1575 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1576 {
1577 return( 1 );
1578 }
1579
1580 return( 0 );
1581}
1582
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001583/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001584 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1585 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001586 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001587static int x509_crt_check_parent( const x509_crt *child,
1588 const x509_crt *parent )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001589{
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001590 if( parent->version == 0 ||
1591 parent->ca_istrue == 0 ||
1592 child->issuer_raw.len != parent->subject_raw.len ||
1593 memcmp( child->issuer_raw.p, parent->subject_raw.p,
1594 child->issuer_raw.len ) != 0 )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001595 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001596 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001597 }
1598
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001599#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1600 if( x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
1601 return( -1 );
1602#endif
1603
1604 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001605}
1606
Paul Bakkerddf26b42013-09-18 13:46:23 +02001607static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001608 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001609 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001610 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611 void *p_vrfy )
1612{
1613 int ret;
1614 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1615 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1616 const md_info_t *md_info;
1617
Paul Bakker86d0c192013-09-18 11:11:02 +02001618 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619 *flags |= BADCERT_EXPIRED;
1620
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001621 if( x509_time_future( &child->valid_from ) )
1622 *flags |= BADCERT_FUTURE;
1623
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624 /*
1625 * Child is the top of the chain. Check against the trust_ca list.
1626 */
1627 *flags |= BADCERT_NOT_TRUSTED;
1628
1629 md_info = md_info_from_type( child->sig_md );
1630 if( md_info == NULL )
1631 {
1632 /*
1633 * Cannot check 'unknown', no need to try any CA
1634 */
1635 trust_ca = NULL;
1636 }
1637 else
1638 md( md_info, child->tbs.p, child->tbs.len, hash );
1639
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001640 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001642 if( x509_crt_check_parent( child, trust_ca ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001643 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644
1645 /*
1646 * Reduce path_len to check against if top of the chain is
1647 * the same as the trusted CA
1648 */
1649 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1650 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1651 child->issuer_raw.len ) == 0 )
1652 {
1653 check_path_cnt--;
1654 }
1655
1656 if( trust_ca->max_pathlen > 0 &&
1657 trust_ca->max_pathlen < check_path_cnt )
1658 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659 continue;
1660 }
1661
Manuel Pégourié-Gonnard920e1cd2014-06-02 18:11:07 +02001662#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
1663 if( child->sig_pk == POLARSSL_PK_RSASSA_PSS )
1664 {
1665 if( pk_can_do( &trust_ca->pk, POLARSSL_PK_RSA ) == 0 ||
1666 rsa_rsassa_pss_verify( pk_rsa( trust_ca->pk ),
1667 NULL, NULL, RSA_PUBLIC,
1668 child->sig_md,
1669 md_info->size, hash,
1670 child->sig.p ) != 0 )
1671 {
1672 continue;
1673 }
1674 }
1675 else
1676#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001677 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1678 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1679 child->sig.p, child->sig.len ) != 0 )
1680 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001681 continue;
1682 }
1683
1684 /*
1685 * Top of chain is signed by a trusted CA
1686 */
1687 *flags &= ~BADCERT_NOT_TRUSTED;
1688 break;
1689 }
1690
1691 /*
1692 * If top of chain is not the same as the trusted CA send a verify request
1693 * to the callback for any issues with validity and CRL presence for the
1694 * trusted CA certificate.
1695 */
1696 if( trust_ca != NULL &&
1697 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1698 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1699 child->issuer_raw.len ) != 0 ) )
1700 {
1701#if defined(POLARSSL_X509_CRL_PARSE_C)
1702 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001703 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001704#else
1705 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001706#endif
1707
Paul Bakker86d0c192013-09-18 11:11:02 +02001708 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001709 ca_flags |= BADCERT_EXPIRED;
1710
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001711 if( x509_time_future( &trust_ca->valid_from ) )
1712 ca_flags |= BADCERT_FUTURE;
1713
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001714 if( NULL != f_vrfy )
1715 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001716 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1717 &ca_flags ) ) != 0 )
1718 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001719 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001720 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721 }
1722 }
1723
1724 /* Call callback on top cert */
1725 if( NULL != f_vrfy )
1726 {
1727 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1728 return( ret );
1729 }
1730
1731 *flags |= ca_flags;
1732
1733 return( 0 );
1734}
1735
Paul Bakkerddf26b42013-09-18 13:46:23 +02001736static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001737 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001738 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001739 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001740 void *p_vrfy )
1741{
1742 int ret;
1743 int parent_flags = 0;
1744 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001745 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001746 const md_info_t *md_info;
1747
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001748 if( x509_time_expired( &child->valid_to ) )
1749 *flags |= BADCERT_EXPIRED;
1750
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001751 if( x509_time_future( &child->valid_from ) )
1752 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001753
1754 md_info = md_info_from_type( child->sig_md );
1755 if( md_info == NULL )
1756 {
1757 /*
1758 * Cannot check 'unknown' hash
1759 */
1760 *flags |= BADCERT_NOT_TRUSTED;
1761 }
1762 else
1763 {
1764 md( md_info, child->tbs.p, child->tbs.len, hash );
1765
Manuel Pégourié-Gonnard920e1cd2014-06-02 18:11:07 +02001766#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
1767 if( child->sig_pk == POLARSSL_PK_RSASSA_PSS )
1768 {
1769 if( pk_can_do( &parent->pk, POLARSSL_PK_RSA ) == 0 ||
1770 rsa_rsassa_pss_verify( pk_rsa( parent->pk ),
1771 NULL, NULL, RSA_PUBLIC,
1772 child->sig_md,
1773 md_info->size, hash,
1774 child->sig.p ) != 0 )
1775 {
1776 *flags |= BADCERT_NOT_TRUSTED;
1777 }
1778 }
1779 else
1780#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001781 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1782 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1783 child->sig.p, child->sig.len ) != 0 )
1784 {
1785 *flags |= BADCERT_NOT_TRUSTED;
1786 }
1787 }
1788
1789#if defined(POLARSSL_X509_CRL_PARSE_C)
1790 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001791 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001792#endif
1793
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001794 /* Look for a grandparent upwards the chain */
1795 for( grandparent = parent->next;
1796 grandparent != NULL;
1797 grandparent = grandparent->next )
1798 {
1799 if( x509_crt_check_parent( parent, grandparent ) == 0 )
1800 break;
1801 }
1802
1803 /* Is our parent part of the chain or at the top? */
1804 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001805 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001806 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1807 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808 if( ret != 0 )
1809 return( ret );
1810 }
1811 else
1812 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001813 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1814 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001815 if( ret != 0 )
1816 return( ret );
1817 }
1818
1819 /* child is verified to be a child of the parent, call verify callback */
1820 if( NULL != f_vrfy )
1821 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1822 return( ret );
1823
1824 *flags |= parent_flags;
1825
1826 return( 0 );
1827}
1828
1829/*
1830 * Verify the certificate validity
1831 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001832int x509_crt_verify( x509_crt *crt,
1833 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001834 x509_crl *ca_crl,
1835 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001836 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001837 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001838{
1839 size_t cn_len;
1840 int ret;
1841 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001842 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001843 x509_name *name;
1844 x509_sequence *cur = NULL;
1845
1846 *flags = 0;
1847
1848 if( cn != NULL )
1849 {
1850 name = &crt->subject;
1851 cn_len = strlen( cn );
1852
1853 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1854 {
1855 cur = &crt->subject_alt_names;
1856
1857 while( cur != NULL )
1858 {
1859 if( cur->buf.len == cn_len &&
1860 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1861 break;
1862
1863 if( cur->buf.len > 2 &&
1864 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1865 x509_wildcard_verify( cn, &cur->buf ) )
1866 break;
1867
1868 cur = cur->next;
1869 }
1870
1871 if( cur == NULL )
1872 *flags |= BADCERT_CN_MISMATCH;
1873 }
1874 else
1875 {
1876 while( name != NULL )
1877 {
1878 if( OID_CMP( OID_AT_CN, &name->oid ) )
1879 {
1880 if( name->val.len == cn_len &&
1881 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1882 break;
1883
1884 if( name->val.len > 2 &&
1885 memcmp( name->val.p, "*.", 2 ) == 0 &&
1886 x509_wildcard_verify( cn, &name->val ) )
1887 break;
1888 }
1889
1890 name = name->next;
1891 }
1892
1893 if( name == NULL )
1894 *flags |= BADCERT_CN_MISMATCH;
1895 }
1896 }
1897
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001898 /* Look for a parent upwards the chain */
1899 for( parent = crt->next; parent != NULL; parent = parent->next )
1900 {
1901 if( x509_crt_check_parent( crt, parent ) == 0 )
1902 break;
1903 }
1904
1905 /* Are we part of the chain or at the top? */
1906 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001907 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001908 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1909 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001910 if( ret != 0 )
1911 return( ret );
1912 }
1913 else
1914 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001915 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1916 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001917 if( ret != 0 )
1918 return( ret );
1919 }
1920
1921 if( *flags != 0 )
1922 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1923
1924 return( 0 );
1925}
1926
1927/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001928 * Initialize a certificate chain
1929 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001930void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001931{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001932 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001933}
1934
1935/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001936 * Unallocate all certificate data
1937 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001938void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001939{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001940 x509_crt *cert_cur = crt;
1941 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001942 x509_name *name_cur;
1943 x509_name *name_prv;
1944 x509_sequence *seq_cur;
1945 x509_sequence *seq_prv;
1946
1947 if( crt == NULL )
1948 return;
1949
1950 do
1951 {
1952 pk_free( &cert_cur->pk );
1953
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001954#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
1955 polarssl_free( cert_cur->sig_opts );
1956#endif
1957
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001958 name_cur = cert_cur->issuer.next;
1959 while( name_cur != NULL )
1960 {
1961 name_prv = name_cur;
1962 name_cur = name_cur->next;
1963 memset( name_prv, 0, sizeof( x509_name ) );
1964 polarssl_free( name_prv );
1965 }
1966
1967 name_cur = cert_cur->subject.next;
1968 while( name_cur != NULL )
1969 {
1970 name_prv = name_cur;
1971 name_cur = name_cur->next;
1972 memset( name_prv, 0, sizeof( x509_name ) );
1973 polarssl_free( name_prv );
1974 }
1975
1976 seq_cur = cert_cur->ext_key_usage.next;
1977 while( seq_cur != NULL )
1978 {
1979 seq_prv = seq_cur;
1980 seq_cur = seq_cur->next;
1981 memset( seq_prv, 0, sizeof( x509_sequence ) );
1982 polarssl_free( seq_prv );
1983 }
1984
1985 seq_cur = cert_cur->subject_alt_names.next;
1986 while( seq_cur != NULL )
1987 {
1988 seq_prv = seq_cur;
1989 seq_cur = seq_cur->next;
1990 memset( seq_prv, 0, sizeof( x509_sequence ) );
1991 polarssl_free( seq_prv );
1992 }
1993
1994 if( cert_cur->raw.p != NULL )
1995 {
1996 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1997 polarssl_free( cert_cur->raw.p );
1998 }
1999
2000 cert_cur = cert_cur->next;
2001 }
2002 while( cert_cur != NULL );
2003
2004 cert_cur = crt;
2005 do
2006 {
2007 cert_prv = cert_cur;
2008 cert_cur = cert_cur->next;
2009
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002010 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002011 if( cert_prv != crt )
2012 polarssl_free( cert_prv );
2013 }
2014 while( cert_cur != NULL );
2015}
2016
Paul Bakker9af723c2014-05-01 13:03:14 +02002017#endif /* POLARSSL_X509_CRT_PARSE_C */