blob: 7b6481529b0a2549b0c6300f3868761a225e639d [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
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047 */
48/*
49 * The ITU-T X.509 standard defines a certificate format for PKI.
50 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020051 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
52 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
53 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054 *
55 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
56 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
57 */
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020061#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020063#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/x509_crt.h"
68#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000069
Rich Evans00ab4702015-02-06 13:43:58 +000070#include <string.h>
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#endif
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000077#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078#else
Simon Butchere5eb2582018-10-03 15:11:19 +010079#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000080#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020082#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084#endif
85
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000087#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010088#endif
89
Paul Bakkerfa6a6202013-10-28 18:48:30 +010090#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091#include <windows.h>
92#else
93#include <time.h>
94#endif
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000097#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020098#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099#include <sys/types.h>
100#include <sys/stat.h>
101#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +0000102#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103#endif
104
Paul Bakker34617722014-06-13 17:20:13 +0200105/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +0200107 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
108}
109
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200111 * Default profile
112 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200113const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
114{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200115#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200116 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200117 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200118#endif
119 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200120 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
121 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
122 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
123 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
124 0xFFFFFFF, /* Any PK alg */
125 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200126 2048,
127};
128
129/*
130 * Next-default profile
131 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200132const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
133{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200134 /* Hashes from SHA-256 and above */
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
136 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
138 0xFFFFFFF, /* Any PK alg */
139#if defined(MBEDTLS_ECP_C)
140 /* Curves at or above 128-bit security level */
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
142 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
143 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
144 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
145 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
146 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
147 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
148#else
149 0,
150#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200151 2048,
152};
153
154/*
155 * NSA Suite B Profile
156 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200157const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
158{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200159 /* Only SHA-256 and 384 */
160 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
161 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
162 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200163 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
164 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200165#if defined(MBEDTLS_ECP_C)
166 /* Only NIST P-256 and P-384 */
167 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
168 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
169#else
170 0,
171#endif
172 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200173};
174
175/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200176 * Check md_alg against profile
177 * Return 0 if md_alg acceptable for this profile, -1 otherwise
178 */
179static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
180 mbedtls_md_type_t md_alg )
181{
Philippe Antoine84cc74e2018-05-11 11:06:29 +0200182 if( md_alg == MBEDTLS_MD_NONE )
183 return( -1 );
184
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200185 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
186 return( 0 );
187
188 return( -1 );
189}
190
191/*
192 * Check pk_alg against profile
193 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
194 */
195static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
196 mbedtls_pk_type_t pk_alg )
197{
Philippe Antoine84cc74e2018-05-11 11:06:29 +0200198 if( pk_alg == MBEDTLS_PK_NONE )
199 return( -1 );
200
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200201 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
202 return( 0 );
203
204 return( -1 );
205}
206
207/*
208 * Check key against profile
209 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
210 */
211static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
212 mbedtls_pk_type_t pk_alg,
213 const mbedtls_pk_context *pk )
214{
215#if defined(MBEDTLS_RSA_C)
216 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
217 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200218 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200219 return( 0 );
220
221 return( -1 );
222 }
223#endif
224
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200225#if defined(MBEDTLS_ECP_C)
226 if( pk_alg == MBEDTLS_PK_ECDSA ||
227 pk_alg == MBEDTLS_PK_ECKEY ||
228 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200229 {
230 mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
231
Philippe Antoine84cc74e2018-05-11 11:06:29 +0200232 if( gid == MBEDTLS_ECP_DP_NONE )
233 return( -1 );
234
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200235 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
236 return( 0 );
237
238 return( -1 );
239 }
240#endif
241
242 return( -1 );
243}
244
245/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200246 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
247 */
248static int x509_get_version( unsigned char **p,
249 const unsigned char *end,
250 int *ver )
251{
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_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200259 {
260 *ver = 0;
261 return( 0 );
262 }
263
Hanno Becker19557c22019-02-12 11:52:10 +0000264 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265 }
266
267 end = *p + len;
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
270 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271
272 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_X509_INVALID_VERSION +
274 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275
276 return( 0 );
277}
278
279/*
280 * Validity ::= SEQUENCE {
281 * notBefore Time,
282 * notAfter Time }
283 */
284static int x509_get_dates( unsigned char **p,
285 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_x509_time *from,
287 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288{
289 int ret;
290 size_t len;
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
293 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
294 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200295
296 end = *p + len;
297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200299 return( ret );
300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200302 return( ret );
303
304 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 return( MBEDTLS_ERR_X509_INVALID_DATE +
306 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307
308 return( 0 );
309}
310
311/*
312 * X.509 v2/v3 unique identifier (not parsed)
313 */
314static int x509_get_uid( unsigned char **p,
315 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200317{
318 int ret;
319
320 if( *p == end )
321 return( 0 );
322
323 uid->tag = **p;
324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
326 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 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 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 return( 0 );
330
Hanno Becker19557c22019-02-12 11:52:10 +0000331 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200332 }
333
334 uid->p = *p;
335 *p += uid->len;
336
337 return( 0 );
338}
339
340static int x509_get_basic_constraints( unsigned char **p,
341 const unsigned char *end,
342 int *ca_istrue,
343 int *max_pathlen )
344{
345 int ret;
346 size_t len;
347
348 /*
349 * BasicConstraints ::= SEQUENCE {
350 * cA BOOLEAN DEFAULT FALSE,
351 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
352 */
353 *ca_istrue = 0; /* DEFAULT FALSE */
354 *max_pathlen = 0; /* endless */
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
357 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
358 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359
360 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200361 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
366 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367
368 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370
371 if( *ca_istrue != 0 )
372 *ca_istrue = 1;
373 }
374
375 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200376 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
379 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380
381 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
383 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384
Andrzej Kurek3fd92972020-04-14 09:49:52 -0400385 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
386 * overflow, which is an undefined behavior. */
387 if( *max_pathlen == INT_MAX )
388 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
389 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
390
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 (*max_pathlen)++;
392
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200393 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200394}
395
396static int x509_get_ns_cert_type( unsigned char **p,
397 const unsigned char *end,
398 unsigned char *ns_cert_type)
399{
400 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
404 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405
406 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
408 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409
410 /* Get actual bitstring */
411 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200412 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413}
414
415static int x509_get_key_usage( unsigned char **p,
416 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100417 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418{
419 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200420 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
424 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
426 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
428 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429
430 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200431 *key_usage = 0;
432 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
433 {
434 *key_usage |= (unsigned int) bs.p[i] << (8*i);
435 }
436
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200437 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438}
439
440/*
441 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
442 *
443 * KeyPurposeId ::= OBJECT IDENTIFIER
444 */
445static int x509_get_ext_key_usage( unsigned char **p,
446 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448{
449 int ret;
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
452 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453
454 /* Sequence length must be >= 1 */
455 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
457 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200459 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460}
461
462/*
463 * SubjectAltName ::= GeneralNames
464 *
465 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
466 *
467 * GeneralName ::= CHOICE {
468 * otherName [0] OtherName,
469 * rfc822Name [1] IA5String,
470 * dNSName [2] IA5String,
471 * x400Address [3] ORAddress,
472 * directoryName [4] Name,
473 * ediPartyName [5] EDIPartyName,
474 * uniformResourceIdentifier [6] IA5String,
475 * iPAddress [7] OCTET STRING,
476 * registeredID [8] OBJECT IDENTIFIER }
477 *
478 * OtherName ::= SEQUENCE {
479 * type-id OBJECT IDENTIFIER,
480 * value [0] EXPLICIT ANY DEFINED BY type-id }
481 *
482 * EDIPartyName ::= SEQUENCE {
483 * nameAssigner [0] DirectoryString OPTIONAL,
484 * partyName [1] DirectoryString }
485 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000486 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487 */
488static int x509_get_subject_alt_name( unsigned char **p,
489 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491{
492 int ret;
493 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497
498 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
500 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
501 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502
503 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
505 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
507 while( *p < end )
508 {
509 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
511 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512
513 tag = **p;
514 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
516 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
Andres Amaya Garcia64519092017-08-25 17:13:12 +0100518 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
519 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
522 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Andres Amaya Garcia64519092017-08-25 17:13:12 +0100523 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200525 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527 {
528 *p += tag_len;
529 continue;
530 }
531
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200533 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100535 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100537
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200538 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539
540 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200542 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544 cur = cur->next;
545 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200546
547 buf = &(cur->buf);
548 buf->tag = tag;
549 buf->p = *p;
550 buf->len = tag_len;
551 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552 }
553
554 /* Set final sequence entry's next pointer to NULL */
555 cur->next = NULL;
556
557 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
559 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560
561 return( 0 );
562}
563
564/*
565 * X.509 v3 extensions
566 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567 */
568static int x509_get_crt_ext( unsigned char **p,
569 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571{
572 int ret;
573 size_t len;
574 unsigned char *end_ext_data, *end_ext_octet;
575
Hanno Becker1de13db2019-02-12 17:22:36 +0000576 if( *p == end )
577 return( 0 );
578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581
Hanno Becker1de13db2019-02-12 17:22:36 +0000582 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583 while( *p < end )
584 {
585 /*
586 * Extension ::= SEQUENCE {
587 * extnID OBJECT IDENTIFIER,
588 * critical BOOLEAN DEFAULT FALSE,
589 * extnValue OCTET STRING }
590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592 int is_critical = 0; /* DEFAULT FALSE */
593 int ext_type = 0;
594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
596 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
597 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200598
599 end_ext_data = *p + len;
600
601 /* Get extension ID */
k-stachowiak69789492018-07-16 10:49:12 +0200602 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiakf4a66882018-07-24 12:54:39 +0200603 MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605
k-stachowiak69789492018-07-16 10:49:12 +0200606 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200607 extn_oid.p = *p;
608 *p += extn_oid.len;
609
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
612 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
613 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614
615 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
617 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
618 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619
620 end_ext_octet = *p + len;
621
622 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
624 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625
626 /*
627 * Detect supported extensions
628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630
631 if( ret != 0 )
632 {
633 /* No parser found, skip extension */
634 *p = end_ext_octet;
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637 if( is_critical )
638 {
639 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
641 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 }
643#endif
644 continue;
645 }
646
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100647 /* Forbid repeated extensions */
648 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100650
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651 crt->ext_types |= ext_type;
652
653 switch( ext_type )
654 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100655 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656 /* Parse basic constraints */
657 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
658 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200659 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660 break;
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663 /* Parse key usage */
664 if( ( ret = x509_get_key_usage( p, end_ext_octet,
665 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200666 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200667 break;
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670 /* Parse extended key usage */
671 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
672 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200673 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 break;
675
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100676 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 /* Parse subject alt name */
678 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
679 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200680 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681 break;
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684 /* Parse netscape certificate type */
685 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
686 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200687 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688 break;
689
690 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692 }
693 }
694
695 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
697 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698
699 return( 0 );
700}
701
702/*
703 * Parse and fill a single X.509 certificate in DER format
704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200706 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707{
708 int ret;
709 size_t len;
710 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
714 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
715 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716
717 /*
718 * Check for valid input
719 */
720 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722
Janos Follathcc0e49d2016-02-17 14:34:12 +0000723 // Use the original buffer until we figure out actual length
724 p = (unsigned char*) buf;
725 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726 end = p + len;
727
728 /*
729 * Certificate ::= SEQUENCE {
730 * tbsCertificate TBSCertificate,
731 * signatureAlgorithm AlgorithmIdentifier,
732 * signatureValue BIT STRING }
733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
735 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_x509_crt_free( crt );
738 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 }
740
741 if( len > (size_t) ( end - p ) )
742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_x509_crt_free( crt );
744 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
745 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746 }
747 crt_end = p + len;
748
Janos Follathcc0e49d2016-02-17 14:34:12 +0000749 // Create and populate a new buffer for the raw field
750 crt->raw.len = crt_end - buf;
751 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
752 if( p == NULL )
753 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
754
755 memcpy( p, buf, crt->raw.len );
756
Hanno Becker2e7fee02017-09-25 10:47:58 +0100757 // Direct pointers to the new buffer
Janos Follathcc0e49d2016-02-17 14:34:12 +0000758 p += crt->raw.len - len;
759 end = crt_end = p + len;
760
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761 /*
762 * TBSCertificate ::= SEQUENCE {
763 */
764 crt->tbs.p = p;
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
767 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_x509_crt_free( crt );
770 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 }
772
773 end = p + len;
774 crt->tbs.len = end - crt->tbs.p;
775
776 /*
777 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
778 *
779 * CertificateSerialNumber ::= INTEGER
780 *
781 * signature AlgorithmIdentifier
782 */
783 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
785 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200786 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789 return( ret );
790 }
791
Andres AG80164742017-03-09 16:16:11 +0000792 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_x509_crt_free( crt );
795 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796 }
797
Andres AG80164742017-03-09 16:16:11 +0000798 crt->version++;
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200801 &crt->sig_md, &crt->sig_pk,
802 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200805 return( ret );
806 }
807
808 /*
809 * issuer Name
810 */
811 crt->issuer_raw.p = p;
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
814 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_x509_crt_free( crt );
817 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818 }
819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823 return( ret );
824 }
825
826 crt->issuer_raw.len = p - crt->issuer_raw.p;
827
828 /*
829 * Validity ::= SEQUENCE {
830 * notBefore Time,
831 * notAfter Time }
832 *
833 */
834 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
835 &crt->valid_to ) ) != 0 )
836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200838 return( ret );
839 }
840
841 /*
842 * subject Name
843 */
844 crt->subject_raw.p = p;
845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
847 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_x509_crt_free( crt );
850 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851 }
852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856 return( ret );
857 }
858
859 crt->subject_raw.len = p - crt->subject_raw.p;
860
861 /*
862 * SubjectPublicKeyInfo
863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200867 return( ret );
868 }
869
870 /*
871 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
872 * -- If present, version shall be v2 or v3
873 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
874 * -- If present, version shall be v2 or v3
875 * extensions [3] EXPLICIT Extensions OPTIONAL
876 * -- If present, version shall be v3
877 */
878 if( crt->version == 2 || crt->version == 3 )
879 {
880 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
881 if( ret != 0 )
882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200884 return( ret );
885 }
886 }
887
888 if( crt->version == 2 || crt->version == 3 )
889 {
890 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
891 if( ret != 0 )
892 {
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 }
897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200900#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100901 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200902 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 if( ret != 0 )
904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906 return( ret );
907 }
908 }
909
910 if( p != end )
911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_x509_crt_free( crt );
913 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
914 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915 }
916
917 end = crt_end;
918
919 /*
920 * }
921 * -- end of TBSCertificate
922 *
923 * signatureAlgorithm AlgorithmIdentifier,
924 * signatureValue BIT STRING
925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929 return( ret );
930 }
931
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100932 if( crt->sig_oid.len != sig_oid2.len ||
933 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200934 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200935 ( sig_params1.len != 0 &&
936 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 mbedtls_x509_crt_free( crt );
939 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940 }
941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945 return( ret );
946 }
947
948 if( p != end )
949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_x509_crt_free( crt );
951 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
952 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953 }
954
955 return( 0 );
956}
957
958/*
959 * Parse one X.509 certificate in DER format from a buffer and add them to a
960 * chained list
961 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200963 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964{
965 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967
968 /*
969 * Check for valid input
970 */
971 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973
974 while( crt->version != 0 && crt->next != NULL )
975 {
976 prev = crt;
977 crt = crt->next;
978 }
979
980 /*
981 * Add new certificate on the end of the chain if needed.
982 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200983 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200985 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986
987 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200988 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989
990 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 }
994
Paul Bakkerddf26b42013-09-18 13:46:23 +0200995 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996 {
997 if( prev )
998 prev->next = NULL;
999
1000 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002
1003 return( ret );
1004 }
1005
1006 return( 0 );
1007}
1008
1009/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001010 * Parse one or more PEM certificates from a buffer and add them to the chained
1011 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014{
Janos Follath98e28a72016-05-31 14:03:54 +01001015#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001016 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001018#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019
1020 /*
1021 * Check for valid input
1022 */
1023 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025
1026 /*
1027 * Determine buffer content. Buffer contains either one DER certificate or
1028 * one or more PEM certificates.
1029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001031 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001032 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001035 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1038 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001039#else
1040 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1041#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#if defined(MBEDTLS_PEM_PARSE_C)
1044 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045 {
1046 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001049 /* 1 rather than 0 since the terminating NULL byte is counted in */
1050 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051 {
1052 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001054
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001055 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001057 "-----BEGIN CERTIFICATE-----",
1058 "-----END CERTIFICATE-----",
1059 buf, NULL, 0, &use_len );
1060
1061 if( ret == 0 )
1062 {
1063 /*
1064 * Was PEM encoded
1065 */
1066 buflen -= use_len;
1067 buf += use_len;
1068 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070 {
1071 return( ret );
1072 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076
1077 /*
1078 * PEM header and footer were found
1079 */
1080 buflen -= use_len;
1081 buf += use_len;
1082
1083 if( first_error == 0 )
1084 first_error = ret;
1085
Paul Bakker5a5fa922014-09-26 14:53:04 +02001086 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001087 continue;
1088 }
1089 else
1090 break;
1091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095
1096 if( ret != 0 )
1097 {
1098 /*
1099 * Quit parsing on a memory error
1100 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001101 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102 return( ret );
1103
1104 if( first_error == 0 )
1105 first_error = ret;
1106
1107 total_failed++;
1108 continue;
1109 }
1110
1111 success = 1;
1112 }
1113 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114
1115 if( success )
1116 return( total_failed );
1117 else if( first_error )
1118 return( first_error );
1119 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001121#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122}
1123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001125/*
1126 * Load one or more certificates and add them to the chained list
1127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129{
1130 int ret;
1131 size_t n;
1132 unsigned char *buf;
1133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001135 return( ret );
1136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001138
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001139 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001141
1142 return( ret );
1143}
1144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001146{
1147 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001148#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149 int w_ret;
1150 WCHAR szDir[MAX_PATH];
1151 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001152 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001153 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154
Paul Bakker9af723c2014-05-01 13:03:14 +02001155 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 HANDLE hFind;
1157
1158 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160
Paul Bakker9af723c2014-05-01 13:03:14 +02001161 memset( szDir, 0, sizeof(szDir) );
1162 memset( filename, 0, MAX_PATH );
1163 memcpy( filename, path, len );
1164 filename[len++] = '\\';
1165 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166 filename[len++] = '*';
1167
Simon B3c6b18d2016-11-03 01:11:37 +00001168 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001169 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001170 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172
1173 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001174 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176
1177 len = MAX_PATH - len;
1178 do
1179 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001180 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181
1182 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1183 continue;
1184
Paul Bakker9af723c2014-05-01 13:03:14 +02001185 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001186 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001187 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001188 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001189 if( w_ret == 0 )
Ron Eldor3e19df52017-01-09 15:09:16 +02001190 {
1191 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1192 goto cleanup;
1193 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001196 if( w_ret < 0 )
1197 ret++;
1198 else
1199 ret += w_ret;
1200 }
1201 while( FindNextFileW( hFind, &file_data ) != 0 );
1202
Paul Bakker66d5d072014-06-17 16:39:18 +02001203 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001205
Ron Eldor3e19df52017-01-09 15:09:16 +02001206cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001207 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001208#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001209 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001210 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001211 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001212 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001213 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001214 DIR *dir = opendir( path );
1215
Paul Bakker66d5d072014-06-17 16:39:18 +02001216 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001218
Ron Eldor8ab05952017-01-09 19:27:59 +02001219#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001220 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001221 {
1222 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001223 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001224 }
Ron Eldor8ab05952017-01-09 19:27:59 +02001225#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001226
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001227 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001228 {
Andres AGf9113192016-09-02 14:06:04 +01001229 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1230 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001231
Andres AGf9113192016-09-02 14:06:04 +01001232 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001233 {
Andres AGf9113192016-09-02 14:06:04 +01001234 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1235 goto cleanup;
1236 }
1237 else if( stat( entry_name, &sb ) == -1 )
1238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001240 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001241 }
1242
Janos Follathb40d60f2020-02-04 14:47:45 +00001243 if( !S_ISREG( sb.st_mode ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001244 continue;
1245
1246 // Ignore parse errors
1247 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001249 if( t_ret < 0 )
1250 ret++;
1251 else
1252 ret += t_ret;
1253 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001254
1255cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001256 closedir( dir );
1257
Ron Eldor8ab05952017-01-09 19:27:59 +02001258#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001259 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor8ab05952017-01-09 19:27:59 +02001261#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001262
Paul Bakkerbe089b02013-10-14 15:51:50 +02001263#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001264
1265 return( ret );
1266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001268
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001269static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001271{
1272 size_t i;
1273 size_t n = *size;
1274 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001276 const char *sep = "";
1277 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001278
1279 while( cur != NULL )
1280 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001281 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001282 {
1283 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001284 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001285 }
1286
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001287 n -= cur->buf.len + sep_len;
1288 for( i = 0; i < sep_len; i++ )
1289 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001290 for( i = 0; i < cur->buf.len; i++ )
1291 *p++ = cur->buf.p[i];
1292
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001293 sep = ", ";
1294 sep_len = 2;
1295
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001296 cur = cur->next;
1297 }
1298
1299 *p = '\0';
1300
1301 *size = n;
1302 *buf = p;
1303
1304 return( 0 );
1305}
1306
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001307#define PRINT_ITEM(i) \
1308 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001310 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001311 sep = ", "; \
1312 }
1313
1314#define CERT_TYPE(type,name) \
1315 if( ns_cert_type & type ) \
1316 PRINT_ITEM( name );
1317
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001318static int x509_info_cert_type( char **buf, size_t *size,
1319 unsigned char ns_cert_type )
1320{
1321 int ret;
1322 size_t n = *size;
1323 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001324 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001327 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1329 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1330 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001331 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1332 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1333 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001334
1335 *size = n;
1336 *buf = p;
1337
1338 return( 0 );
1339}
1340
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001341#define KEY_USAGE(code,name) \
1342 if( key_usage & code ) \
1343 PRINT_ITEM( name );
1344
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001345static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001346 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001347{
1348 int ret;
1349 size_t n = *size;
1350 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001351 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1354 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001355 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1356 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1357 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1359 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001360 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1361 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001362
1363 *size = n;
1364 *buf = p;
1365
1366 return( 0 );
1367}
1368
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001369static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001371{
1372 int ret;
1373 const char *desc;
1374 size_t n = *size;
1375 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001377 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001378
1379 while( cur != NULL )
1380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001382 desc = "???";
1383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001385 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001386
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001387 sep = ", ";
1388
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001389 cur = cur->next;
1390 }
1391
1392 *size = n;
1393 *buf = p;
1394
1395 return( 0 );
1396}
1397
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001398/*
Hanno Becker52da7ee2018-11-06 13:18:23 +00001399 * Like memcmp, but case-insensitive and always returns -1 if different
1400 */
1401static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
1402{
1403 size_t i;
1404 unsigned char diff;
1405 const unsigned char *n1 = s1, *n2 = s2;
1406
1407 for( i = 0; i < len; i++ )
1408 {
1409 diff = n1[i] ^ n2[i];
1410
1411 if( diff == 0 )
1412 continue;
1413
1414 if( diff == 32 &&
1415 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1416 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1417 {
1418 continue;
1419 }
1420
1421 return( -1 );
1422 }
1423
1424 return( 0 );
1425}
1426
1427/*
1428 * Return 0 if name matches wildcard, -1 otherwise
1429 */
1430static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
1431{
1432 size_t i;
1433 size_t cn_idx = 0, cn_len = strlen( cn );
1434
1435 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1436 return( 0 );
1437
1438 for( i = 0; i < cn_len; ++i )
1439 {
1440 if( cn[i] == '.' )
1441 {
1442 cn_idx = i;
1443 break;
1444 }
1445 }
1446
1447 if( cn_idx == 0 )
1448 return( -1 );
1449
1450 if( cn_len - cn_idx == name->len - 1 &&
1451 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1452 {
1453 return( 0 );
1454 }
1455
1456 return( -1 );
1457}
1458
1459/*
1460 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1461 * variations (but not all).
1462 *
1463 * Return 0 if equal, -1 otherwise.
1464 */
1465static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
1466{
1467 if( a->tag == b->tag &&
1468 a->len == b->len &&
1469 memcmp( a->p, b->p, b->len ) == 0 )
1470 {
1471 return( 0 );
1472 }
1473
1474 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1475 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1476 a->len == b->len &&
1477 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1478 {
1479 return( 0 );
1480 }
1481
1482 return( -1 );
1483}
1484
1485/*
1486 * Compare two X.509 Names (aka rdnSequence).
1487 *
1488 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1489 * we sometimes return unequal when the full algorithm would return equal,
1490 * but never the other way. (In particular, we don't do Unicode normalisation
1491 * or space folding.)
1492 *
1493 * Return 0 if equal, -1 otherwise.
1494 */
1495static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
1496{
1497 /* Avoid recursion, it might not be optimised by the compiler */
1498 while( a != NULL || b != NULL )
1499 {
1500 if( a == NULL || b == NULL )
1501 return( -1 );
1502
1503 /* type */
1504 if( a->oid.tag != b->oid.tag ||
1505 a->oid.len != b->oid.len ||
1506 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1507 {
1508 return( -1 );
1509 }
1510
1511 /* value */
1512 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1513 return( -1 );
1514
1515 /* structure of the list of sets */
1516 if( a->next_merged != b->next_merged )
1517 return( -1 );
1518
1519 a = a->next;
1520 b = b->next;
1521 }
1522
1523 /* a == NULL == b */
1524 return( 0 );
1525}
1526
1527/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001528 * Return an informational string about the certificate.
1529 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001530#define BEFORE_COLON 18
1531#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1533 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534{
1535 int ret;
1536 size_t n;
1537 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001538 char key_size_str[BEFORE_COLON];
1539
1540 p = buf;
1541 n = size;
1542
Janos Follath98e28a72016-05-31 14:03:54 +01001543 if( NULL == crt )
1544 {
1545 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
1546 MBEDTLS_X509_SAFE_SNPRINTF;
1547
1548 return( (int) ( size - n ) );
1549 }
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001553 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001556 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001559 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001562 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001564 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001567 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001569 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001572 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1573 crt->valid_from.year, crt->valid_from.mon,
1574 crt->valid_from.day, crt->valid_from.hour,
1575 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001576 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1580 crt->valid_to.year, crt->valid_to.mon,
1581 crt->valid_to.day, crt->valid_to.hour,
1582 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001583 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001586 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001589 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001590 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001592 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1594 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001595 {
1596 return( ret );
1597 }
1598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001600 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001601 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001603 /*
1604 * Optional extensions
1605 */
1606
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001607 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001610 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001611 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001612
1613 if( crt->max_pathlen > 0 )
1614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001616 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001617 }
1618 }
1619
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001620 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001623 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001624
1625 if( ( ret = x509_info_subject_alt_name( &p, &n,
1626 &crt->subject_alt_names ) ) != 0 )
1627 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001628 }
1629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001633 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001634
1635 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1636 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001637 }
1638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001642 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001643
1644 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1645 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001646 }
1647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001651 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001652
1653 if( ( ret = x509_info_ext_key_usage( &p, &n,
1654 &crt->ext_key_usage ) ) != 0 )
1655 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001656 }
1657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001659 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001660
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661 return( (int) ( size - n ) );
1662}
1663
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001664struct x509_crt_verify_string {
1665 int code;
1666 const char *string;
1667};
1668
1669static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001670 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001671 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1672 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1673 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1674 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1675 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001676 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1677 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1678 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001679 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001680 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1681 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1682 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1683 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001684 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1685 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1686 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1687 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1688 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1689 { 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 +01001690 { 0, NULL }
1691};
1692
1693int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001694 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001695{
1696 int ret;
1697 const struct x509_crt_verify_string *cur;
1698 char *p = buf;
1699 size_t n = size;
1700
1701 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1702 {
1703 if( ( flags & cur->code ) == 0 )
1704 continue;
1705
1706 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001707 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001708 flags ^= cur->code;
1709 }
1710
1711 if( flags != 0 )
1712 {
1713 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1714 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001715 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001716 }
1717
1718 return( (int) ( size - n ) );
1719}
1720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001722int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1723 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001724{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001725 unsigned int usage_must, usage_may;
1726 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1727 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1728
1729 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1730 return( 0 );
1731
1732 usage_must = usage & ~may_mask;
1733
1734 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1735 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1736
1737 usage_may = usage & may_mask;
1738
1739 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001741
1742 return( 0 );
1743}
1744#endif
1745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1747int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001748 const char *usage_oid,
1749 size_t usage_len )
1750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001752
1753 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001755 return( 0 );
1756
1757 /*
1758 * Look for the requested usage (or wildcard ANY) in our list
1759 */
1760 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001763
1764 if( cur_oid->len == usage_len &&
1765 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1766 {
1767 return( 0 );
1768 }
1769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001771 return( 0 );
1772 }
1773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001775}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001779/*
1780 * Return 1 if the certificate is revoked, or 0 otherwise.
1781 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001782int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001783{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001785
1786 while( cur != NULL && cur->serial.len != 0 )
1787 {
1788 if( crt->serial.len == cur->serial.len &&
1789 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1790 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001791 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001792 return( 1 );
1793 }
1794
1795 cur = cur->next;
1796 }
1797
1798 return( 0 );
1799}
1800
1801/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01001802 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard75d35602018-03-05 13:22:59 +01001803 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001806 mbedtls_x509_crl *crl_list,
1807 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808{
1809 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1811 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001812
1813 if( ca == NULL )
1814 return( flags );
1815
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001816 while( crl_list != NULL )
1817 {
1818 if( crl_list->version == 0 ||
Hanno Becker18a4cbf2018-11-02 09:19:54 +00001819 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001820 {
1821 crl_list = crl_list->next;
1822 continue;
1823 }
1824
1825 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001826 * Check if the CA is configured to sign CRLs
1827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker18a4cbf2018-11-02 09:19:54 +00001829 if( mbedtls_x509_crt_check_key_usage( ca,
1830 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001833 break;
1834 }
1835#endif
1836
1837 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001838 * Check if CRL is correctly signed by the trusted CA
1839 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001840 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1841 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1842
1843 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1844 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnardb9c72eb2017-06-26 12:22:17 +02001847 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001848 {
Manuel Pégourié-Gonnardb9c72eb2017-06-26 12:22:17 +02001849 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001851 break;
1852 }
1853
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001854 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1855 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1858 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001859 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001862 break;
1863 }
1864
1865 /*
1866 * Check for validity of CRL (Do not drop out)
1867 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001868 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001870
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001871 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001872 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001873
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001874 /*
1875 * Check if certificate is revoked
1876 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001877 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001880 break;
1881 }
1882
1883 crl_list = crl_list->next;
1884 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001885
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001886 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001887}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001889
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001890/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001891 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1892 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001893 *
1894 * top means parent is a locally-trusted certificate
1895 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1898 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001899 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001900{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001901 int need_ca_bit;
1902
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001903 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001904 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001905 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001906
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001907 /* Parent must have the basicConstraints CA bit set as a general rule */
1908 need_ca_bit = 1;
1909
1910 /* Exception: v1/v2 certificates that are locally trusted. */
1911 if( top && parent->version < 3 )
1912 need_ca_bit = 0;
1913
1914 /* Exception: self-signed end-entity certs that are locally trusted. */
1915 if( top && bottom &&
1916 child->raw.len == parent->raw.len &&
1917 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1918 {
1919 need_ca_bit = 0;
1920 }
1921
1922 if( need_ca_bit && ! parent->ca_istrue )
1923 return( -1 );
1924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001926 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001928 {
1929 return( -1 );
1930 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001931#endif
1932
1933 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001934}
1935
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001936/*
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01001937 * Verify a certificate with no parent inside the chain
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001938 * (either the parent is a trusted root, or there is no parent)
1939 *
1940 * See comments for mbedtls_x509_crt_verify_with_profile()
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01001941 * (also for notation used below)
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001942 *
1943 * This function is called in two cases:
1944 * - child was found to have a parent in trusted roots, in which case we're
1945 * called with trust_ca pointing directly to that parent (not the full list)
Manuel Pégourié-Gonnard71df3732018-03-07 09:36:30 +01001946 * - this is cases 1, 2 and 3 of the comment on verify_with_profile()
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001947 * - case 1 is special as child and trust_ca point to copies of the same
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01001948 * certificate then
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001949 * - child was found to have no parent either in the chain or in trusted CAs
Manuel Pégourié-Gonnard71df3732018-03-07 09:36:30 +01001950 * - this is cases 4 and 5 of the comment on verify_with_profile()
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001951 *
1952 * For historical reasons, the function currently does not assume that
1953 * trust_ca points directly to the right root in the first case, and it
1954 * doesn't know in which case it starts, so it always starts by searching for
1955 * a parent in trust_ca.
1956 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001957static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001959 mbedtls_x509_crl *ca_crl,
1960 const mbedtls_x509_crt_profile *profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02001961 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001962 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001963 void *p_vrfy )
1964{
1965 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001966 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001967 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1969 const mbedtls_md_info_t *md_info;
Andres AGd1650662016-12-09 17:26:23 +00001970 mbedtls_x509_crt *future_past_ca = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001971
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001972 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001973 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001974
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001975 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001977
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001978 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
1979 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
1980
1981 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
1982 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
1983
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001984 /*
1985 * Child is the top of the chain. Check against the trust_ca list.
1986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 md_info = mbedtls_md_info_from_type( child->sig_md );
Manuel Pégourié-Gonnardb9c72eb2017-06-26 12:22:17 +02001990 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001991 {
Manuel Pégourié-Gonnardb9c72eb2017-06-26 12:22:17 +02001992 /* Note: this can't happen except after an internal error */
1993 /* Cannot check signature, no need to try any CA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001994 trust_ca = NULL;
1995 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001996
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001997 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001998 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001999 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002000 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002001
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01002002 check_path_cnt = path_cnt + 1;
2003
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002004 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01002005 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002006 * the same as the trusted CA
2007 */
2008 if( child->subject_raw.len == trust_ca->subject_raw.len &&
2009 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Hanno Becker2e7fee02017-09-25 10:47:58 +01002010 child->subject_raw.len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002011 {
2012 check_path_cnt--;
2013 }
2014
Janos Follath5dd4fe12015-10-12 09:02:20 +02002015 /* Self signed certificates do not count towards the limit */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002016 if( trust_ca->max_pathlen > 0 &&
Janos Follath5dd4fe12015-10-12 09:02:20 +02002017 trust_ca->max_pathlen < check_path_cnt - self_cnt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002018 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002019 continue;
2020 }
2021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
2023 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002024 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002025 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002026 continue;
2027 }
2028
Andres AGd1650662016-12-09 17:26:23 +00002029 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) ||
2030 mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
2031 {
2032 if ( future_past_ca == NULL )
2033 future_past_ca = trust_ca;
2034
2035 continue;
2036 }
2037
2038 break;
2039 }
2040
2041 if( trust_ca != NULL || ( trust_ca = future_past_ca ) != NULL )
2042 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002043 /*
2044 * Top of chain is signed by a trusted CA
2045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardfa67eba2015-06-27 14:41:38 +02002047
2048 if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
2049 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002050 }
2051
2052 /*
2053 * If top of chain is not the same as the trusted CA send a verify request
2054 * to the callback for any issues with validity and CRL presence for the
2055 * trusted CA certificate.
2056 */
2057 if( trust_ca != NULL &&
2058 ( child->subject_raw.len != trust_ca->subject_raw.len ||
2059 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Hanno Becker2e7fee02017-09-25 10:47:58 +01002060 child->subject_raw.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002063 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002064 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02002065#else
2066 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002067#endif
2068
Andres AGd1650662016-12-09 17:26:23 +00002069 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
2070 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
2071
2072 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
2073 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
2074
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002075 if( NULL != f_vrfy )
2076 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002077 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
2078 &ca_flags ) ) != 0 )
2079 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002081 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002082 }
2083 }
2084
2085 /* Call callback on top cert */
2086 if( NULL != f_vrfy )
2087 {
Paul Bakker66d5d072014-06-17 16:39:18 +02002088 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002089 return( ret );
2090 }
2091
2092 *flags |= ca_flags;
2093
2094 return( 0 );
2095}
2096
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002097/*
2098 * Verify a certificate with a parent inside the chain
2099 *
2100 * See comments for mbedtls_x509_crt_verify_with_profile()
2101 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002102static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002103 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
2104 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
2105 const mbedtls_x509_crt_profile *profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002106 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002107 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002108 void *p_vrfy )
2109{
2110 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002111 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2113 mbedtls_x509_crt *grandparent;
2114 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002115
Janos Follath5dd4fe12015-10-12 09:02:20 +02002116 /* Counting intermediate self signed certificates */
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01002117 if( ( path_cnt != 0 ) && x509_name_cmp( &child->issuer, &child->subject ) == 0 )
Janos Follath5dd4fe12015-10-12 09:02:20 +02002118 self_cnt++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002119
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002120 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002122 {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002123 /* return immediately as the goal is to avoid unbounded recursion */
2124 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002125 }
2126
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002127 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002128 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02002129
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002130 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002132
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002133 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2134 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2135
2136 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2137 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 md_info = mbedtls_md_info_from_type( child->sig_md );
Manuel Pégourié-Gonnarde786a7e2018-03-07 09:41:20 +01002140 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002141 {
Manuel Pégourié-Gonnarde786a7e2018-03-07 09:41:20 +01002142 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002144 }
2145 else
2146 {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002147 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2148 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2151 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002152 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002155 }
2156 }
2157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002159 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002160 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002161#endif
2162
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002163 /* Look for a grandparent in trusted CAs */
2164 for( grandparent = trust_ca;
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002165 grandparent != NULL;
2166 grandparent = grandparent->next )
2167 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002168 if( x509_crt_check_parent( parent, grandparent,
2169 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002170 break;
2171 }
2172
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002173 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002174 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002175 ret = x509_crt_verify_top( parent, grandparent, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002176 path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002177 if( ret != 0 )
2178 return( ret );
2179 }
2180 else
2181 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002182 /* Look for a grandparent upwards the chain */
2183 for( grandparent = parent->next;
2184 grandparent != NULL;
2185 grandparent = grandparent->next )
2186 {
Janos Follath5dd4fe12015-10-12 09:02:20 +02002187 /* +2 because the current step is not yet accounted for
2188 * and because max_pathlen is one higher than it should be.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01002189 * Also self signed certificates do not count to the limit. */
Janos Follath5dd4fe12015-10-12 09:02:20 +02002190 if( grandparent->max_pathlen > 0 &&
2191 grandparent->max_pathlen < 2 + path_cnt - self_cnt )
2192 {
2193 continue;
2194 }
2195
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002196 if( x509_crt_check_parent( parent, grandparent,
2197 0, path_cnt == 0 ) == 0 )
2198 break;
2199 }
2200
2201 /* Is our parent part of the chain or at the top? */
2202 if( grandparent != NULL )
2203 {
2204 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002205 profile, path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002206 f_vrfy, p_vrfy );
2207 if( ret != 0 )
2208 return( ret );
2209 }
2210 else
2211 {
2212 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002213 path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002214 f_vrfy, p_vrfy );
2215 if( ret != 0 )
2216 return( ret );
2217 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002218 }
2219
2220 /* child is verified to be a child of the parent, call verify callback */
2221 if( NULL != f_vrfy )
2222 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2223 return( ret );
2224
2225 *flags |= parent_flags;
2226
2227 return( 0 );
2228}
2229
2230/*
2231 * Verify the certificate validity
2232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2234 mbedtls_x509_crt *trust_ca,
2235 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002236 const char *cn, uint32_t *flags,
2237 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002238 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002239{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002240 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002241 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002242}
2243
2244
2245/*
2246 * Verify the certificate validity, with profile
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002247 *
2248 * The chain building/verification is spread accross 4 functions:
2249 * - this one
2250 * - x509_crt_verify_child()
2251 * - x509_crt_verify_top()
2252 * - x509_crt_check_parent()
2253 *
2254 * There are five main cases to consider. Let's introduce some notation:
2255 * - E means the end-entity certificate
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01002256 * - I an intermediate CA
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002257 * - R the trusted root CA this chain anchors to
2258 * - T the list of trusted roots (R and possible some others)
2259 *
2260 * The main cases with the calling sequence of the crt_verify_xxx() are:
2261 * 1. E = R (explicitly trusted EE cert)
2262 * verify(E, T) -> verify_top(E, R)
2263 * 2. E -> R (EE signed by trusted root)
2264 * verify(E, T) -> verify_top(E, R)
2265 * 3. E -> I -> R (EE signed by intermediate signed by trusted root)
2266 * verify(E, T) -> verify_child(E, I, T) -> verify_top(I, R)
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01002267 * (plus variant with multiple intermediates)
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002268 * 4. E -> I (EE signed by intermediate that's not trusted)
2269 * verify(E, T) -> verify_child(E, I, T) -> verify_top(I, T)
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01002270 * (plus variant with multiple intermediates)
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002271 * 5. E (EE not trusted)
2272 * verify(E, T) -> verify_top(E, T)
Manuel Pégourié-Gonnard71df3732018-03-07 09:36:30 +01002273 *
2274 * Note: this notation and case numbering is also used in x509_crt_verify_top()
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002275 */
2276int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2277 mbedtls_x509_crt *trust_ca,
2278 mbedtls_x509_crl *ca_crl,
2279 const mbedtls_x509_crt_profile *profile,
2280 const char *cn, uint32_t *flags,
2281 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2282 void *p_vrfy )
2283{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002284 size_t cn_len;
2285 int ret;
Janos Follath5dd4fe12015-10-12 09:02:20 +02002286 int pathlen = 0, selfsigned = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 mbedtls_x509_crt *parent;
2288 mbedtls_x509_name *name;
2289 mbedtls_x509_sequence *cur = NULL;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002290 mbedtls_pk_type_t pk_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002291
2292 *flags = 0;
2293
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002294 if( profile == NULL )
2295 {
2296 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2297 goto exit;
2298 }
2299
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002300 if( cn != NULL )
2301 {
2302 name = &crt->subject;
2303 cn_len = strlen( cn );
2304
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002305 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002306 {
2307 cur = &crt->subject_alt_names;
2308
2309 while( cur != NULL )
2310 {
2311 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002312 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002313 break;
2314
2315 if( cur->buf.len > 2 &&
2316 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002317 x509_check_wildcard( cn, &cur->buf ) == 0 )
2318 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002319 break;
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002320 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002321
2322 cur = cur->next;
2323 }
2324
2325 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002327 }
2328 else
2329 {
2330 while( name != NULL )
2331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002333 {
2334 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002335 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002336 break;
2337
2338 if( name->val.len > 2 &&
2339 memcmp( name->val.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002340 x509_check_wildcard( cn, &name->val ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002341 break;
2342 }
2343
2344 name = name->next;
2345 }
2346
2347 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002349 }
2350 }
2351
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002352 /* Check the type and size of the key */
2353 pk_type = mbedtls_pk_get_type( &crt->pk );
2354
2355 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
2356 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2357
2358 if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
2359 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2360
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002361 /* Look for a parent in trusted CAs */
2362 for( parent = trust_ca; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002363 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002364 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002365 break;
2366 }
2367
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002368 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002369 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002370 ret = x509_crt_verify_top( crt, parent, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002371 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002372 if( ret != 0 )
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002373 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002374 }
2375 else
2376 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002377 /* Look for a parent upwards the chain */
2378 for( parent = crt->next; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002379 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
2380 break;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002381
2382 /* Are we part of the chain or at the top? */
2383 if( parent != NULL )
2384 {
2385 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002386 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002387 if( ret != 0 )
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002388 goto exit;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002389 }
2390 else
2391 {
2392 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002393 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002394 if( ret != 0 )
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002395 goto exit;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002396 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002397 }
2398
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002399exit:
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02002400 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2401 * the SSL module for authmode optional, but non-zero return from the
2402 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002403 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2404 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2405
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002406 if( ret != 0 )
2407 {
2408 *flags = (uint32_t) -1;
2409 return( ret );
2410 }
2411
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002412 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002414
2415 return( 0 );
2416}
2417
2418/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002419 * Initialize a certificate chain
2420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002422{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002424}
2425
2426/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002427 * Unallocate all certificate data
2428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002430{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 mbedtls_x509_crt *cert_cur = crt;
2432 mbedtls_x509_crt *cert_prv;
2433 mbedtls_x509_name *name_cur;
2434 mbedtls_x509_name *name_prv;
2435 mbedtls_x509_sequence *seq_cur;
2436 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002437
2438 if( crt == NULL )
2439 return;
2440
2441 do
2442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2446 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002447#endif
2448
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002449 name_cur = cert_cur->issuer.next;
2450 while( name_cur != NULL )
2451 {
2452 name_prv = name_cur;
2453 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2455 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002456 }
2457
2458 name_cur = cert_cur->subject.next;
2459 while( name_cur != NULL )
2460 {
2461 name_prv = name_cur;
2462 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2464 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002465 }
2466
2467 seq_cur = cert_cur->ext_key_usage.next;
2468 while( seq_cur != NULL )
2469 {
2470 seq_prv = seq_cur;
2471 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2473 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002474 }
2475
2476 seq_cur = cert_cur->subject_alt_names.next;
2477 while( seq_cur != NULL )
2478 {
2479 seq_prv = seq_cur;
2480 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2482 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002483 }
2484
2485 if( cert_cur->raw.p != NULL )
2486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2488 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002489 }
2490
2491 cert_cur = cert_cur->next;
2492 }
2493 while( cert_cur != NULL );
2494
2495 cert_cur = crt;
2496 do
2497 {
2498 cert_prv = cert_cur;
2499 cert_cur = cert_cur->next;
2500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002502 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002504 }
2505 while( cert_cur != NULL );
2506}
2507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#endif /* MBEDTLS_X509_CRT_PARSE_C */