blob: 6751da0d209ca29884634cd2e65ba38c0afd70d1 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 */
21/*
22 * The ITU-T X.509 standard defines a certificate format for PKI.
23 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020024 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
30 */
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/x509_crt.h"
41#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000042
43#include <stdio.h>
44#include <string.h>
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020048#endif
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052#else
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020055#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010061#endif
62
Paul Bakkerfa6a6202013-10-28 18:48:30 +010063#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064#include <windows.h>
65#else
66#include <time.h>
67#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000070#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020071#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#include <sys/types.h>
73#include <sys/stat.h>
74#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000075#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#endif
77
Paul Bakker34617722014-06-13 17:20:13 +020078/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020080 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
81}
82
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020084 * Default profile
85 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020086const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
87{
Gilles Peskine5d2511c2017-05-12 13:16:40 +020088#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +020089 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020090 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +020091#endif
92 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020093 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
94 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
95 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
96 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
97 0xFFFFFFF, /* Any PK alg */
98 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020099 2048,
100};
101
102/*
103 * Next-default profile
104 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200105const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
106{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200107 /* Hashes from SHA-256 and above */
108 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
110 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
111 0xFFFFFFF, /* Any PK alg */
112#if defined(MBEDTLS_ECP_C)
113 /* Curves at or above 128-bit security level */
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
120 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
121#else
122 0,
123#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200124 2048,
125};
126
127/*
128 * NSA Suite B Profile
129 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200130const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
131{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200132 /* Only SHA-256 and 384 */
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
135 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200136 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200138#if defined(MBEDTLS_ECP_C)
139 /* Only NIST P-256 and P-384 */
140 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
142#else
143 0,
144#endif
145 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200146};
147
148/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200149 * Check md_alg against profile
150 * Return 0 if md_alg acceptable for this profile, -1 otherwise
151 */
152static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
153 mbedtls_md_type_t md_alg )
154{
155 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
156 return( 0 );
157
158 return( -1 );
159}
160
161/*
162 * Check pk_alg against profile
163 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
164 */
165static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
166 mbedtls_pk_type_t pk_alg )
167{
168 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
169 return( 0 );
170
171 return( -1 );
172}
173
174/*
175 * Check key against profile
176 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
177 */
178static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
179 mbedtls_pk_type_t pk_alg,
180 const mbedtls_pk_context *pk )
181{
182#if defined(MBEDTLS_RSA_C)
183 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
184 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200185 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200186 return( 0 );
187
188 return( -1 );
189 }
190#endif
191
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200192#if defined(MBEDTLS_ECP_C)
193 if( pk_alg == MBEDTLS_PK_ECDSA ||
194 pk_alg == MBEDTLS_PK_ECKEY ||
195 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200196 {
197 mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
198
199 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
200 return( 0 );
201
202 return( -1 );
203 }
204#endif
205
206 return( -1 );
207}
208
209/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200210 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
211 */
212static int x509_get_version( unsigned char **p,
213 const unsigned char *end,
214 int *ver )
215{
216 int ret;
217 size_t len;
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
220 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200223 {
224 *ver = 0;
225 return( 0 );
226 }
227
228 return( ret );
229 }
230
231 end = *p + len;
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
234 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200235
236 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 return( MBEDTLS_ERR_X509_INVALID_VERSION +
238 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200239
240 return( 0 );
241}
242
243/*
244 * Validity ::= SEQUENCE {
245 * notBefore Time,
246 * notAfter Time }
247 */
248static int x509_get_dates( unsigned char **p,
249 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_x509_time *from,
251 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200252{
253 int ret;
254 size_t len;
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
257 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
258 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200259
260 end = *p + len;
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200263 return( ret );
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200266 return( ret );
267
268 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 return( MBEDTLS_ERR_X509_INVALID_DATE +
270 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271
272 return( 0 );
273}
274
275/*
276 * X.509 v2/v3 unique identifier (not parsed)
277 */
278static int x509_get_uid( unsigned char **p,
279 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281{
282 int ret;
283
284 if( *p == end )
285 return( 0 );
286
287 uid->tag = **p;
288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
290 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293 return( 0 );
294
295 return( ret );
296 }
297
298 uid->p = *p;
299 *p += uid->len;
300
301 return( 0 );
302}
303
304static int x509_get_basic_constraints( unsigned char **p,
305 const unsigned char *end,
306 int *ca_istrue,
307 int *max_pathlen )
308{
309 int ret;
310 size_t len;
311
312 /*
313 * BasicConstraints ::= SEQUENCE {
314 * cA BOOLEAN DEFAULT FALSE,
315 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
316 */
317 *ca_istrue = 0; /* DEFAULT FALSE */
318 *max_pathlen = 0; /* endless */
319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
321 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
322 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323
324 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200325 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
330 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200331
332 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200334
335 if( *ca_istrue != 0 )
336 *ca_istrue = 1;
337 }
338
339 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200340 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
343 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344
345 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
347 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348
349 (*max_pathlen)++;
350
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200351 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352}
353
354static int x509_get_ns_cert_type( unsigned char **p,
355 const unsigned char *end,
356 unsigned char *ns_cert_type)
357{
358 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
362 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363
364 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
366 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367
368 /* Get actual bitstring */
369 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200370 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200371}
372
373static int x509_get_key_usage( unsigned char **p,
374 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100375 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376{
377 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200378 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
382 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383
384 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
386 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387
388 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200389 *key_usage = 0;
390 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
391 {
392 *key_usage |= (unsigned int) bs.p[i] << (8*i);
393 }
394
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200395 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396}
397
398/*
399 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
400 *
401 * KeyPurposeId ::= OBJECT IDENTIFIER
402 */
403static int x509_get_ext_key_usage( unsigned char **p,
404 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406{
407 int ret;
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
410 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411
412 /* Sequence length must be >= 1 */
413 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
415 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200417 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418}
419
420/*
421 * SubjectAltName ::= GeneralNames
422 *
423 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
424 *
425 * GeneralName ::= CHOICE {
426 * otherName [0] OtherName,
427 * rfc822Name [1] IA5String,
428 * dNSName [2] IA5String,
429 * x400Address [3] ORAddress,
430 * directoryName [4] Name,
431 * ediPartyName [5] EDIPartyName,
432 * uniformResourceIdentifier [6] IA5String,
433 * iPAddress [7] OCTET STRING,
434 * registeredID [8] OBJECT IDENTIFIER }
435 *
436 * OtherName ::= SEQUENCE {
437 * type-id OBJECT IDENTIFIER,
438 * value [0] EXPLICIT ANY DEFINED BY type-id }
439 *
440 * EDIPartyName ::= SEQUENCE {
441 * nameAssigner [0] DirectoryString OPTIONAL,
442 * partyName [1] DirectoryString }
443 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000444 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445 */
446static int x509_get_subject_alt_name( unsigned char **p,
447 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449{
450 int ret;
451 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455
456 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
458 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
459 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
461 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
463 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
465 while( *p < end )
466 {
467 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
469 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470
471 tag = **p;
472 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
474 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475
Andres Amaya Garcia64519092017-08-25 17:13:12 +0100476 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
477 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
480 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Andres Amaya Garcia64519092017-08-25 17:13:12 +0100481 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200483 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 {
486 *p += tag_len;
487 continue;
488 }
489
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200491 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100493 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100495
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200496 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497
498 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200500 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502 cur = cur->next;
503 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200504
505 buf = &(cur->buf);
506 buf->tag = tag;
507 buf->p = *p;
508 buf->len = tag_len;
509 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510 }
511
512 /* Set final sequence entry's next pointer to NULL */
513 cur->next = NULL;
514
515 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
517 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
519 return( 0 );
520}
521
522/*
523 * X.509 v3 extensions
524 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 */
526static int x509_get_crt_ext( unsigned char **p,
527 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529{
530 int ret;
531 size_t len;
532 unsigned char *end_ext_data, *end_ext_octet;
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537 return( 0 );
538
539 return( ret );
540 }
541
542 while( *p < end )
543 {
544 /*
545 * Extension ::= SEQUENCE {
546 * extnID OBJECT IDENTIFIER,
547 * critical BOOLEAN DEFAULT FALSE,
548 * extnValue OCTET STRING }
549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551 int is_critical = 0; /* DEFAULT FALSE */
552 int ext_type = 0;
553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
555 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
556 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557
558 end_ext_data = *p + len;
559
560 /* Get extension ID */
561 extn_oid.tag = **p;
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
564 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565
566 extn_oid.p = *p;
567 *p += extn_oid.len;
568
569 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
571 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572
573 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
575 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
576 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577
578 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
580 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
581 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582
583 end_ext_octet = *p + len;
584
585 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
587 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588
589 /*
590 * Detect supported extensions
591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593
594 if( ret != 0 )
595 {
596 /* No parser found, skip extension */
597 *p = end_ext_octet;
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200600 if( is_critical )
601 {
602 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
604 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605 }
606#endif
607 continue;
608 }
609
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100610 /* Forbid repeated extensions */
611 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100613
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614 crt->ext_types |= ext_type;
615
616 switch( ext_type )
617 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100618 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619 /* Parse basic constraints */
620 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
621 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200622 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623 break;
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 /* Parse key usage */
627 if( ( ret = x509_get_key_usage( p, end_ext_octet,
628 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200629 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 break;
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633 /* Parse extended key usage */
634 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
635 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200636 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637 break;
638
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100639 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 /* Parse subject alt name */
641 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
642 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200643 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644 break;
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647 /* Parse netscape certificate type */
648 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
649 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200650 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651 break;
652
653 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 }
656 }
657
658 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
660 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661
662 return( 0 );
663}
664
665/*
666 * Parse and fill a single X.509 certificate in DER format
667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200669 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670{
671 int ret;
672 size_t len;
673 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
677 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
678 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200679
680 /*
681 * Check for valid input
682 */
683 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685
Janos Follathcc0e49d2016-02-17 14:34:12 +0000686 // Use the original buffer until we figure out actual length
687 p = (unsigned char*) buf;
688 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689 end = p + len;
690
691 /*
692 * Certificate ::= SEQUENCE {
693 * tbsCertificate TBSCertificate,
694 * signatureAlgorithm AlgorithmIdentifier,
695 * signatureValue BIT STRING }
696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
698 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_x509_crt_free( crt );
701 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702 }
703
704 if( len > (size_t) ( end - p ) )
705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_x509_crt_free( crt );
707 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
708 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709 }
710 crt_end = p + len;
711
Janos Follathcc0e49d2016-02-17 14:34:12 +0000712 // Create and populate a new buffer for the raw field
713 crt->raw.len = crt_end - buf;
714 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
715 if( p == NULL )
716 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
717
718 memcpy( p, buf, crt->raw.len );
719
Hanno Becker2e7fee02017-09-25 10:47:58 +0100720 // Direct pointers to the new buffer
Janos Follathcc0e49d2016-02-17 14:34:12 +0000721 p += crt->raw.len - len;
722 end = crt_end = p + len;
723
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724 /*
725 * TBSCertificate ::= SEQUENCE {
726 */
727 crt->tbs.p = p;
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
730 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_x509_crt_free( crt );
733 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200734 }
735
736 end = p + len;
737 crt->tbs.len = end - crt->tbs.p;
738
739 /*
740 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
741 *
742 * CertificateSerialNumber ::= INTEGER
743 *
744 * signature AlgorithmIdentifier
745 */
746 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
748 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200749 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752 return( ret );
753 }
754
Andres AG80164742017-03-09 16:16:11 +0000755 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_x509_crt_free( crt );
758 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200759 }
760
Andres AG80164742017-03-09 16:16:11 +0000761 crt->version++;
762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200764 &crt->sig_md, &crt->sig_pk,
765 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 return( ret );
769 }
770
771 /*
772 * issuer Name
773 */
774 crt->issuer_raw.p = p;
775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
777 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_x509_crt_free( crt );
780 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781 }
782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786 return( ret );
787 }
788
789 crt->issuer_raw.len = p - crt->issuer_raw.p;
790
791 /*
792 * Validity ::= SEQUENCE {
793 * notBefore Time,
794 * notAfter Time }
795 *
796 */
797 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
798 &crt->valid_to ) ) != 0 )
799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801 return( ret );
802 }
803
804 /*
805 * subject Name
806 */
807 crt->subject_raw.p = p;
808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
810 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_x509_crt_free( crt );
813 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200814 }
815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200819 return( ret );
820 }
821
822 crt->subject_raw.len = p - crt->subject_raw.p;
823
824 /*
825 * SubjectPublicKeyInfo
826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830 return( ret );
831 }
832
833 /*
834 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
835 * -- If present, version shall be v2 or v3
836 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
837 * -- If present, version shall be v2 or v3
838 * extensions [3] EXPLICIT Extensions OPTIONAL
839 * -- If present, version shall be v3
840 */
841 if( crt->version == 2 || crt->version == 3 )
842 {
843 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
844 if( ret != 0 )
845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200847 return( ret );
848 }
849 }
850
851 if( crt->version == 2 || crt->version == 3 )
852 {
853 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
854 if( ret != 0 )
855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200857 return( ret );
858 }
859 }
860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200862 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200863#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100864 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200865 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866 if( ret != 0 )
867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869 return( ret );
870 }
871 }
872
873 if( p != end )
874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 mbedtls_x509_crt_free( crt );
876 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
877 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878 }
879
880 end = crt_end;
881
882 /*
883 * }
884 * -- end of TBSCertificate
885 *
886 * signatureAlgorithm AlgorithmIdentifier,
887 * signatureValue BIT STRING
888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 {
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
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100895 if( crt->sig_oid.len != sig_oid2.len ||
896 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200897 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200898 ( sig_params1.len != 0 &&
899 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_x509_crt_free( crt );
902 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 }
904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908 return( ret );
909 }
910
911 if( p != end )
912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_x509_crt_free( crt );
914 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
915 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916 }
917
918 return( 0 );
919}
920
921/*
922 * Parse one X.509 certificate in DER format from a buffer and add them to a
923 * chained list
924 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200926 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927{
928 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930
931 /*
932 * Check for valid input
933 */
934 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936
937 while( crt->version != 0 && crt->next != NULL )
938 {
939 prev = crt;
940 crt = crt->next;
941 }
942
943 /*
944 * Add new certificate on the end of the chain if needed.
945 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200946 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200948 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949
950 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200951 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952
953 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956 }
957
Paul Bakkerddf26b42013-09-18 13:46:23 +0200958 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 {
960 if( prev )
961 prev->next = NULL;
962
963 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965
966 return( ret );
967 }
968
969 return( 0 );
970}
971
972/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200973 * Parse one or more PEM certificates from a buffer and add them to the chained
974 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977{
Janos Follath98e28a72016-05-31 14:03:54 +0100978#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +0000979 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +0100981#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982
983 /*
984 * Check for valid input
985 */
986 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988
989 /*
990 * Determine buffer content. Buffer contains either one DER certificate or
991 * one or more PEM certificates.
992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +0200994 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200995 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200998 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1001 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001002#else
1003 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1004#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#if defined(MBEDTLS_PEM_PARSE_C)
1007 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008 {
1009 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001012 /* 1 rather than 0 since the terminating NULL byte is counted in */
1013 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014 {
1015 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001018 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 "-----BEGIN CERTIFICATE-----",
1021 "-----END CERTIFICATE-----",
1022 buf, NULL, 0, &use_len );
1023
1024 if( ret == 0 )
1025 {
1026 /*
1027 * Was PEM encoded
1028 */
1029 buflen -= use_len;
1030 buf += use_len;
1031 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033 {
1034 return( ret );
1035 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039
1040 /*
1041 * PEM header and footer were found
1042 */
1043 buflen -= use_len;
1044 buf += use_len;
1045
1046 if( first_error == 0 )
1047 first_error = ret;
1048
Paul Bakker5a5fa922014-09-26 14:53:04 +02001049 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050 continue;
1051 }
1052 else
1053 break;
1054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001058
1059 if( ret != 0 )
1060 {
1061 /*
1062 * Quit parsing on a memory error
1063 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001064 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065 return( ret );
1066
1067 if( first_error == 0 )
1068 first_error = ret;
1069
1070 total_failed++;
1071 continue;
1072 }
1073
1074 success = 1;
1075 }
1076 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077
1078 if( success )
1079 return( total_failed );
1080 else if( first_error )
1081 return( first_error );
1082 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001084#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085}
1086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088/*
1089 * Load one or more certificates and add them to the chained list
1090 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092{
1093 int ret;
1094 size_t n;
1095 unsigned char *buf;
1096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 return( ret );
1099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001102 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104
1105 return( ret );
1106}
1107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109{
1110 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001111#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112 int w_ret;
1113 WCHAR szDir[MAX_PATH];
1114 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001115 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001116 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117
Paul Bakker9af723c2014-05-01 13:03:14 +02001118 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119 HANDLE hFind;
1120
1121 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123
Paul Bakker9af723c2014-05-01 13:03:14 +02001124 memset( szDir, 0, sizeof(szDir) );
1125 memset( filename, 0, MAX_PATH );
1126 memcpy( filename, path, len );
1127 filename[len++] = '\\';
1128 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129 filename[len++] = '*';
1130
Simon B3c6b18d2016-11-03 01:11:37 +00001131 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001132 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001133 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001135
1136 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001137 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139
1140 len = MAX_PATH - len;
1141 do
1142 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001143 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001144
1145 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1146 continue;
1147
Paul Bakker9af723c2014-05-01 13:03:14 +02001148 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001149 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001150 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001151 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001152 if( w_ret == 0 )
Ron Eldor3e19df52017-01-09 15:09:16 +02001153 {
1154 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1155 goto cleanup;
1156 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001159 if( w_ret < 0 )
1160 ret++;
1161 else
1162 ret += w_ret;
1163 }
1164 while( FindNextFileW( hFind, &file_data ) != 0 );
1165
Paul Bakker66d5d072014-06-17 16:39:18 +02001166 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168
Ron Eldor3e19df52017-01-09 15:09:16 +02001169cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001171#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001172 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001173 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001175 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001176 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001177 DIR *dir = opendir( path );
1178
Paul Bakker66d5d072014-06-17 16:39:18 +02001179 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181
Ron Eldor8ab05952017-01-09 19:27:59 +02001182#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001183 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001184 {
1185 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001186 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001187 }
Ron Eldor8ab05952017-01-09 19:27:59 +02001188#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001189
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001190 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 {
Andres AGf9113192016-09-02 14:06:04 +01001192 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1193 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194
Andres AGf9113192016-09-02 14:06:04 +01001195 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001196 {
Andres AGf9113192016-09-02 14:06:04 +01001197 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1198 goto cleanup;
1199 }
1200 else if( stat( entry_name, &sb ) == -1 )
1201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001203 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001204 }
1205
1206 if( !S_ISREG( sb.st_mode ) )
1207 continue;
1208
1209 // Ignore parse errors
1210 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001212 if( t_ret < 0 )
1213 ret++;
1214 else
1215 ret += t_ret;
1216 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001217
1218cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001219 closedir( dir );
1220
Ron Eldor8ab05952017-01-09 19:27:59 +02001221#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001222 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor8ab05952017-01-09 19:27:59 +02001224#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001225
Paul Bakkerbe089b02013-10-14 15:51:50 +02001226#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227
1228 return( ret );
1229}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001231
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001232static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001234{
1235 size_t i;
1236 size_t n = *size;
1237 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001239 const char *sep = "";
1240 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001241
1242 while( cur != NULL )
1243 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001244 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001245 {
1246 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001247 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001248 }
1249
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001250 n -= cur->buf.len + sep_len;
1251 for( i = 0; i < sep_len; i++ )
1252 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001253 for( i = 0; i < cur->buf.len; i++ )
1254 *p++ = cur->buf.p[i];
1255
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001256 sep = ", ";
1257 sep_len = 2;
1258
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001259 cur = cur->next;
1260 }
1261
1262 *p = '\0';
1263
1264 *size = n;
1265 *buf = p;
1266
1267 return( 0 );
1268}
1269
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001270#define PRINT_ITEM(i) \
1271 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001273 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001274 sep = ", "; \
1275 }
1276
1277#define CERT_TYPE(type,name) \
1278 if( ns_cert_type & type ) \
1279 PRINT_ITEM( name );
1280
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001281static int x509_info_cert_type( char **buf, size_t *size,
1282 unsigned char ns_cert_type )
1283{
1284 int ret;
1285 size_t n = *size;
1286 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001287 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001290 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1292 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1293 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001294 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1295 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1296 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001297
1298 *size = n;
1299 *buf = p;
1300
1301 return( 0 );
1302}
1303
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001304#define KEY_USAGE(code,name) \
1305 if( key_usage & code ) \
1306 PRINT_ITEM( name );
1307
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001308static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001309 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001310{
1311 int ret;
1312 size_t n = *size;
1313 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001314 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1317 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001318 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1319 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1320 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1322 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001323 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1324 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001325
1326 *size = n;
1327 *buf = p;
1328
1329 return( 0 );
1330}
1331
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001332static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001334{
1335 int ret;
1336 const char *desc;
1337 size_t n = *size;
1338 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001340 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001341
1342 while( cur != NULL )
1343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001345 desc = "???";
1346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001348 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001349
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001350 sep = ", ";
1351
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001352 cur = cur->next;
1353 }
1354
1355 *size = n;
1356 *buf = p;
1357
1358 return( 0 );
1359}
1360
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361/*
1362 * Return an informational string about the certificate.
1363 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001364#define BEFORE_COLON 18
1365#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1367 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368{
1369 int ret;
1370 size_t n;
1371 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372 char key_size_str[BEFORE_COLON];
1373
1374 p = buf;
1375 n = size;
1376
Janos Follath98e28a72016-05-31 14:03:54 +01001377 if( NULL == crt )
1378 {
1379 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
1380 MBEDTLS_X509_SAFE_SNPRINTF;
1381
1382 return( (int) ( size - n ) );
1383 }
1384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001386 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001387 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001390 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001393 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001396 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001398 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001401 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001403 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1407 crt->valid_from.year, crt->valid_from.mon,
1408 crt->valid_from.day, crt->valid_from.hour,
1409 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001410 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001413 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1414 crt->valid_to.year, crt->valid_to.mon,
1415 crt->valid_to.day, crt->valid_to.hour,
1416 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001417 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001420 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001423 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001424 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001426 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1428 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429 {
1430 return( ret );
1431 }
1432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001434 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001435 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001437 /*
1438 * Optional extensions
1439 */
1440
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001441 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001444 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001445 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001446
1447 if( crt->max_pathlen > 0 )
1448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001450 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001451 }
1452 }
1453
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001454 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001457 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001458
1459 if( ( ret = x509_info_subject_alt_name( &p, &n,
1460 &crt->subject_alt_names ) ) != 0 )
1461 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001462 }
1463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001467 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001468
1469 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1470 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001471 }
1472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001476 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001477
1478 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1479 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001480 }
1481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001485 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001486
1487 if( ( ret = x509_info_ext_key_usage( &p, &n,
1488 &crt->ext_key_usage ) ) != 0 )
1489 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001490 }
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001493 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001494
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495 return( (int) ( size - n ) );
1496}
1497
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001498struct x509_crt_verify_string {
1499 int code;
1500 const char *string;
1501};
1502
1503static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001504 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001505 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1506 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1507 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1508 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1509 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001510 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1511 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1512 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001513 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001514 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1515 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1516 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1517 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001518 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1519 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1520 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1521 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1522 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1523 { 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 +01001524 { 0, NULL }
1525};
1526
1527int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001528 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001529{
1530 int ret;
1531 const struct x509_crt_verify_string *cur;
1532 char *p = buf;
1533 size_t n = size;
1534
1535 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1536 {
1537 if( ( flags & cur->code ) == 0 )
1538 continue;
1539
1540 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001541 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001542 flags ^= cur->code;
1543 }
1544
1545 if( flags != 0 )
1546 {
1547 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1548 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001549 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001550 }
1551
1552 return( (int) ( size - n ) );
1553}
1554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001556int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1557 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001558{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001559 unsigned int usage_must, usage_may;
1560 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1561 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1562
1563 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1564 return( 0 );
1565
1566 usage_must = usage & ~may_mask;
1567
1568 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1569 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1570
1571 usage_may = usage & may_mask;
1572
1573 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001575
1576 return( 0 );
1577}
1578#endif
1579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1581int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001582 const char *usage_oid,
1583 size_t usage_len )
1584{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001586
1587 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001589 return( 0 );
1590
1591 /*
1592 * Look for the requested usage (or wildcard ANY) in our list
1593 */
1594 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001597
1598 if( cur_oid->len == usage_len &&
1599 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1600 {
1601 return( 0 );
1602 }
1603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001605 return( 0 );
1606 }
1607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001609}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001613/*
1614 * Return 1 if the certificate is revoked, or 0 otherwise.
1615 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001616int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619
1620 while( cur != NULL && cur->serial.len != 0 )
1621 {
1622 if( crt->serial.len == cur->serial.len &&
1623 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1624 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001625 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001626 return( 1 );
1627 }
1628
1629 cur = cur->next;
1630 }
1631
1632 return( 0 );
1633}
1634
1635/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01001636 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard75d35602018-03-05 13:22:59 +01001637 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001640 mbedtls_x509_crl *crl_list,
1641 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001642{
1643 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1645 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646
1647 if( ca == NULL )
1648 return( flags );
1649
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001650 while( crl_list != NULL )
1651 {
1652 if( crl_list->version == 0 ||
1653 crl_list->issuer_raw.len != ca->subject_raw.len ||
1654 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1655 crl_list->issuer_raw.len ) != 0 )
1656 {
1657 crl_list = crl_list->next;
1658 continue;
1659 }
1660
1661 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001662 * Check if the CA is configured to sign CRLs
1663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1665 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001668 break;
1669 }
1670#endif
1671
1672 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001673 * Check if CRL is correctly signed by the trusted CA
1674 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001675 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1676 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1677
1678 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1679 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnardb9c72eb2017-06-26 12:22:17 +02001682 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001683 {
Manuel Pégourié-Gonnardb9c72eb2017-06-26 12:22:17 +02001684 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001686 break;
1687 }
1688
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001689 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1690 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1693 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001694 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001697 break;
1698 }
1699
1700 /*
1701 * Check for validity of CRL (Do not drop out)
1702 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001703 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001706 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001707 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001708
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001709 /*
1710 * Check if certificate is revoked
1711 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001712 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001715 break;
1716 }
1717
1718 crl_list = crl_list->next;
1719 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001720
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001721 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001722}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001724
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001725/*
1726 * Like memcmp, but case-insensitive and always returns -1 if different
1727 */
1728static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001729{
1730 size_t i;
1731 unsigned char diff;
1732 const unsigned char *n1 = s1, *n2 = s2;
1733
1734 for( i = 0; i < len; i++ )
1735 {
1736 diff = n1[i] ^ n2[i];
1737
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001738 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001739 continue;
1740
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001741 if( diff == 32 &&
1742 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1743 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1744 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001746 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001747
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001748 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001749 }
1750
1751 return( 0 );
1752}
1753
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001754/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001755 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001756 */
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001757static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001758{
1759 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001760 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001761
1762 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1763 return( 0 );
1764
Paul Bakker14b16c62014-05-28 11:33:54 +02001765 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001766 {
1767 if( cn[i] == '.' )
1768 {
1769 cn_idx = i;
1770 break;
1771 }
1772 }
1773
1774 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001775 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001776
Paul Bakker14b16c62014-05-28 11:33:54 +02001777 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001778 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001779 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001780 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001781 }
1782
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001783 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001784}
1785
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001786/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001787 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1788 * variations (but not all).
1789 *
1790 * Return 0 if equal, -1 otherwise.
1791 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001793{
1794 if( a->tag == b->tag &&
1795 a->len == b->len &&
1796 memcmp( a->p, b->p, b->len ) == 0 )
1797 {
1798 return( 0 );
1799 }
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1802 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001803 a->len == b->len &&
1804 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1805 {
1806 return( 0 );
1807 }
1808
1809 return( -1 );
1810}
1811
1812/*
1813 * Compare two X.509 Names (aka rdnSequence).
1814 *
1815 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1816 * we sometimes return unequal when the full algorithm would return equal,
1817 * but never the other way. (In particular, we don't do Unicode normalisation
1818 * or space folding.)
1819 *
1820 * Return 0 if equal, -1 otherwise.
1821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001823{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001824 /* Avoid recursion, it might not be optimised by the compiler */
1825 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001826 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001827 if( a == NULL || b == NULL )
1828 return( -1 );
1829
1830 /* type */
1831 if( a->oid.tag != b->oid.tag ||
1832 a->oid.len != b->oid.len ||
1833 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1834 {
1835 return( -1 );
1836 }
1837
1838 /* value */
1839 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1840 return( -1 );
1841
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001842 /* structure of the list of sets */
1843 if( a->next_merged != b->next_merged )
1844 return( -1 );
1845
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001846 a = a->next;
1847 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001848 }
1849
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001850 /* a == NULL == b */
1851 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001852}
1853
1854/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001855 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1856 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001857 *
1858 * top means parent is a locally-trusted certificate
1859 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1862 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001863 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001864{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001865 int need_ca_bit;
1866
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001867 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001868 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001869 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001870
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001871 /* Parent must have the basicConstraints CA bit set as a general rule */
1872 need_ca_bit = 1;
1873
1874 /* Exception: v1/v2 certificates that are locally trusted. */
1875 if( top && parent->version < 3 )
1876 need_ca_bit = 0;
1877
1878 /* Exception: self-signed end-entity certs that are locally trusted. */
1879 if( top && bottom &&
1880 child->raw.len == parent->raw.len &&
1881 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1882 {
1883 need_ca_bit = 0;
1884 }
1885
1886 if( need_ca_bit && ! parent->ca_istrue )
1887 return( -1 );
1888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001890 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001892 {
1893 return( -1 );
1894 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001895#endif
1896
1897 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001898}
1899
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001900/*
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01001901 * Verify a certificate with no parent inside the chain
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001902 * (either the parent is a trusted root, or there is no parent)
1903 *
1904 * See comments for mbedtls_x509_crt_verify_with_profile()
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01001905 * (also for notation used below)
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001906 *
1907 * This function is called in two cases:
1908 * - child was found to have a parent in trusted roots, in which case we're
1909 * called with trust_ca pointing directly to that parent (not the full list)
Manuel Pégourié-Gonnard71df3732018-03-07 09:36:30 +01001910 * - this is cases 1, 2 and 3 of the comment on verify_with_profile()
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001911 * - case 1 is special as child and trust_ca point to copies of the same
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01001912 * certificate then
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001913 * - child was found to have no parent either in the chain or in trusted CAs
Manuel Pégourié-Gonnard71df3732018-03-07 09:36:30 +01001914 * - this is cases 4 and 5 of the comment on verify_with_profile()
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02001915 *
1916 * For historical reasons, the function currently does not assume that
1917 * trust_ca points directly to the right root in the first case, and it
1918 * doesn't know in which case it starts, so it always starts by searching for
1919 * a parent in trust_ca.
1920 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001921static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001923 mbedtls_x509_crl *ca_crl,
1924 const mbedtls_x509_crt_profile *profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02001925 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001926 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001927 void *p_vrfy )
1928{
1929 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001930 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001931 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1933 const mbedtls_md_info_t *md_info;
Andres AGd1650662016-12-09 17:26:23 +00001934 mbedtls_x509_crt *future_past_ca = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001935
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001936 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001937 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001938
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001939 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001941
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001942 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
1943 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
1944
1945 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
1946 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
1947
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001948 /*
1949 * Child is the top of the chain. Check against the trust_ca list.
1950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 md_info = mbedtls_md_info_from_type( child->sig_md );
Manuel Pégourié-Gonnardb9c72eb2017-06-26 12:22:17 +02001954 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001955 {
Manuel Pégourié-Gonnardb9c72eb2017-06-26 12:22:17 +02001956 /* Note: this can't happen except after an internal error */
1957 /* Cannot check signature, no need to try any CA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001958 trust_ca = NULL;
1959 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001960
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001961 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001962 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001963 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001964 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001965
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001966 check_path_cnt = path_cnt + 1;
1967
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001968 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001969 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001970 * the same as the trusted CA
1971 */
1972 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1973 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Hanno Becker2e7fee02017-09-25 10:47:58 +01001974 child->subject_raw.len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001975 {
1976 check_path_cnt--;
1977 }
1978
Janos Follath5dd4fe12015-10-12 09:02:20 +02001979 /* Self signed certificates do not count towards the limit */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001980 if( trust_ca->max_pathlen > 0 &&
Janos Follath5dd4fe12015-10-12 09:02:20 +02001981 trust_ca->max_pathlen < check_path_cnt - self_cnt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001982 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001983 continue;
1984 }
1985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1987 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001988 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001989 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001990 continue;
1991 }
1992
Andres AGd1650662016-12-09 17:26:23 +00001993 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) ||
1994 mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
1995 {
1996 if ( future_past_ca == NULL )
1997 future_past_ca = trust_ca;
1998
1999 continue;
2000 }
2001
2002 break;
2003 }
2004
2005 if( trust_ca != NULL || ( trust_ca = future_past_ca ) != NULL )
2006 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002007 /*
2008 * Top of chain is signed by a trusted CA
2009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardfa67eba2015-06-27 14:41:38 +02002011
2012 if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
2013 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002014 }
2015
2016 /*
2017 * If top of chain is not the same as the trusted CA send a verify request
2018 * to the callback for any issues with validity and CRL presence for the
2019 * trusted CA certificate.
2020 */
2021 if( trust_ca != NULL &&
2022 ( child->subject_raw.len != trust_ca->subject_raw.len ||
2023 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Hanno Becker2e7fee02017-09-25 10:47:58 +01002024 child->subject_raw.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002027 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002028 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02002029#else
2030 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002031#endif
2032
Andres AGd1650662016-12-09 17:26:23 +00002033 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
2034 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
2035
2036 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
2037 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
2038
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002039 if( NULL != f_vrfy )
2040 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002041 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
2042 &ca_flags ) ) != 0 )
2043 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002044 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002045 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002046 }
2047 }
2048
2049 /* Call callback on top cert */
2050 if( NULL != f_vrfy )
2051 {
Paul Bakker66d5d072014-06-17 16:39:18 +02002052 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002053 return( ret );
2054 }
2055
2056 *flags |= ca_flags;
2057
2058 return( 0 );
2059}
2060
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002061/*
2062 * Verify a certificate with a parent inside the chain
2063 *
2064 * See comments for mbedtls_x509_crt_verify_with_profile()
2065 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002066static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002067 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
2068 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
2069 const mbedtls_x509_crt_profile *profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002070 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002071 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002072 void *p_vrfy )
2073{
2074 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002075 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2077 mbedtls_x509_crt *grandparent;
2078 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002079
Janos Follath5dd4fe12015-10-12 09:02:20 +02002080 /* Counting intermediate self signed certificates */
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01002081 if( ( path_cnt != 0 ) && x509_name_cmp( &child->issuer, &child->subject ) == 0 )
Janos Follath5dd4fe12015-10-12 09:02:20 +02002082 self_cnt++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002083
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002084 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002086 {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002087 /* return immediately as the goal is to avoid unbounded recursion */
2088 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002089 }
2090
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002091 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002092 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02002093
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002094 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002096
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002097 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2098 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2099
2100 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2101 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 md_info = mbedtls_md_info_from_type( child->sig_md );
Manuel Pégourié-Gonnarde786a7e2018-03-07 09:41:20 +01002104 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002105 {
Manuel Pégourié-Gonnarde786a7e2018-03-07 09:41:20 +01002106 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002108 }
2109 else
2110 {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002111 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2112 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2115 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002116 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002119 }
2120 }
2121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002123 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002124 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002125#endif
2126
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002127 /* Look for a grandparent in trusted CAs */
2128 for( grandparent = trust_ca;
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002129 grandparent != NULL;
2130 grandparent = grandparent->next )
2131 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002132 if( x509_crt_check_parent( parent, grandparent,
2133 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002134 break;
2135 }
2136
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002137 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002138 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002139 ret = x509_crt_verify_top( parent, grandparent, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002140 path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002141 if( ret != 0 )
2142 return( ret );
2143 }
2144 else
2145 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002146 /* Look for a grandparent upwards the chain */
2147 for( grandparent = parent->next;
2148 grandparent != NULL;
2149 grandparent = grandparent->next )
2150 {
Janos Follath5dd4fe12015-10-12 09:02:20 +02002151 /* +2 because the current step is not yet accounted for
2152 * and because max_pathlen is one higher than it should be.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01002153 * Also self signed certificates do not count to the limit. */
Janos Follath5dd4fe12015-10-12 09:02:20 +02002154 if( grandparent->max_pathlen > 0 &&
2155 grandparent->max_pathlen < 2 + path_cnt - self_cnt )
2156 {
2157 continue;
2158 }
2159
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002160 if( x509_crt_check_parent( parent, grandparent,
2161 0, path_cnt == 0 ) == 0 )
2162 break;
2163 }
2164
2165 /* Is our parent part of the chain or at the top? */
2166 if( grandparent != NULL )
2167 {
2168 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002169 profile, path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002170 f_vrfy, p_vrfy );
2171 if( ret != 0 )
2172 return( ret );
2173 }
2174 else
2175 {
2176 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002177 path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002178 f_vrfy, p_vrfy );
2179 if( ret != 0 )
2180 return( ret );
2181 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002182 }
2183
2184 /* child is verified to be a child of the parent, call verify callback */
2185 if( NULL != f_vrfy )
2186 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2187 return( ret );
2188
2189 *flags |= parent_flags;
2190
2191 return( 0 );
2192}
2193
2194/*
2195 * Verify the certificate validity
2196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2198 mbedtls_x509_crt *trust_ca,
2199 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002200 const char *cn, uint32_t *flags,
2201 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002202 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002203{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002204 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002205 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002206}
2207
2208
2209/*
2210 * Verify the certificate validity, with profile
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002211 *
2212 * The chain building/verification is spread accross 4 functions:
2213 * - this one
2214 * - x509_crt_verify_child()
2215 * - x509_crt_verify_top()
2216 * - x509_crt_check_parent()
2217 *
2218 * There are five main cases to consider. Let's introduce some notation:
2219 * - E means the end-entity certificate
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01002220 * - I an intermediate CA
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002221 * - R the trusted root CA this chain anchors to
2222 * - T the list of trusted roots (R and possible some others)
2223 *
2224 * The main cases with the calling sequence of the crt_verify_xxx() are:
2225 * 1. E = R (explicitly trusted EE cert)
2226 * verify(E, T) -> verify_top(E, R)
2227 * 2. E -> R (EE signed by trusted root)
2228 * verify(E, T) -> verify_top(E, R)
2229 * 3. E -> I -> R (EE signed by intermediate signed by trusted root)
2230 * verify(E, T) -> verify_child(E, I, T) -> verify_top(I, R)
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01002231 * (plus variant with multiple intermediates)
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002232 * 4. E -> I (EE signed by intermediate that's not trusted)
2233 * verify(E, T) -> verify_child(E, I, T) -> verify_top(I, T)
Manuel Pégourié-Gonnard35eb39a2018-03-06 10:34:11 +01002234 * (plus variant with multiple intermediates)
Manuel Pégourié-Gonnardc08e9062017-06-29 10:45:25 +02002235 * 5. E (EE not trusted)
2236 * verify(E, T) -> verify_top(E, T)
Manuel Pégourié-Gonnard71df3732018-03-07 09:36:30 +01002237 *
2238 * Note: this notation and case numbering is also used in x509_crt_verify_top()
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002239 */
2240int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2241 mbedtls_x509_crt *trust_ca,
2242 mbedtls_x509_crl *ca_crl,
2243 const mbedtls_x509_crt_profile *profile,
2244 const char *cn, uint32_t *flags,
2245 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2246 void *p_vrfy )
2247{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002248 size_t cn_len;
2249 int ret;
Janos Follath5dd4fe12015-10-12 09:02:20 +02002250 int pathlen = 0, selfsigned = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 mbedtls_x509_crt *parent;
2252 mbedtls_x509_name *name;
2253 mbedtls_x509_sequence *cur = NULL;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002254 mbedtls_pk_type_t pk_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002255
2256 *flags = 0;
2257
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002258 if( profile == NULL )
2259 {
2260 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2261 goto exit;
2262 }
2263
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002264 if( cn != NULL )
2265 {
2266 name = &crt->subject;
2267 cn_len = strlen( cn );
2268
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002269 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002270 {
2271 cur = &crt->subject_alt_names;
2272
2273 while( cur != NULL )
2274 {
2275 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002276 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002277 break;
2278
2279 if( cur->buf.len > 2 &&
2280 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002281 x509_check_wildcard( cn, &cur->buf ) == 0 )
2282 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002283 break;
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002284 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002285
2286 cur = cur->next;
2287 }
2288
2289 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002291 }
2292 else
2293 {
2294 while( name != NULL )
2295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002297 {
2298 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002299 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002300 break;
2301
2302 if( name->val.len > 2 &&
2303 memcmp( name->val.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002304 x509_check_wildcard( cn, &name->val ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002305 break;
2306 }
2307
2308 name = name->next;
2309 }
2310
2311 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002313 }
2314 }
2315
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002316 /* Check the type and size of the key */
2317 pk_type = mbedtls_pk_get_type( &crt->pk );
2318
2319 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
2320 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2321
2322 if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
2323 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2324
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002325 /* Look for a parent in trusted CAs */
2326 for( parent = trust_ca; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002327 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002328 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002329 break;
2330 }
2331
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002332 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002333 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002334 ret = x509_crt_verify_top( crt, parent, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002335 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002336 if( ret != 0 )
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002337 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002338 }
2339 else
2340 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002341 /* Look for a parent upwards the chain */
2342 for( parent = crt->next; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002343 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
2344 break;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002345
2346 /* Are we part of the chain or at the top? */
2347 if( parent != NULL )
2348 {
2349 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002350 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002351 if( ret != 0 )
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002352 goto exit;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002353 }
2354 else
2355 {
2356 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
Janos Follath5dd4fe12015-10-12 09:02:20 +02002357 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002358 if( ret != 0 )
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002359 goto exit;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002360 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002361 }
2362
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002363exit:
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02002364 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2365 * the SSL module for authmode optional, but non-zero return from the
2366 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002367 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2368 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2369
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002370 if( ret != 0 )
2371 {
2372 *flags = (uint32_t) -1;
2373 return( ret );
2374 }
2375
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002376 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002378
2379 return( 0 );
2380}
2381
2382/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002383 * Initialize a certificate chain
2384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002386{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002388}
2389
2390/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002391 * Unallocate all certificate data
2392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002394{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 mbedtls_x509_crt *cert_cur = crt;
2396 mbedtls_x509_crt *cert_prv;
2397 mbedtls_x509_name *name_cur;
2398 mbedtls_x509_name *name_prv;
2399 mbedtls_x509_sequence *seq_cur;
2400 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002401
2402 if( crt == NULL )
2403 return;
2404
2405 do
2406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2410 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002411#endif
2412
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002413 name_cur = cert_cur->issuer.next;
2414 while( name_cur != NULL )
2415 {
2416 name_prv = name_cur;
2417 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2419 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002420 }
2421
2422 name_cur = cert_cur->subject.next;
2423 while( name_cur != NULL )
2424 {
2425 name_prv = name_cur;
2426 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2428 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002429 }
2430
2431 seq_cur = cert_cur->ext_key_usage.next;
2432 while( seq_cur != NULL )
2433 {
2434 seq_prv = seq_cur;
2435 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2437 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002438 }
2439
2440 seq_cur = cert_cur->subject_alt_names.next;
2441 while( seq_cur != NULL )
2442 {
2443 seq_prv = seq_cur;
2444 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2446 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002447 }
2448
2449 if( cert_cur->raw.p != NULL )
2450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2452 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002453 }
2454
2455 cert_cur = cert_cur->next;
2456 }
2457 while( cert_cur != NULL );
2458
2459 cert_cur = crt;
2460 do
2461 {
2462 cert_prv = cert_cur;
2463 cert_cur = cert_cur->next;
2464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002466 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002468 }
2469 while( cert_cur != NULL );
2470}
2471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472#endif /* MBEDTLS_X509_CRT_PARSE_C */