blob: d045cfe0aa72a8e63705eabec12959ce67d035db [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é-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 */
21/*
22 * The ITU-T X.509 standard defines a certificate format for PKI.
23 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020024 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
30 */
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/x509_crt.h"
41#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000042
43#include <stdio.h>
44#include <string.h>
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020048#endif
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052#else
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020055#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010061#endif
62
Paul Bakkerfa6a6202013-10-28 18:48:30 +010063#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064#include <windows.h>
65#else
66#include <time.h>
67#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000070#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020071#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#include <sys/types.h>
73#include <sys/stat.h>
74#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000075#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#endif
77
Paul Bakker34617722014-06-13 17:20:13 +020078/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020080 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
81}
82
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020084 * Default profile
85 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020086const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
87{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020088 /* Hashes from SHA-1 and above */
89 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
90 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
91 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
92 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
93 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
94 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
95 0xFFFFFFF, /* Any PK alg */
96 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020097 2048,
98};
99
100/*
101 * Next-default profile
102 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200103const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
104{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200105 /* Hashes from SHA-256 and above */
106 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
107 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
108 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
109 0xFFFFFFF, /* Any PK alg */
110#if defined(MBEDTLS_ECP_C)
111 /* Curves at or above 128-bit security level */
112 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
113 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
119#else
120 0,
121#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200122 2048,
123};
124
125/*
126 * NSA Suite B Profile
127 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200128const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
129{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200130 /* Only SHA-256 and 384 */
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
133 /* Only ECDSA */
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ),
135#if defined(MBEDTLS_ECP_C)
136 /* Only NIST P-256 and P-384 */
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
138 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
139#else
140 0,
141#endif
142 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200143};
144
145/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200146 * Check md_alg against profile
147 * Return 0 if md_alg acceptable for this profile, -1 otherwise
148 */
149static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
150 mbedtls_md_type_t md_alg )
151{
152 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
153 return( 0 );
154
155 return( -1 );
156}
157
158/*
159 * Check pk_alg against profile
160 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
161 */
162static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
163 mbedtls_pk_type_t pk_alg )
164{
165 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
166 return( 0 );
167
168 return( -1 );
169}
170
171/*
172 * Check key against profile
173 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
174 */
175static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
176 mbedtls_pk_type_t pk_alg,
177 const mbedtls_pk_context *pk )
178{
179#if defined(MBEDTLS_RSA_C)
180 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
181 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200182 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200183 return( 0 );
184
185 return( -1 );
186 }
187#endif
188
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200189#if defined(MBEDTLS_ECP_C)
190 if( pk_alg == MBEDTLS_PK_ECDSA ||
191 pk_alg == MBEDTLS_PK_ECKEY ||
192 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200193 {
194 mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
195
196 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
197 return( 0 );
198
199 return( -1 );
200 }
201#endif
202
203 return( -1 );
204}
205
206/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200207 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
208 */
209static int x509_get_version( unsigned char **p,
210 const unsigned char *end,
211 int *ver )
212{
213 int ret;
214 size_t len;
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
217 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200220 {
221 *ver = 0;
222 return( 0 );
223 }
224
225 return( ret );
226 }
227
228 end = *p + len;
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
231 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200232
233 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 return( MBEDTLS_ERR_X509_INVALID_VERSION +
235 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236
237 return( 0 );
238}
239
240/*
241 * Validity ::= SEQUENCE {
242 * notBefore Time,
243 * notAfter Time }
244 */
245static int x509_get_dates( unsigned char **p,
246 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_x509_time *from,
248 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249{
250 int ret;
251 size_t len;
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
254 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
255 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256
257 end = *p + len;
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260 return( ret );
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200263 return( ret );
264
265 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 return( MBEDTLS_ERR_X509_INVALID_DATE +
267 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268
269 return( 0 );
270}
271
272/*
273 * X.509 v2/v3 unique identifier (not parsed)
274 */
275static int x509_get_uid( unsigned char **p,
276 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278{
279 int ret;
280
281 if( *p == end )
282 return( 0 );
283
284 uid->tag = **p;
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
287 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290 return( 0 );
291
292 return( ret );
293 }
294
295 uid->p = *p;
296 *p += uid->len;
297
298 return( 0 );
299}
300
301static int x509_get_basic_constraints( unsigned char **p,
302 const unsigned char *end,
303 int *ca_istrue,
304 int *max_pathlen )
305{
306 int ret;
307 size_t len;
308
309 /*
310 * BasicConstraints ::= SEQUENCE {
311 * cA BOOLEAN DEFAULT FALSE,
312 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
313 */
314 *ca_istrue = 0; /* DEFAULT FALSE */
315 *max_pathlen = 0; /* endless */
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
318 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
319 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200320
321 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200322 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
327 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328
329 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200331
332 if( *ca_istrue != 0 )
333 *ca_istrue = 1;
334 }
335
336 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200337 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
340 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341
342 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
344 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345
346 (*max_pathlen)++;
347
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200348 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349}
350
351static int x509_get_ns_cert_type( unsigned char **p,
352 const unsigned char *end,
353 unsigned char *ns_cert_type)
354{
355 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
359 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360
361 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
363 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364
365 /* Get actual bitstring */
366 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200367 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368}
369
370static int x509_get_key_usage( unsigned char **p,
371 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100372 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200373{
374 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200375 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
379 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380
381 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
383 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384
385 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200386 *key_usage = 0;
387 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
388 {
389 *key_usage |= (unsigned int) bs.p[i] << (8*i);
390 }
391
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200392 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393}
394
395/*
396 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
397 *
398 * KeyPurposeId ::= OBJECT IDENTIFIER
399 */
400static int x509_get_ext_key_usage( unsigned char **p,
401 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403{
404 int ret;
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
407 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408
409 /* Sequence length must be >= 1 */
410 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
412 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200414 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415}
416
417/*
418 * SubjectAltName ::= GeneralNames
419 *
420 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
421 *
422 * GeneralName ::= CHOICE {
423 * otherName [0] OtherName,
424 * rfc822Name [1] IA5String,
425 * dNSName [2] IA5String,
426 * x400Address [3] ORAddress,
427 * directoryName [4] Name,
428 * ediPartyName [5] EDIPartyName,
429 * uniformResourceIdentifier [6] IA5String,
430 * iPAddress [7] OCTET STRING,
431 * registeredID [8] OBJECT IDENTIFIER }
432 *
433 * OtherName ::= SEQUENCE {
434 * type-id OBJECT IDENTIFIER,
435 * value [0] EXPLICIT ANY DEFINED BY type-id }
436 *
437 * EDIPartyName ::= SEQUENCE {
438 * nameAssigner [0] DirectoryString OPTIONAL,
439 * partyName [1] DirectoryString }
440 *
Manuel Pégourié-Gonnard3ad94372016-01-28 10:50:58 +0100441 * NOTE: we only parse and use dNSName and iPAddress at this point.
442 * NOTE: update x509_info_subject_alt_name() and x509_crt_verify() if and when
443 * adding support for more name types.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444 */
Manuel Pégourié-Gonnard3ad94372016-01-28 10:50:58 +0100445#define X509_CRT_SAN_DNS_NAME 2
446#define X509_CRT_SAN_IP_ADDRESS 7
447
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448static int x509_get_subject_alt_name( unsigned char **p,
449 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451{
452 int ret;
453 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457
458 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
460 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
461 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462
463 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
465 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
467 while( *p < end )
468 {
469 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
471 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
473 tag = **p;
474 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
476 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 if( ( tag & MBEDTLS_ASN1_CONTEXT_SPECIFIC ) != MBEDTLS_ASN1_CONTEXT_SPECIFIC )
479 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
480 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481
Manuel Pégourié-Gonnard3ad94372016-01-28 10:50:58 +0100482 /* Skip everything but DNS name and IP address */
483#if defined(MBEDTLS_X509_SAN_IP_ADDRESS_SUPPORT)
484 if( tag == ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | X509_CRT_SAN_IP_ADDRESS ) )
485 {
486 /* If IP adress, only valid lengths are 4 and 16 */
487 if( tag_len != 4 && tag_len != 16 )
488 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
489 }
490 else
491#endif
492 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | X509_CRT_SAN_DNS_NAME ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 {
494 *p += tag_len;
495 continue;
496 }
497
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200499 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100501 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100503
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200504 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
506 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200508 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510 cur = cur->next;
511 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200512
513 buf = &(cur->buf);
514 buf->tag = tag;
515 buf->p = *p;
516 buf->len = tag_len;
517 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518 }
519
520 /* Set final sequence entry's next pointer to NULL */
521 cur->next = NULL;
522
523 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
525 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526
527 return( 0 );
528}
529
530/*
531 * X.509 v3 extensions
532 *
533 * TODO: Perform all of the basic constraints tests required by the RFC
534 * TODO: Set values for undetected extensions to a sane default?
535 *
536 */
537static int x509_get_crt_ext( unsigned char **p,
538 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540{
541 int ret;
542 size_t len;
543 unsigned char *end_ext_data, *end_ext_octet;
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548 return( 0 );
549
550 return( ret );
551 }
552
553 while( *p < end )
554 {
555 /*
556 * Extension ::= SEQUENCE {
557 * extnID OBJECT IDENTIFIER,
558 * critical BOOLEAN DEFAULT FALSE,
559 * extnValue OCTET STRING }
560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562 int is_critical = 0; /* DEFAULT FALSE */
563 int ext_type = 0;
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
566 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
567 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568
569 end_ext_data = *p + len;
570
571 /* Get extension ID */
572 extn_oid.tag = **p;
573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
575 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576
577 extn_oid.p = *p;
578 *p += extn_oid.len;
579
580 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
582 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583
584 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
586 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
587 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588
589 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
591 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
592 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593
594 end_ext_octet = *p + len;
595
596 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
598 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599
600 /*
601 * Detect supported extensions
602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200604
605 if( ret != 0 )
606 {
607 /* No parser found, skip extension */
608 *p = end_ext_octet;
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611 if( is_critical )
612 {
613 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
615 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200616 }
617#endif
618 continue;
619 }
620
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100621 /* Forbid repeated extensions */
622 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100624
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625 crt->ext_types |= ext_type;
626
627 switch( ext_type )
628 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100629 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 /* Parse basic constraints */
631 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
632 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200633 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634 break;
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637 /* Parse key usage */
638 if( ( ret = x509_get_key_usage( p, end_ext_octet,
639 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200640 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641 break;
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644 /* Parse extended key usage */
645 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
646 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200647 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 break;
649
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100650 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651 /* Parse subject alt name */
652 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
653 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200654 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 break;
656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658 /* Parse netscape certificate type */
659 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
660 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200661 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662 break;
663
664 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666 }
667 }
668
669 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
671 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672
673 return( 0 );
674}
675
676/*
677 * Parse and fill a single X.509 certificate in DER format
678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200680 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681{
682 int ret;
683 size_t len;
684 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
688 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
689 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690
691 /*
692 * Check for valid input
693 */
694 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200697 p = mbedtls_calloc( 1, len = buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698 if( p == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200699 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700
701 memcpy( p, buf, buflen );
702
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703 crt->raw.p = p;
704 crt->raw.len = len;
705 end = p + len;
706
707 /*
708 * Certificate ::= SEQUENCE {
709 * tbsCertificate TBSCertificate,
710 * signatureAlgorithm AlgorithmIdentifier,
711 * signatureValue BIT STRING }
712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
714 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_x509_crt_free( crt );
717 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718 }
719
720 if( len > (size_t) ( end - p ) )
721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 mbedtls_x509_crt_free( crt );
723 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
724 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725 }
726 crt_end = p + len;
727
728 /*
729 * TBSCertificate ::= SEQUENCE {
730 */
731 crt->tbs.p = p;
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
734 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_x509_crt_free( crt );
737 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 }
739
740 end = p + len;
741 crt->tbs.len = end - crt->tbs.p;
742
743 /*
744 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
745 *
746 * CertificateSerialNumber ::= INTEGER
747 *
748 * signature AlgorithmIdentifier
749 */
750 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
752 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200753 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200756 return( ret );
757 }
758
759 crt->version++;
760
761 if( crt->version > 3 )
762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_x509_crt_free( crt );
764 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
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_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200768 &crt->sig_md, &crt->sig_pk,
769 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772 return( ret );
773 }
774
775 /*
776 * issuer Name
777 */
778 crt->issuer_raw.p = p;
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
781 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_x509_crt_free( crt );
784 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785 }
786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200790 return( ret );
791 }
792
793 crt->issuer_raw.len = p - crt->issuer_raw.p;
794
795 /*
796 * Validity ::= SEQUENCE {
797 * notBefore Time,
798 * notAfter Time }
799 *
800 */
801 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
802 &crt->valid_to ) ) != 0 )
803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200805 return( ret );
806 }
807
808 /*
809 * subject Name
810 */
811 crt->subject_raw.p = p;
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
814 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_x509_crt_free( crt );
817 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818 }
819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823 return( ret );
824 }
825
826 crt->subject_raw.len = p - crt->subject_raw.p;
827
828 /*
829 * SubjectPublicKeyInfo
830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200834 return( ret );
835 }
836
837 /*
838 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
839 * -- If present, version shall be v2 or v3
840 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
841 * -- If present, version shall be v2 or v3
842 * extensions [3] EXPLICIT Extensions OPTIONAL
843 * -- If present, version shall be v3
844 */
845 if( crt->version == 2 || crt->version == 3 )
846 {
847 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
848 if( ret != 0 )
849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851 return( ret );
852 }
853 }
854
855 if( crt->version == 2 || crt->version == 3 )
856 {
857 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
858 if( ret != 0 )
859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200861 return( ret );
862 }
863 }
864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200867#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100868 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200869 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870 if( ret != 0 )
871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873 return( ret );
874 }
875 }
876
877 if( p != end )
878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_x509_crt_free( crt );
880 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
881 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882 }
883
884 end = crt_end;
885
886 /*
887 * }
888 * -- end of TBSCertificate
889 *
890 * signatureAlgorithm AlgorithmIdentifier,
891 * signatureValue BIT STRING
892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896 return( ret );
897 }
898
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100899 if( crt->sig_oid.len != sig_oid2.len ||
900 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200901 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200902 ( sig_params1.len != 0 &&
903 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 mbedtls_x509_crt_free( crt );
906 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907 }
908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912 return( ret );
913 }
914
915 if( p != end )
916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 mbedtls_x509_crt_free( crt );
918 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
919 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920 }
921
922 return( 0 );
923}
924
925/*
926 * Parse one X.509 certificate in DER format from a buffer and add them to a
927 * chained list
928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200930 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931{
932 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934
935 /*
936 * Check for valid input
937 */
938 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940
941 while( crt->version != 0 && crt->next != NULL )
942 {
943 prev = crt;
944 crt = crt->next;
945 }
946
947 /*
948 * Add new certificate on the end of the chain if needed.
949 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200950 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200952 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953
954 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200955 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956
957 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960 }
961
Paul Bakkerddf26b42013-09-18 13:46:23 +0200962 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963 {
964 if( prev )
965 prev->next = NULL;
966
967 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969
970 return( ret );
971 }
972
973 return( 0 );
974}
975
976/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200977 * Parse one or more PEM certificates from a buffer and add them to the chained
978 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981{
982 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 int buf_format = MBEDTLS_X509_FORMAT_DER;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984
985 /*
986 * Check for valid input
987 */
988 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990
991 /*
992 * Determine buffer content. Buffer contains either one DER certificate or
993 * one or more PEM certificates.
994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +0200996 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200997 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001000 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001#endif
1002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1004 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#if defined(MBEDTLS_PEM_PARSE_C)
1007 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008 {
1009 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001012 /* 1 rather than 0 since the terminating NULL byte is counted in */
1013 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014 {
1015 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001018 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 "-----BEGIN CERTIFICATE-----",
1021 "-----END CERTIFICATE-----",
1022 buf, NULL, 0, &use_len );
1023
1024 if( ret == 0 )
1025 {
1026 /*
1027 * Was PEM encoded
1028 */
1029 buflen -= use_len;
1030 buf += use_len;
1031 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033 {
1034 return( ret );
1035 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039
1040 /*
1041 * PEM header and footer were found
1042 */
1043 buflen -= use_len;
1044 buf += use_len;
1045
1046 if( first_error == 0 )
1047 first_error = ret;
1048
Paul Bakker5a5fa922014-09-26 14:53:04 +02001049 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050 continue;
1051 }
1052 else
1053 break;
1054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001058
1059 if( ret != 0 )
1060 {
1061 /*
1062 * Quit parsing on a memory error
1063 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001064 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065 return( ret );
1066
1067 if( first_error == 0 )
1068 first_error = ret;
1069
1070 total_failed++;
1071 continue;
1072 }
1073
1074 success = 1;
1075 }
1076 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078
1079 if( success )
1080 return( total_failed );
1081 else if( first_error )
1082 return( first_error );
1083 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085}
1086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088/*
1089 * Load one or more certificates and add them to the chained list
1090 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092{
1093 int ret;
1094 size_t n;
1095 unsigned char *buf;
1096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 return( ret );
1099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001102 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104
1105 return( ret );
1106}
1107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109{
1110 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001111#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112 int w_ret;
1113 WCHAR szDir[MAX_PATH];
1114 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001115 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001116 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117
Paul Bakker9af723c2014-05-01 13:03:14 +02001118 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119 HANDLE hFind;
1120
1121 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123
Paul Bakker9af723c2014-05-01 13:03:14 +02001124 memset( szDir, 0, sizeof(szDir) );
1125 memset( filename, 0, MAX_PATH );
1126 memcpy( filename, path, len );
1127 filename[len++] = '\\';
1128 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129 filename[len++] = '*';
1130
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001131 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
1132 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001133 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001135
1136 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001137 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139
1140 len = MAX_PATH - len;
1141 do
1142 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001143 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001144
1145 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1146 continue;
1147
Paul Bakker9af723c2014-05-01 13:03:14 +02001148 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001149 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001150 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001151 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001152 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 if( w_ret < 0 )
1157 ret++;
1158 else
1159 ret += w_ret;
1160 }
1161 while( FindNextFileW( hFind, &file_data ) != 0 );
1162
Paul Bakker66d5d072014-06-17 16:39:18 +02001163 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001165
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001167#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001168 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001170 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 char entry_name[255];
1172 DIR *dir = opendir( path );
1173
Paul Bakker66d5d072014-06-17 16:39:18 +02001174 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001178 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001179 {
1180 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001181 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001182 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001183#endif
1184
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001185 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 mbedtls_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001188
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001189 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001190 {
1191 closedir( dir );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001193 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 }
1195
1196 if( !S_ISREG( sb.st_mode ) )
1197 continue;
1198
1199 // Ignore parse errors
1200 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001202 if( t_ret < 0 )
1203 ret++;
1204 else
1205 ret += t_ret;
1206 }
1207 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001208
1209cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001211 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001213#endif
1214
Paul Bakkerbe089b02013-10-14 15:51:50 +02001215#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001216
1217 return( ret );
1218}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001220
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001221static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001223{
1224 size_t i;
1225 size_t n = *size;
1226 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001228 const char *sep = "";
1229 size_t sep_len = 0;
Manuel Pégourié-Gonnard3ad94372016-01-28 10:50:58 +01001230 int ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001231
1232 while( cur != NULL )
1233 {
Manuel Pégourié-Gonnard3ad94372016-01-28 10:50:58 +01001234 if( cur->buf.tag == ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | X509_CRT_SAN_DNS_NAME ) )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001235 {
Manuel Pégourié-Gonnard3ad94372016-01-28 10:50:58 +01001236 if( cur->buf.len + sep_len >= n )
1237 {
1238 *p = '\0';
1239 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1240 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001241
Manuel Pégourié-Gonnard3ad94372016-01-28 10:50:58 +01001242 n -= cur->buf.len + sep_len;
1243 for( i = 0; i < sep_len; i++ )
1244 *p++ = sep[i];
1245 for( i = 0; i < cur->buf.len; i++ )
1246 *p++ = cur->buf.p[i];
1247 }
1248#if defined(MBEDTLS_X509_SAN_IP_ADDRESS_SUPPORT)
1249 else if( cur->buf.tag == ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | X509_CRT_SAN_IP_ADDRESS ) )
1250 {
1251 if( cur->buf.len == 4 )
1252 {
1253 ret = mbedtls_snprintf( p, n, "%sIP:%d.%d.%d.%d", sep,
1254 cur->buf.p[0], cur->buf.p[1],
1255 cur->buf.p[2], cur->buf.p[4] );
1256 MBEDTLS_X509_SAFE_SNPRINTF;
1257 }
1258 else if( cur->buf.len == 16 )
1259 {
1260 if( sep_len + 2 >= n )
1261 {
1262 *p = '\0';
1263 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1264 }
1265
1266 memcpy( p, sep, sep_len );
1267 p += sep_len;
1268 n -= sep_len;
1269
1270 memcpy( p, "IP", 2 );
1271 p += 2;
1272 n -= 2;
1273
1274 for( i = 0; i < 8; i++ )
1275 {
1276 ret = mbedtls_snprintf( p, n, ":%02x%02x",
1277 cur->buf.p[2 * i],
1278 cur->buf.p[2 * i + 1] );
1279 MBEDTLS_X509_SAFE_SNPRINTF;
1280 }
1281 }
1282 else
1283 {
1284 ret = mbedtls_snprintf( p, n, "IP:???" );
1285 MBEDTLS_X509_SAFE_SNPRINTF;
1286 }
1287 }
1288#endif /* MBEDTLS_X509_SAN_IP_ADDRESS_SUPPORT */
1289 /*
1290 * tag == 0 happens when the extension is present but with only unknow
1291 * types: we get a list with one uninitialized element
1292 */
1293 else if( cur->buf.tag != 0 )
1294 {
1295 ret = mbedtls_snprintf( p, n, "???unknown name type???" );
1296 MBEDTLS_X509_SAFE_SNPRINTF;
1297 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001298
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001299 sep = ", ";
1300 sep_len = 2;
1301
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001302 cur = cur->next;
1303 }
1304
1305 *p = '\0';
1306
1307 *size = n;
1308 *buf = p;
1309
1310 return( 0 );
1311}
1312
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001313#define PRINT_ITEM(i) \
1314 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001316 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001317 sep = ", "; \
1318 }
1319
1320#define CERT_TYPE(type,name) \
1321 if( ns_cert_type & type ) \
1322 PRINT_ITEM( name );
1323
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001324static int x509_info_cert_type( char **buf, size_t *size,
1325 unsigned char ns_cert_type )
1326{
1327 int ret;
1328 size_t n = *size;
1329 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001330 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001333 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1335 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1336 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001337 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1338 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1339 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001340
1341 *size = n;
1342 *buf = p;
1343
1344 return( 0 );
1345}
1346
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001347#define KEY_USAGE(code,name) \
1348 if( key_usage & code ) \
1349 PRINT_ITEM( name );
1350
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001351static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001352 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001353{
1354 int ret;
1355 size_t n = *size;
1356 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001357 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1360 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001361 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1362 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1363 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1365 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001366 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1367 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001368
1369 *size = n;
1370 *buf = p;
1371
1372 return( 0 );
1373}
1374
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001375static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001377{
1378 int ret;
1379 const char *desc;
1380 size_t n = *size;
1381 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001383 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001384
1385 while( cur != NULL )
1386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001388 desc = "???";
1389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001391 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001392
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001393 sep = ", ";
1394
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001395 cur = cur->next;
1396 }
1397
1398 *size = n;
1399 *buf = p;
1400
1401 return( 0 );
1402}
1403
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001404/*
1405 * Return an informational string about the certificate.
1406 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001407#define BEFORE_COLON 18
1408#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1410 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001411{
1412 int ret;
1413 size_t n;
1414 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415 char key_size_str[BEFORE_COLON];
1416
1417 p = buf;
1418 n = size;
1419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001422 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001424 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001425 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001428 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001431 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001433 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001436 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001438 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1442 crt->valid_from.year, crt->valid_from.mon,
1443 crt->valid_from.day, crt->valid_from.hour,
1444 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001445 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001448 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1449 crt->valid_to.year, crt->valid_to.mon,
1450 crt->valid_to.day, crt->valid_to.hour,
1451 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001452 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001455 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001458 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001459 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001461 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1463 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001464 {
1465 return( ret );
1466 }
1467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001469 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001470 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001472 /*
1473 * Optional extensions
1474 */
1475
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001476 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001479 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001480 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001481
1482 if( crt->max_pathlen > 0 )
1483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001485 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001486 }
1487 }
1488
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001489 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001492 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001493
1494 if( ( ret = x509_info_subject_alt_name( &p, &n,
1495 &crt->subject_alt_names ) ) != 0 )
1496 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001497 }
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001502 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001503
1504 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1505 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001506 }
1507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001511 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001512
1513 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1514 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001515 }
1516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001520 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001521
1522 if( ( ret = x509_info_ext_key_usage( &p, &n,
1523 &crt->ext_key_usage ) ) != 0 )
1524 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001525 }
1526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001528 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001529
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001530 return( (int) ( size - n ) );
1531}
1532
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001533struct x509_crt_verify_string {
1534 int code;
1535 const char *string;
1536};
1537
1538static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001539 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001540 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
Manuel Pégourié-Gonnard803183f2016-01-27 15:16:16 +01001541 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) or subjectAltName (SAN) does not match with the expected name" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001542 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1543 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1544 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001545 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1546 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1547 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001548 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001549 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1550 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1551 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1552 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001553 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1554 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1555 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1556 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1557 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1558 { MBEDTLS_X509_BADCRL_BAD_KEY, "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001559 { 0, NULL }
1560};
1561
1562int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001563 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001564{
1565 int ret;
1566 const struct x509_crt_verify_string *cur;
1567 char *p = buf;
1568 size_t n = size;
1569
1570 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1571 {
1572 if( ( flags & cur->code ) == 0 )
1573 continue;
1574
1575 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001576 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001577 flags ^= cur->code;
1578 }
1579
1580 if( flags != 0 )
1581 {
1582 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1583 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001584 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001585 }
1586
1587 return( (int) ( size - n ) );
1588}
1589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001591int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1592 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001593{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001594 unsigned int usage_must, usage_may;
1595 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1596 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1597
1598 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1599 return( 0 );
1600
1601 usage_must = usage & ~may_mask;
1602
1603 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1604 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1605
1606 usage_may = usage & may_mask;
1607
1608 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001610
1611 return( 0 );
1612}
1613#endif
1614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1616int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001617 const char *usage_oid,
1618 size_t usage_len )
1619{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001621
1622 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001624 return( 0 );
1625
1626 /*
1627 * Look for the requested usage (or wildcard ANY) in our list
1628 */
1629 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001632
1633 if( cur_oid->len == usage_len &&
1634 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1635 {
1636 return( 0 );
1637 }
1638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001640 return( 0 );
1641 }
1642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001644}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001648/*
1649 * Return 1 if the certificate is revoked, or 0 otherwise.
1650 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001651int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001652{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001654
1655 while( cur != NULL && cur->serial.len != 0 )
1656 {
1657 if( crt->serial.len == cur->serial.len &&
1658 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1659 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001660 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661 return( 1 );
1662 }
1663
1664 cur = cur->next;
1665 }
1666
1667 return( 0 );
1668}
1669
1670/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001671 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001674 mbedtls_x509_crl *crl_list,
1675 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001676{
1677 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1679 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680
1681 if( ca == NULL )
1682 return( flags );
1683
1684 /*
1685 * TODO: What happens if no CRL is present?
1686 * Suggestion: Revocation state should be unknown if no CRL is present.
1687 * For backwards compatibility this is not yet implemented.
1688 */
1689
1690 while( crl_list != NULL )
1691 {
1692 if( crl_list->version == 0 ||
1693 crl_list->issuer_raw.len != ca->subject_raw.len ||
1694 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1695 crl_list->issuer_raw.len ) != 0 )
1696 {
1697 crl_list = crl_list->next;
1698 continue;
1699 }
1700
1701 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001702 * Check if the CA is configured to sign CRLs
1703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1705 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001708 break;
1709 }
1710#endif
1711
1712 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001713 * Check if CRL is correctly signed by the trusted CA
1714 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001715 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1716 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1717
1718 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1719 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001722 if( md_info == NULL )
1723 {
1724 /*
1725 * Cannot check 'unknown' hash
1726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001728 break;
1729 }
1730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001732
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001733 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1734 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1737 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001738 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001741 break;
1742 }
1743
1744 /*
1745 * Check for validity of CRL (Do not drop out)
1746 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001747 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001749
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001750 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001751 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001752
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001753 /*
1754 * Check if certificate is revoked
1755 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001756 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001759 break;
1760 }
1761
1762 crl_list = crl_list->next;
1763 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001764
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001765 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001766}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001769/*
1770 * Like memcmp, but case-insensitive and always returns -1 if different
1771 */
1772static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001773{
1774 size_t i;
1775 unsigned char diff;
1776 const unsigned char *n1 = s1, *n2 = s2;
1777
1778 for( i = 0; i < len; i++ )
1779 {
1780 diff = n1[i] ^ n2[i];
1781
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001782 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001783 continue;
1784
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001785 if( diff == 32 &&
1786 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1787 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1788 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001789 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001790 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001791
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001792 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001793 }
1794
1795 return( 0 );
1796}
1797
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001798/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001799 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001800 */
Manuel Pégourié-Gonnard187ba482016-01-28 17:37:42 +01001801static int x509_check_wildcard( const char *exp_name,
1802 const mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001803{
1804 size_t i;
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +01001805 size_t idx = 0, exp_len = strlen( exp_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001806
1807 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1808 return( 0 );
1809
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +01001810 for( i = 0; i < exp_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001811 {
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +01001812 if( exp_name[i] == '.' )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001813 {
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +01001814 idx = i;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001815 break;
1816 }
1817 }
1818
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +01001819 if( idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001820 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001821
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +01001822 if( exp_len - idx == name->len - 1 &&
1823 x509_memcasecmp( name->p + 1, exp_name + idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001824 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001825 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001826 }
1827
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001828 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001829}
1830
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001831/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001832 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1833 * variations (but not all).
1834 *
1835 * Return 0 if equal, -1 otherwise.
1836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001838{
1839 if( a->tag == b->tag &&
1840 a->len == b->len &&
1841 memcmp( a->p, b->p, b->len ) == 0 )
1842 {
1843 return( 0 );
1844 }
1845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1847 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001848 a->len == b->len &&
1849 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1850 {
1851 return( 0 );
1852 }
1853
1854 return( -1 );
1855}
1856
1857/*
1858 * Compare two X.509 Names (aka rdnSequence).
1859 *
1860 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1861 * we sometimes return unequal when the full algorithm would return equal,
1862 * but never the other way. (In particular, we don't do Unicode normalisation
1863 * or space folding.)
1864 *
1865 * Return 0 if equal, -1 otherwise.
1866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001868{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001869 /* Avoid recursion, it might not be optimised by the compiler */
1870 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001871 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001872 if( a == NULL || b == NULL )
1873 return( -1 );
1874
1875 /* type */
1876 if( a->oid.tag != b->oid.tag ||
1877 a->oid.len != b->oid.len ||
1878 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1879 {
1880 return( -1 );
1881 }
1882
1883 /* value */
1884 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1885 return( -1 );
1886
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001887 /* structure of the list of sets */
1888 if( a->next_merged != b->next_merged )
1889 return( -1 );
1890
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001891 a = a->next;
1892 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001893 }
1894
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001895 /* a == NULL == b */
1896 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001897}
1898
1899/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001900 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1901 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001902 *
1903 * top means parent is a locally-trusted certificate
1904 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1907 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001908 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001909{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001910 int need_ca_bit;
1911
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001912 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001913 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001914 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001915
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001916 /* Parent must have the basicConstraints CA bit set as a general rule */
1917 need_ca_bit = 1;
1918
1919 /* Exception: v1/v2 certificates that are locally trusted. */
1920 if( top && parent->version < 3 )
1921 need_ca_bit = 0;
1922
1923 /* Exception: self-signed end-entity certs that are locally trusted. */
1924 if( top && bottom &&
1925 child->raw.len == parent->raw.len &&
1926 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1927 {
1928 need_ca_bit = 0;
1929 }
1930
1931 if( need_ca_bit && ! parent->ca_istrue )
1932 return( -1 );
1933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001935 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001937 {
1938 return( -1 );
1939 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001940#endif
1941
1942 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001943}
1944
Paul Bakkerddf26b42013-09-18 13:46:23 +02001945static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001947 mbedtls_x509_crl *ca_crl,
1948 const mbedtls_x509_crt_profile *profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02001949 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001950 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001951 void *p_vrfy )
1952{
1953 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001954 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001955 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1957 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001958
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001959 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001960 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001961
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001962 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001964
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001965 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
1966 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
1967
1968 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
1969 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
1970
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001971 /*
1972 * Child is the top of the chain. Check against the trust_ca list.
1973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001977 if( md_info == NULL )
1978 {
1979 /*
1980 * Cannot check 'unknown', no need to try any CA
1981 */
1982 trust_ca = NULL;
1983 }
1984 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001986
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001987 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001988 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001989 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001990 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001991
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001992 check_path_cnt = path_cnt + 1;
1993
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001994 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001995 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001996 * the same as the trusted CA
1997 */
1998 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1999 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
2000 child->issuer_raw.len ) == 0 )
2001 {
2002 check_path_cnt--;
2003 }
2004
Janos Follath5dd4fe12015-10-12 09:02:20 +02002005 /* Self signed certificates do not count towards the limit */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002006 if( trust_ca->max_pathlen > 0 &&
Janos Follath5dd4fe12015-10-12 09:02:20 +02002007 trust_ca->max_pathlen < check_path_cnt - self_cnt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002008 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002009 continue;
2010 }
2011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
2013 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002014 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002015 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002016 continue;
2017 }
2018
2019 /*
2020 * Top of chain is signed by a trusted CA
2021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardfa67eba2015-06-27 14:41:38 +02002023
2024 if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
2025 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2026
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002027 break;
2028 }
2029
2030 /*
2031 * If top of chain is not the same as the trusted CA send a verify request
2032 * to the callback for any issues with validity and CRL presence for the
2033 * trusted CA certificate.
2034 */
2035 if( trust_ca != NULL &&
2036 ( child->subject_raw.len != trust_ca->subject_raw.len ||
2037 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
2038 child->issuer_raw.len ) != 0 ) )
2039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002041 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002042 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02002043#else
2044 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002045#endif
2046
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002047 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002048 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002049
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002050 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002052
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002053 if( NULL != f_vrfy )
2054 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002055 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
2056 &ca_flags ) ) != 0 )
2057 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002058 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002059 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002060 }
2061 }
2062
2063 /* Call callback on top cert */
2064 if( NULL != f_vrfy )
2065 {
Paul Bakker66d5d072014-06-17 16:39:18 +02002066 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002067 return( ret );
2068 }
2069
2070 *flags |= ca_flags;
2071
2072 return( 0 );
2073}
2074
Paul Bakkerddf26b42013-09-18 13:46:23 +02002075static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002076 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
2077 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
2078 const mbedtls_x509_crt_profile *profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002079 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002080 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002081 void *p_vrfy )
2082{
2083 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002084 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2086 mbedtls_x509_crt *grandparent;
2087 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002088
Janos Follath5dd4fe12015-10-12 09:02:20 +02002089 /* Counting intermediate self signed certificates */
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01002090 if( ( path_cnt != 0 ) && x509_name_cmp( &child->issuer, &child->subject ) == 0 )
Janos Follath5dd4fe12015-10-12 09:02:20 +02002091 self_cnt++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002092
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002093 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2097 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002098 }
2099
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002100 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002101 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02002102
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002103 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002105
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002106 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2107 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2108
2109 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2110 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002113 if( md_info == NULL )
2114 {
2115 /*
2116 * Cannot check 'unknown' hash
2117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002119 }
2120 else
2121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002123
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002124 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2125 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2128 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002129 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002132 }
2133 }
2134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002136 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002137 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002138#endif
2139
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002140 /* Look for a grandparent in trusted CAs */
2141 for( grandparent = trust_ca;
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002142 grandparent != NULL;
2143 grandparent = grandparent->next )
2144 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002145 if( x509_crt_check_parent( parent, grandparent,
2146 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002147 break;
2148 }
2149
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002150 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002151 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002152 ret = x509_crt_verify_top( parent, grandparent, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002153 path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002154 if( ret != 0 )
2155 return( ret );
2156 }
2157 else
2158 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002159 /* Look for a grandparent upwards the chain */
2160 for( grandparent = parent->next;
2161 grandparent != NULL;
2162 grandparent = grandparent->next )
2163 {
Janos Follath5dd4fe12015-10-12 09:02:20 +02002164 /* +2 because the current step is not yet accounted for
2165 * and because max_pathlen is one higher than it should be.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01002166 * Also self signed certificates do not count to the limit. */
Janos Follath5dd4fe12015-10-12 09:02:20 +02002167 if( grandparent->max_pathlen > 0 &&
2168 grandparent->max_pathlen < 2 + path_cnt - self_cnt )
2169 {
2170 continue;
2171 }
2172
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002173 if( x509_crt_check_parent( parent, grandparent,
2174 0, path_cnt == 0 ) == 0 )
2175 break;
2176 }
2177
2178 /* Is our parent part of the chain or at the top? */
2179 if( grandparent != NULL )
2180 {
2181 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002182 profile, path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002183 f_vrfy, p_vrfy );
2184 if( ret != 0 )
2185 return( ret );
2186 }
2187 else
2188 {
2189 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002190 path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002191 f_vrfy, p_vrfy );
2192 if( ret != 0 )
2193 return( ret );
2194 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002195 }
2196
2197 /* child is verified to be a child of the parent, call verify callback */
2198 if( NULL != f_vrfy )
2199 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2200 return( ret );
2201
2202 *flags |= parent_flags;
2203
2204 return( 0 );
2205}
2206
2207/*
2208 * Verify the certificate validity
2209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2211 mbedtls_x509_crt *trust_ca,
2212 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +01002213 const char *exp_name, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002214 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002215 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002216{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002217 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +01002218 &mbedtls_x509_crt_profile_default,
2219 exp_name, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002220}
2221
Manuel Pégourié-Gonnardaf588942016-01-28 16:52:31 +01002222/*
2223 * Verify that the certificate matches wit the expected name
2224 */
Manuel Pégourié-Gonnard187ba482016-01-28 17:37:42 +01002225static int x509_crt_verify_name( const mbedtls_x509_crt *crt,
2226 const char *exp_name )
Manuel Pégourié-Gonnardaf588942016-01-28 16:52:31 +01002227{
Manuel Pégourié-Gonnard187ba482016-01-28 17:37:42 +01002228 const size_t exp_len = strlen( exp_name );
Manuel Pégourié-Gonnardaf588942016-01-28 16:52:31 +01002229
2230 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
2231 {
Manuel Pégourié-Gonnard187ba482016-01-28 17:37:42 +01002232 const mbedtls_x509_sequence *cur = &crt->subject_alt_names;
Manuel Pégourié-Gonnardaf588942016-01-28 16:52:31 +01002233
2234 while( cur != NULL )
2235 {
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +01002236 if( cur->buf.len == exp_len &&
2237 x509_memcasecmp( exp_name, cur->buf.p, exp_len ) == 0 )
Manuel Pégourié-Gonnardaf588942016-01-28 16:52:31 +01002238 {
2239 return( 0 );
2240 }
2241
2242 if( cur->buf.len > 2 &&
2243 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +01002244 x509_check_wildcard( exp_name, &cur->buf ) == 0 )
Manuel Pégourié-Gonnardaf588942016-01-28 16:52:31 +01002245 {
2246 return( 0 );
2247 }
2248
2249 cur = cur->next;
2250 }
2251 }
2252 else
2253 {
Manuel Pégourié-Gonnard187ba482016-01-28 17:37:42 +01002254 const mbedtls_x509_name *name = &crt->subject;
2255
Manuel Pégourié-Gonnardaf588942016-01-28 16:52:31 +01002256 while( name != NULL )
2257 {
2258 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
2259 {
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +01002260 if( name->val.len == exp_len &&
2261 x509_memcasecmp( name->val.p, exp_name, exp_len ) == 0 )
Manuel Pégourié-Gonnardaf588942016-01-28 16:52:31 +01002262 {
2263 return( 0 );
2264 }
2265
2266 if( name->val.len > 2 &&
2267 memcmp( name->val.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +01002268 x509_check_wildcard( exp_name, &name->val ) == 0 )
Manuel Pégourié-Gonnardaf588942016-01-28 16:52:31 +01002269 {
2270 return( 0 );
2271 }
2272 }
2273
2274 name = name->next;
2275 }
2276 }
2277
2278 return( -1 );
2279}
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002280
2281/*
2282 * Verify the certificate validity, with profile
2283 */
2284int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2285 mbedtls_x509_crt *trust_ca,
2286 mbedtls_x509_crl *ca_crl,
2287 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +01002288 const char *exp_name, uint32_t *flags,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002289 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2290 void *p_vrfy )
2291{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002292 int ret;
Janos Follath5dd4fe12015-10-12 09:02:20 +02002293 int pathlen = 0, selfsigned = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002295 mbedtls_pk_type_t pk_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002296
Manuel Pégourié-Gonnarda83e4e22015-06-17 11:53:48 +02002297 if( profile == NULL )
2298 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2299
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002300 *flags = 0;
2301
Manuel Pégourié-Gonnardaf588942016-01-28 16:52:31 +01002302 /* Check the expected name */
Manuel Pégourié-Gonnardb74b50b2016-01-28 17:04:28 +01002303 if( exp_name != NULL && x509_crt_verify_name( crt, exp_name ) != 0 )
Manuel Pégourié-Gonnardaf588942016-01-28 16:52:31 +01002304 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002305
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002306 /* Check the type and size of the key */
2307 pk_type = mbedtls_pk_get_type( &crt->pk );
2308
2309 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
2310 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2311
2312 if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
2313 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2314
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002315 /* Look for a parent in trusted CAs */
2316 for( parent = trust_ca; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002317 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002318 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002319 break;
2320 }
2321
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002322 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002323 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002324 ret = x509_crt_verify_top( crt, parent, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002325 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002326 if( ret != 0 )
2327 return( ret );
2328 }
2329 else
2330 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002331 /* Look for a parent upwards the chain */
2332 for( parent = crt->next; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002333 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
2334 break;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002335
2336 /* Are we part of the chain or at the top? */
2337 if( parent != NULL )
2338 {
2339 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002340 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002341 if( ret != 0 )
2342 return( ret );
2343 }
2344 else
2345 {
2346 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002347 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002348 if( ret != 0 )
2349 return( ret );
2350 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002351 }
2352
2353 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002355
2356 return( 0 );
2357}
2358
2359/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002360 * Initialize a certificate chain
2361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002363{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002365}
2366
2367/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002368 * Unallocate all certificate data
2369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002371{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 mbedtls_x509_crt *cert_cur = crt;
2373 mbedtls_x509_crt *cert_prv;
2374 mbedtls_x509_name *name_cur;
2375 mbedtls_x509_name *name_prv;
2376 mbedtls_x509_sequence *seq_cur;
2377 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002378
2379 if( crt == NULL )
2380 return;
2381
2382 do
2383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2387 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002388#endif
2389
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002390 name_cur = cert_cur->issuer.next;
2391 while( name_cur != NULL )
2392 {
2393 name_prv = name_cur;
2394 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2396 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002397 }
2398
2399 name_cur = cert_cur->subject.next;
2400 while( name_cur != NULL )
2401 {
2402 name_prv = name_cur;
2403 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2405 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002406 }
2407
2408 seq_cur = cert_cur->ext_key_usage.next;
2409 while( seq_cur != NULL )
2410 {
2411 seq_prv = seq_cur;
2412 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2414 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002415 }
2416
2417 seq_cur = cert_cur->subject_alt_names.next;
2418 while( seq_cur != NULL )
2419 {
2420 seq_prv = seq_cur;
2421 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2423 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002424 }
2425
2426 if( cert_cur->raw.p != NULL )
2427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2429 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002430 }
2431
2432 cert_cur = cert_cur->next;
2433 }
2434 while( cert_cur != NULL );
2435
2436 cert_cur = crt;
2437 do
2438 {
2439 cert_prv = cert_cur;
2440 cert_cur = cert_cur->next;
2441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002443 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002445 }
2446 while( cert_cur != NULL );
2447}
2448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449#endif /* MBEDTLS_X509_CRT_PARSE_C */