blob: acda1f22c4b4f8a8bd7617bfe0c1d9d446a05c07 [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 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
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 * **********
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045 */
46/*
47 * The ITU-T X.509 standard defines a certificate format for PKI.
48 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020049 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
50 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
51 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052 *
53 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
54 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
55 */
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020061#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/x509_crt.h"
66#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000067
Rich Evans00ab4702015-02-06 13:43:58 +000068#include <string.h>
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#endif
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#else
Simon Butchere5eb2582018-10-03 15:11:19 +010077#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000078#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020080#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082#endif
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000085#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010086#endif
87
Paul Bakkerfa6a6202013-10-28 18:48:30 +010088#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089#include <windows.h>
90#else
91#include <time.h>
92#endif
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000095#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020096#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097#include <sys/types.h>
98#include <sys/stat.h>
99#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +0000100#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101#endif
102
Paul Bakker34617722014-06-13 17:20:13 +0200103/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +0200105 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
106}
107
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200108/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200109 * Default profile
110 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200111const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
112{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200113#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200114 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200115 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200116#endif
117 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200118 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
120 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
121 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
122 0xFFFFFFF, /* Any PK alg */
123 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200124 2048,
125};
126
127/*
128 * Next-default profile
129 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200130const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
131{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200132 /* Hashes from SHA-256 and above */
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
136 0xFFFFFFF, /* Any PK alg */
137#if defined(MBEDTLS_ECP_C)
138 /* Curves at or above 128-bit security level */
139 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
140 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
142 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
143 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
144 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
145 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
146#else
147 0,
148#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200149 2048,
150};
151
152/*
153 * NSA Suite B Profile
154 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200155const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
156{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200157 /* Only SHA-256 and 384 */
158 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
159 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
160 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200161 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
162 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200163#if defined(MBEDTLS_ECP_C)
164 /* Only NIST P-256 and P-384 */
165 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
166 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
167#else
168 0,
169#endif
170 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200171};
172
173/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200174 * Check md_alg against profile
175 * Return 0 if md_alg acceptable for this profile, -1 otherwise
176 */
177static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
178 mbedtls_md_type_t md_alg )
179{
Philippe Antoine84cc74e2018-05-11 11:06:29 +0200180 if( md_alg == MBEDTLS_MD_NONE )
181 return( -1 );
182
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200183 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
184 return( 0 );
185
186 return( -1 );
187}
188
189/*
190 * Check pk_alg against profile
191 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
192 */
193static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
194 mbedtls_pk_type_t pk_alg )
195{
Philippe Antoine84cc74e2018-05-11 11:06:29 +0200196 if( pk_alg == MBEDTLS_PK_NONE )
197 return( -1 );
198
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200199 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
200 return( 0 );
201
202 return( -1 );
203}
204
205/*
206 * Check key against profile
207 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
208 */
209static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
210 mbedtls_pk_type_t pk_alg,
211 const mbedtls_pk_context *pk )
212{
213#if defined(MBEDTLS_RSA_C)
214 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
215 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200216 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200217 return( 0 );
218
219 return( -1 );
220 }
221#endif
222
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200223#if defined(MBEDTLS_ECP_C)
224 if( pk_alg == MBEDTLS_PK_ECDSA ||
225 pk_alg == MBEDTLS_PK_ECKEY ||
226 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200227 {
228 mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
229
Philippe Antoine84cc74e2018-05-11 11:06:29 +0200230 if( gid == MBEDTLS_ECP_DP_NONE )
231 return( -1 );
232
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200233 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
234 return( 0 );
235
236 return( -1 );
237 }
238#endif
239
240 return( -1 );
241}
242
243/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200244 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
245 */
246static int x509_get_version( unsigned char **p,
247 const unsigned char *end,
248 int *ver )
249{
250 int ret;
251 size_t len;
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
254 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200257 {
258 *ver = 0;
259 return( 0 );
260 }
261
Hanno Becker19557c22019-02-12 11:52:10 +0000262 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200263 }
264
265 end = *p + len;
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
268 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269
270 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 return( MBEDTLS_ERR_X509_INVALID_VERSION +
272 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200273
274 return( 0 );
275}
276
277/*
278 * Validity ::= SEQUENCE {
279 * notBefore Time,
280 * notAfter Time }
281 */
282static int x509_get_dates( unsigned char **p,
283 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_x509_time *from,
285 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286{
287 int ret;
288 size_t len;
289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
291 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
292 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293
294 end = *p + len;
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200297 return( ret );
298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 return( ret );
301
302 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 return( MBEDTLS_ERR_X509_INVALID_DATE +
304 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305
306 return( 0 );
307}
308
309/*
310 * X.509 v2/v3 unique identifier (not parsed)
311 */
312static int x509_get_uid( unsigned char **p,
313 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200315{
316 int ret;
317
318 if( *p == end )
319 return( 0 );
320
321 uid->tag = **p;
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
324 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327 return( 0 );
328
Hanno Becker19557c22019-02-12 11:52:10 +0000329 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200330 }
331
332 uid->p = *p;
333 *p += uid->len;
334
335 return( 0 );
336}
337
338static int x509_get_basic_constraints( unsigned char **p,
339 const unsigned char *end,
340 int *ca_istrue,
341 int *max_pathlen )
342{
343 int ret;
344 size_t len;
345
346 /*
347 * BasicConstraints ::= SEQUENCE {
348 * cA BOOLEAN DEFAULT FALSE,
349 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
350 */
351 *ca_istrue = 0; /* DEFAULT FALSE */
352 *max_pathlen = 0; /* endless */
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
355 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
356 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357
358 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200359 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
364 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365
366 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368
369 if( *ca_istrue != 0 )
370 *ca_istrue = 1;
371 }
372
373 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200374 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
377 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378
379 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
381 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382
Andrzej Kurek3fd92972020-04-14 09:49:52 -0400383 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
384 * overflow, which is an undefined behavior. */
385 if( *max_pathlen == INT_MAX )
386 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
387 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
388
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389 (*max_pathlen)++;
390
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200391 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392}
393
394static int x509_get_ns_cert_type( unsigned char **p,
395 const unsigned char *end,
396 unsigned char *ns_cert_type)
397{
398 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
402 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403
404 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
406 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407
408 /* Get actual bitstring */
409 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200410 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411}
412
413static int x509_get_key_usage( unsigned char **p,
414 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100415 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416{
417 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200418 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
422 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423
424 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
426 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427
428 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200429 *key_usage = 0;
430 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
431 {
432 *key_usage |= (unsigned int) bs.p[i] << (8*i);
433 }
434
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200435 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436}
437
438/*
439 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
440 *
441 * KeyPurposeId ::= OBJECT IDENTIFIER
442 */
443static int x509_get_ext_key_usage( unsigned char **p,
444 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446{
447 int ret;
448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
450 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
452 /* Sequence length must be >= 1 */
453 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
455 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200457 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458}
459
460/*
461 * SubjectAltName ::= GeneralNames
462 *
463 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
464 *
465 * GeneralName ::= CHOICE {
466 * otherName [0] OtherName,
467 * rfc822Name [1] IA5String,
468 * dNSName [2] IA5String,
469 * x400Address [3] ORAddress,
470 * directoryName [4] Name,
471 * ediPartyName [5] EDIPartyName,
472 * uniformResourceIdentifier [6] IA5String,
473 * iPAddress [7] OCTET STRING,
474 * registeredID [8] OBJECT IDENTIFIER }
475 *
476 * OtherName ::= SEQUENCE {
477 * type-id OBJECT IDENTIFIER,
478 * value [0] EXPLICIT ANY DEFINED BY type-id }
479 *
480 * EDIPartyName ::= SEQUENCE {
481 * nameAssigner [0] DirectoryString OPTIONAL,
482 * partyName [1] DirectoryString }
483 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000484 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 */
486static int x509_get_subject_alt_name( unsigned char **p,
487 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489{
490 int ret;
491 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495
496 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
498 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
499 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500
501 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
503 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504
505 while( *p < end )
506 {
507 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
509 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510
511 tag = **p;
512 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
514 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Andres Amaya Garcia64519092017-08-25 17:13:12 +0100516 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
517 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
520 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Andres Amaya Garcia64519092017-08-25 17:13:12 +0100521 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200523 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 {
526 *p += tag_len;
527 continue;
528 }
529
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200531 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100533 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100535
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200536 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
538 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200540 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542 cur = cur->next;
543 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200544
545 buf = &(cur->buf);
546 buf->tag = tag;
547 buf->p = *p;
548 buf->len = tag_len;
549 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550 }
551
552 /* Set final sequence entry's next pointer to NULL */
553 cur->next = NULL;
554
555 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
557 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
559 return( 0 );
560}
561
562/*
563 * X.509 v3 extensions
564 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565 */
566static int x509_get_crt_ext( unsigned char **p,
567 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569{
570 int ret;
571 size_t len;
572 unsigned char *end_ext_data, *end_ext_octet;
573
Hanno Becker1de13db2019-02-12 17:22:36 +0000574 if( *p == end )
575 return( 0 );
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579
Hanno Becker1de13db2019-02-12 17:22:36 +0000580 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581 while( *p < end )
582 {
583 /*
584 * Extension ::= SEQUENCE {
585 * extnID OBJECT IDENTIFIER,
586 * critical BOOLEAN DEFAULT FALSE,
587 * extnValue OCTET STRING }
588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590 int is_critical = 0; /* DEFAULT FALSE */
591 int ext_type = 0;
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
594 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
595 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200596
597 end_ext_data = *p + len;
598
599 /* Get extension ID */
k-stachowiak69789492018-07-16 10:49:12 +0200600 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiakf4a66882018-07-24 12:54:39 +0200601 MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200603
k-stachowiak69789492018-07-16 10:49:12 +0200604 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605 extn_oid.p = *p;
606 *p += extn_oid.len;
607
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
610 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
611 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612
613 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
615 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
616 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617
618 end_ext_octet = *p + len;
619
620 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
622 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623
624 /*
625 * Detect supported extensions
626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628
629 if( ret != 0 )
630 {
631 /* No parser found, skip extension */
632 *p = end_ext_octet;
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 if( is_critical )
636 {
637 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
639 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 }
641#endif
642 continue;
643 }
644
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100645 /* Forbid repeated extensions */
646 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100648
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649 crt->ext_types |= ext_type;
650
651 switch( ext_type )
652 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100653 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654 /* Parse basic constraints */
655 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
656 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200657 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658 break;
659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661 /* Parse key usage */
662 if( ( ret = x509_get_key_usage( p, end_ext_octet,
663 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200664 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665 break;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668 /* Parse extended key usage */
669 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
670 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200671 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672 break;
673
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100674 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675 /* Parse subject alt name */
676 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
677 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200678 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200679 break;
680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682 /* Parse netscape certificate type */
683 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
684 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200685 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686 break;
687
688 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690 }
691 }
692
693 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
695 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696
697 return( 0 );
698}
699
700/*
701 * Parse and fill a single X.509 certificate in DER format
702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200704 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705{
706 int ret;
707 size_t len;
708 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
712 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
713 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714
715 /*
716 * Check for valid input
717 */
718 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720
Janos Follathcc0e49d2016-02-17 14:34:12 +0000721 // Use the original buffer until we figure out actual length
722 p = (unsigned char*) buf;
723 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724 end = p + len;
725
726 /*
727 * Certificate ::= SEQUENCE {
728 * tbsCertificate TBSCertificate,
729 * signatureAlgorithm AlgorithmIdentifier,
730 * signatureValue BIT STRING }
731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
733 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_x509_crt_free( crt );
736 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737 }
738
739 if( len > (size_t) ( end - p ) )
740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_x509_crt_free( crt );
742 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
743 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200744 }
745 crt_end = p + len;
746
Janos Follathcc0e49d2016-02-17 14:34:12 +0000747 // Create and populate a new buffer for the raw field
748 crt->raw.len = crt_end - buf;
749 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
750 if( p == NULL )
751 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
752
753 memcpy( p, buf, crt->raw.len );
754
Hanno Becker2e7fee02017-09-25 10:47:58 +0100755 // Direct pointers to the new buffer
Janos Follathcc0e49d2016-02-17 14:34:12 +0000756 p += crt->raw.len - len;
757 end = crt_end = p + len;
758
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200759 /*
760 * TBSCertificate ::= SEQUENCE {
761 */
762 crt->tbs.p = p;
763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
765 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_x509_crt_free( crt );
768 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 }
770
771 end = p + len;
772 crt->tbs.len = end - crt->tbs.p;
773
774 /*
775 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
776 *
777 * CertificateSerialNumber ::= INTEGER
778 *
779 * signature AlgorithmIdentifier
780 */
781 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
783 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200784 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200787 return( ret );
788 }
789
Andres AG80164742017-03-09 16:16:11 +0000790 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_x509_crt_free( crt );
793 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794 }
795
Andres AG80164742017-03-09 16:16:11 +0000796 crt->version++;
797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200799 &crt->sig_md, &crt->sig_pk,
800 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803 return( ret );
804 }
805
806 /*
807 * issuer Name
808 */
809 crt->issuer_raw.p = p;
810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
812 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 mbedtls_x509_crt_free( crt );
815 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 }
817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821 return( ret );
822 }
823
824 crt->issuer_raw.len = p - crt->issuer_raw.p;
825
826 /*
827 * Validity ::= SEQUENCE {
828 * notBefore Time,
829 * notAfter Time }
830 *
831 */
832 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
833 &crt->valid_to ) ) != 0 )
834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836 return( ret );
837 }
838
839 /*
840 * subject Name
841 */
842 crt->subject_raw.p = p;
843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
845 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 mbedtls_x509_crt_free( crt );
848 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200849 }
850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200854 return( ret );
855 }
856
857 crt->subject_raw.len = p - crt->subject_raw.p;
858
859 /*
860 * SubjectPublicKeyInfo
861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200865 return( ret );
866 }
867
868 /*
869 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
870 * -- If present, version shall be v2 or v3
871 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
872 * -- If present, version shall be v2 or v3
873 * extensions [3] EXPLICIT Extensions OPTIONAL
874 * -- If present, version shall be v3
875 */
876 if( crt->version == 2 || crt->version == 3 )
877 {
878 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
879 if( ret != 0 )
880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882 return( ret );
883 }
884 }
885
886 if( crt->version == 2 || crt->version == 3 )
887 {
888 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
889 if( ret != 0 )
890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 return( ret );
893 }
894 }
895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200898#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100899 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200900 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901 if( ret != 0 )
902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 return( ret );
905 }
906 }
907
908 if( p != end )
909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_x509_crt_free( crt );
911 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
912 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913 }
914
915 end = crt_end;
916
917 /*
918 * }
919 * -- end of TBSCertificate
920 *
921 * signatureAlgorithm AlgorithmIdentifier,
922 * signatureValue BIT STRING
923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927 return( ret );
928 }
929
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100930 if( crt->sig_oid.len != sig_oid2.len ||
931 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200932 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200933 ( sig_params1.len != 0 &&
934 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_x509_crt_free( crt );
937 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938 }
939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943 return( ret );
944 }
945
946 if( p != end )
947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 mbedtls_x509_crt_free( crt );
949 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
950 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 }
952
953 return( 0 );
954}
955
956/*
957 * Parse one X.509 certificate in DER format from a buffer and add them to a
958 * chained list
959 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200961 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962{
963 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965
966 /*
967 * Check for valid input
968 */
969 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971
972 while( crt->version != 0 && crt->next != NULL )
973 {
974 prev = crt;
975 crt = crt->next;
976 }
977
978 /*
979 * Add new certificate on the end of the chain if needed.
980 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200981 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200983 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984
985 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200986 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987
988 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991 }
992
Paul Bakkerddf26b42013-09-18 13:46:23 +0200993 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994 {
995 if( prev )
996 prev->next = NULL;
997
998 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000
1001 return( ret );
1002 }
1003
1004 return( 0 );
1005}
1006
1007/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001008 * Parse one or more PEM certificates from a buffer and add them to the chained
1009 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012{
Janos Follath98e28a72016-05-31 14:03:54 +01001013#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001014 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001016#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017
1018 /*
1019 * Check for valid input
1020 */
1021 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023
1024 /*
1025 * Determine buffer content. Buffer contains either one DER certificate or
1026 * one or more PEM certificates.
1027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001029 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001030 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001033 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1036 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001037#else
1038 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1039#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041#if defined(MBEDTLS_PEM_PARSE_C)
1042 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 {
1044 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001047 /* 1 rather than 0 since the terminating NULL byte is counted in */
1048 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001049 {
1050 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001052
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001053 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055 "-----BEGIN CERTIFICATE-----",
1056 "-----END CERTIFICATE-----",
1057 buf, NULL, 0, &use_len );
1058
1059 if( ret == 0 )
1060 {
1061 /*
1062 * Was PEM encoded
1063 */
1064 buflen -= use_len;
1065 buf += use_len;
1066 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068 {
1069 return( ret );
1070 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074
1075 /*
1076 * PEM header and footer were found
1077 */
1078 buflen -= use_len;
1079 buf += use_len;
1080
1081 if( first_error == 0 )
1082 first_error = ret;
1083
Paul Bakker5a5fa922014-09-26 14:53:04 +02001084 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085 continue;
1086 }
1087 else
1088 break;
1089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093
1094 if( ret != 0 )
1095 {
1096 /*
1097 * Quit parsing on a memory error
1098 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001099 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100 return( ret );
1101
1102 if( first_error == 0 )
1103 first_error = ret;
1104
1105 total_failed++;
1106 continue;
1107 }
1108
1109 success = 1;
1110 }
1111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112
1113 if( success )
1114 return( total_failed );
1115 else if( first_error )
1116 return( first_error );
1117 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001119#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120}
1121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123/*
1124 * Load one or more certificates and add them to the chained list
1125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127{
1128 int ret;
1129 size_t n;
1130 unsigned char *buf;
1131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001133 return( ret );
1134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001136
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001137 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139
1140 return( ret );
1141}
1142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001144{
1145 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001146#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001147 int w_ret;
1148 WCHAR szDir[MAX_PATH];
1149 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001150 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001151 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152
Paul Bakker9af723c2014-05-01 13:03:14 +02001153 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154 HANDLE hFind;
1155
1156 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158
Paul Bakker9af723c2014-05-01 13:03:14 +02001159 memset( szDir, 0, sizeof(szDir) );
1160 memset( filename, 0, MAX_PATH );
1161 memcpy( filename, path, len );
1162 filename[len++] = '\\';
1163 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164 filename[len++] = '*';
1165
Simon B3c6b18d2016-11-03 01:11:37 +00001166 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001167 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001168 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170
1171 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001172 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174
1175 len = MAX_PATH - len;
1176 do
1177 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001178 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001179
1180 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1181 continue;
1182
Paul Bakker9af723c2014-05-01 13:03:14 +02001183 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001184 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001185 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001186 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001187 if( w_ret == 0 )
Ron Eldor3e19df52017-01-09 15:09:16 +02001188 {
1189 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1190 goto cleanup;
1191 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 if( w_ret < 0 )
1195 ret++;
1196 else
1197 ret += w_ret;
1198 }
1199 while( FindNextFileW( hFind, &file_data ) != 0 );
1200
Paul Bakker66d5d072014-06-17 16:39:18 +02001201 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203
Ron Eldor3e19df52017-01-09 15:09:16 +02001204cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001205 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001206#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001207 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001208 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001210 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001211 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001212 DIR *dir = opendir( path );
1213
Paul Bakker66d5d072014-06-17 16:39:18 +02001214 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001216
Ron Eldor8ab05952017-01-09 19:27:59 +02001217#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001218 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001219 {
1220 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001221 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001222 }
Ron Eldor8ab05952017-01-09 19:27:59 +02001223#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001224
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001225 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001226 {
Andres AGf9113192016-09-02 14:06:04 +01001227 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1228 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229
Andres AGf9113192016-09-02 14:06:04 +01001230 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001231 {
Andres AGf9113192016-09-02 14:06:04 +01001232 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1233 goto cleanup;
1234 }
1235 else if( stat( entry_name, &sb ) == -1 )
1236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001238 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001239 }
1240
Janos Follathb40d60f2020-02-04 14:47:45 +00001241 if( !S_ISREG( sb.st_mode ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242 continue;
1243
1244 // Ignore parse errors
1245 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001247 if( t_ret < 0 )
1248 ret++;
1249 else
1250 ret += t_ret;
1251 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001252
1253cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001254 closedir( dir );
1255
Ron Eldor8ab05952017-01-09 19:27:59 +02001256#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001257 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor8ab05952017-01-09 19:27:59 +02001259#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001260
Paul Bakkerbe089b02013-10-14 15:51:50 +02001261#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001262
1263 return( ret );
1264}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001266
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001267static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001269{
1270 size_t i;
1271 size_t n = *size;
1272 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001274 const char *sep = "";
1275 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001276
1277 while( cur != NULL )
1278 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001279 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001280 {
1281 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001282 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001283 }
1284
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001285 n -= cur->buf.len + sep_len;
1286 for( i = 0; i < sep_len; i++ )
1287 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001288 for( i = 0; i < cur->buf.len; i++ )
1289 *p++ = cur->buf.p[i];
1290
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001291 sep = ", ";
1292 sep_len = 2;
1293
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001294 cur = cur->next;
1295 }
1296
1297 *p = '\0';
1298
1299 *size = n;
1300 *buf = p;
1301
1302 return( 0 );
1303}
1304
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001305#define PRINT_ITEM(i) \
1306 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001308 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001309 sep = ", "; \
1310 }
1311
1312#define CERT_TYPE(type,name) \
1313 if( ns_cert_type & type ) \
1314 PRINT_ITEM( name );
1315
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001316static int x509_info_cert_type( char **buf, size_t *size,
1317 unsigned char ns_cert_type )
1318{
1319 int ret;
1320 size_t n = *size;
1321 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001322 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001325 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1327 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1328 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001329 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1330 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1331 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001332
1333 *size = n;
1334 *buf = p;
1335
1336 return( 0 );
1337}
1338
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001339#define KEY_USAGE(code,name) \
1340 if( key_usage & code ) \
1341 PRINT_ITEM( name );
1342
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001343static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001344 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001345{
1346 int ret;
1347 size_t n = *size;
1348 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001349 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1352 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001353 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1354 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1355 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1357 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001358 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1359 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001360
1361 *size = n;
1362 *buf = p;
1363
1364 return( 0 );
1365}
1366
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001367static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001369{
1370 int ret;
1371 const char *desc;
1372 size_t n = *size;
1373 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001375 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001376
1377 while( cur != NULL )
1378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001380 desc = "???";
1381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001383 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001384
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001385 sep = ", ";
1386
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001387 cur = cur->next;
1388 }
1389
1390 *size = n;
1391 *buf = p;
1392
1393 return( 0 );
1394}
1395
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001396/*
Hanno Becker52da7ee2018-11-06 13:18:23 +00001397 * Like memcmp, but case-insensitive and always returns -1 if different
1398 */
1399static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
1400{
1401 size_t i;
1402 unsigned char diff;
1403 const unsigned char *n1 = s1, *n2 = s2;
1404
1405 for( i = 0; i < len; i++ )
1406 {
1407 diff = n1[i] ^ n2[i];
1408
1409 if( diff == 0 )
1410 continue;
1411
1412 if( diff == 32 &&
1413 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1414 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1415 {
1416 continue;
1417 }
1418
1419 return( -1 );
1420 }
1421
1422 return( 0 );
1423}
1424
1425/*
1426 * Return 0 if name matches wildcard, -1 otherwise
1427 */
1428static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
1429{
1430 size_t i;
1431 size_t cn_idx = 0, cn_len = strlen( cn );
1432
1433 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1434 return( 0 );
1435
1436 for( i = 0; i < cn_len; ++i )
1437 {
1438 if( cn[i] == '.' )
1439 {
1440 cn_idx = i;
1441 break;
1442 }
1443 }
1444
1445 if( cn_idx == 0 )
1446 return( -1 );
1447
1448 if( cn_len - cn_idx == name->len - 1 &&
1449 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1450 {
1451 return( 0 );
1452 }
1453
1454 return( -1 );
1455}
1456
1457/*
1458 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1459 * variations (but not all).
1460 *
1461 * Return 0 if equal, -1 otherwise.
1462 */
1463static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
1464{
1465 if( a->tag == b->tag &&
1466 a->len == b->len &&
1467 memcmp( a->p, b->p, b->len ) == 0 )
1468 {
1469 return( 0 );
1470 }
1471
1472 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1473 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1474 a->len == b->len &&
1475 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1476 {
1477 return( 0 );
1478 }
1479
1480 return( -1 );
1481}
1482
1483/*
1484 * Compare two X.509 Names (aka rdnSequence).
1485 *
1486 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1487 * we sometimes return unequal when the full algorithm would return equal,
1488 * but never the other way. (In particular, we don't do Unicode normalisation
1489 * or space folding.)
1490 *
1491 * Return 0 if equal, -1 otherwise.
1492 */
1493static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
1494{
1495 /* Avoid recursion, it might not be optimised by the compiler */
1496 while( a != NULL || b != NULL )
1497 {
1498 if( a == NULL || b == NULL )
1499 return( -1 );
1500
1501 /* type */
1502 if( a->oid.tag != b->oid.tag ||
1503 a->oid.len != b->oid.len ||
1504 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1505 {
1506 return( -1 );
1507 }
1508
1509 /* value */
1510 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1511 return( -1 );
1512
1513 /* structure of the list of sets */
1514 if( a->next_merged != b->next_merged )
1515 return( -1 );
1516
1517 a = a->next;
1518 b = b->next;
1519 }
1520
1521 /* a == NULL == b */
1522 return( 0 );
1523}
1524
1525/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526 * Return an informational string about the certificate.
1527 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001528#define BEFORE_COLON 18
1529#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1531 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532{
1533 int ret;
1534 size_t n;
1535 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536 char key_size_str[BEFORE_COLON];
1537
1538 p = buf;
1539 n = size;
1540
Janos Follath98e28a72016-05-31 14:03:54 +01001541 if( NULL == crt )
1542 {
1543 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
1544 MBEDTLS_X509_SAFE_SNPRINTF;
1545
1546 return( (int) ( size - n ) );
1547 }
1548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001550 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001551 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001554 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001557 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001560 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001562 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001565 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001567 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1571 crt->valid_from.year, crt->valid_from.mon,
1572 crt->valid_from.day, crt->valid_from.hour,
1573 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001574 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1578 crt->valid_to.year, crt->valid_to.mon,
1579 crt->valid_to.day, crt->valid_to.hour,
1580 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001581 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001584 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001587 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001588 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001590 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1592 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593 {
1594 return( ret );
1595 }
1596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001598 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001599 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001601 /*
1602 * Optional extensions
1603 */
1604
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001605 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001608 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001609 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001610
1611 if( crt->max_pathlen > 0 )
1612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001614 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001615 }
1616 }
1617
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001618 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001621 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001622
1623 if( ( ret = x509_info_subject_alt_name( &p, &n,
1624 &crt->subject_alt_names ) ) != 0 )
1625 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001626 }
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001631 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001632
1633 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1634 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001635 }
1636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001640 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001641
1642 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1643 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001644 }
1645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001649 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001650
1651 if( ( ret = x509_info_ext_key_usage( &p, &n,
1652 &crt->ext_key_usage ) ) != 0 )
1653 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001654 }
1655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001657 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001658
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659 return( (int) ( size - n ) );
1660}
1661
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001662struct x509_crt_verify_string {
1663 int code;
1664 const char *string;
1665};
1666
1667static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001668 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001669 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1670 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1671 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1672 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1673 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001674 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1675 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1676 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001677 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001678 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1679 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1680 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1681 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001682 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1683 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1684 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1685 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1686 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1687 { 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 +01001688 { 0, NULL }
1689};
1690
1691int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001692 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001693{
1694 int ret;
1695 const struct x509_crt_verify_string *cur;
1696 char *p = buf;
1697 size_t n = size;
1698
1699 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1700 {
1701 if( ( flags & cur->code ) == 0 )
1702 continue;
1703
1704 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001705 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001706 flags ^= cur->code;
1707 }
1708
1709 if( flags != 0 )
1710 {
1711 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1712 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001713 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001714 }
1715
1716 return( (int) ( size - n ) );
1717}
1718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001720int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1721 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001722{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001723 unsigned int usage_must, usage_may;
1724 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1725 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1726
1727 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1728 return( 0 );
1729
1730 usage_must = usage & ~may_mask;
1731
1732 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1733 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1734
1735 usage_may = usage & may_mask;
1736
1737 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001739
1740 return( 0 );
1741}
1742#endif
1743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1745int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001746 const char *usage_oid,
1747 size_t usage_len )
1748{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001750
1751 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001753 return( 0 );
1754
1755 /*
1756 * Look for the requested usage (or wildcard ANY) in our list
1757 */
1758 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001761
1762 if( cur_oid->len == usage_len &&
1763 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1764 {
1765 return( 0 );
1766 }
1767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001769 return( 0 );
1770 }
1771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001773}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001777/*
1778 * Return 1 if the certificate is revoked, or 0 otherwise.
1779 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001780int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001781{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001783
1784 while( cur != NULL && cur->serial.len != 0 )
1785 {
1786 if( crt->serial.len == cur->serial.len &&
1787 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1788 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001789 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001790 return( 1 );
1791 }
1792
1793 cur = cur->next;
1794 }
1795
1796 return( 0 );
1797}
1798
1799/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01001800 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard75d35602018-03-05 13:22:59 +01001801 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001804 mbedtls_x509_crl *crl_list,
1805 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001806{
1807 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1809 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001810
1811 if( ca == NULL )
1812 return( flags );
1813
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001814 while( crl_list != NULL )
1815 {
1816 if( crl_list->version == 0 ||
Hanno Becker18a4cbf2018-11-02 09:19:54 +00001817 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001818 {
1819 crl_list = crl_list->next;
1820 continue;
1821 }
1822
1823 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001824 * Check if the CA is configured to sign CRLs
1825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker18a4cbf2018-11-02 09:19:54 +00001827 if( mbedtls_x509_crt_check_key_usage( ca,
1828 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001831 break;
1832 }
1833#endif
1834
1835 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001836 * Check if CRL is correctly signed by the trusted CA
1837 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001838 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1839 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1840
1841 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1842 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnardb9c72eb2017-06-26 12:22:17 +02001845 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001846 {
Manuel Pégourié-Gonnardb9c72eb2017-06-26 12:22:17 +02001847 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001849 break;
1850 }
1851
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001852 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1853 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1856 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001857 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001860 break;
1861 }
1862
1863 /*
1864 * Check for validity of CRL (Do not drop out)
1865 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001866 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001868
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001869 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001870 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001871
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001872 /*
1873 * Check if certificate is revoked
1874 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001875 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001878 break;
1879 }
1880
1881 crl_list = crl_list->next;
1882 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001883
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001884 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001885}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001887
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001888/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001889 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1890 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001891 *
1892 * top means parent is a locally-trusted certificate
1893 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001894 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1896 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001897 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001898{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001899 int need_ca_bit;
1900
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001901 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001902 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001903 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001904
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001905 /* Parent must have the basicConstraints CA bit set as a general rule */
1906 need_ca_bit = 1;
1907
1908 /* Exception: v1/v2 certificates that are locally trusted. */
1909 if( top && parent->version < 3 )
1910 need_ca_bit = 0;
1911
1912 /* Exception: self-signed end-entity certs that are locally trusted. */
1913 if( top && bottom &&
1914 child->raw.len == parent->raw.len &&
1915 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1916 {
1917 need_ca_bit = 0;
1918 }
1919
1920 if( need_ca_bit && ! parent->ca_istrue )
1921 return( -1 );
1922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001924 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001926 {
1927 return( -1 );
1928 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001929#endif
1930
1931 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001932}
1933
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001934/*
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01001935 * Verify a certificate with no parent inside the chain
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001936 * (either the parent is a trusted root, or there is no parent)
1937 *
1938 * See comments for mbedtls_x509_crt_verify_with_profile()
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01001939 * (also for notation used below)
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001940 *
1941 * This function is called in two cases:
1942 * - child was found to have a parent in trusted roots, in which case we're
1943 * called with trust_ca pointing directly to that parent (not the full list)
Manuel Pégourié-Gonnard71df3732018-03-07 09:36:30 +01001944 * - this is cases 1, 2 and 3 of the comment on verify_with_profile()
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001945 * - case 1 is special as child and trust_ca point to copies of the same
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01001946 * certificate then
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001947 * - child was found to have no parent either in the chain or in trusted CAs
Manuel Pégourié-Gonnard71df3732018-03-07 09:36:30 +01001948 * - this is cases 4 and 5 of the comment on verify_with_profile()
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001949 *
1950 * For historical reasons, the function currently does not assume that
1951 * trust_ca points directly to the right root in the first case, and it
1952 * doesn't know in which case it starts, so it always starts by searching for
1953 * a parent in trust_ca.
1954 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001955static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001957 mbedtls_x509_crl *ca_crl,
1958 const mbedtls_x509_crt_profile *profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02001959 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001960 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001961 void *p_vrfy )
1962{
1963 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001964 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001965 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1967 const mbedtls_md_info_t *md_info;
Andres AGd1650662016-12-09 17:26:23 +00001968 mbedtls_x509_crt *future_past_ca = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001969
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001970 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001971 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001972
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001973 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001975
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001976 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
1977 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
1978
1979 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
1980 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
1981
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001982 /*
1983 * Child is the top of the chain. Check against the trust_ca list.
1984 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 md_info = mbedtls_md_info_from_type( child->sig_md );
Manuel Pégourié-Gonnardb9c72eb2017-06-26 12:22:17 +02001988 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001989 {
Manuel Pégourié-Gonnardb9c72eb2017-06-26 12:22:17 +02001990 /* Note: this can't happen except after an internal error */
1991 /* Cannot check signature, no need to try any CA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001992 trust_ca = NULL;
1993 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001994
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001995 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001996 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001997 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001998 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001999
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01002000 check_path_cnt = path_cnt + 1;
2001
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002002 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01002003 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002004 * the same as the trusted CA
2005 */
2006 if( child->subject_raw.len == trust_ca->subject_raw.len &&
2007 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Hanno Becker2e7fee02017-09-25 10:47:58 +01002008 child->subject_raw.len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002009 {
2010 check_path_cnt--;
2011 }
2012
Janos Follath5dd4fe12015-10-12 09:02:20 +02002013 /* Self signed certificates do not count towards the limit */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002014 if( trust_ca->max_pathlen > 0 &&
Janos Follath5dd4fe12015-10-12 09:02:20 +02002015 trust_ca->max_pathlen < check_path_cnt - self_cnt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002016 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002017 continue;
2018 }
2019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
2021 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002022 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002023 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002024 continue;
2025 }
2026
Andres AGd1650662016-12-09 17:26:23 +00002027 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) ||
2028 mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
2029 {
2030 if ( future_past_ca == NULL )
2031 future_past_ca = trust_ca;
2032
2033 continue;
2034 }
2035
2036 break;
2037 }
2038
2039 if( trust_ca != NULL || ( trust_ca = future_past_ca ) != NULL )
2040 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002041 /*
2042 * Top of chain is signed by a trusted CA
2043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardfa67eba2015-06-27 14:41:38 +02002045
2046 if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
2047 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002048 }
2049
2050 /*
2051 * If top of chain is not the same as the trusted CA send a verify request
2052 * to the callback for any issues with validity and CRL presence for the
2053 * trusted CA certificate.
2054 */
2055 if( trust_ca != NULL &&
2056 ( child->subject_raw.len != trust_ca->subject_raw.len ||
2057 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Hanno Becker2e7fee02017-09-25 10:47:58 +01002058 child->subject_raw.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002061 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002062 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02002063#else
2064 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002065#endif
2066
Andres AGd1650662016-12-09 17:26:23 +00002067 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
2068 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
2069
2070 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
2071 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
2072
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002073 if( NULL != f_vrfy )
2074 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002075 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
2076 &ca_flags ) ) != 0 )
2077 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002078 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002079 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080 }
2081 }
2082
2083 /* Call callback on top cert */
2084 if( NULL != f_vrfy )
2085 {
Paul Bakker66d5d072014-06-17 16:39:18 +02002086 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002087 return( ret );
2088 }
2089
2090 *flags |= ca_flags;
2091
2092 return( 0 );
2093}
2094
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002095/*
2096 * Verify a certificate with a parent inside the chain
2097 *
2098 * See comments for mbedtls_x509_crt_verify_with_profile()
2099 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002100static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002101 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
2102 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
2103 const mbedtls_x509_crt_profile *profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002104 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002105 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002106 void *p_vrfy )
2107{
2108 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002109 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2111 mbedtls_x509_crt *grandparent;
2112 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002113
Janos Follath5dd4fe12015-10-12 09:02:20 +02002114 /* Counting intermediate self signed certificates */
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01002115 if( ( path_cnt != 0 ) && x509_name_cmp( &child->issuer, &child->subject ) == 0 )
Janos Follath5dd4fe12015-10-12 09:02:20 +02002116 self_cnt++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002117
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002118 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002120 {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002121 /* return immediately as the goal is to avoid unbounded recursion */
2122 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002123 }
2124
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002125 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002126 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02002127
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002128 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002130
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002131 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2132 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2133
2134 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2135 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 md_info = mbedtls_md_info_from_type( child->sig_md );
Manuel Pégourié-Gonnarde786a7e2018-03-07 09:41:20 +01002138 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002139 {
Manuel Pégourié-Gonnarde786a7e2018-03-07 09:41:20 +01002140 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002142 }
2143 else
2144 {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002145 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2146 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2149 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002150 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002153 }
2154 }
2155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002157 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002158 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002159#endif
2160
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002161 /* Look for a grandparent in trusted CAs */
2162 for( grandparent = trust_ca;
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002163 grandparent != NULL;
2164 grandparent = grandparent->next )
2165 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002166 if( x509_crt_check_parent( parent, grandparent,
2167 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002168 break;
2169 }
2170
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002171 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002172 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002173 ret = x509_crt_verify_top( parent, grandparent, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002174 path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002175 if( ret != 0 )
2176 return( ret );
2177 }
2178 else
2179 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002180 /* Look for a grandparent upwards the chain */
2181 for( grandparent = parent->next;
2182 grandparent != NULL;
2183 grandparent = grandparent->next )
2184 {
Janos Follath5dd4fe12015-10-12 09:02:20 +02002185 /* +2 because the current step is not yet accounted for
2186 * and because max_pathlen is one higher than it should be.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01002187 * Also self signed certificates do not count to the limit. */
Janos Follath5dd4fe12015-10-12 09:02:20 +02002188 if( grandparent->max_pathlen > 0 &&
2189 grandparent->max_pathlen < 2 + path_cnt - self_cnt )
2190 {
2191 continue;
2192 }
2193
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002194 if( x509_crt_check_parent( parent, grandparent,
2195 0, path_cnt == 0 ) == 0 )
2196 break;
2197 }
2198
2199 /* Is our parent part of the chain or at the top? */
2200 if( grandparent != NULL )
2201 {
2202 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002203 profile, path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002204 f_vrfy, p_vrfy );
2205 if( ret != 0 )
2206 return( ret );
2207 }
2208 else
2209 {
2210 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002211 path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002212 f_vrfy, p_vrfy );
2213 if( ret != 0 )
2214 return( ret );
2215 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002216 }
2217
2218 /* child is verified to be a child of the parent, call verify callback */
2219 if( NULL != f_vrfy )
2220 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2221 return( ret );
2222
2223 *flags |= parent_flags;
2224
2225 return( 0 );
2226}
2227
2228/*
2229 * Verify the certificate validity
2230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2232 mbedtls_x509_crt *trust_ca,
2233 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002234 const char *cn, uint32_t *flags,
2235 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002236 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002237{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002238 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002239 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002240}
2241
2242
2243/*
2244 * Verify the certificate validity, with profile
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002245 *
2246 * The chain building/verification is spread accross 4 functions:
2247 * - this one
2248 * - x509_crt_verify_child()
2249 * - x509_crt_verify_top()
2250 * - x509_crt_check_parent()
2251 *
2252 * There are five main cases to consider. Let's introduce some notation:
2253 * - E means the end-entity certificate
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01002254 * - I an intermediate CA
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002255 * - R the trusted root CA this chain anchors to
2256 * - T the list of trusted roots (R and possible some others)
2257 *
2258 * The main cases with the calling sequence of the crt_verify_xxx() are:
2259 * 1. E = R (explicitly trusted EE cert)
2260 * verify(E, T) -> verify_top(E, R)
2261 * 2. E -> R (EE signed by trusted root)
2262 * verify(E, T) -> verify_top(E, R)
2263 * 3. E -> I -> R (EE signed by intermediate signed by trusted root)
2264 * verify(E, T) -> verify_child(E, I, T) -> verify_top(I, R)
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01002265 * (plus variant with multiple intermediates)
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002266 * 4. E -> I (EE signed by intermediate that's not trusted)
2267 * verify(E, T) -> verify_child(E, I, T) -> verify_top(I, T)
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01002268 * (plus variant with multiple intermediates)
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002269 * 5. E (EE not trusted)
2270 * verify(E, T) -> verify_top(E, T)
Manuel Pégourié-Gonnard71df3732018-03-07 09:36:30 +01002271 *
2272 * Note: this notation and case numbering is also used in x509_crt_verify_top()
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002273 */
2274int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2275 mbedtls_x509_crt *trust_ca,
2276 mbedtls_x509_crl *ca_crl,
2277 const mbedtls_x509_crt_profile *profile,
2278 const char *cn, uint32_t *flags,
2279 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2280 void *p_vrfy )
2281{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002282 size_t cn_len;
2283 int ret;
Janos Follath5dd4fe12015-10-12 09:02:20 +02002284 int pathlen = 0, selfsigned = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 mbedtls_x509_crt *parent;
2286 mbedtls_x509_name *name;
2287 mbedtls_x509_sequence *cur = NULL;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002288 mbedtls_pk_type_t pk_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002289
2290 *flags = 0;
2291
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002292 if( profile == NULL )
2293 {
2294 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2295 goto exit;
2296 }
2297
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002298 if( cn != NULL )
2299 {
2300 name = &crt->subject;
2301 cn_len = strlen( cn );
2302
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002303 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002304 {
2305 cur = &crt->subject_alt_names;
2306
2307 while( cur != NULL )
2308 {
2309 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002310 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002311 break;
2312
2313 if( cur->buf.len > 2 &&
2314 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002315 x509_check_wildcard( cn, &cur->buf ) == 0 )
2316 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002317 break;
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002318 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002319
2320 cur = cur->next;
2321 }
2322
2323 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002325 }
2326 else
2327 {
2328 while( name != NULL )
2329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002331 {
2332 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002333 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002334 break;
2335
2336 if( name->val.len > 2 &&
2337 memcmp( name->val.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002338 x509_check_wildcard( cn, &name->val ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002339 break;
2340 }
2341
2342 name = name->next;
2343 }
2344
2345 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002347 }
2348 }
2349
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002350 /* Check the type and size of the key */
2351 pk_type = mbedtls_pk_get_type( &crt->pk );
2352
2353 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
2354 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2355
2356 if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
2357 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2358
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002359 /* Look for a parent in trusted CAs */
2360 for( parent = trust_ca; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002361 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002362 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002363 break;
2364 }
2365
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002366 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002367 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002368 ret = x509_crt_verify_top( crt, parent, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002369 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002370 if( ret != 0 )
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002371 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002372 }
2373 else
2374 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002375 /* Look for a parent upwards the chain */
2376 for( parent = crt->next; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002377 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
2378 break;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002379
2380 /* Are we part of the chain or at the top? */
2381 if( parent != NULL )
2382 {
2383 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002384 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002385 if( ret != 0 )
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002386 goto exit;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002387 }
2388 else
2389 {
2390 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002391 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002392 if( ret != 0 )
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002393 goto exit;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002394 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002395 }
2396
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002397exit:
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02002398 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2399 * the SSL module for authmode optional, but non-zero return from the
2400 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002401 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2402 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2403
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002404 if( ret != 0 )
2405 {
2406 *flags = (uint32_t) -1;
2407 return( ret );
2408 }
2409
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002410 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002412
2413 return( 0 );
2414}
2415
2416/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002417 * Initialize a certificate chain
2418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002420{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002422}
2423
2424/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002425 * Unallocate all certificate data
2426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002428{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 mbedtls_x509_crt *cert_cur = crt;
2430 mbedtls_x509_crt *cert_prv;
2431 mbedtls_x509_name *name_cur;
2432 mbedtls_x509_name *name_prv;
2433 mbedtls_x509_sequence *seq_cur;
2434 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002435
2436 if( crt == NULL )
2437 return;
2438
2439 do
2440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2444 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002445#endif
2446
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002447 name_cur = cert_cur->issuer.next;
2448 while( name_cur != NULL )
2449 {
2450 name_prv = name_cur;
2451 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2453 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002454 }
2455
2456 name_cur = cert_cur->subject.next;
2457 while( name_cur != NULL )
2458 {
2459 name_prv = name_cur;
2460 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2462 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002463 }
2464
2465 seq_cur = cert_cur->ext_key_usage.next;
2466 while( seq_cur != NULL )
2467 {
2468 seq_prv = seq_cur;
2469 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2471 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002472 }
2473
2474 seq_cur = cert_cur->subject_alt_names.next;
2475 while( seq_cur != NULL )
2476 {
2477 seq_prv = seq_cur;
2478 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2480 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002481 }
2482
2483 if( cert_cur->raw.p != NULL )
2484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2486 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002487 }
2488
2489 cert_cur = cert_cur->next;
2490 }
2491 while( cert_cur != NULL );
2492
2493 cert_cur = crt;
2494 do
2495 {
2496 cert_prv = cert_cur;
2497 cert_cur = cert_cur->next;
2498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002500 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002502 }
2503 while( cert_cur != NULL );
2504}
2505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506#endif /* MBEDTLS_X509_CRT_PARSE_C */