blob: bc360dc8f96c16ba4b7afe8b5a445186776464bb [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020040
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/x509_crt.h"
42#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000043
44#include <stdio.h>
45#include <string.h>
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049#endif
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#else
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#define mbedtls_free free
56#define mbedtls_malloc malloc
57#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058#endif
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010062#endif
63
Paul Bakkerfa6a6202013-10-28 18:48:30 +010064#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065#include <windows.h>
66#else
67#include <time.h>
68#endif
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000071#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020072#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073#include <sys/types.h>
74#include <sys/stat.h>
75#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000076#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#endif
78
Paul Bakker34617722014-06-13 17:20:13 +020079/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020081 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
82}
83
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084/*
85 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
86 */
87static int x509_get_version( unsigned char **p,
88 const unsigned char *end,
89 int *ver )
90{
91 int ret;
92 size_t len;
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
95 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098 {
99 *ver = 0;
100 return( 0 );
101 }
102
103 return( ret );
104 }
105
106 end = *p + len;
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
109 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110
111 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 return( MBEDTLS_ERR_X509_INVALID_VERSION +
113 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200114
115 return( 0 );
116}
117
118/*
119 * Validity ::= SEQUENCE {
120 * notBefore Time,
121 * notAfter Time }
122 */
123static int x509_get_dates( unsigned char **p,
124 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_x509_time *from,
126 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127{
128 int ret;
129 size_t len;
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
132 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
133 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200134
135 end = *p + len;
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138 return( ret );
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200141 return( ret );
142
143 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 return( MBEDTLS_ERR_X509_INVALID_DATE +
145 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200146
147 return( 0 );
148}
149
150/*
151 * X.509 v2/v3 unique identifier (not parsed)
152 */
153static int x509_get_uid( unsigned char **p,
154 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200156{
157 int ret;
158
159 if( *p == end )
160 return( 0 );
161
162 uid->tag = **p;
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
165 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200168 return( 0 );
169
170 return( ret );
171 }
172
173 uid->p = *p;
174 *p += uid->len;
175
176 return( 0 );
177}
178
179static int x509_get_basic_constraints( unsigned char **p,
180 const unsigned char *end,
181 int *ca_istrue,
182 int *max_pathlen )
183{
184 int ret;
185 size_t len;
186
187 /*
188 * BasicConstraints ::= SEQUENCE {
189 * cA BOOLEAN DEFAULT FALSE,
190 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
191 */
192 *ca_istrue = 0; /* DEFAULT FALSE */
193 *max_pathlen = 0; /* endless */
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
196 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
197 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200198
199 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200200 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
205 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206
207 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200209
210 if( *ca_istrue != 0 )
211 *ca_istrue = 1;
212 }
213
214 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200215 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
218 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219
220 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
222 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200223
224 (*max_pathlen)++;
225
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200226 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200227}
228
229static int x509_get_ns_cert_type( unsigned char **p,
230 const unsigned char *end,
231 unsigned char *ns_cert_type)
232{
233 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
237 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200238
239 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
241 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242
243 /* Get actual bitstring */
244 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200245 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200246}
247
248static int x509_get_key_usage( unsigned char **p,
249 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100250 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200251{
252 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
256 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200257
258 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
260 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261
262 /* Get actual bitstring */
263 *key_usage = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200264 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265}
266
267/*
268 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
269 *
270 * KeyPurposeId ::= OBJECT IDENTIFIER
271 */
272static int x509_get_ext_key_usage( unsigned char **p,
273 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275{
276 int ret;
277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
279 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280
281 /* Sequence length must be >= 1 */
282 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
284 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200286 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287}
288
289/*
290 * SubjectAltName ::= GeneralNames
291 *
292 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
293 *
294 * GeneralName ::= CHOICE {
295 * otherName [0] OtherName,
296 * rfc822Name [1] IA5String,
297 * dNSName [2] IA5String,
298 * x400Address [3] ORAddress,
299 * directoryName [4] Name,
300 * ediPartyName [5] EDIPartyName,
301 * uniformResourceIdentifier [6] IA5String,
302 * iPAddress [7] OCTET STRING,
303 * registeredID [8] OBJECT IDENTIFIER }
304 *
305 * OtherName ::= SEQUENCE {
306 * type-id OBJECT IDENTIFIER,
307 * value [0] EXPLICIT ANY DEFINED BY type-id }
308 *
309 * EDIPartyName ::= SEQUENCE {
310 * nameAssigner [0] DirectoryString OPTIONAL,
311 * partyName [1] DirectoryString }
312 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000313 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200314 */
315static int x509_get_subject_alt_name( unsigned char **p,
316 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200318{
319 int ret;
320 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200322 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200324
325 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
327 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
328 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329
330 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
332 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333
334 while( *p < end )
335 {
336 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
338 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339
340 tag = **p;
341 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
343 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 if( ( tag & MBEDTLS_ASN1_CONTEXT_SPECIFIC ) != MBEDTLS_ASN1_CONTEXT_SPECIFIC )
346 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
347 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200349 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351 {
352 *p += tag_len;
353 continue;
354 }
355
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200357 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100359 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 cur->next = mbedtls_malloc( sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363
364 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
366 MBEDTLS_ERR_ASN1_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 memset( cur->next, 0, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369 cur = cur->next;
370 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200371
372 buf = &(cur->buf);
373 buf->tag = tag;
374 buf->p = *p;
375 buf->len = tag_len;
376 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377 }
378
379 /* Set final sequence entry's next pointer to NULL */
380 cur->next = NULL;
381
382 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
384 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200385
386 return( 0 );
387}
388
389/*
390 * X.509 v3 extensions
391 *
392 * TODO: Perform all of the basic constraints tests required by the RFC
393 * TODO: Set values for undetected extensions to a sane default?
394 *
395 */
396static int x509_get_crt_ext( unsigned char **p,
397 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399{
400 int ret;
401 size_t len;
402 unsigned char *end_ext_data, *end_ext_octet;
403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407 return( 0 );
408
409 return( ret );
410 }
411
412 while( *p < end )
413 {
414 /*
415 * Extension ::= SEQUENCE {
416 * extnID OBJECT IDENTIFIER,
417 * critical BOOLEAN DEFAULT FALSE,
418 * extnValue OCTET STRING }
419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421 int is_critical = 0; /* DEFAULT FALSE */
422 int ext_type = 0;
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
425 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
426 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427
428 end_ext_data = *p + len;
429
430 /* Get extension ID */
431 extn_oid.tag = **p;
432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
434 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
436 extn_oid.p = *p;
437 *p += extn_oid.len;
438
439 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
441 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442
443 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
445 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
446 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447
448 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
450 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
451 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452
453 end_ext_octet = *p + len;
454
455 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
457 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458
459 /*
460 * Detect supported extensions
461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463
464 if( ret != 0 )
465 {
466 /* No parser found, skip extension */
467 *p = end_ext_octet;
468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470 if( is_critical )
471 {
472 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
474 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475 }
476#endif
477 continue;
478 }
479
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100480 /* Forbid repeated extensions */
481 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100483
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484 crt->ext_types |= ext_type;
485
486 switch( ext_type )
487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 case MBEDTLS_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489 /* Parse basic constraints */
490 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
491 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200492 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 break;
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496 /* Parse key usage */
497 if( ( ret = x509_get_key_usage( p, end_ext_octet,
498 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200499 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500 break;
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503 /* Parse extended key usage */
504 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
505 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200506 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 break;
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 case MBEDTLS_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510 /* Parse subject alt name */
511 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
512 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200513 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514 break;
515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517 /* Parse netscape certificate type */
518 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
519 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200520 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521 break;
522
523 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 }
526 }
527
528 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
530 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531
532 return( 0 );
533}
534
535/*
536 * Parse and fill a single X.509 certificate in DER format
537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200539 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540{
541 int ret;
542 size_t len;
543 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
547 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
548 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549
550 /*
551 * Check for valid input
552 */
553 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 p = mbedtls_malloc( len = buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557 if( p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 return( MBEDTLS_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559
560 memcpy( p, buf, buflen );
561
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562 crt->raw.p = p;
563 crt->raw.len = len;
564 end = p + len;
565
566 /*
567 * Certificate ::= SEQUENCE {
568 * tbsCertificate TBSCertificate,
569 * signatureAlgorithm AlgorithmIdentifier,
570 * signatureValue BIT STRING }
571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
573 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_x509_crt_free( crt );
576 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577 }
578
579 if( len > (size_t) ( end - p ) )
580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_x509_crt_free( crt );
582 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
583 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584 }
585 crt_end = p + len;
586
587 /*
588 * TBSCertificate ::= SEQUENCE {
589 */
590 crt->tbs.p = p;
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
593 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_x509_crt_free( crt );
596 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597 }
598
599 end = p + len;
600 crt->tbs.len = end - crt->tbs.p;
601
602 /*
603 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
604 *
605 * CertificateSerialNumber ::= INTEGER
606 *
607 * signature AlgorithmIdentifier
608 */
609 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
611 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200612 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200615 return( ret );
616 }
617
618 crt->version++;
619
620 if( crt->version > 3 )
621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_x509_crt_free( crt );
623 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624 }
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200627 &crt->sig_md, &crt->sig_pk,
628 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631 return( ret );
632 }
633
634 /*
635 * issuer Name
636 */
637 crt->issuer_raw.p = p;
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
640 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_x509_crt_free( crt );
643 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644 }
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649 return( ret );
650 }
651
652 crt->issuer_raw.len = p - crt->issuer_raw.p;
653
654 /*
655 * Validity ::= SEQUENCE {
656 * notBefore Time,
657 * notAfter Time }
658 *
659 */
660 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
661 &crt->valid_to ) ) != 0 )
662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664 return( ret );
665 }
666
667 /*
668 * subject Name
669 */
670 crt->subject_raw.p = p;
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
673 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_x509_crt_free( crt );
676 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 }
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682 return( ret );
683 }
684
685 crt->subject_raw.len = p - crt->subject_raw.p;
686
687 /*
688 * SubjectPublicKeyInfo
689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693 return( ret );
694 }
695
696 /*
697 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
698 * -- If present, version shall be v2 or v3
699 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
700 * -- If present, version shall be v2 or v3
701 * extensions [3] EXPLICIT Extensions OPTIONAL
702 * -- If present, version shall be v3
703 */
704 if( crt->version == 2 || crt->version == 3 )
705 {
706 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
707 if( ret != 0 )
708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710 return( ret );
711 }
712 }
713
714 if( crt->version == 2 || crt->version == 3 )
715 {
716 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
717 if( ret != 0 )
718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720 return( ret );
721 }
722 }
723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200726#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100727 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200728 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200729 if( ret != 0 )
730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200732 return( ret );
733 }
734 }
735
736 if( p != end )
737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_x509_crt_free( crt );
739 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
740 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741 }
742
743 end = crt_end;
744
745 /*
746 * }
747 * -- end of TBSCertificate
748 *
749 * signatureAlgorithm AlgorithmIdentifier,
750 * signatureValue BIT STRING
751 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 return( ret );
756 }
757
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100758 if( crt->sig_oid.len != sig_oid2.len ||
759 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200760 sig_params1.len != sig_params2.len ||
Paul Bakker66d5d072014-06-17 16:39:18 +0200761 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_x509_crt_free( crt );
764 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765 }
766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200770 return( ret );
771 }
772
773 if( p != end )
774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_x509_crt_free( crt );
776 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
777 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200778 }
779
780 return( 0 );
781}
782
783/*
784 * Parse one X.509 certificate in DER format from a buffer and add them to a
785 * chained list
786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200788 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789{
790 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792
793 /*
794 * Check for valid input
795 */
796 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798
799 while( crt->version != 0 && crt->next != NULL )
800 {
801 prev = crt;
802 crt = crt->next;
803 }
804
805 /*
806 * Add new certificate on the end of the chain if needed.
807 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200808 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 crt->next = mbedtls_malloc( sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811
812 if( crt->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 return( MBEDTLS_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200814
815 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200817 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818 }
819
Paul Bakkerddf26b42013-09-18 13:46:23 +0200820 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821 {
822 if( prev )
823 prev->next = NULL;
824
825 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827
828 return( ret );
829 }
830
831 return( 0 );
832}
833
834/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200835 * Parse one or more PEM certificates from a buffer and add them to the chained
836 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839{
840 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 int buf_format = MBEDTLS_X509_FORMAT_DER;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200842
843 /*
844 * Check for valid input
845 */
846 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200848
849 /*
850 * Determine buffer content. Buffer contains either one DER certificate or
851 * one or more PEM certificates.
852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200854 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 buf_format = MBEDTLS_X509_FORMAT_PEM;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856#endif
857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 if( buf_format == MBEDTLS_X509_FORMAT_DER )
859 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#if defined(MBEDTLS_PEM_PARSE_C)
862 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200863 {
864 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866
867 while( buflen > 0 )
868 {
869 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873 "-----BEGIN CERTIFICATE-----",
874 "-----END CERTIFICATE-----",
875 buf, NULL, 0, &use_len );
876
877 if( ret == 0 )
878 {
879 /*
880 * Was PEM encoded
881 */
882 buflen -= use_len;
883 buf += use_len;
884 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886 {
887 return( ret );
888 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892
893 /*
894 * PEM header and footer were found
895 */
896 buflen -= use_len;
897 buf += use_len;
898
899 if( first_error == 0 )
900 first_error = ret;
901
Paul Bakker5a5fa922014-09-26 14:53:04 +0200902 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 continue;
904 }
905 else
906 break;
907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911
912 if( ret != 0 )
913 {
914 /*
915 * Quit parsing on a memory error
916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 if( ret == MBEDTLS_ERR_X509_MALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918 return( ret );
919
920 if( first_error == 0 )
921 first_error = ret;
922
923 total_failed++;
924 continue;
925 }
926
927 success = 1;
928 }
929 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931
932 if( success )
933 return( total_failed );
934 else if( first_error )
935 return( first_error );
936 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938}
939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200941/*
942 * Load one or more certificates and add them to the chained list
943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945{
946 int ret;
947 size_t n;
948 unsigned char *buf;
949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 return( ret );
952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 mbedtls_zeroize( buf, n + 1 );
956 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957
958 return( ret );
959}
960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#if defined(MBEDTLS_THREADING_PTHREAD)
962static mbedtls_threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100963#endif
964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966{
967 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100968#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969 int w_ret;
970 WCHAR szDir[MAX_PATH];
971 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200972 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200973 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974
Paul Bakker9af723c2014-05-01 13:03:14 +0200975 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976 HANDLE hFind;
977
978 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980
Paul Bakker9af723c2014-05-01 13:03:14 +0200981 memset( szDir, 0, sizeof(szDir) );
982 memset( filename, 0, MAX_PATH );
983 memcpy( filename, path, len );
984 filename[len++] = '\\';
985 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 filename[len++] = '*';
987
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200988 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
989 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +0000990 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992
993 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +0200994 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996
997 len = MAX_PATH - len;
998 do
999 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001000 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001
1002 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1003 continue;
1004
Paul Bakker9af723c2014-05-01 13:03:14 +02001005 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001006 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001007 p, len - 1,
1008 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001009 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 if( w_ret < 0 )
1014 ret++;
1015 else
1016 ret += w_ret;
1017 }
1018 while( FindNextFileW( hFind, &file_data ) != 0 );
1019
Paul Bakker66d5d072014-06-17 16:39:18 +02001020 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001022
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001024#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001025 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001027 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 char entry_name[255];
1029 DIR *dir = opendir( path );
1030
Paul Bakker66d5d072014-06-17 16:39:18 +02001031 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_THREADING_PTHREAD)
1035 if( ( ret = mbedtls_mutex_lock( &readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001036 return( ret );
1037#endif
1038
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001039 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 mbedtls_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001043 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 {
1045 closedir( dir );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001047 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 }
1049
1050 if( !S_ISREG( sb.st_mode ) )
1051 continue;
1052
1053 // Ignore parse errors
1054 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056 if( t_ret < 0 )
1057 ret++;
1058 else
1059 ret += t_ret;
1060 }
1061 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001062
1063cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064#if defined(MBEDTLS_THREADING_PTHREAD)
1065 if( mbedtls_mutex_unlock( &readdir_mutex ) != 0 )
1066 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001067#endif
1068
Paul Bakkerbe089b02013-10-14 15:51:50 +02001069#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070
1071 return( ret );
1072}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074
Paul Bakker6edcd412013-10-29 15:22:54 +01001075#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1076 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077#include <stdarg.h>
1078
1079#if !defined vsnprintf
1080#define vsnprintf _vsnprintf
1081#endif // vsnprintf
1082
1083/*
1084 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1085 * Result value is not size of buffer needed, but -1 if no fit is possible.
1086 *
1087 * This fuction tries to 'fix' this by at least suggesting enlarging the
1088 * size by 20.
1089 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001090static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091{
1092 va_list ap;
1093 int res = -1;
1094
1095 va_start( ap, format );
1096
1097 res = vsnprintf( str, size, format, ap );
1098
1099 va_end( ap );
1100
1101 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +02001102 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103 return( (int) size + 20 );
1104
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001105 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106}
1107
1108#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001109#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001111#define ERR_BUF_TOO_SMALL -2
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001113#define SAFE_SNPRINTF() \
1114{ \
1115 if( ret == -1 ) \
1116 return( -1 ); \
1117 \
Paul Bakker66d5d072014-06-17 16:39:18 +02001118 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001119 p[n - 1] = '\0'; \
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001120 return( ERR_BUF_TOO_SMALL ); \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001121 } \
1122 \
1123 n -= (unsigned int) ret; \
1124 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001125}
1126
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001127static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001129{
1130 size_t i;
1131 size_t n = *size;
1132 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001134 const char *sep = "";
1135 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001136
1137 while( cur != NULL )
1138 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001139 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001140 {
1141 *p = '\0';
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001142 return( ERR_BUF_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001143 }
1144
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001145 n -= cur->buf.len + sep_len;
1146 for( i = 0; i < sep_len; i++ )
1147 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001148 for( i = 0; i < cur->buf.len; i++ )
1149 *p++ = cur->buf.p[i];
1150
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001151 sep = ", ";
1152 sep_len = 2;
1153
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001154 cur = cur->next;
1155 }
1156
1157 *p = '\0';
1158
1159 *size = n;
1160 *buf = p;
1161
1162 return( 0 );
1163}
1164
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001165#define PRINT_ITEM(i) \
1166 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001168 SAFE_SNPRINTF(); \
1169 sep = ", "; \
1170 }
1171
1172#define CERT_TYPE(type,name) \
1173 if( ns_cert_type & type ) \
1174 PRINT_ITEM( name );
1175
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001176static int x509_info_cert_type( char **buf, size_t *size,
1177 unsigned char ns_cert_type )
1178{
1179 int ret;
1180 size_t n = *size;
1181 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001182 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1185 CERT_TYPE( MBEDTLS_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1186 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1187 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1188 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
1189 CERT_TYPE( MBEDTLS_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1190 CERT_TYPE( MBEDTLS_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1191 CERT_TYPE( MBEDTLS_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001192
1193 *size = n;
1194 *buf = p;
1195
1196 return( 0 );
1197}
1198
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001199#define KEY_USAGE(code,name) \
1200 if( key_usage & code ) \
1201 PRINT_ITEM( name );
1202
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001203static int x509_info_key_usage( char **buf, size_t *size,
1204 unsigned char key_usage )
1205{
1206 int ret;
1207 size_t n = *size;
1208 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001209 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1212 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
1213 KEY_USAGE( MBEDTLS_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1214 KEY_USAGE( MBEDTLS_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1215 KEY_USAGE( MBEDTLS_KU_KEY_AGREEMENT, "Key Agreement" );
1216 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1217 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001218
1219 *size = n;
1220 *buf = p;
1221
1222 return( 0 );
1223}
1224
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001225static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001227{
1228 int ret;
1229 const char *desc;
1230 size_t n = *size;
1231 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001233 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001234
1235 while( cur != NULL )
1236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001238 desc = "???";
1239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001241 SAFE_SNPRINTF();
1242
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001243 sep = ", ";
1244
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001245 cur = cur->next;
1246 }
1247
1248 *size = n;
1249 *buf = p;
1250
1251 return( 0 );
1252}
1253
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001254/*
1255 * Return an informational string about the certificate.
1256 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001257#define BEFORE_COLON 18
1258#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1260 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001261{
1262 int ret;
1263 size_t n;
1264 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001265 char key_size_str[BEFORE_COLON];
1266
1267 p = buf;
1268 n = size;
1269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001271 prefix, crt->version );
1272 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001274 prefix );
1275 SAFE_SNPRINTF();
1276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001278 SAFE_SNPRINTF();
1279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001283 SAFE_SNPRINTF();
1284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001288 SAFE_SNPRINTF();
1289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001291 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1292 crt->valid_from.year, crt->valid_from.mon,
1293 crt->valid_from.day, crt->valid_from.hour,
1294 crt->valid_from.min, crt->valid_from.sec );
1295 SAFE_SNPRINTF();
1296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001298 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1299 crt->valid_to.year, crt->valid_to.mon,
1300 crt->valid_to.day, crt->valid_to.hour,
1301 crt->valid_to.min, crt->valid_to.sec );
1302 SAFE_SNPRINTF();
1303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001305 SAFE_SNPRINTF();
1306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001308 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001309 SAFE_SNPRINTF();
1310
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001311 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1313 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 {
1315 return( ret );
1316 }
1317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1319 (int) mbedtls_pk_get_size( &crt->pk ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001320 SAFE_SNPRINTF();
1321
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001322 /*
1323 * Optional extensions
1324 */
1325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 if( crt->ext_types & MBEDTLS_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001329 crt->ca_istrue ? "true" : "false" );
1330 SAFE_SNPRINTF();
1331
1332 if( crt->max_pathlen > 0 )
1333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001335 SAFE_SNPRINTF();
1336 }
1337 }
1338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 if( crt->ext_types & MBEDTLS_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001342 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001343
1344 if( ( ret = x509_info_subject_alt_name( &p, &n,
1345 &crt->subject_alt_names ) ) != 0 )
1346 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001347 }
1348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001352 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001353
1354 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1355 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001356 }
1357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001361 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001362
1363 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1364 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001365 }
1366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001370 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001371
1372 if( ( ret = x509_info_ext_key_usage( &p, &n,
1373 &crt->ext_key_usage ) ) != 0 )
1374 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001375 }
1376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001378 SAFE_SNPRINTF();
1379
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380 return( (int) ( size - n ) );
1381}
1382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1384int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt, unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) != 0 &&
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001387 ( crt->key_usage & usage ) != usage )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001389
1390 return( 0 );
1391}
1392#endif
1393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1395int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001396 const char *usage_oid,
1397 size_t usage_len )
1398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001400
1401 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001403 return( 0 );
1404
1405 /*
1406 * Look for the requested usage (or wildcard ANY) in our list
1407 */
1408 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001411
1412 if( cur_oid->len == usage_len &&
1413 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1414 {
1415 return( 0 );
1416 }
1417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001419 return( 0 );
1420 }
1421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001423}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427/*
1428 * Return 1 if the certificate is revoked, or 0 otherwise.
1429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430int mbedtls_x509_crt_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433
1434 while( cur != NULL && cur->serial.len != 0 )
1435 {
1436 if( crt->serial.len == cur->serial.len &&
1437 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 if( mbedtls_x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440 return( 1 );
1441 }
1442
1443 cur = cur->next;
1444 }
1445
1446 return( 0 );
1447}
1448
1449/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001450 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
1453 mbedtls_x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454{
1455 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1457 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001458
1459 if( ca == NULL )
1460 return( flags );
1461
1462 /*
1463 * TODO: What happens if no CRL is present?
1464 * Suggestion: Revocation state should be unknown if no CRL is present.
1465 * For backwards compatibility this is not yet implemented.
1466 */
1467
1468 while( crl_list != NULL )
1469 {
1470 if( crl_list->version == 0 ||
1471 crl_list->issuer_raw.len != ca->subject_raw.len ||
1472 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1473 crl_list->issuer_raw.len ) != 0 )
1474 {
1475 crl_list = crl_list->next;
1476 continue;
1477 }
1478
1479 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001480 * Check if the CA is configured to sign CRLs
1481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1483 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001486 break;
1487 }
1488#endif
1489
1490 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491 * Check if CRL is correctly signed by the trusted CA
1492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001494 if( md_info == NULL )
1495 {
1496 /*
1497 * Cannot check 'unknown' hash
1498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001500 break;
1501 }
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1506 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001507 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510 break;
1511 }
1512
1513 /*
1514 * Check for validity of CRL (Do not drop out)
1515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 if( mbedtls_x509_time_expired( &crl_list->next_update ) )
1517 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 if( mbedtls_x509_time_future( &crl_list->this_update ) )
1520 flags |= MBEDTLS_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001521
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522 /*
1523 * Check if certificate is revoked
1524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 if( mbedtls_x509_crt_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001528 break;
1529 }
1530
1531 crl_list = crl_list->next;
1532 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001533 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001537/*
1538 * Like memcmp, but case-insensitive and always returns -1 if different
1539 */
1540static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541{
1542 size_t i;
1543 unsigned char diff;
1544 const unsigned char *n1 = s1, *n2 = s2;
1545
1546 for( i = 0; i < len; i++ )
1547 {
1548 diff = n1[i] ^ n2[i];
1549
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001550 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551 continue;
1552
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001553 if( diff == 32 &&
1554 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1555 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1556 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001558 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001560 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561 }
1562
1563 return( 0 );
1564}
1565
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001566/*
1567 * Return 1 if match, 0 if not
1568 * TODO: inverted return value!
1569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570static int x509_wildcard_verify( const char *cn, mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571{
1572 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001573 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001574
1575 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1576 return( 0 );
1577
Paul Bakker14b16c62014-05-28 11:33:54 +02001578 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579 {
1580 if( cn[i] == '.' )
1581 {
1582 cn_idx = i;
1583 break;
1584 }
1585 }
1586
1587 if( cn_idx == 0 )
1588 return( 0 );
1589
Paul Bakker14b16c62014-05-28 11:33:54 +02001590 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001591 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592 {
1593 return( 1 );
1594 }
1595
1596 return( 0 );
1597}
1598
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001599/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001600 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1601 * variations (but not all).
1602 *
1603 * Return 0 if equal, -1 otherwise.
1604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001606{
1607 if( a->tag == b->tag &&
1608 a->len == b->len &&
1609 memcmp( a->p, b->p, b->len ) == 0 )
1610 {
1611 return( 0 );
1612 }
1613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1615 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001616 a->len == b->len &&
1617 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1618 {
1619 return( 0 );
1620 }
1621
1622 return( -1 );
1623}
1624
1625/*
1626 * Compare two X.509 Names (aka rdnSequence).
1627 *
1628 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1629 * we sometimes return unequal when the full algorithm would return equal,
1630 * but never the other way. (In particular, we don't do Unicode normalisation
1631 * or space folding.)
1632 *
1633 * Return 0 if equal, -1 otherwise.
1634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001636{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001637 /* Avoid recursion, it might not be optimised by the compiler */
1638 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001639 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001640 if( a == NULL || b == NULL )
1641 return( -1 );
1642
1643 /* type */
1644 if( a->oid.tag != b->oid.tag ||
1645 a->oid.len != b->oid.len ||
1646 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1647 {
1648 return( -1 );
1649 }
1650
1651 /* value */
1652 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1653 return( -1 );
1654
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001655 /* structure of the list of sets */
1656 if( a->next_merged != b->next_merged )
1657 return( -1 );
1658
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001659 a = a->next;
1660 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001661 }
1662
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001663 /* a == NULL == b */
1664 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001665}
1666
1667/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001668 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1669 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001670 *
1671 * top means parent is a locally-trusted certificate
1672 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1675 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001676 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001677{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001678 int need_ca_bit;
1679
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001680 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001681 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001682 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001683
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001684 /* Parent must have the basicConstraints CA bit set as a general rule */
1685 need_ca_bit = 1;
1686
1687 /* Exception: v1/v2 certificates that are locally trusted. */
1688 if( top && parent->version < 3 )
1689 need_ca_bit = 0;
1690
1691 /* Exception: self-signed end-entity certs that are locally trusted. */
1692 if( top && bottom &&
1693 child->raw.len == parent->raw.len &&
1694 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1695 {
1696 need_ca_bit = 0;
1697 }
1698
1699 if( need_ca_bit && ! parent->ca_istrue )
1700 return( -1 );
1701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001703 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001705 {
1706 return( -1 );
1707 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001708#endif
1709
1710 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001711}
1712
Paul Bakkerddf26b42013-09-18 13:46:23 +02001713static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
1715 mbedtls_x509_crl *ca_crl, int path_cnt, int *flags,
1716 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001717 void *p_vrfy )
1718{
1719 int ret;
1720 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1722 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 if( mbedtls_x509_time_expired( &child->valid_to ) )
1725 *flags |= MBEDTLS_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 if( mbedtls_x509_time_future( &child->valid_from ) )
1728 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001729
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001730 /*
1731 * Child is the top of the chain. Check against the trust_ca list.
1732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736 if( md_info == NULL )
1737 {
1738 /*
1739 * Cannot check 'unknown', no need to try any CA
1740 */
1741 trust_ca = NULL;
1742 }
1743 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001746 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001747 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001748 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001749 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001750
1751 /*
1752 * Reduce path_len to check against if top of the chain is
1753 * the same as the trusted CA
1754 */
1755 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1756 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1757 child->issuer_raw.len ) == 0 )
1758 {
1759 check_path_cnt--;
1760 }
1761
1762 if( trust_ca->max_pathlen > 0 &&
1763 trust_ca->max_pathlen < check_path_cnt )
1764 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001765 continue;
1766 }
1767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1769 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001770 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001771 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001772 continue;
1773 }
1774
1775 /*
1776 * Top of chain is signed by a trusted CA
1777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001779 break;
1780 }
1781
1782 /*
1783 * If top of chain is not the same as the trusted CA send a verify request
1784 * to the callback for any issues with validity and CRL presence for the
1785 * trusted CA certificate.
1786 */
1787 if( trust_ca != NULL &&
1788 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1789 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1790 child->issuer_raw.len ) != 0 ) )
1791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001793 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001794 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001795#else
1796 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001797#endif
1798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 if( mbedtls_x509_time_expired( &trust_ca->valid_to ) )
1800 ca_flags |= MBEDTLS_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 if( mbedtls_x509_time_future( &trust_ca->valid_from ) )
1803 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001804
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001805 if( NULL != f_vrfy )
1806 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001807 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1808 &ca_flags ) ) != 0 )
1809 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001810 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001811 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001812 }
1813 }
1814
1815 /* Call callback on top cert */
1816 if( NULL != f_vrfy )
1817 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001818 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001819 return( ret );
1820 }
1821
1822 *flags |= ca_flags;
1823
1824 return( 0 );
1825}
1826
Paul Bakkerddf26b42013-09-18 13:46:23 +02001827static int x509_crt_verify_child(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 mbedtls_x509_crt *child, mbedtls_x509_crt *parent, mbedtls_x509_crt *trust_ca,
1829 mbedtls_x509_crl *ca_crl, int path_cnt, int *flags,
1830 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001831 void *p_vrfy )
1832{
1833 int ret;
1834 int parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1836 mbedtls_x509_crt *grandparent;
1837 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001838
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001839 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
1843 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001844 }
1845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 if( mbedtls_x509_time_expired( &child->valid_to ) )
1847 *flags |= MBEDTLS_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 if( mbedtls_x509_time_future( &child->valid_from ) )
1850 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001853 if( md_info == NULL )
1854 {
1855 /*
1856 * Cannot check 'unknown' hash
1857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001859 }
1860 else
1861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1865 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001866 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001869 }
1870 }
1871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001873 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001874 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001875#endif
1876
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001877 /* Look for a grandparent upwards the chain */
1878 for( grandparent = parent->next;
1879 grandparent != NULL;
1880 grandparent = grandparent->next )
1881 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001882 if( x509_crt_check_parent( parent, grandparent,
1883 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001884 break;
1885 }
1886
1887 /* Is our parent part of the chain or at the top? */
1888 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001889 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001890 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1891 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001892 if( ret != 0 )
1893 return( ret );
1894 }
1895 else
1896 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001897 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1898 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001899 if( ret != 0 )
1900 return( ret );
1901 }
1902
1903 /* child is verified to be a child of the parent, call verify callback */
1904 if( NULL != f_vrfy )
1905 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1906 return( ret );
1907
1908 *flags |= parent_flags;
1909
1910 return( 0 );
1911}
1912
1913/*
1914 * Verify the certificate validity
1915 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
1917 mbedtls_x509_crt *trust_ca,
1918 mbedtls_x509_crl *ca_crl,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001919 const char *cn, int *flags,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001921 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001922{
1923 size_t cn_len;
1924 int ret;
1925 int pathlen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 mbedtls_x509_crt *parent;
1927 mbedtls_x509_name *name;
1928 mbedtls_x509_sequence *cur = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001929
1930 *flags = 0;
1931
1932 if( cn != NULL )
1933 {
1934 name = &crt->subject;
1935 cn_len = strlen( cn );
1936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 if( crt->ext_types & MBEDTLS_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001938 {
1939 cur = &crt->subject_alt_names;
1940
1941 while( cur != NULL )
1942 {
1943 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001944 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001945 break;
1946
1947 if( cur->buf.len > 2 &&
1948 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1949 x509_wildcard_verify( cn, &cur->buf ) )
1950 break;
1951
1952 cur = cur->next;
1953 }
1954
1955 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001957 }
1958 else
1959 {
1960 while( name != NULL )
1961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001963 {
1964 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001965 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001966 break;
1967
1968 if( name->val.len > 2 &&
1969 memcmp( name->val.p, "*.", 2 ) == 0 &&
1970 x509_wildcard_verify( cn, &name->val ) )
1971 break;
1972 }
1973
1974 name = name->next;
1975 }
1976
1977 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001979 }
1980 }
1981
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001982 /* Look for a parent upwards the chain */
1983 for( parent = crt->next; parent != NULL; parent = parent->next )
1984 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001985 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001986 break;
1987 }
1988
1989 /* Are we part of the chain or at the top? */
1990 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001991 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001992 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1993 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001994 if( ret != 0 )
1995 return( ret );
1996 }
1997 else
1998 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001999 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
2000 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002001 if( ret != 0 )
2002 return( ret );
2003 }
2004
2005 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002007
2008 return( 0 );
2009}
2010
2011/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002012 * Initialize a certificate chain
2013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002015{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002017}
2018
2019/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002020 * Unallocate all certificate data
2021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002023{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 mbedtls_x509_crt *cert_cur = crt;
2025 mbedtls_x509_crt *cert_prv;
2026 mbedtls_x509_name *name_cur;
2027 mbedtls_x509_name *name_prv;
2028 mbedtls_x509_sequence *seq_cur;
2029 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002030
2031 if( crt == NULL )
2032 return;
2033
2034 do
2035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2039 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002040#endif
2041
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002042 name_cur = cert_cur->issuer.next;
2043 while( name_cur != NULL )
2044 {
2045 name_prv = name_cur;
2046 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2048 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002049 }
2050
2051 name_cur = cert_cur->subject.next;
2052 while( name_cur != NULL )
2053 {
2054 name_prv = name_cur;
2055 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2057 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002058 }
2059
2060 seq_cur = cert_cur->ext_key_usage.next;
2061 while( seq_cur != NULL )
2062 {
2063 seq_prv = seq_cur;
2064 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2066 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002067 }
2068
2069 seq_cur = cert_cur->subject_alt_names.next;
2070 while( seq_cur != NULL )
2071 {
2072 seq_prv = seq_cur;
2073 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2075 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002076 }
2077
2078 if( cert_cur->raw.p != NULL )
2079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2081 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002082 }
2083
2084 cert_cur = cert_cur->next;
2085 }
2086 while( cert_cur != NULL );
2087
2088 cert_cur = crt;
2089 do
2090 {
2091 cert_prv = cert_cur;
2092 cert_cur = cert_cur->next;
2093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002095 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002097 }
2098 while( cert_cur != NULL );
2099}
2100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101#endif /* MBEDTLS_X509_CRT_PARSE_C */