blob: eea4f16579201d106764797e61a7bc482f502b00 [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
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047#endif
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051#else
Simon Butcherd975e462018-10-03 15:11:19 +010052#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020055#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010061#endif
62
Paul Bakkerfa6a6202013-10-28 18:48:30 +010063#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064#include <windows.h>
65#else
66#include <time.h>
67#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000070#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020071#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#include <sys/types.h>
73#include <sys/stat.h>
74#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000075#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#endif
77
Paul Bakker34617722014-06-13 17:20:13 +020078/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020080 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
81}
82
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020084 * Default profile
85 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020086const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
87{
Gilles Peskine7344e1b2017-05-12 13:16:40 +020088#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine955738a2017-05-04 16:17:21 +020089 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020090 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine955738a2017-05-04 16:17:21 +020091#endif
92 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020093 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
94 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
95 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
96 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
97 0xFFFFFFF, /* Any PK alg */
98 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020099 2048,
100};
101
102/*
103 * Next-default profile
104 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200105const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
106{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200107 /* Hashes from SHA-256 and above */
108 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
110 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
111 0xFFFFFFF, /* Any PK alg */
112#if defined(MBEDTLS_ECP_C)
113 /* Curves at or above 128-bit security level */
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
120 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
121#else
122 0,
123#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200124 2048,
125};
126
127/*
128 * NSA Suite B Profile
129 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200130const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
131{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200132 /* Only SHA-256 and 384 */
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
135 /* Only ECDSA */
Ron Eldor3a3b6542018-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{
Philippe Antoine795eea62018-05-11 11:06:29 +0200155 if( md_alg == MBEDTLS_MD_NONE )
156 return( -1 );
157
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200158 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
159 return( 0 );
160
161 return( -1 );
162}
163
164/*
165 * Check pk_alg against profile
166 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
167 */
168static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
169 mbedtls_pk_type_t pk_alg )
170{
Philippe Antoine795eea62018-05-11 11:06:29 +0200171 if( pk_alg == MBEDTLS_PK_NONE )
172 return( -1 );
173
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200174 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
175 return( 0 );
176
177 return( -1 );
178}
179
180/*
181 * Check key against profile
182 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
183 */
184static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
185 mbedtls_pk_type_t pk_alg,
186 const mbedtls_pk_context *pk )
187{
188#if defined(MBEDTLS_RSA_C)
189 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
190 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200191 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200192 return( 0 );
193
194 return( -1 );
195 }
196#endif
197
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +0200198#if defined(MBEDTLS_ECP_C)
199 if( pk_alg == MBEDTLS_PK_ECDSA ||
200 pk_alg == MBEDTLS_PK_ECKEY ||
201 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200202 {
203 mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
204
Philippe Antoine795eea62018-05-11 11:06:29 +0200205 if( gid == MBEDTLS_ECP_DP_NONE )
206 return( -1 );
207
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200208 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
209 return( 0 );
210
211 return( -1 );
212 }
213#endif
214
215 return( -1 );
216}
217
218/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
220 */
221static int x509_get_version( unsigned char **p,
222 const unsigned char *end,
223 int *ver )
224{
225 int ret;
226 size_t len;
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
229 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200232 {
233 *ver = 0;
234 return( 0 );
235 }
236
237 return( ret );
238 }
239
240 end = *p + len;
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
243 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200244
245 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 return( MBEDTLS_ERR_X509_INVALID_VERSION +
247 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200248
249 return( 0 );
250}
251
252/*
253 * Validity ::= SEQUENCE {
254 * notBefore Time,
255 * notAfter Time }
256 */
257static int x509_get_dates( unsigned char **p,
258 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_x509_time *from,
260 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261{
262 int ret;
263 size_t len;
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
266 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
267 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268
269 end = *p + len;
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200272 return( ret );
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275 return( ret );
276
277 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 return( MBEDTLS_ERR_X509_INVALID_DATE +
279 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280
281 return( 0 );
282}
283
284/*
285 * X.509 v2/v3 unique identifier (not parsed)
286 */
287static int x509_get_uid( unsigned char **p,
288 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290{
291 int ret;
292
293 if( *p == end )
294 return( 0 );
295
296 uid->tag = **p;
297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
299 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200302 return( 0 );
303
304 return( ret );
305 }
306
307 uid->p = *p;
308 *p += uid->len;
309
310 return( 0 );
311}
312
313static int x509_get_basic_constraints( unsigned char **p,
314 const unsigned char *end,
315 int *ca_istrue,
316 int *max_pathlen )
317{
318 int ret;
319 size_t len;
320
321 /*
322 * BasicConstraints ::= SEQUENCE {
323 * cA BOOLEAN DEFAULT FALSE,
324 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
325 */
326 *ca_istrue = 0; /* DEFAULT FALSE */
327 *max_pathlen = 0; /* endless */
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
330 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
331 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200332
333 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200334 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
339 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200340
341 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343
344 if( *ca_istrue != 0 )
345 *ca_istrue = 1;
346 }
347
348 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200349 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
352 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353
354 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
356 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357
358 (*max_pathlen)++;
359
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200360 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361}
362
363static int x509_get_ns_cert_type( unsigned char **p,
364 const unsigned char *end,
365 unsigned char *ns_cert_type)
366{
367 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
371 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372
373 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
375 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376
377 /* Get actual bitstring */
378 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200379 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380}
381
382static int x509_get_key_usage( unsigned char **p,
383 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100384 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200385{
386 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200387 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
391 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392
393 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
395 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396
397 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200398 *key_usage = 0;
399 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
400 {
401 *key_usage |= (unsigned int) bs.p[i] << (8*i);
402 }
403
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200404 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405}
406
407/*
408 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
409 *
410 * KeyPurposeId ::= OBJECT IDENTIFIER
411 */
412static int x509_get_ext_key_usage( unsigned char **p,
413 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415{
416 int ret;
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
419 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420
421 /* Sequence length must be >= 1 */
422 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
424 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200426 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427}
428
429/*
430 * SubjectAltName ::= GeneralNames
431 *
432 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
433 *
434 * GeneralName ::= CHOICE {
435 * otherName [0] OtherName,
436 * rfc822Name [1] IA5String,
437 * dNSName [2] IA5String,
438 * x400Address [3] ORAddress,
439 * directoryName [4] Name,
440 * ediPartyName [5] EDIPartyName,
441 * uniformResourceIdentifier [6] IA5String,
442 * iPAddress [7] OCTET STRING,
443 * registeredID [8] OBJECT IDENTIFIER }
444 *
445 * OtherName ::= SEQUENCE {
446 * type-id OBJECT IDENTIFIER,
447 * value [0] EXPLICIT ANY DEFINED BY type-id }
448 *
449 * EDIPartyName ::= SEQUENCE {
450 * nameAssigner [0] DirectoryString OPTIONAL,
451 * partyName [1] DirectoryString }
452 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000453 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454 */
455static int x509_get_subject_alt_name( unsigned char **p,
456 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458{
459 int ret;
460 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
465 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
467 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
468 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469
470 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
472 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473
474 while( *p < end )
475 {
476 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
478 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479
480 tag = **p;
481 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
483 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484
Andres Amaya Garciaf6a6b822017-08-25 17:13:12 +0100485 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
486 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
489 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Andres Amaya Garciaf6a6b822017-08-25 17:13:12 +0100490 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200492 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494 {
495 *p += tag_len;
496 continue;
497 }
498
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200500 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100502 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100504
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200505 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
507 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200509 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511 cur = cur->next;
512 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200513
514 buf = &(cur->buf);
515 buf->tag = tag;
516 buf->p = *p;
517 buf->len = tag_len;
518 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519 }
520
521 /* Set final sequence entry's next pointer to NULL */
522 cur->next = NULL;
523
524 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
526 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527
528 return( 0 );
529}
530
531/*
532 * X.509 v3 extensions
533 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534 */
535static int x509_get_crt_ext( unsigned char **p,
536 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538{
539 int ret;
540 size_t len;
541 unsigned char *end_ext_data, *end_ext_octet;
542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546 return( 0 );
547
548 return( ret );
549 }
550
551 while( *p < end )
552 {
553 /*
554 * Extension ::= SEQUENCE {
555 * extnID OBJECT IDENTIFIER,
556 * critical BOOLEAN DEFAULT FALSE,
557 * extnValue OCTET STRING }
558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560 int is_critical = 0; /* DEFAULT FALSE */
561 int ext_type = 0;
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
564 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
565 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566
567 end_ext_data = *p + len;
568
569 /* Get extension ID */
k-stachowiak2d2d80b2018-07-16 10:49:12 +0200570 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiakd21e9582018-07-24 12:53:54 +0200571 MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573
k-stachowiak2d2d80b2018-07-16 10:49:12 +0200574 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575 extn_oid.p = *p;
576 *p += extn_oid.len;
577
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
580 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
581 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582
583 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
585 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
586 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587
588 end_ext_octet = *p + len;
589
590 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
592 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593
594 /*
595 * Detect supported extensions
596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200598
599 if( ret != 0 )
600 {
601 /* No parser found, skip extension */
602 *p = end_ext_octet;
603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605 if( is_critical )
606 {
607 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
609 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610 }
611#endif
612 continue;
613 }
614
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100615 /* Forbid repeated extensions */
616 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100618
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619 crt->ext_types |= ext_type;
620
621 switch( ext_type )
622 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100623 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624 /* Parse basic constraints */
625 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
626 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200627 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628 break;
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631 /* Parse key usage */
632 if( ( ret = x509_get_key_usage( p, end_ext_octet,
633 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200634 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 break;
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638 /* Parse extended key usage */
639 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
640 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200641 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 break;
643
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100644 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645 /* Parse subject alt name */
646 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
647 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200648 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649 break;
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652 /* Parse netscape certificate type */
653 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
654 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200655 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656 break;
657
658 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660 }
661 }
662
663 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
665 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666
667 return( 0 );
668}
669
670/*
671 * Parse and fill a single X.509 certificate in DER format
672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200674 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675{
676 int ret;
677 size_t len;
678 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
682 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
683 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684
685 /*
686 * Check for valid input
687 */
688 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690
Janos Follath16734f02016-02-17 14:34:12 +0000691 // Use the original buffer until we figure out actual length
692 p = (unsigned char*) buf;
693 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694 end = p + len;
695
696 /*
697 * Certificate ::= SEQUENCE {
698 * tbsCertificate TBSCertificate,
699 * signatureAlgorithm AlgorithmIdentifier,
700 * signatureValue BIT STRING }
701 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
703 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_x509_crt_free( crt );
706 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707 }
708
709 if( len > (size_t) ( end - p ) )
710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_x509_crt_free( crt );
712 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
713 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714 }
715 crt_end = p + len;
716
Janos Follath16734f02016-02-17 14:34:12 +0000717 // Create and populate a new buffer for the raw field
718 crt->raw.len = crt_end - buf;
719 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
720 if( p == NULL )
721 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
722
723 memcpy( p, buf, crt->raw.len );
724
Hanno Beckerdc8751d2017-09-25 10:47:58 +0100725 // Direct pointers to the new buffer
Janos Follath16734f02016-02-17 14:34:12 +0000726 p += crt->raw.len - len;
727 end = crt_end = p + len;
728
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200729 /*
730 * TBSCertificate ::= SEQUENCE {
731 */
732 crt->tbs.p = p;
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
735 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_x509_crt_free( crt );
738 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 }
740
741 end = p + len;
742 crt->tbs.len = end - crt->tbs.p;
743
744 /*
745 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
746 *
747 * CertificateSerialNumber ::= INTEGER
748 *
749 * signature AlgorithmIdentifier
750 */
751 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
753 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200754 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757 return( ret );
758 }
759
Andres AG1f06d9b2017-03-09 16:16:11 +0000760 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_x509_crt_free( crt );
763 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764 }
765
Andres AG1f06d9b2017-03-09 16:16:11 +0000766 crt->version++;
767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200769 &crt->sig_md, &crt->sig_pk,
770 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773 return( ret );
774 }
775
776 /*
777 * issuer Name
778 */
779 crt->issuer_raw.p = p;
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
782 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 mbedtls_x509_crt_free( crt );
785 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786 }
787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791 return( ret );
792 }
793
794 crt->issuer_raw.len = p - crt->issuer_raw.p;
795
796 /*
797 * Validity ::= SEQUENCE {
798 * notBefore Time,
799 * notAfter Time }
800 *
801 */
802 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
803 &crt->valid_to ) ) != 0 )
804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200806 return( ret );
807 }
808
809 /*
810 * subject Name
811 */
812 crt->subject_raw.p = p;
813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
815 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_x509_crt_free( crt );
818 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200819 }
820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824 return( ret );
825 }
826
827 crt->subject_raw.len = p - crt->subject_raw.p;
828
829 /*
830 * SubjectPublicKeyInfo
831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200835 return( ret );
836 }
837
838 /*
839 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
840 * -- If present, version shall be v2 or v3
841 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
842 * -- If present, version shall be v2 or v3
843 * extensions [3] EXPLICIT Extensions OPTIONAL
844 * -- If present, version shall be v3
845 */
846 if( crt->version == 2 || crt->version == 3 )
847 {
848 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
849 if( ret != 0 )
850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200852 return( ret );
853 }
854 }
855
856 if( crt->version == 2 || crt->version == 3 )
857 {
858 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
859 if( ret != 0 )
860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200862 return( ret );
863 }
864 }
865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200867 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200868#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100869 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200870 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200871 if( ret != 0 )
872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874 return( ret );
875 }
876 }
877
878 if( p != end )
879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_x509_crt_free( crt );
881 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
882 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883 }
884
885 end = crt_end;
886
887 /*
888 * }
889 * -- end of TBSCertificate
890 *
891 * signatureAlgorithm AlgorithmIdentifier,
892 * signatureValue BIT STRING
893 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897 return( ret );
898 }
899
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100900 if( crt->sig_oid.len != sig_oid2.len ||
901 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200902 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200903 ( sig_params1.len != 0 &&
904 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_x509_crt_free( crt );
907 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908 }
909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913 return( ret );
914 }
915
916 if( p != end )
917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_x509_crt_free( crt );
919 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
920 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 }
922
923 return( 0 );
924}
925
926/*
927 * Parse one X.509 certificate in DER format from a buffer and add them to a
928 * chained list
929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200931 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932{
933 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935
936 /*
937 * Check for valid input
938 */
939 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200941
942 while( crt->version != 0 && crt->next != NULL )
943 {
944 prev = crt;
945 crt = crt->next;
946 }
947
948 /*
949 * Add new certificate on the end of the chain if needed.
950 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200951 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200953 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
955 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200956 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957
958 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961 }
962
Paul Bakkerddf26b42013-09-18 13:46:23 +0200963 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964 {
965 if( prev )
966 prev->next = NULL;
967
968 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970
971 return( ret );
972 }
973
974 return( 0 );
975}
976
977/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200978 * Parse one or more PEM certificates from a buffer and add them to the chained
979 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982{
983 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 int buf_format = MBEDTLS_X509_FORMAT_DER;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985
986 /*
987 * Check for valid input
988 */
989 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991
992 /*
993 * Determine buffer content. Buffer contains either one DER certificate or
994 * one or more PEM certificates.
995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +0200997 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200998 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001001 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002#endif
1003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1005 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#if defined(MBEDTLS_PEM_PARSE_C)
1008 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 {
1010 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001013 /* 1 rather than 0 since the terminating NULL byte is counted in */
1014 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015 {
1016 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001019 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 "-----BEGIN CERTIFICATE-----",
1022 "-----END CERTIFICATE-----",
1023 buf, NULL, 0, &use_len );
1024
1025 if( ret == 0 )
1026 {
1027 /*
1028 * Was PEM encoded
1029 */
1030 buflen -= use_len;
1031 buf += use_len;
1032 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 {
1035 return( ret );
1036 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040
1041 /*
1042 * PEM header and footer were found
1043 */
1044 buflen -= use_len;
1045 buf += use_len;
1046
1047 if( first_error == 0 )
1048 first_error = ret;
1049
Paul Bakker5a5fa922014-09-26 14:53:04 +02001050 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051 continue;
1052 }
1053 else
1054 break;
1055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059
1060 if( ret != 0 )
1061 {
1062 /*
1063 * Quit parsing on a memory error
1064 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001065 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001066 return( ret );
1067
1068 if( first_error == 0 )
1069 first_error = ret;
1070
1071 total_failed++;
1072 continue;
1073 }
1074
1075 success = 1;
1076 }
1077 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079
1080 if( success )
1081 return( total_failed );
1082 else if( first_error )
1083 return( first_error );
1084 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086}
1087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089/*
1090 * Load one or more certificates and add them to the chained list
1091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093{
1094 int ret;
1095 size_t n;
1096 unsigned char *buf;
1097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099 return( ret );
1100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001103 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105
1106 return( ret );
1107}
1108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110{
1111 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001112#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113 int w_ret;
1114 WCHAR szDir[MAX_PATH];
1115 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001116 char *p;
Manuel Pégourié-Gonnard9dc66f42015-10-21 10:16:29 +02001117 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118
Paul Bakker9af723c2014-05-01 13:03:14 +02001119 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120 HANDLE hFind;
1121
1122 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124
Paul Bakker9af723c2014-05-01 13:03:14 +02001125 memset( szDir, 0, sizeof(szDir) );
1126 memset( filename, 0, MAX_PATH );
1127 memcpy( filename, path, len );
1128 filename[len++] = '\\';
1129 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001130 filename[len++] = '*';
1131
Simon B635f2152016-11-03 01:11:37 +00001132 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001133 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001134 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001136
1137 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001138 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140
1141 len = MAX_PATH - len;
1142 do
1143 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001144 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001145
1146 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1147 continue;
1148
Paul Bakker9af723c2014-05-01 13:03:14 +02001149 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001150 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard9dc66f42015-10-21 10:16:29 +02001151 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001152 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001153 if( w_ret == 0 )
Ron Eldor0fb3e0a2017-01-09 15:09:16 +02001154 {
1155 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1156 goto cleanup;
1157 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160 if( w_ret < 0 )
1161 ret++;
1162 else
1163 ret += w_ret;
1164 }
1165 while( FindNextFileW( hFind, &file_data ) != 0 );
1166
Paul Bakker66d5d072014-06-17 16:39:18 +02001167 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169
Ron Eldor0fb3e0a2017-01-09 15:09:16 +02001170cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001172#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001173 int t_ret;
Andres AG879e6262016-09-02 14:06:04 +01001174 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001175 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001176 struct dirent *entry;
Andres AG879e6262016-09-02 14:06:04 +01001177 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001178 DIR *dir = opendir( path );
1179
Paul Bakker66d5d072014-06-17 16:39:18 +02001180 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182
Ron Eldoree709f42017-01-09 19:27:59 +02001183#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001184 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001185 {
1186 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001187 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001188 }
Ron Eldoree709f42017-01-09 19:27:59 +02001189#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001190
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001191 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 {
Andres AG879e6262016-09-02 14:06:04 +01001193 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1194 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001195
Andres AG879e6262016-09-02 14:06:04 +01001196 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001197 {
Andres AG879e6262016-09-02 14:06:04 +01001198 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1199 goto cleanup;
1200 }
1201 else if( stat( entry_name, &sb ) == -1 )
1202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001204 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001205 }
1206
1207 if( !S_ISREG( sb.st_mode ) )
1208 continue;
1209
1210 // Ignore parse errors
1211 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001213 if( t_ret < 0 )
1214 ret++;
1215 else
1216 ret += t_ret;
1217 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001218
1219cleanup:
Andres AG879e6262016-09-02 14:06:04 +01001220 closedir( dir );
1221
Ron Eldoree709f42017-01-09 19:27:59 +02001222#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001223 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldoree709f42017-01-09 19:27:59 +02001225#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001226
Paul Bakkerbe089b02013-10-14 15:51:50 +02001227#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001228
1229 return( ret );
1230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001232
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001233static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001235{
1236 size_t i;
1237 size_t n = *size;
1238 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001240 const char *sep = "";
1241 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001242
1243 while( cur != NULL )
1244 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001245 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001246 {
1247 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001248 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001249 }
1250
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001251 n -= cur->buf.len + sep_len;
1252 for( i = 0; i < sep_len; i++ )
1253 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001254 for( i = 0; i < cur->buf.len; i++ )
1255 *p++ = cur->buf.p[i];
1256
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001257 sep = ", ";
1258 sep_len = 2;
1259
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001260 cur = cur->next;
1261 }
1262
1263 *p = '\0';
1264
1265 *size = n;
1266 *buf = p;
1267
1268 return( 0 );
1269}
1270
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001271#define PRINT_ITEM(i) \
1272 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001274 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001275 sep = ", "; \
1276 }
1277
1278#define CERT_TYPE(type,name) \
1279 if( ns_cert_type & type ) \
1280 PRINT_ITEM( name );
1281
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001282static int x509_info_cert_type( char **buf, size_t *size,
1283 unsigned char ns_cert_type )
1284{
1285 int ret;
1286 size_t n = *size;
1287 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001288 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001291 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1293 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1294 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001295 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1296 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1297 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001298
1299 *size = n;
1300 *buf = p;
1301
1302 return( 0 );
1303}
1304
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001305#define KEY_USAGE(code,name) \
1306 if( key_usage & code ) \
1307 PRINT_ITEM( name );
1308
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001309static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001310 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001311{
1312 int ret;
1313 size_t n = *size;
1314 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001315 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1318 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001319 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1320 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1321 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1323 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001324 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1325 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001326
1327 *size = n;
1328 *buf = p;
1329
1330 return( 0 );
1331}
1332
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001333static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001335{
1336 int ret;
1337 const char *desc;
1338 size_t n = *size;
1339 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001341 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001342
1343 while( cur != NULL )
1344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001346 desc = "???";
1347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001349 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001350
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001351 sep = ", ";
1352
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001353 cur = cur->next;
1354 }
1355
1356 *size = n;
1357 *buf = p;
1358
1359 return( 0 );
1360}
1361
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001362/*
1363 * Return an informational string about the certificate.
1364 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001365#define BEFORE_COLON 18
1366#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1368 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369{
1370 int ret;
1371 size_t n;
1372 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373 char key_size_str[BEFORE_COLON];
1374
1375 p = buf;
1376 n = size;
1377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001379 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001380 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001383 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001386 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001389 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001391 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001394 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001396 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001399 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1400 crt->valid_from.year, crt->valid_from.mon,
1401 crt->valid_from.day, crt->valid_from.hour,
1402 crt->valid_from.min, crt->valid_from.sec );
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%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1407 crt->valid_to.year, crt->valid_to.mon,
1408 crt->valid_to.day, crt->valid_to.hour,
1409 crt->valid_to.min, crt->valid_to.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%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001413 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001416 crt->sig_md, crt->sig_opts );
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é-Gonnardb28487d2014-04-01 12:19:09 +02001419 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1421 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001422 {
1423 return( ret );
1424 }
1425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001427 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001428 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001430 /*
1431 * Optional extensions
1432 */
1433
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001434 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001437 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001438 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001439
1440 if( crt->max_pathlen > 0 )
1441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001443 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001444 }
1445 }
1446
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001447 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001450 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001451
1452 if( ( ret = x509_info_subject_alt_name( &p, &n,
1453 &crt->subject_alt_names ) ) != 0 )
1454 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001455 }
1456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001460 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001461
1462 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1463 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001464 }
1465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001469 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001470
1471 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1472 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001473 }
1474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001478 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001479
1480 if( ( ret = x509_info_ext_key_usage( &p, &n,
1481 &crt->ext_key_usage ) ) != 0 )
1482 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001483 }
1484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001486 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001487
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488 return( (int) ( size - n ) );
1489}
1490
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001491struct x509_crt_verify_string {
1492 int code;
1493 const char *string;
1494};
1495
1496static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001497 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001498 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1499 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1500 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1501 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1502 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001503 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1504 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1505 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001506 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001507 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1508 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1509 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1510 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001511 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1512 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1513 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1514 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1515 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1516 { 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 +01001517 { 0, NULL }
1518};
1519
1520int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001521 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001522{
1523 int ret;
1524 const struct x509_crt_verify_string *cur;
1525 char *p = buf;
1526 size_t n = size;
1527
1528 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1529 {
1530 if( ( flags & cur->code ) == 0 )
1531 continue;
1532
1533 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001534 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001535 flags ^= cur->code;
1536 }
1537
1538 if( flags != 0 )
1539 {
1540 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1541 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001542 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001543 }
1544
1545 return( (int) ( size - n ) );
1546}
1547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001549int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1550 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001551{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001552 unsigned int usage_must, usage_may;
1553 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1554 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1555
1556 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1557 return( 0 );
1558
1559 usage_must = usage & ~may_mask;
1560
1561 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1562 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1563
1564 usage_may = usage & may_mask;
1565
1566 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001568
1569 return( 0 );
1570}
1571#endif
1572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1574int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001575 const char *usage_oid,
1576 size_t usage_len )
1577{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001579
1580 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001582 return( 0 );
1583
1584 /*
1585 * Look for the requested usage (or wildcard ANY) in our list
1586 */
1587 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001590
1591 if( cur_oid->len == usage_len &&
1592 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1593 {
1594 return( 0 );
1595 }
1596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001598 return( 0 );
1599 }
1600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001602}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001606/*
1607 * Return 1 if the certificate is revoked, or 0 otherwise.
1608 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001609int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001610{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612
1613 while( cur != NULL && cur->serial.len != 0 )
1614 {
1615 if( crt->serial.len == cur->serial.len &&
1616 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1617 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001618 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619 return( 1 );
1620 }
1621
1622 cur = cur->next;
1623 }
1624
1625 return( 0 );
1626}
1627
1628/*
Manuel Pégourié-Gonnard78df7fc2018-03-05 13:22:59 +01001629 * Check that the given certificate is not revoked according to the CRL.
1630 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001633 mbedtls_x509_crl *crl_list,
1634 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635{
1636 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1638 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639
1640 if( ca == NULL )
1641 return( flags );
1642
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001643 while( crl_list != NULL )
1644 {
1645 if( crl_list->version == 0 ||
1646 crl_list->issuer_raw.len != ca->subject_raw.len ||
1647 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1648 crl_list->issuer_raw.len ) != 0 )
1649 {
1650 crl_list = crl_list->next;
1651 continue;
1652 }
1653
1654 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001655 * Check if the CA is configured to sign CRLs
1656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1658 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001661 break;
1662 }
1663#endif
1664
1665 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001666 * Check if CRL is correctly signed by the trusted CA
1667 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001668 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1669 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1670
1671 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1672 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard081ed062017-06-26 12:22:17 +02001675 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001676 {
Manuel Pégourié-Gonnard081ed062017-06-26 12:22:17 +02001677 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679 break;
1680 }
1681
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001682 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1683 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1686 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001687 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001690 break;
1691 }
1692
1693 /*
1694 * Check for validity of CRL (Do not drop out)
1695 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001696 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001698
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001699 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001700 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001701
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702 /*
1703 * Check if certificate is revoked
1704 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001705 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001708 break;
1709 }
1710
1711 crl_list = crl_list->next;
1712 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001713
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001714 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001715}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001717
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001718/*
1719 * Like memcmp, but case-insensitive and always returns -1 if different
1720 */
1721static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001722{
1723 size_t i;
1724 unsigned char diff;
1725 const unsigned char *n1 = s1, *n2 = s2;
1726
1727 for( i = 0; i < len; i++ )
1728 {
1729 diff = n1[i] ^ n2[i];
1730
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001731 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001732 continue;
1733
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001734 if( diff == 32 &&
1735 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1736 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1737 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001738 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001739 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001740
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001741 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001742 }
1743
1744 return( 0 );
1745}
1746
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001747/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001748 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001749 */
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001750static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001751{
1752 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001753 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001754
1755 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1756 return( 0 );
1757
Paul Bakker14b16c62014-05-28 11:33:54 +02001758 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001759 {
1760 if( cn[i] == '.' )
1761 {
1762 cn_idx = i;
1763 break;
1764 }
1765 }
1766
1767 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001768 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001769
Paul Bakker14b16c62014-05-28 11:33:54 +02001770 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001771 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001772 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001773 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001774 }
1775
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001776 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001777}
1778
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001779/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001780 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1781 * variations (but not all).
1782 *
1783 * Return 0 if equal, -1 otherwise.
1784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001786{
1787 if( a->tag == b->tag &&
1788 a->len == b->len &&
1789 memcmp( a->p, b->p, b->len ) == 0 )
1790 {
1791 return( 0 );
1792 }
1793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1795 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001796 a->len == b->len &&
1797 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1798 {
1799 return( 0 );
1800 }
1801
1802 return( -1 );
1803}
1804
1805/*
1806 * Compare two X.509 Names (aka rdnSequence).
1807 *
1808 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1809 * we sometimes return unequal when the full algorithm would return equal,
1810 * but never the other way. (In particular, we don't do Unicode normalisation
1811 * or space folding.)
1812 *
1813 * Return 0 if equal, -1 otherwise.
1814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001816{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001817 /* Avoid recursion, it might not be optimised by the compiler */
1818 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001819 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001820 if( a == NULL || b == NULL )
1821 return( -1 );
1822
1823 /* type */
1824 if( a->oid.tag != b->oid.tag ||
1825 a->oid.len != b->oid.len ||
1826 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1827 {
1828 return( -1 );
1829 }
1830
1831 /* value */
1832 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1833 return( -1 );
1834
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001835 /* structure of the list of sets */
1836 if( a->next_merged != b->next_merged )
1837 return( -1 );
1838
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001839 a = a->next;
1840 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001841 }
1842
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001843 /* a == NULL == b */
1844 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001845}
1846
1847/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001848 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1849 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001850 *
1851 * top means parent is a locally-trusted certificate
1852 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1855 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001856 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001857{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001858 int need_ca_bit;
1859
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001860 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001861 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001862 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001863
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001864 /* Parent must have the basicConstraints CA bit set as a general rule */
1865 need_ca_bit = 1;
1866
1867 /* Exception: v1/v2 certificates that are locally trusted. */
1868 if( top && parent->version < 3 )
1869 need_ca_bit = 0;
1870
1871 /* Exception: self-signed end-entity certs that are locally trusted. */
1872 if( top && bottom &&
1873 child->raw.len == parent->raw.len &&
1874 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1875 {
1876 need_ca_bit = 0;
1877 }
1878
1879 if( need_ca_bit && ! parent->ca_istrue )
1880 return( -1 );
1881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001883 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001885 {
1886 return( -1 );
1887 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001888#endif
1889
1890 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001891}
1892
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001893/*
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01001894 * Verify a certificate with no parent inside the chain
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001895 * (either the parent is a trusted root, or there is no parent)
1896 *
1897 * See comments for mbedtls_x509_crt_verify_with_profile()
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01001898 * (also for notation used below)
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001899 *
1900 * This function is called in two cases:
1901 * - child was found to have a parent in trusted roots, in which case we're
1902 * called with trust_ca pointing directly to that parent (not the full list)
Manuel Pégourié-Gonnard19d77b62018-03-07 09:36:30 +01001903 * - this is cases 1, 2 and 3 of the comment on verify_with_profile()
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001904 * - case 1 is special as child and trust_ca point to copies of the same
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01001905 * certificate then
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001906 * - child was found to have no parent either in the chain or in trusted CAs
Manuel Pégourié-Gonnard19d77b62018-03-07 09:36:30 +01001907 * - this is cases 4 and 5 of the comment on verify_with_profile()
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001908 *
1909 * For historical reasons, the function currently does not assume that
1910 * trust_ca points directly to the right root in the first case, and it
1911 * doesn't know in which case it starts, so it always starts by searching for
1912 * a parent in trust_ca.
1913 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001914static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001916 mbedtls_x509_crl *ca_crl,
1917 const mbedtls_x509_crt_profile *profile,
Janos Follath860f2392015-10-12 09:02:20 +02001918 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001919 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001920 void *p_vrfy )
1921{
1922 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001923 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001924 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1926 const mbedtls_md_info_t *md_info;
Andres AG8136e822016-12-09 17:26:23 +00001927 mbedtls_x509_crt *future_past_ca = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001928
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001929 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001930 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001931
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001932 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001934
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001935 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
1936 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
1937
1938 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
1939 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
1940
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001941 /*
1942 * Child is the top of the chain. Check against the trust_ca list.
1943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 md_info = mbedtls_md_info_from_type( child->sig_md );
Manuel Pégourié-Gonnard081ed062017-06-26 12:22:17 +02001947 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001948 {
Manuel Pégourié-Gonnard081ed062017-06-26 12:22:17 +02001949 /* Note: this can't happen except after an internal error */
1950 /* Cannot check signature, no need to try any CA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001951 trust_ca = NULL;
1952 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001953
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001954 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001955 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001956 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001957 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001958
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001959 check_path_cnt = path_cnt + 1;
1960
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001961 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001962 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001963 * the same as the trusted CA
1964 */
1965 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1966 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Hanno Beckerdc8751d2017-09-25 10:47:58 +01001967 child->subject_raw.len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001968 {
1969 check_path_cnt--;
1970 }
1971
Janos Follath860f2392015-10-12 09:02:20 +02001972 /* Self signed certificates do not count towards the limit */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001973 if( trust_ca->max_pathlen > 0 &&
Janos Follath860f2392015-10-12 09:02:20 +02001974 trust_ca->max_pathlen < check_path_cnt - self_cnt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001975 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001976 continue;
1977 }
1978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1980 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001981 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001982 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001983 continue;
1984 }
1985
Andres AG8136e822016-12-09 17:26:23 +00001986 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) ||
1987 mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
1988 {
1989 if ( future_past_ca == NULL )
1990 future_past_ca = trust_ca;
1991
1992 continue;
1993 }
1994
1995 break;
1996 }
1997
1998 if( trust_ca != NULL || ( trust_ca = future_past_ca ) != NULL )
1999 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002000 /*
2001 * Top of chain is signed by a trusted CA
2002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardfa67eba2015-06-27 14:41:38 +02002004
2005 if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
2006 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002007 }
2008
2009 /*
2010 * If top of chain is not the same as the trusted CA send a verify request
2011 * to the callback for any issues with validity and CRL presence for the
2012 * trusted CA certificate.
2013 */
2014 if( trust_ca != NULL &&
2015 ( child->subject_raw.len != trust_ca->subject_raw.len ||
2016 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Hanno Beckerdc8751d2017-09-25 10:47:58 +01002017 child->subject_raw.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002020 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002021 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02002022#else
2023 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002024#endif
2025
Andres AG8136e822016-12-09 17:26:23 +00002026 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
2027 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
2028
2029 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
2030 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
2031
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002032 if( NULL != f_vrfy )
2033 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002034 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
2035 &ca_flags ) ) != 0 )
2036 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002037 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002038 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002039 }
2040 }
2041
2042 /* Call callback on top cert */
2043 if( NULL != f_vrfy )
2044 {
Paul Bakker66d5d072014-06-17 16:39:18 +02002045 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002046 return( ret );
2047 }
2048
2049 *flags |= ca_flags;
2050
2051 return( 0 );
2052}
2053
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002054/*
2055 * Verify a certificate with a parent inside the chain
2056 *
2057 * See comments for mbedtls_x509_crt_verify_with_profile()
2058 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002059static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002060 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
2061 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
2062 const mbedtls_x509_crt_profile *profile,
Janos Follath860f2392015-10-12 09:02:20 +02002063 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002064 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002065 void *p_vrfy )
2066{
2067 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002068 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2070 mbedtls_x509_crt *grandparent;
2071 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002072
Janos Follath860f2392015-10-12 09:02:20 +02002073 /* Counting intermediate self signed certificates */
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002074 if( ( path_cnt != 0 ) && x509_name_cmp( &child->issuer, &child->subject ) == 0 )
Janos Follath860f2392015-10-12 09:02:20 +02002075 self_cnt++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002076
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002077 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002079 {
Manuel Pégourié-Gonnardc3863172017-06-26 10:11:49 +02002080 /* return immediately as the goal is to avoid unbounded recursion */
2081 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002082 }
2083
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002084 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002085 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02002086
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002087 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002089
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002090 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2091 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2092
2093 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2094 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 md_info = mbedtls_md_info_from_type( child->sig_md );
Manuel Pégourié-Gonnardac54cea2018-03-07 09:41:20 +01002097 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002098 {
Manuel Pégourié-Gonnardac54cea2018-03-07 09:41:20 +01002099 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002101 }
2102 else
2103 {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002104 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2105 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2108 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002109 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112 }
2113 }
2114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002116 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002117 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118#endif
2119
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002120 /* Look for a grandparent in trusted CAs */
2121 for( grandparent = trust_ca;
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002122 grandparent != NULL;
2123 grandparent = grandparent->next )
2124 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002125 if( x509_crt_check_parent( parent, grandparent,
2126 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002127 break;
2128 }
2129
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002130 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002132 ret = x509_crt_verify_top( parent, grandparent, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002133 path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002134 if( ret != 0 )
2135 return( ret );
2136 }
2137 else
2138 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002139 /* Look for a grandparent upwards the chain */
2140 for( grandparent = parent->next;
2141 grandparent != NULL;
2142 grandparent = grandparent->next )
2143 {
Janos Follath860f2392015-10-12 09:02:20 +02002144 /* +2 because the current step is not yet accounted for
2145 * and because max_pathlen is one higher than it should be.
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002146 * Also self signed certificates do not count to the limit. */
Janos Follath860f2392015-10-12 09:02:20 +02002147 if( grandparent->max_pathlen > 0 &&
2148 grandparent->max_pathlen < 2 + path_cnt - self_cnt )
2149 {
2150 continue;
2151 }
2152
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002153 if( x509_crt_check_parent( parent, grandparent,
2154 0, path_cnt == 0 ) == 0 )
2155 break;
2156 }
2157
2158 /* Is our parent part of the chain or at the top? */
2159 if( grandparent != NULL )
2160 {
2161 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
Janos Follath860f2392015-10-12 09:02:20 +02002162 profile, path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002163 f_vrfy, p_vrfy );
2164 if( ret != 0 )
2165 return( ret );
2166 }
2167 else
2168 {
2169 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002170 path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002171 f_vrfy, p_vrfy );
2172 if( ret != 0 )
2173 return( ret );
2174 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002175 }
2176
2177 /* child is verified to be a child of the parent, call verify callback */
2178 if( NULL != f_vrfy )
2179 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2180 return( ret );
2181
2182 *flags |= parent_flags;
2183
2184 return( 0 );
2185}
2186
2187/*
2188 * Verify the certificate validity
2189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2191 mbedtls_x509_crt *trust_ca,
2192 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002193 const char *cn, uint32_t *flags,
2194 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002195 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002196{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002197 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002198 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002199}
2200
2201
2202/*
2203 * Verify the certificate validity, with profile
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002204 *
2205 * The chain building/verification is spread accross 4 functions:
2206 * - this one
2207 * - x509_crt_verify_child()
2208 * - x509_crt_verify_top()
2209 * - x509_crt_check_parent()
2210 *
2211 * There are five main cases to consider. Let's introduce some notation:
2212 * - E means the end-entity certificate
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01002213 * - I an intermediate CA
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002214 * - R the trusted root CA this chain anchors to
2215 * - T the list of trusted roots (R and possible some others)
2216 *
2217 * The main cases with the calling sequence of the crt_verify_xxx() are:
2218 * 1. E = R (explicitly trusted EE cert)
2219 * verify(E, T) -> verify_top(E, R)
2220 * 2. E -> R (EE signed by trusted root)
2221 * verify(E, T) -> verify_top(E, R)
2222 * 3. E -> I -> R (EE signed by intermediate signed by trusted root)
2223 * verify(E, T) -> verify_child(E, I, T) -> verify_top(I, R)
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01002224 * (plus variant with multiple intermediates)
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002225 * 4. E -> I (EE signed by intermediate that's not trusted)
2226 * verify(E, T) -> verify_child(E, I, T) -> verify_top(I, T)
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01002227 * (plus variant with multiple intermediates)
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002228 * 5. E (EE not trusted)
2229 * verify(E, T) -> verify_top(E, T)
Manuel Pégourié-Gonnard19d77b62018-03-07 09:36:30 +01002230 *
2231 * Note: this notation and case numbering is also used in x509_crt_verify_top()
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002232 */
2233int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2234 mbedtls_x509_crt *trust_ca,
2235 mbedtls_x509_crl *ca_crl,
2236 const mbedtls_x509_crt_profile *profile,
2237 const char *cn, uint32_t *flags,
2238 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2239 void *p_vrfy )
2240{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002241 size_t cn_len;
2242 int ret;
Janos Follath860f2392015-10-12 09:02:20 +02002243 int pathlen = 0, selfsigned = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 mbedtls_x509_crt *parent;
2245 mbedtls_x509_name *name;
2246 mbedtls_x509_sequence *cur = NULL;
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +02002247 mbedtls_pk_type_t pk_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002248
2249 *flags = 0;
2250
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002251 if( profile == NULL )
2252 {
2253 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2254 goto exit;
2255 }
2256
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002257 if( cn != NULL )
2258 {
2259 name = &crt->subject;
2260 cn_len = strlen( cn );
2261
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002262 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002263 {
2264 cur = &crt->subject_alt_names;
2265
2266 while( cur != NULL )
2267 {
2268 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002269 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002270 break;
2271
2272 if( cur->buf.len > 2 &&
2273 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002274 x509_check_wildcard( cn, &cur->buf ) == 0 )
2275 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002276 break;
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002277 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002278
2279 cur = cur->next;
2280 }
2281
2282 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002284 }
2285 else
2286 {
2287 while( name != NULL )
2288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002290 {
2291 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002292 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002293 break;
2294
2295 if( name->val.len > 2 &&
2296 memcmp( name->val.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002297 x509_check_wildcard( cn, &name->val ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002298 break;
2299 }
2300
2301 name = name->next;
2302 }
2303
2304 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002306 }
2307 }
2308
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +02002309 /* Check the type and size of the key */
2310 pk_type = mbedtls_pk_get_type( &crt->pk );
2311
2312 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
2313 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2314
2315 if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
2316 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2317
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002318 /* Look for a parent in trusted CAs */
2319 for( parent = trust_ca; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002320 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002321 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002322 break;
2323 }
2324
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002325 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002326 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002327 ret = x509_crt_verify_top( crt, parent, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002328 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002329 if( ret != 0 )
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002330 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002331 }
2332 else
2333 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002334 /* Look for a parent upwards the chain */
2335 for( parent = crt->next; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002336 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
2337 break;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002338
2339 /* Are we part of the chain or at the top? */
2340 if( parent != NULL )
2341 {
2342 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002343 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002344 if( ret != 0 )
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002345 goto exit;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002346 }
2347 else
2348 {
2349 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
Janos Follath860f2392015-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é-Gonnard489939f2017-06-22 12:19:27 +02002352 goto exit;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002353 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002354 }
2355
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002356exit:
Manuel Pégourié-Gonnardcdb4dc92017-07-06 12:16:25 +02002357 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2358 * the SSL module for authmode optional, but non-zero return from the
2359 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnardc3863172017-06-26 10:11:49 +02002360 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2361 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2362
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002363 if( ret != 0 )
2364 {
2365 *flags = (uint32_t) -1;
2366 return( ret );
2367 }
2368
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002369 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002371
2372 return( 0 );
2373}
2374
2375/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002376 * Initialize a certificate chain
2377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002379{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002381}
2382
2383/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002384 * Unallocate all certificate data
2385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 mbedtls_x509_crt *cert_cur = crt;
2389 mbedtls_x509_crt *cert_prv;
2390 mbedtls_x509_name *name_cur;
2391 mbedtls_x509_name *name_prv;
2392 mbedtls_x509_sequence *seq_cur;
2393 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002394
2395 if( crt == NULL )
2396 return;
2397
2398 do
2399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2403 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002404#endif
2405
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002406 name_cur = cert_cur->issuer.next;
2407 while( name_cur != NULL )
2408 {
2409 name_prv = name_cur;
2410 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2412 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002413 }
2414
2415 name_cur = cert_cur->subject.next;
2416 while( name_cur != NULL )
2417 {
2418 name_prv = name_cur;
2419 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2421 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002422 }
2423
2424 seq_cur = cert_cur->ext_key_usage.next;
2425 while( seq_cur != NULL )
2426 {
2427 seq_prv = seq_cur;
2428 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2430 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002431 }
2432
2433 seq_cur = cert_cur->subject_alt_names.next;
2434 while( seq_cur != NULL )
2435 {
2436 seq_prv = seq_cur;
2437 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2439 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002440 }
2441
2442 if( cert_cur->raw.p != NULL )
2443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2445 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002446 }
2447
2448 cert_cur = cert_cur->next;
2449 }
2450 while( cert_cur != NULL );
2451
2452 cert_cur = crt;
2453 do
2454 {
2455 cert_prv = cert_cur;
2456 cert_cur = cert_cur->next;
2457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002459 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002461 }
2462 while( cert_cur != NULL );
2463}
2464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465#endif /* MBEDTLS_X509_CRT_PARSE_C */