blob: 55293667eaf00e88536cfd2d475f0efc2f6e36ad [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{
Gilles Peskine7344e1b2017-05-12 13:16:40 +020088#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine955738a2017-05-04 16:17:21 +020089 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020090 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine955738a2017-05-04 16:17:21 +020091#endif
92 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020093 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
94 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
95 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
96 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
97 0xFFFFFFF, /* Any PK alg */
98 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020099 2048,
100};
101
102/*
103 * Next-default profile
104 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200105const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
106{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200107 /* Hashes from SHA-256 and above */
108 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
110 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
111 0xFFFFFFF, /* Any PK alg */
112#if defined(MBEDTLS_ECP_C)
113 /* Curves at or above 128-bit security level */
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
120 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
121#else
122 0,
123#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200124 2048,
125};
126
127/*
128 * NSA Suite B Profile
129 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200130const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
131{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200132 /* Only SHA-256 and 384 */
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
135 /* Only ECDSA */
136 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ),
137#if defined(MBEDTLS_ECP_C)
138 /* Only NIST P-256 and P-384 */
139 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
140 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
141#else
142 0,
143#endif
144 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200145};
146
147/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200148 * Check md_alg against profile
149 * Return 0 if md_alg acceptable for this profile, -1 otherwise
150 */
151static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
152 mbedtls_md_type_t md_alg )
153{
154 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
155 return( 0 );
156
157 return( -1 );
158}
159
160/*
161 * Check pk_alg against profile
162 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
163 */
164static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
165 mbedtls_pk_type_t pk_alg )
166{
167 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
168 return( 0 );
169
170 return( -1 );
171}
172
173/*
174 * Check key against profile
175 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
176 */
177static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
178 mbedtls_pk_type_t pk_alg,
179 const mbedtls_pk_context *pk )
180{
181#if defined(MBEDTLS_RSA_C)
182 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
183 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200184 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200185 return( 0 );
186
187 return( -1 );
188 }
189#endif
190
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +0200191#if defined(MBEDTLS_ECP_C)
192 if( pk_alg == MBEDTLS_PK_ECDSA ||
193 pk_alg == MBEDTLS_PK_ECKEY ||
194 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200195 {
196 mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
197
198 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
199 return( 0 );
200
201 return( -1 );
202 }
203#endif
204
205 return( -1 );
206}
207
208/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200209 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
210 */
211static int x509_get_version( unsigned char **p,
212 const unsigned char *end,
213 int *ver )
214{
215 int ret;
216 size_t len;
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
219 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200222 {
223 *ver = 0;
224 return( 0 );
225 }
226
227 return( ret );
228 }
229
230 end = *p + len;
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
233 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200234
235 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 return( MBEDTLS_ERR_X509_INVALID_VERSION +
237 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200238
239 return( 0 );
240}
241
242/*
243 * Validity ::= SEQUENCE {
244 * notBefore Time,
245 * notAfter Time }
246 */
247static int x509_get_dates( unsigned char **p,
248 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_x509_time *from,
250 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200251{
252 int ret;
253 size_t len;
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
256 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
257 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200258
259 end = *p + len;
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200262 return( ret );
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265 return( ret );
266
267 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 return( MBEDTLS_ERR_X509_INVALID_DATE +
269 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200270
271 return( 0 );
272}
273
274/*
275 * X.509 v2/v3 unique identifier (not parsed)
276 */
277static int x509_get_uid( unsigned char **p,
278 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280{
281 int ret;
282
283 if( *p == end )
284 return( 0 );
285
286 uid->tag = **p;
287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
289 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200292 return( 0 );
293
294 return( ret );
295 }
296
297 uid->p = *p;
298 *p += uid->len;
299
300 return( 0 );
301}
302
303static int x509_get_basic_constraints( unsigned char **p,
304 const unsigned char *end,
305 int *ca_istrue,
306 int *max_pathlen )
307{
308 int ret;
309 size_t len;
310
311 /*
312 * BasicConstraints ::= SEQUENCE {
313 * cA BOOLEAN DEFAULT FALSE,
314 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
315 */
316 *ca_istrue = 0; /* DEFAULT FALSE */
317 *max_pathlen = 0; /* endless */
318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
320 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
321 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200322
323 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200324 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
329 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200330
331 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333
334 if( *ca_istrue != 0 )
335 *ca_istrue = 1;
336 }
337
338 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200339 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
342 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343
344 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
346 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347
348 (*max_pathlen)++;
349
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200350 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351}
352
353static int x509_get_ns_cert_type( unsigned char **p,
354 const unsigned char *end,
355 unsigned char *ns_cert_type)
356{
357 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
361 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362
363 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
365 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366
367 /* Get actual bitstring */
368 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200369 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370}
371
372static int x509_get_key_usage( unsigned char **p,
373 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100374 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375{
376 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200377 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
381 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382
383 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
385 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386
387 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200388 *key_usage = 0;
389 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
390 {
391 *key_usage |= (unsigned int) bs.p[i] << (8*i);
392 }
393
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200394 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200395}
396
397/*
398 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
399 *
400 * KeyPurposeId ::= OBJECT IDENTIFIER
401 */
402static int x509_get_ext_key_usage( unsigned char **p,
403 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405{
406 int ret;
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
409 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410
411 /* Sequence length must be >= 1 */
412 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
414 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200416 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417}
418
419/*
420 * SubjectAltName ::= GeneralNames
421 *
422 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
423 *
424 * GeneralName ::= CHOICE {
425 * otherName [0] OtherName,
426 * rfc822Name [1] IA5String,
427 * dNSName [2] IA5String,
428 * x400Address [3] ORAddress,
429 * directoryName [4] Name,
430 * ediPartyName [5] EDIPartyName,
431 * uniformResourceIdentifier [6] IA5String,
432 * iPAddress [7] OCTET STRING,
433 * registeredID [8] OBJECT IDENTIFIER }
434 *
435 * OtherName ::= SEQUENCE {
436 * type-id OBJECT IDENTIFIER,
437 * value [0] EXPLICIT ANY DEFINED BY type-id }
438 *
439 * EDIPartyName ::= SEQUENCE {
440 * nameAssigner [0] DirectoryString OPTIONAL,
441 * partyName [1] DirectoryString }
442 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000443 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444 */
445static int x509_get_subject_alt_name( unsigned char **p,
446 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448{
449 int ret;
450 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454
455 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
457 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
458 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459
460 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
462 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463
464 while( *p < end )
465 {
466 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
468 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469
470 tag = **p;
471 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
473 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
Andres Amaya Garciaf6a6b822017-08-25 17:13:12 +0100475 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
476 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
479 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Andres Amaya Garciaf6a6b822017-08-25 17:13:12 +0100480 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200482 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484 {
485 *p += tag_len;
486 continue;
487 }
488
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200490 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100492 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100494
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200495 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496
497 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200499 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501 cur = cur->next;
502 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200503
504 buf = &(cur->buf);
505 buf->tag = tag;
506 buf->p = *p;
507 buf->len = tag_len;
508 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509 }
510
511 /* Set final sequence entry's next pointer to NULL */
512 cur->next = NULL;
513
514 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
516 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
518 return( 0 );
519}
520
521/*
522 * X.509 v3 extensions
523 *
524 * TODO: Perform all of the basic constraints tests required by the RFC
525 * TODO: Set values for undetected extensions to a sane default?
526 *
527 */
528static int x509_get_crt_ext( unsigned char **p,
529 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531{
532 int ret;
533 size_t len;
534 unsigned char *end_ext_data, *end_ext_octet;
535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539 return( 0 );
540
541 return( ret );
542 }
543
544 while( *p < end )
545 {
546 /*
547 * Extension ::= SEQUENCE {
548 * extnID OBJECT IDENTIFIER,
549 * critical BOOLEAN DEFAULT FALSE,
550 * extnValue OCTET STRING }
551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553 int is_critical = 0; /* DEFAULT FALSE */
554 int ext_type = 0;
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
557 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
558 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559
560 end_ext_data = *p + len;
561
562 /* Get extension ID */
563 extn_oid.tag = **p;
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
566 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
568 extn_oid.p = *p;
569 *p += extn_oid.len;
570
571 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
573 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574
575 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
577 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
578 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579
580 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
582 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
583 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584
585 end_ext_octet = *p + len;
586
587 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
589 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590
591 /*
592 * Detect supported extensions
593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595
596 if( ret != 0 )
597 {
598 /* No parser found, skip extension */
599 *p = end_ext_octet;
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602 if( is_critical )
603 {
604 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
606 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200607 }
608#endif
609 continue;
610 }
611
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100612 /* Forbid repeated extensions */
613 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100615
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200616 crt->ext_types |= ext_type;
617
618 switch( ext_type )
619 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100620 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621 /* Parse basic constraints */
622 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
623 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200624 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625 break;
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628 /* Parse key usage */
629 if( ( ret = x509_get_key_usage( p, end_ext_octet,
630 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200631 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632 break;
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 /* Parse extended key usage */
636 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
637 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200638 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639 break;
640
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100641 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 /* Parse subject alt name */
643 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
644 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200645 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646 break;
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649 /* Parse netscape certificate type */
650 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
651 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200652 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653 break;
654
655 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657 }
658 }
659
660 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
662 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663
664 return( 0 );
665}
666
667/*
668 * Parse and fill a single X.509 certificate in DER format
669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200671 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672{
673 int ret;
674 size_t len;
675 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
679 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
680 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
682 /*
683 * Check for valid input
684 */
685 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687
Janos Follath16734f02016-02-17 14:34:12 +0000688 // Use the original buffer until we figure out actual length
689 p = (unsigned char*) buf;
690 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 end = p + len;
692
693 /*
694 * Certificate ::= SEQUENCE {
695 * tbsCertificate TBSCertificate,
696 * signatureAlgorithm AlgorithmIdentifier,
697 * signatureValue BIT STRING }
698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
700 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_x509_crt_free( crt );
703 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704 }
705
706 if( len > (size_t) ( end - p ) )
707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_x509_crt_free( crt );
709 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
710 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711 }
712 crt_end = p + len;
713
Janos Follath16734f02016-02-17 14:34:12 +0000714 // Create and populate a new buffer for the raw field
715 crt->raw.len = crt_end - buf;
716 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
717 if( p == NULL )
718 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
719
720 memcpy( p, buf, crt->raw.len );
721
722 // Direct pointers to the new buffer
723 p += crt->raw.len - len;
724 end = crt_end = p + len;
725
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726 /*
727 * TBSCertificate ::= SEQUENCE {
728 */
729 crt->tbs.p = p;
730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
732 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_x509_crt_free( crt );
735 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736 }
737
738 end = p + len;
739 crt->tbs.len = end - crt->tbs.p;
740
741 /*
742 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
743 *
744 * CertificateSerialNumber ::= INTEGER
745 *
746 * signature AlgorithmIdentifier
747 */
748 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
750 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200751 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200754 return( ret );
755 }
756
Andres AG1f06d9b2017-03-09 16:16:11 +0000757 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_x509_crt_free( crt );
760 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761 }
762
Andres AG1f06d9b2017-03-09 16:16:11 +0000763 crt->version++;
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200766 &crt->sig_md, &crt->sig_pk,
767 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200770 return( ret );
771 }
772
773 /*
774 * issuer Name
775 */
776 crt->issuer_raw.p = p;
777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
779 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_x509_crt_free( crt );
782 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200783 }
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200788 return( ret );
789 }
790
791 crt->issuer_raw.len = p - crt->issuer_raw.p;
792
793 /*
794 * Validity ::= SEQUENCE {
795 * notBefore Time,
796 * notAfter Time }
797 *
798 */
799 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
800 &crt->valid_to ) ) != 0 )
801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803 return( ret );
804 }
805
806 /*
807 * subject Name
808 */
809 crt->subject_raw.p = p;
810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
812 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 mbedtls_x509_crt_free( crt );
815 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 }
817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821 return( ret );
822 }
823
824 crt->subject_raw.len = p - crt->subject_raw.p;
825
826 /*
827 * SubjectPublicKeyInfo
828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832 return( ret );
833 }
834
835 /*
836 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
837 * -- If present, version shall be v2 or v3
838 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
839 * -- If present, version shall be v2 or v3
840 * extensions [3] EXPLICIT Extensions OPTIONAL
841 * -- If present, version shall be v3
842 */
843 if( crt->version == 2 || crt->version == 3 )
844 {
845 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
846 if( ret != 0 )
847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200849 return( ret );
850 }
851 }
852
853 if( crt->version == 2 || crt->version == 3 )
854 {
855 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
856 if( ret != 0 )
857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200859 return( ret );
860 }
861 }
862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200864 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200865#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100866 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200867 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868 if( ret != 0 )
869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200871 return( ret );
872 }
873 }
874
875 if( p != end )
876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_x509_crt_free( crt );
878 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
879 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 }
881
882 end = crt_end;
883
884 /*
885 * }
886 * -- end of TBSCertificate
887 *
888 * signatureAlgorithm AlgorithmIdentifier,
889 * signatureValue BIT STRING
890 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894 return( ret );
895 }
896
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100897 if( crt->sig_oid.len != sig_oid2.len ||
898 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200899 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200900 ( sig_params1.len != 0 &&
901 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_x509_crt_free( crt );
904 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 }
906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910 return( ret );
911 }
912
913 if( p != end )
914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 mbedtls_x509_crt_free( crt );
916 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
917 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918 }
919
920 return( 0 );
921}
922
923/*
924 * Parse one X.509 certificate in DER format from a buffer and add them to a
925 * chained list
926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200928 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929{
930 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932
933 /*
934 * Check for valid input
935 */
936 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938
939 while( crt->version != 0 && crt->next != NULL )
940 {
941 prev = crt;
942 crt = crt->next;
943 }
944
945 /*
946 * Add new certificate on the end of the chain if needed.
947 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200948 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200950 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951
952 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200953 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
955 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958 }
959
Paul Bakkerddf26b42013-09-18 13:46:23 +0200960 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961 {
962 if( prev )
963 prev->next = NULL;
964
965 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967
968 return( ret );
969 }
970
971 return( 0 );
972}
973
974/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200975 * Parse one or more PEM certificates from a buffer and add them to the chained
976 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979{
980 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 int buf_format = MBEDTLS_X509_FORMAT_DER;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982
983 /*
984 * Check for valid input
985 */
986 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988
989 /*
990 * Determine buffer content. Buffer contains either one DER certificate or
991 * one or more PEM certificates.
992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +0200994 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200995 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200998 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999#endif
1000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1002 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#if defined(MBEDTLS_PEM_PARSE_C)
1005 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006 {
1007 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001010 /* 1 rather than 0 since the terminating NULL byte is counted in */
1011 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012 {
1013 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001016 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018 "-----BEGIN CERTIFICATE-----",
1019 "-----END CERTIFICATE-----",
1020 buf, NULL, 0, &use_len );
1021
1022 if( ret == 0 )
1023 {
1024 /*
1025 * Was PEM encoded
1026 */
1027 buflen -= use_len;
1028 buf += use_len;
1029 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 {
1032 return( ret );
1033 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037
1038 /*
1039 * PEM header and footer were found
1040 */
1041 buflen -= use_len;
1042 buf += use_len;
1043
1044 if( first_error == 0 )
1045 first_error = ret;
1046
Paul Bakker5a5fa922014-09-26 14:53:04 +02001047 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 continue;
1049 }
1050 else
1051 break;
1052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056
1057 if( ret != 0 )
1058 {
1059 /*
1060 * Quit parsing on a memory error
1061 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001062 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063 return( ret );
1064
1065 if( first_error == 0 )
1066 first_error = ret;
1067
1068 total_failed++;
1069 continue;
1070 }
1071
1072 success = 1;
1073 }
1074 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076
1077 if( success )
1078 return( total_failed );
1079 else if( first_error )
1080 return( first_error );
1081 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083}
1084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086/*
1087 * Load one or more certificates and add them to the chained list
1088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090{
1091 int ret;
1092 size_t n;
1093 unsigned char *buf;
1094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096 return( ret );
1097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001100 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102
1103 return( ret );
1104}
1105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001107{
1108 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001109#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110 int w_ret;
1111 WCHAR szDir[MAX_PATH];
1112 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001113 char *p;
Manuel Pégourié-Gonnard9dc66f42015-10-21 10:16:29 +02001114 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001115
Paul Bakker9af723c2014-05-01 13:03:14 +02001116 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117 HANDLE hFind;
1118
1119 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121
Paul Bakker9af723c2014-05-01 13:03:14 +02001122 memset( szDir, 0, sizeof(szDir) );
1123 memset( filename, 0, MAX_PATH );
1124 memcpy( filename, path, len );
1125 filename[len++] = '\\';
1126 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127 filename[len++] = '*';
1128
Simon B635f2152016-11-03 01:11:37 +00001129 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001130 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001131 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001133
1134 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001135 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001137
1138 len = MAX_PATH - len;
1139 do
1140 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001141 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142
1143 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1144 continue;
1145
Paul Bakker9af723c2014-05-01 13:03:14 +02001146 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001147 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard9dc66f42015-10-21 10:16:29 +02001148 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001149 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001150 if( w_ret == 0 )
Ron Eldor0fb3e0a2017-01-09 15:09:16 +02001151 {
1152 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1153 goto cleanup;
1154 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 if( w_ret < 0 )
1158 ret++;
1159 else
1160 ret += w_ret;
1161 }
1162 while( FindNextFileW( hFind, &file_data ) != 0 );
1163
Paul Bakker66d5d072014-06-17 16:39:18 +02001164 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166
Ron Eldor0fb3e0a2017-01-09 15:09:16 +02001167cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001169#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001170 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001172 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173 char entry_name[255];
1174 DIR *dir = opendir( path );
1175
Paul Bakker66d5d072014-06-17 16:39:18 +02001176 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001178
Ron Eldoree709f42017-01-09 19:27:59 +02001179#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001180 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001181 {
1182 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001183 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001184 }
Ron Eldoree709f42017-01-09 19:27:59 +02001185#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001186
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001187 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 mbedtls_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001190
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001191 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 {
1193 closedir( dir );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001195 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001196 }
1197
1198 if( !S_ISREG( sb.st_mode ) )
1199 continue;
1200
1201 // Ignore parse errors
1202 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001204 if( t_ret < 0 )
1205 ret++;
1206 else
1207 ret += t_ret;
1208 }
1209 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001210
1211cleanup:
Ron Eldoree709f42017-01-09 19:27:59 +02001212#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001213 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldoree709f42017-01-09 19:27:59 +02001215#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001216
Paul Bakkerbe089b02013-10-14 15:51:50 +02001217#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001218
1219 return( ret );
1220}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001222
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001223static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001225{
1226 size_t i;
1227 size_t n = *size;
1228 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001230 const char *sep = "";
1231 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001232
1233 while( cur != NULL )
1234 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001235 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001236 {
1237 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001238 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001239 }
1240
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001241 n -= cur->buf.len + sep_len;
1242 for( i = 0; i < sep_len; i++ )
1243 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001244 for( i = 0; i < cur->buf.len; i++ )
1245 *p++ = cur->buf.p[i];
1246
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001247 sep = ", ";
1248 sep_len = 2;
1249
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001250 cur = cur->next;
1251 }
1252
1253 *p = '\0';
1254
1255 *size = n;
1256 *buf = p;
1257
1258 return( 0 );
1259}
1260
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001261#define PRINT_ITEM(i) \
1262 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001264 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001265 sep = ", "; \
1266 }
1267
1268#define CERT_TYPE(type,name) \
1269 if( ns_cert_type & type ) \
1270 PRINT_ITEM( name );
1271
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001272static int x509_info_cert_type( char **buf, size_t *size,
1273 unsigned char ns_cert_type )
1274{
1275 int ret;
1276 size_t n = *size;
1277 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001278 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001281 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1283 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1284 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001285 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1286 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1287 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001288
1289 *size = n;
1290 *buf = p;
1291
1292 return( 0 );
1293}
1294
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001295#define KEY_USAGE(code,name) \
1296 if( key_usage & code ) \
1297 PRINT_ITEM( name );
1298
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001299static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001300 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001301{
1302 int ret;
1303 size_t n = *size;
1304 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001305 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1308 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001309 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1310 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1311 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1313 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001314 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1315 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001316
1317 *size = n;
1318 *buf = p;
1319
1320 return( 0 );
1321}
1322
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001323static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001325{
1326 int ret;
1327 const char *desc;
1328 size_t n = *size;
1329 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001331 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001332
1333 while( cur != NULL )
1334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001336 desc = "???";
1337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001339 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001340
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001341 sep = ", ";
1342
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001343 cur = cur->next;
1344 }
1345
1346 *size = n;
1347 *buf = p;
1348
1349 return( 0 );
1350}
1351
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352/*
1353 * Return an informational string about the certificate.
1354 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001355#define BEFORE_COLON 18
1356#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1358 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001359{
1360 int ret;
1361 size_t n;
1362 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363 char key_size_str[BEFORE_COLON];
1364
1365 p = buf;
1366 n = size;
1367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001370 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001373 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001376 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001379 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001381 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001384 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001386 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1390 crt->valid_from.year, crt->valid_from.mon,
1391 crt->valid_from.day, crt->valid_from.hour,
1392 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001393 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001396 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1397 crt->valid_to.year, crt->valid_to.mon,
1398 crt->valid_to.day, crt->valid_to.hour,
1399 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001400 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001403 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001406 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001407 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001408
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001409 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1411 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001412 {
1413 return( ret );
1414 }
1415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001417 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001418 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001419
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001420 /*
1421 * Optional extensions
1422 */
1423
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001424 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001427 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001428 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001429
1430 if( crt->max_pathlen > 0 )
1431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001433 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001434 }
1435 }
1436
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001437 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001440 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001441
1442 if( ( ret = x509_info_subject_alt_name( &p, &n,
1443 &crt->subject_alt_names ) ) != 0 )
1444 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001445 }
1446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001450 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001451
1452 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1453 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001454 }
1455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001459 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001460
1461 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1462 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001463 }
1464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001468 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001469
1470 if( ( ret = x509_info_ext_key_usage( &p, &n,
1471 &crt->ext_key_usage ) ) != 0 )
1472 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001473 }
1474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001476 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001477
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001478 return( (int) ( size - n ) );
1479}
1480
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001481struct x509_crt_verify_string {
1482 int code;
1483 const char *string;
1484};
1485
1486static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001487 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001488 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1489 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1490 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1491 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1492 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001493 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1494 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1495 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001496 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001497 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1498 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1499 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1500 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001501 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1502 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1503 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1504 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1505 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1506 { 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 +01001507 { 0, NULL }
1508};
1509
1510int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001511 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001512{
1513 int ret;
1514 const struct x509_crt_verify_string *cur;
1515 char *p = buf;
1516 size_t n = size;
1517
1518 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1519 {
1520 if( ( flags & cur->code ) == 0 )
1521 continue;
1522
1523 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001524 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001525 flags ^= cur->code;
1526 }
1527
1528 if( flags != 0 )
1529 {
1530 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1531 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001532 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001533 }
1534
1535 return( (int) ( size - n ) );
1536}
1537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001539int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1540 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001541{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001542 unsigned int usage_must, usage_may;
1543 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1544 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1545
1546 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1547 return( 0 );
1548
1549 usage_must = usage & ~may_mask;
1550
1551 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1552 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1553
1554 usage_may = usage & may_mask;
1555
1556 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001558
1559 return( 0 );
1560}
1561#endif
1562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1564int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001565 const char *usage_oid,
1566 size_t usage_len )
1567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001569
1570 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001572 return( 0 );
1573
1574 /*
1575 * Look for the requested usage (or wildcard ANY) in our list
1576 */
1577 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001580
1581 if( cur_oid->len == usage_len &&
1582 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1583 {
1584 return( 0 );
1585 }
1586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001588 return( 0 );
1589 }
1590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001592}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596/*
1597 * Return 1 if the certificate is revoked, or 0 otherwise.
1598 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001599int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602
1603 while( cur != NULL && cur->serial.len != 0 )
1604 {
1605 if( crt->serial.len == cur->serial.len &&
1606 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1607 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001608 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001609 return( 1 );
1610 }
1611
1612 cur = cur->next;
1613 }
1614
1615 return( 0 );
1616}
1617
1618/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001619 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001622 mbedtls_x509_crl *crl_list,
1623 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624{
1625 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1627 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628
1629 if( ca == NULL )
1630 return( flags );
1631
1632 /*
1633 * TODO: What happens if no CRL is present?
1634 * Suggestion: Revocation state should be unknown if no CRL is present.
1635 * For backwards compatibility this is not yet implemented.
1636 */
1637
1638 while( crl_list != NULL )
1639 {
1640 if( crl_list->version == 0 ||
1641 crl_list->issuer_raw.len != ca->subject_raw.len ||
1642 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1643 crl_list->issuer_raw.len ) != 0 )
1644 {
1645 crl_list = crl_list->next;
1646 continue;
1647 }
1648
1649 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001650 * Check if the CA is configured to sign CRLs
1651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1653 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001656 break;
1657 }
1658#endif
1659
1660 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661 * Check if CRL is correctly signed by the trusted CA
1662 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001663 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1664 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1665
1666 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1667 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670 if( md_info == NULL )
1671 {
1672 /*
1673 * Cannot check 'unknown' hash
1674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001676 break;
1677 }
1678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001681 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1682 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1685 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001686 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001689 break;
1690 }
1691
1692 /*
1693 * Check for validity of CRL (Do not drop out)
1694 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001695 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001697
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001698 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001699 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001700
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001701 /*
1702 * Check if certificate is revoked
1703 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001704 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001707 break;
1708 }
1709
1710 crl_list = crl_list->next;
1711 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001712
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001713 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001714}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001716
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001717/*
1718 * Like memcmp, but case-insensitive and always returns -1 if different
1719 */
1720static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721{
1722 size_t i;
1723 unsigned char diff;
1724 const unsigned char *n1 = s1, *n2 = s2;
1725
1726 for( i = 0; i < len; i++ )
1727 {
1728 diff = n1[i] ^ n2[i];
1729
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001730 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001731 continue;
1732
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001733 if( diff == 32 &&
1734 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1735 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1736 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001737 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001738 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001739
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001740 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001741 }
1742
1743 return( 0 );
1744}
1745
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001746/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001747 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001748 */
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001749static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001750{
1751 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001752 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001753
1754 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1755 return( 0 );
1756
Paul Bakker14b16c62014-05-28 11:33:54 +02001757 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001758 {
1759 if( cn[i] == '.' )
1760 {
1761 cn_idx = i;
1762 break;
1763 }
1764 }
1765
1766 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001767 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768
Paul Bakker14b16c62014-05-28 11:33:54 +02001769 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001770 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001771 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001772 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001773 }
1774
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001775 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001776}
1777
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001778/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001779 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1780 * variations (but not all).
1781 *
1782 * Return 0 if equal, -1 otherwise.
1783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001785{
1786 if( a->tag == b->tag &&
1787 a->len == b->len &&
1788 memcmp( a->p, b->p, b->len ) == 0 )
1789 {
1790 return( 0 );
1791 }
1792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1794 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001795 a->len == b->len &&
1796 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1797 {
1798 return( 0 );
1799 }
1800
1801 return( -1 );
1802}
1803
1804/*
1805 * Compare two X.509 Names (aka rdnSequence).
1806 *
1807 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1808 * we sometimes return unequal when the full algorithm would return equal,
1809 * but never the other way. (In particular, we don't do Unicode normalisation
1810 * or space folding.)
1811 *
1812 * Return 0 if equal, -1 otherwise.
1813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001815{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001816 /* Avoid recursion, it might not be optimised by the compiler */
1817 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001818 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001819 if( a == NULL || b == NULL )
1820 return( -1 );
1821
1822 /* type */
1823 if( a->oid.tag != b->oid.tag ||
1824 a->oid.len != b->oid.len ||
1825 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1826 {
1827 return( -1 );
1828 }
1829
1830 /* value */
1831 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1832 return( -1 );
1833
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001834 /* structure of the list of sets */
1835 if( a->next_merged != b->next_merged )
1836 return( -1 );
1837
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001838 a = a->next;
1839 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001840 }
1841
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001842 /* a == NULL == b */
1843 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001844}
1845
1846/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001847 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1848 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001849 *
1850 * top means parent is a locally-trusted certificate
1851 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1854 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001855 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001856{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001857 int need_ca_bit;
1858
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001859 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001860 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001861 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001862
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001863 /* Parent must have the basicConstraints CA bit set as a general rule */
1864 need_ca_bit = 1;
1865
1866 /* Exception: v1/v2 certificates that are locally trusted. */
1867 if( top && parent->version < 3 )
1868 need_ca_bit = 0;
1869
1870 /* Exception: self-signed end-entity certs that are locally trusted. */
1871 if( top && bottom &&
1872 child->raw.len == parent->raw.len &&
1873 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1874 {
1875 need_ca_bit = 0;
1876 }
1877
1878 if( need_ca_bit && ! parent->ca_istrue )
1879 return( -1 );
1880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001882 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001884 {
1885 return( -1 );
1886 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001887#endif
1888
1889 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001890}
1891
Paul Bakkerddf26b42013-09-18 13:46:23 +02001892static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001894 mbedtls_x509_crl *ca_crl,
1895 const mbedtls_x509_crt_profile *profile,
Janos Follath860f2392015-10-12 09:02:20 +02001896 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001897 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001898 void *p_vrfy )
1899{
1900 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001901 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001902 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1904 const mbedtls_md_info_t *md_info;
Andres AG8136e822016-12-09 17:26:23 +00001905 mbedtls_x509_crt *future_past_ca = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001906
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001907 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001908 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001909
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001910 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001912
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001913 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
1914 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
1915
1916 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
1917 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
1918
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001919 /*
1920 * Child is the top of the chain. Check against the trust_ca list.
1921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001925 if( md_info == NULL )
1926 {
1927 /*
1928 * Cannot check 'unknown', no need to try any CA
1929 */
1930 trust_ca = NULL;
1931 }
1932 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001934
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001935 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001936 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001937 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001938 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001939
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001940 check_path_cnt = path_cnt + 1;
1941
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001942 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001943 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001944 * the same as the trusted CA
1945 */
1946 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1947 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1948 child->issuer_raw.len ) == 0 )
1949 {
1950 check_path_cnt--;
1951 }
1952
Janos Follath860f2392015-10-12 09:02:20 +02001953 /* Self signed certificates do not count towards the limit */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001954 if( trust_ca->max_pathlen > 0 &&
Janos Follath860f2392015-10-12 09:02:20 +02001955 trust_ca->max_pathlen < check_path_cnt - self_cnt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001956 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001957 continue;
1958 }
1959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1961 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001962 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001963 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001964 continue;
1965 }
1966
Andres AG8136e822016-12-09 17:26:23 +00001967 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) ||
1968 mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
1969 {
1970 if ( future_past_ca == NULL )
1971 future_past_ca = trust_ca;
1972
1973 continue;
1974 }
1975
1976 break;
1977 }
1978
1979 if( trust_ca != NULL || ( trust_ca = future_past_ca ) != NULL )
1980 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001981 /*
1982 * Top of chain is signed by a trusted CA
1983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardfa67eba2015-06-27 14:41:38 +02001985
1986 if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
1987 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001988 }
1989
1990 /*
1991 * If top of chain is not the same as the trusted CA send a verify request
1992 * to the callback for any issues with validity and CRL presence for the
1993 * trusted CA certificate.
1994 */
1995 if( trust_ca != NULL &&
1996 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1997 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1998 child->issuer_raw.len ) != 0 ) )
1999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002001 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002002 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02002003#else
2004 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002005#endif
2006
Andres AG8136e822016-12-09 17:26:23 +00002007 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
2008 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
2009
2010 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
2011 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
2012
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002013 if( NULL != f_vrfy )
2014 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002015 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
2016 &ca_flags ) ) != 0 )
2017 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002018 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002019 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002020 }
2021 }
2022
2023 /* Call callback on top cert */
2024 if( NULL != f_vrfy )
2025 {
Paul Bakker66d5d072014-06-17 16:39:18 +02002026 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002027 return( ret );
2028 }
2029
2030 *flags |= ca_flags;
2031
2032 return( 0 );
2033}
2034
Paul Bakkerddf26b42013-09-18 13:46:23 +02002035static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002036 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
2037 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
2038 const mbedtls_x509_crt_profile *profile,
Janos Follath860f2392015-10-12 09:02:20 +02002039 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002040 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002041 void *p_vrfy )
2042{
2043 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002044 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2046 mbedtls_x509_crt *grandparent;
2047 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002048
Janos Follath860f2392015-10-12 09:02:20 +02002049 /* Counting intermediate self signed certificates */
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002050 if( ( path_cnt != 0 ) && x509_name_cmp( &child->issuer, &child->subject ) == 0 )
Janos Follath860f2392015-10-12 09:02:20 +02002051 self_cnt++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002052
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002053 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002055 {
Manuel Pégourié-Gonnardc3863172017-06-26 10:11:49 +02002056 /* return immediately as the goal is to avoid unbounded recursion */
2057 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002058 }
2059
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002060 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002061 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02002062
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002063 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002065
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002066 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2067 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2068
2069 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2070 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002073 if( md_info == NULL )
2074 {
2075 /*
2076 * Cannot check 'unknown' hash
2077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002079 }
2080 else
2081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002083
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002084 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2085 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2088 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002089 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002092 }
2093 }
2094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002096 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002097 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002098#endif
2099
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002100 /* Look for a grandparent in trusted CAs */
2101 for( grandparent = trust_ca;
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002102 grandparent != NULL;
2103 grandparent = grandparent->next )
2104 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002105 if( x509_crt_check_parent( parent, grandparent,
2106 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002107 break;
2108 }
2109
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002110 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002111 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002112 ret = x509_crt_verify_top( parent, grandparent, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002113 path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002114 if( ret != 0 )
2115 return( ret );
2116 }
2117 else
2118 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002119 /* Look for a grandparent upwards the chain */
2120 for( grandparent = parent->next;
2121 grandparent != NULL;
2122 grandparent = grandparent->next )
2123 {
Janos Follath860f2392015-10-12 09:02:20 +02002124 /* +2 because the current step is not yet accounted for
2125 * and because max_pathlen is one higher than it should be.
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002126 * Also self signed certificates do not count to the limit. */
Janos Follath860f2392015-10-12 09:02:20 +02002127 if( grandparent->max_pathlen > 0 &&
2128 grandparent->max_pathlen < 2 + path_cnt - self_cnt )
2129 {
2130 continue;
2131 }
2132
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002133 if( x509_crt_check_parent( parent, grandparent,
2134 0, path_cnt == 0 ) == 0 )
2135 break;
2136 }
2137
2138 /* Is our parent part of the chain or at the top? */
2139 if( grandparent != NULL )
2140 {
2141 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
Janos Follath860f2392015-10-12 09:02:20 +02002142 profile, path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002143 f_vrfy, p_vrfy );
2144 if( ret != 0 )
2145 return( ret );
2146 }
2147 else
2148 {
2149 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002150 path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002151 f_vrfy, p_vrfy );
2152 if( ret != 0 )
2153 return( ret );
2154 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002155 }
2156
2157 /* child is verified to be a child of the parent, call verify callback */
2158 if( NULL != f_vrfy )
2159 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2160 return( ret );
2161
2162 *flags |= parent_flags;
2163
2164 return( 0 );
2165}
2166
2167/*
2168 * Verify the certificate validity
2169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2171 mbedtls_x509_crt *trust_ca,
2172 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002173 const char *cn, uint32_t *flags,
2174 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002175 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002176{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002177 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002178 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002179}
2180
2181
2182/*
2183 * Verify the certificate validity, with profile
2184 */
2185int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2186 mbedtls_x509_crt *trust_ca,
2187 mbedtls_x509_crl *ca_crl,
2188 const mbedtls_x509_crt_profile *profile,
2189 const char *cn, uint32_t *flags,
2190 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2191 void *p_vrfy )
2192{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002193 size_t cn_len;
2194 int ret;
Janos Follath860f2392015-10-12 09:02:20 +02002195 int pathlen = 0, selfsigned = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 mbedtls_x509_crt *parent;
2197 mbedtls_x509_name *name;
2198 mbedtls_x509_sequence *cur = NULL;
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +02002199 mbedtls_pk_type_t pk_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002200
2201 *flags = 0;
2202
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002203 if( profile == NULL )
2204 {
2205 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2206 goto exit;
2207 }
2208
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002209 if( cn != NULL )
2210 {
2211 name = &crt->subject;
2212 cn_len = strlen( cn );
2213
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002214 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002215 {
2216 cur = &crt->subject_alt_names;
2217
2218 while( cur != NULL )
2219 {
2220 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002221 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002222 break;
2223
2224 if( cur->buf.len > 2 &&
2225 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002226 x509_check_wildcard( cn, &cur->buf ) == 0 )
2227 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002228 break;
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002229 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002230
2231 cur = cur->next;
2232 }
2233
2234 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002236 }
2237 else
2238 {
2239 while( name != NULL )
2240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002242 {
2243 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002244 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002245 break;
2246
2247 if( name->val.len > 2 &&
2248 memcmp( name->val.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002249 x509_check_wildcard( cn, &name->val ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002250 break;
2251 }
2252
2253 name = name->next;
2254 }
2255
2256 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002258 }
2259 }
2260
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +02002261 /* Check the type and size of the key */
2262 pk_type = mbedtls_pk_get_type( &crt->pk );
2263
2264 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
2265 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2266
2267 if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
2268 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2269
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002270 /* Look for a parent in trusted CAs */
2271 for( parent = trust_ca; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002272 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002273 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002274 break;
2275 }
2276
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002277 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002278 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002279 ret = x509_crt_verify_top( crt, parent, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002280 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002281 if( ret != 0 )
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002282 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002283 }
2284 else
2285 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002286 /* Look for a parent upwards the chain */
2287 for( parent = crt->next; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002288 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
2289 break;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002290
2291 /* Are we part of the chain or at the top? */
2292 if( parent != NULL )
2293 {
2294 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002295 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002296 if( ret != 0 )
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002297 goto exit;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002298 }
2299 else
2300 {
2301 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002302 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002303 if( ret != 0 )
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002304 goto exit;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002305 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002306 }
2307
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002308exit:
Manuel Pégourié-Gonnardcdb4dc92017-07-06 12:16:25 +02002309 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2310 * the SSL module for authmode optional, but non-zero return from the
2311 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnardc3863172017-06-26 10:11:49 +02002312 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2313 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2314
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002315 if( ret != 0 )
2316 {
2317 *flags = (uint32_t) -1;
2318 return( ret );
2319 }
2320
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002321 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002323
2324 return( 0 );
2325}
2326
2327/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002328 * Initialize a certificate chain
2329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002331{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002333}
2334
2335/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002336 * Unallocate all certificate data
2337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002339{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 mbedtls_x509_crt *cert_cur = crt;
2341 mbedtls_x509_crt *cert_prv;
2342 mbedtls_x509_name *name_cur;
2343 mbedtls_x509_name *name_prv;
2344 mbedtls_x509_sequence *seq_cur;
2345 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002346
2347 if( crt == NULL )
2348 return;
2349
2350 do
2351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2355 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002356#endif
2357
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002358 name_cur = cert_cur->issuer.next;
2359 while( name_cur != NULL )
2360 {
2361 name_prv = name_cur;
2362 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2364 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002365 }
2366
2367 name_cur = cert_cur->subject.next;
2368 while( name_cur != NULL )
2369 {
2370 name_prv = name_cur;
2371 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2373 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002374 }
2375
2376 seq_cur = cert_cur->ext_key_usage.next;
2377 while( seq_cur != NULL )
2378 {
2379 seq_prv = seq_cur;
2380 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2382 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002383 }
2384
2385 seq_cur = cert_cur->subject_alt_names.next;
2386 while( seq_cur != NULL )
2387 {
2388 seq_prv = seq_cur;
2389 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2391 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002392 }
2393
2394 if( cert_cur->raw.p != NULL )
2395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2397 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002398 }
2399
2400 cert_cur = cert_cur->next;
2401 }
2402 while( cert_cur != NULL );
2403
2404 cert_cur = crt;
2405 do
2406 {
2407 cert_prv = cert_cur;
2408 cert_cur = cert_cur->next;
2409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002411 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002413 }
2414 while( cert_cur != NULL );
2415}
2416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#endif /* MBEDTLS_X509_CRT_PARSE_C */