blob: 1eaa55bcdc9599021ad959d5a4a3c42fb897db75 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 */
21/*
22 * The ITU-T X.509 standard defines a certificate format for PKI.
23 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020024 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
30 */
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/x509_crt.h"
41#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000042
43#include <stdio.h>
44#include <string.h>
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020048#endif
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052#else
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020055#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010061#endif
62
Paul Bakkerfa6a6202013-10-28 18:48:30 +010063#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064#include <windows.h>
65#else
66#include <time.h>
67#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000070#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020071#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#include <sys/types.h>
73#include <sys/stat.h>
74#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000075#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#endif
77
Paul Bakker34617722014-06-13 17:20:13 +020078/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020080 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
81}
82
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020084 * Default profile
85 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020086const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
87{
Gilles Peskine7344e1b2017-05-12 13:16:40 +020088#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine955738a2017-05-04 16:17:21 +020089 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020090 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine955738a2017-05-04 16:17:21 +020091#endif
92 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020093 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
94 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
95 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
96 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
97 0xFFFFFFF, /* Any PK alg */
98 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020099 2048,
100};
101
102/*
103 * Next-default profile
104 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200105const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
106{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200107 /* Hashes from SHA-256 and above */
108 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
110 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
111 0xFFFFFFF, /* Any PK alg */
112#if defined(MBEDTLS_ECP_C)
113 /* Curves at or above 128-bit security level */
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
120 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
121#else
122 0,
123#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200124 2048,
125};
126
127/*
128 * NSA Suite B Profile
129 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200130const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
131{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200132 /* Only SHA-256 and 384 */
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
135 /* Only ECDSA */
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 */
570 extn_oid.tag = **p;
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
573 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574
575 extn_oid.p = *p;
576 *p += extn_oid.len;
577
578 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
580 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581
582 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
584 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
585 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586
587 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
589 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
590 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591
592 end_ext_octet = *p + len;
593
594 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
596 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597
598 /*
599 * Detect supported extensions
600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602
603 if( ret != 0 )
604 {
605 /* No parser found, skip extension */
606 *p = end_ext_octet;
607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200609 if( is_critical )
610 {
611 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
613 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614 }
615#endif
616 continue;
617 }
618
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100619 /* Forbid repeated extensions */
620 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100622
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623 crt->ext_types |= ext_type;
624
625 switch( ext_type )
626 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100627 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628 /* Parse basic constraints */
629 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
630 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200631 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632 break;
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 /* Parse key usage */
636 if( ( ret = x509_get_key_usage( p, end_ext_octet,
637 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200638 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639 break;
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 /* Parse extended key usage */
643 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
644 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200645 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646 break;
647
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100648 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649 /* Parse subject alt name */
650 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
651 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200652 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653 break;
654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656 /* Parse netscape certificate type */
657 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
658 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200659 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660 break;
661
662 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664 }
665 }
666
667 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
669 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670
671 return( 0 );
672}
673
674/*
675 * Parse and fill a single X.509 certificate in DER format
676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200678 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200679{
680 int ret;
681 size_t len;
682 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
686 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
687 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688
689 /*
690 * Check for valid input
691 */
692 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694
Janos Follath16734f02016-02-17 14:34:12 +0000695 // Use the original buffer until we figure out actual length
696 p = (unsigned char*) buf;
697 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698 end = p + len;
699
700 /*
701 * Certificate ::= SEQUENCE {
702 * tbsCertificate TBSCertificate,
703 * signatureAlgorithm AlgorithmIdentifier,
704 * signatureValue BIT STRING }
705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
707 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_x509_crt_free( crt );
710 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711 }
712
713 if( len > (size_t) ( end - p ) )
714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_x509_crt_free( crt );
716 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
717 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718 }
719 crt_end = p + len;
720
Janos Follath16734f02016-02-17 14:34:12 +0000721 // Create and populate a new buffer for the raw field
722 crt->raw.len = crt_end - buf;
723 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
724 if( p == NULL )
725 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
726
727 memcpy( p, buf, crt->raw.len );
728
Hanno Beckerdc8751d2017-09-25 10:47:58 +0100729 // Direct pointers to the new buffer
Janos Follath16734f02016-02-17 14:34:12 +0000730 p += crt->raw.len - len;
731 end = crt_end = p + len;
732
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733 /*
734 * TBSCertificate ::= SEQUENCE {
735 */
736 crt->tbs.p = p;
737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
739 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_x509_crt_free( crt );
742 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200743 }
744
745 end = p + len;
746 crt->tbs.len = end - crt->tbs.p;
747
748 /*
749 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
750 *
751 * CertificateSerialNumber ::= INTEGER
752 *
753 * signature AlgorithmIdentifier
754 */
755 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
757 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200758 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761 return( ret );
762 }
763
Andres AG1f06d9b2017-03-09 16:16:11 +0000764 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_x509_crt_free( crt );
767 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 }
769
Andres AG1f06d9b2017-03-09 16:16:11 +0000770 crt->version++;
771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200773 &crt->sig_md, &crt->sig_pk,
774 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777 return( ret );
778 }
779
780 /*
781 * issuer Name
782 */
783 crt->issuer_raw.p = p;
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
786 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_x509_crt_free( crt );
789 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200790 }
791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200795 return( ret );
796 }
797
798 crt->issuer_raw.len = p - crt->issuer_raw.p;
799
800 /*
801 * Validity ::= SEQUENCE {
802 * notBefore Time,
803 * notAfter Time }
804 *
805 */
806 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
807 &crt->valid_to ) ) != 0 )
808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200810 return( ret );
811 }
812
813 /*
814 * subject Name
815 */
816 crt->subject_raw.p = p;
817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
819 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 mbedtls_x509_crt_free( crt );
822 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823 }
824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200828 return( ret );
829 }
830
831 crt->subject_raw.len = p - crt->subject_raw.p;
832
833 /*
834 * SubjectPublicKeyInfo
835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839 return( ret );
840 }
841
842 /*
843 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
844 * -- If present, version shall be v2 or v3
845 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
846 * -- If present, version shall be v2 or v3
847 * extensions [3] EXPLICIT Extensions OPTIONAL
848 * -- If present, version shall be v3
849 */
850 if( crt->version == 2 || crt->version == 3 )
851 {
852 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
853 if( ret != 0 )
854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856 return( ret );
857 }
858 }
859
860 if( crt->version == 2 || crt->version == 3 )
861 {
862 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
863 if( ret != 0 )
864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866 return( ret );
867 }
868 }
869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200871 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200872#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100873 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200874 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200875 if( ret != 0 )
876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878 return( ret );
879 }
880 }
881
882 if( p != end )
883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 mbedtls_x509_crt_free( crt );
885 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
886 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 }
888
889 end = crt_end;
890
891 /*
892 * }
893 * -- end of TBSCertificate
894 *
895 * signatureAlgorithm AlgorithmIdentifier,
896 * signatureValue BIT STRING
897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901 return( ret );
902 }
903
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100904 if( crt->sig_oid.len != sig_oid2.len ||
905 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200906 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200907 ( sig_params1.len != 0 &&
908 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_x509_crt_free( crt );
911 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912 }
913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917 return( ret );
918 }
919
920 if( p != end )
921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 mbedtls_x509_crt_free( crt );
923 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
924 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925 }
926
927 return( 0 );
928}
929
930/*
931 * Parse one X.509 certificate in DER format from a buffer and add them to a
932 * chained list
933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200935 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936{
937 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939
940 /*
941 * Check for valid input
942 */
943 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945
946 while( crt->version != 0 && crt->next != NULL )
947 {
948 prev = crt;
949 crt = crt->next;
950 }
951
952 /*
953 * Add new certificate on the end of the chain if needed.
954 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200955 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200957 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958
959 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200960 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961
962 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965 }
966
Paul Bakkerddf26b42013-09-18 13:46:23 +0200967 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968 {
969 if( prev )
970 prev->next = NULL;
971
972 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974
975 return( ret );
976 }
977
978 return( 0 );
979}
980
981/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200982 * Parse one or more PEM certificates from a buffer and add them to the chained
983 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986{
987 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 int buf_format = MBEDTLS_X509_FORMAT_DER;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989
990 /*
991 * Check for valid input
992 */
993 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995
996 /*
997 * Determine buffer content. Buffer contains either one DER certificate or
998 * one or more PEM certificates.
999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001001 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001002 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001005 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006#endif
1007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1009 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#if defined(MBEDTLS_PEM_PARSE_C)
1012 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 {
1014 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001017 /* 1 rather than 0 since the terminating NULL byte is counted in */
1018 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 {
1020 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001022
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001023 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025 "-----BEGIN CERTIFICATE-----",
1026 "-----END CERTIFICATE-----",
1027 buf, NULL, 0, &use_len );
1028
1029 if( ret == 0 )
1030 {
1031 /*
1032 * Was PEM encoded
1033 */
1034 buflen -= use_len;
1035 buf += use_len;
1036 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038 {
1039 return( ret );
1040 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044
1045 /*
1046 * PEM header and footer were found
1047 */
1048 buflen -= use_len;
1049 buf += use_len;
1050
1051 if( first_error == 0 )
1052 first_error = ret;
1053
Paul Bakker5a5fa922014-09-26 14:53:04 +02001054 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055 continue;
1056 }
1057 else
1058 break;
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063
1064 if( ret != 0 )
1065 {
1066 /*
1067 * Quit parsing on a memory error
1068 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001069 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070 return( ret );
1071
1072 if( first_error == 0 )
1073 first_error = ret;
1074
1075 total_failed++;
1076 continue;
1077 }
1078
1079 success = 1;
1080 }
1081 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083
1084 if( success )
1085 return( total_failed );
1086 else if( first_error )
1087 return( first_error );
1088 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090}
1091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093/*
1094 * Load one or more certificates and add them to the chained list
1095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097{
1098 int ret;
1099 size_t n;
1100 unsigned char *buf;
1101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103 return( ret );
1104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001107 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109
1110 return( ret );
1111}
1112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114{
1115 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001116#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117 int w_ret;
1118 WCHAR szDir[MAX_PATH];
1119 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001120 char *p;
Manuel Pégourié-Gonnard9dc66f42015-10-21 10:16:29 +02001121 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122
Paul Bakker9af723c2014-05-01 13:03:14 +02001123 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124 HANDLE hFind;
1125
1126 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128
Paul Bakker9af723c2014-05-01 13:03:14 +02001129 memset( szDir, 0, sizeof(szDir) );
1130 memset( filename, 0, MAX_PATH );
1131 memcpy( filename, path, len );
1132 filename[len++] = '\\';
1133 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001134 filename[len++] = '*';
1135
Simon B635f2152016-11-03 01:11:37 +00001136 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001137 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001138 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140
1141 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001142 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001144
1145 len = MAX_PATH - len;
1146 do
1147 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001148 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149
1150 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1151 continue;
1152
Paul Bakker9af723c2014-05-01 13:03:14 +02001153 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001154 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard9dc66f42015-10-21 10:16:29 +02001155 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001156 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001157 if( w_ret == 0 )
Ron Eldor0fb3e0a2017-01-09 15:09:16 +02001158 {
1159 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1160 goto cleanup;
1161 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164 if( w_ret < 0 )
1165 ret++;
1166 else
1167 ret += w_ret;
1168 }
1169 while( FindNextFileW( hFind, &file_data ) != 0 );
1170
Paul Bakker66d5d072014-06-17 16:39:18 +02001171 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173
Ron Eldor0fb3e0a2017-01-09 15:09:16 +02001174cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001175 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001176#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001177 int t_ret;
Andres AG879e6262016-09-02 14:06:04 +01001178 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001179 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001180 struct dirent *entry;
Andres AG879e6262016-09-02 14:06:04 +01001181 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 DIR *dir = opendir( path );
1183
Paul Bakker66d5d072014-06-17 16:39:18 +02001184 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186
Ron Eldoree709f42017-01-09 19:27:59 +02001187#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001188 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001189 {
1190 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001191 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001192 }
Ron Eldoree709f42017-01-09 19:27:59 +02001193#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001194
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001195 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001196 {
Andres AG879e6262016-09-02 14:06:04 +01001197 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1198 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001199
Andres AG879e6262016-09-02 14:06:04 +01001200 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001201 {
Andres AG879e6262016-09-02 14:06:04 +01001202 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1203 goto cleanup;
1204 }
1205 else if( stat( entry_name, &sb ) == -1 )
1206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001208 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209 }
1210
1211 if( !S_ISREG( sb.st_mode ) )
1212 continue;
1213
1214 // Ignore parse errors
1215 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001217 if( t_ret < 0 )
1218 ret++;
1219 else
1220 ret += t_ret;
1221 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001222
1223cleanup:
Andres AG879e6262016-09-02 14:06:04 +01001224 closedir( dir );
1225
Ron Eldoree709f42017-01-09 19:27:59 +02001226#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001227 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldoree709f42017-01-09 19:27:59 +02001229#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001230
Paul Bakkerbe089b02013-10-14 15:51:50 +02001231#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001232
1233 return( ret );
1234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001236
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001237static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001239{
1240 size_t i;
1241 size_t n = *size;
1242 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001244 const char *sep = "";
1245 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001246
1247 while( cur != NULL )
1248 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001249 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001250 {
1251 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001252 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001253 }
1254
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001255 n -= cur->buf.len + sep_len;
1256 for( i = 0; i < sep_len; i++ )
1257 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001258 for( i = 0; i < cur->buf.len; i++ )
1259 *p++ = cur->buf.p[i];
1260
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001261 sep = ", ";
1262 sep_len = 2;
1263
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001264 cur = cur->next;
1265 }
1266
1267 *p = '\0';
1268
1269 *size = n;
1270 *buf = p;
1271
1272 return( 0 );
1273}
1274
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001275#define PRINT_ITEM(i) \
1276 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001278 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001279 sep = ", "; \
1280 }
1281
1282#define CERT_TYPE(type,name) \
1283 if( ns_cert_type & type ) \
1284 PRINT_ITEM( name );
1285
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001286static int x509_info_cert_type( char **buf, size_t *size,
1287 unsigned char ns_cert_type )
1288{
1289 int ret;
1290 size_t n = *size;
1291 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001292 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001295 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1297 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1298 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001299 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1300 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1301 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001302
1303 *size = n;
1304 *buf = p;
1305
1306 return( 0 );
1307}
1308
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001309#define KEY_USAGE(code,name) \
1310 if( key_usage & code ) \
1311 PRINT_ITEM( name );
1312
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001313static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001314 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001315{
1316 int ret;
1317 size_t n = *size;
1318 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001319 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1322 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001323 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1324 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1325 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1327 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001328 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1329 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001330
1331 *size = n;
1332 *buf = p;
1333
1334 return( 0 );
1335}
1336
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001337static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001339{
1340 int ret;
1341 const char *desc;
1342 size_t n = *size;
1343 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001345 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001346
1347 while( cur != NULL )
1348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001350 desc = "???";
1351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001353 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001354
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001355 sep = ", ";
1356
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001357 cur = cur->next;
1358 }
1359
1360 *size = n;
1361 *buf = p;
1362
1363 return( 0 );
1364}
1365
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366/*
1367 * Return an informational string about the certificate.
1368 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001369#define BEFORE_COLON 18
1370#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1372 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373{
1374 int ret;
1375 size_t n;
1376 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001377 char key_size_str[BEFORE_COLON];
1378
1379 p = buf;
1380 n = size;
1381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001384 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001386 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001387 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001390 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001393 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001395 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001398 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001400 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001403 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1404 crt->valid_from.year, crt->valid_from.mon,
1405 crt->valid_from.day, crt->valid_from.hour,
1406 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001407 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001410 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1411 crt->valid_to.year, crt->valid_to.mon,
1412 crt->valid_to.day, crt->valid_to.hour,
1413 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001414 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001417 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001420 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001421 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001422
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001423 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1425 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001426 {
1427 return( ret );
1428 }
1429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001431 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001432 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001434 /*
1435 * Optional extensions
1436 */
1437
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001438 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001441 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001442 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001443
1444 if( crt->max_pathlen > 0 )
1445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001447 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001448 }
1449 }
1450
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001451 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001454 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001455
1456 if( ( ret = x509_info_subject_alt_name( &p, &n,
1457 &crt->subject_alt_names ) ) != 0 )
1458 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001459 }
1460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001464 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001465
1466 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1467 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001468 }
1469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001473 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001474
1475 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1476 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001477 }
1478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001482 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001483
1484 if( ( ret = x509_info_ext_key_usage( &p, &n,
1485 &crt->ext_key_usage ) ) != 0 )
1486 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001487 }
1488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001490 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001491
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001492 return( (int) ( size - n ) );
1493}
1494
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001495struct x509_crt_verify_string {
1496 int code;
1497 const char *string;
1498};
1499
1500static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001501 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001502 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1503 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1504 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1505 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1506 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001507 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1508 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1509 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001510 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001511 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1512 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1513 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1514 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001515 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1516 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1517 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1518 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1519 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1520 { 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 +01001521 { 0, NULL }
1522};
1523
1524int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001525 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001526{
1527 int ret;
1528 const struct x509_crt_verify_string *cur;
1529 char *p = buf;
1530 size_t n = size;
1531
1532 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1533 {
1534 if( ( flags & cur->code ) == 0 )
1535 continue;
1536
1537 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001538 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001539 flags ^= cur->code;
1540 }
1541
1542 if( flags != 0 )
1543 {
1544 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1545 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001546 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001547 }
1548
1549 return( (int) ( size - n ) );
1550}
1551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001553int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1554 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001555{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001556 unsigned int usage_must, usage_may;
1557 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1558 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1559
1560 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1561 return( 0 );
1562
1563 usage_must = usage & ~may_mask;
1564
1565 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1566 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1567
1568 usage_may = usage & may_mask;
1569
1570 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001572
1573 return( 0 );
1574}
1575#endif
1576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1578int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001579 const char *usage_oid,
1580 size_t usage_len )
1581{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001583
1584 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001586 return( 0 );
1587
1588 /*
1589 * Look for the requested usage (or wildcard ANY) in our list
1590 */
1591 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001594
1595 if( cur_oid->len == usage_len &&
1596 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1597 {
1598 return( 0 );
1599 }
1600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001602 return( 0 );
1603 }
1604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001606}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001610/*
1611 * Return 1 if the certificate is revoked, or 0 otherwise.
1612 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001613int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616
1617 while( cur != NULL && cur->serial.len != 0 )
1618 {
1619 if( crt->serial.len == cur->serial.len &&
1620 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1621 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001622 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001623 return( 1 );
1624 }
1625
1626 cur = cur->next;
1627 }
1628
1629 return( 0 );
1630}
1631
1632/*
Manuel Pégourié-Gonnard78df7fc2018-03-05 13:22:59 +01001633 * Check that the given certificate is not revoked according to the CRL.
1634 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001637 mbedtls_x509_crl *crl_list,
1638 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639{
1640 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1642 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001643
1644 if( ca == NULL )
1645 return( flags );
1646
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001647 while( crl_list != NULL )
1648 {
1649 if( crl_list->version == 0 ||
1650 crl_list->issuer_raw.len != ca->subject_raw.len ||
1651 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1652 crl_list->issuer_raw.len ) != 0 )
1653 {
1654 crl_list = crl_list->next;
1655 continue;
1656 }
1657
1658 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001659 * Check if the CA is configured to sign CRLs
1660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1662 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001665 break;
1666 }
1667#endif
1668
1669 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670 * Check if CRL is correctly signed by the trusted CA
1671 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001672 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1673 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1674
1675 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1676 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard081ed062017-06-26 12:22:17 +02001679 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680 {
Manuel Pégourié-Gonnard081ed062017-06-26 12:22:17 +02001681 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001683 break;
1684 }
1685
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001686 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1687 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1690 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001691 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001694 break;
1695 }
1696
1697 /*
1698 * Check for validity of CRL (Do not drop out)
1699 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001700 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001703 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001704 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001705
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001706 /*
1707 * Check if certificate is revoked
1708 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001709 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001712 break;
1713 }
1714
1715 crl_list = crl_list->next;
1716 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001717
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001718 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001719}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001722/*
1723 * Like memcmp, but case-insensitive and always returns -1 if different
1724 */
1725static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001726{
1727 size_t i;
1728 unsigned char diff;
1729 const unsigned char *n1 = s1, *n2 = s2;
1730
1731 for( i = 0; i < len; i++ )
1732 {
1733 diff = n1[i] ^ n2[i];
1734
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001735 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736 continue;
1737
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001738 if( diff == 32 &&
1739 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1740 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1741 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001742 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001743 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001744
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001745 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001746 }
1747
1748 return( 0 );
1749}
1750
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001751/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001752 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001753 */
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001754static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001755{
1756 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001757 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001758
1759 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1760 return( 0 );
1761
Paul Bakker14b16c62014-05-28 11:33:54 +02001762 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001763 {
1764 if( cn[i] == '.' )
1765 {
1766 cn_idx = i;
1767 break;
1768 }
1769 }
1770
1771 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001772 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001773
Paul Bakker14b16c62014-05-28 11:33:54 +02001774 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001775 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001776 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001777 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001778 }
1779
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001780 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001781}
1782
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001783/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001784 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1785 * variations (but not all).
1786 *
1787 * Return 0 if equal, -1 otherwise.
1788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001790{
1791 if( a->tag == b->tag &&
1792 a->len == b->len &&
1793 memcmp( a->p, b->p, b->len ) == 0 )
1794 {
1795 return( 0 );
1796 }
1797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1799 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001800 a->len == b->len &&
1801 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1802 {
1803 return( 0 );
1804 }
1805
1806 return( -1 );
1807}
1808
1809/*
1810 * Compare two X.509 Names (aka rdnSequence).
1811 *
1812 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1813 * we sometimes return unequal when the full algorithm would return equal,
1814 * but never the other way. (In particular, we don't do Unicode normalisation
1815 * or space folding.)
1816 *
1817 * Return 0 if equal, -1 otherwise.
1818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001820{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001821 /* Avoid recursion, it might not be optimised by the compiler */
1822 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001823 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001824 if( a == NULL || b == NULL )
1825 return( -1 );
1826
1827 /* type */
1828 if( a->oid.tag != b->oid.tag ||
1829 a->oid.len != b->oid.len ||
1830 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1831 {
1832 return( -1 );
1833 }
1834
1835 /* value */
1836 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1837 return( -1 );
1838
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001839 /* structure of the list of sets */
1840 if( a->next_merged != b->next_merged )
1841 return( -1 );
1842
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001843 a = a->next;
1844 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001845 }
1846
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001847 /* a == NULL == b */
1848 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001849}
1850
1851/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001852 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1853 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001854 *
1855 * top means parent is a locally-trusted certificate
1856 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1859 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001860 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001861{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001862 int need_ca_bit;
1863
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001864 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001865 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001866 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001867
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001868 /* Parent must have the basicConstraints CA bit set as a general rule */
1869 need_ca_bit = 1;
1870
1871 /* Exception: v1/v2 certificates that are locally trusted. */
1872 if( top && parent->version < 3 )
1873 need_ca_bit = 0;
1874
1875 /* Exception: self-signed end-entity certs that are locally trusted. */
1876 if( top && bottom &&
1877 child->raw.len == parent->raw.len &&
1878 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1879 {
1880 need_ca_bit = 0;
1881 }
1882
1883 if( need_ca_bit && ! parent->ca_istrue )
1884 return( -1 );
1885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001887 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001889 {
1890 return( -1 );
1891 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001892#endif
1893
1894 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001895}
1896
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001897/*
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01001898 * Verify a certificate with no parent inside the chain
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001899 * (either the parent is a trusted root, or there is no parent)
1900 *
1901 * See comments for mbedtls_x509_crt_verify_with_profile()
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01001902 * (also for notation used below)
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001903 *
1904 * This function is called in two cases:
1905 * - child was found to have a parent in trusted roots, in which case we're
1906 * called with trust_ca pointing directly to that parent (not the full list)
Manuel Pégourié-Gonnard19d77b62018-03-07 09:36:30 +01001907 * - this is cases 1, 2 and 3 of the comment on verify_with_profile()
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001908 * - case 1 is special as child and trust_ca point to copies of the same
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01001909 * certificate then
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001910 * - child was found to have no parent either in the chain or in trusted CAs
Manuel Pégourié-Gonnard19d77b62018-03-07 09:36:30 +01001911 * - this is cases 4 and 5 of the comment on verify_with_profile()
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001912 *
1913 * For historical reasons, the function currently does not assume that
1914 * trust_ca points directly to the right root in the first case, and it
1915 * doesn't know in which case it starts, so it always starts by searching for
1916 * a parent in trust_ca.
1917 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001918static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001920 mbedtls_x509_crl *ca_crl,
1921 const mbedtls_x509_crt_profile *profile,
Janos Follath860f2392015-10-12 09:02:20 +02001922 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001923 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001924 void *p_vrfy )
1925{
1926 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001927 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001928 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1930 const mbedtls_md_info_t *md_info;
Andres AG8136e822016-12-09 17:26:23 +00001931 mbedtls_x509_crt *future_past_ca = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001932
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001933 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001934 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001935
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001936 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001938
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001939 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
1940 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
1941
1942 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
1943 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
1944
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001945 /*
1946 * Child is the top of the chain. Check against the trust_ca list.
1947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 md_info = mbedtls_md_info_from_type( child->sig_md );
Manuel Pégourié-Gonnard081ed062017-06-26 12:22:17 +02001951 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001952 {
Manuel Pégourié-Gonnard081ed062017-06-26 12:22:17 +02001953 /* Note: this can't happen except after an internal error */
1954 /* Cannot check signature, no need to try any CA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001955 trust_ca = NULL;
1956 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001957
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001958 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001959 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001960 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001961 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001962
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001963 check_path_cnt = path_cnt + 1;
1964
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001965 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001966 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001967 * the same as the trusted CA
1968 */
1969 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1970 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Hanno Beckerdc8751d2017-09-25 10:47:58 +01001971 child->subject_raw.len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001972 {
1973 check_path_cnt--;
1974 }
1975
Janos Follath860f2392015-10-12 09:02:20 +02001976 /* Self signed certificates do not count towards the limit */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001977 if( trust_ca->max_pathlen > 0 &&
Janos Follath860f2392015-10-12 09:02:20 +02001978 trust_ca->max_pathlen < check_path_cnt - self_cnt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001979 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001980 continue;
1981 }
1982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1984 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001985 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001986 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001987 continue;
1988 }
1989
Andres AG8136e822016-12-09 17:26:23 +00001990 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) ||
1991 mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
1992 {
1993 if ( future_past_ca == NULL )
1994 future_past_ca = trust_ca;
1995
1996 continue;
1997 }
1998
1999 break;
2000 }
2001
2002 if( trust_ca != NULL || ( trust_ca = future_past_ca ) != NULL )
2003 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002004 /*
2005 * Top of chain is signed by a trusted CA
2006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardfa67eba2015-06-27 14:41:38 +02002008
2009 if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
2010 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002011 }
2012
2013 /*
2014 * If top of chain is not the same as the trusted CA send a verify request
2015 * to the callback for any issues with validity and CRL presence for the
2016 * trusted CA certificate.
2017 */
2018 if( trust_ca != NULL &&
2019 ( child->subject_raw.len != trust_ca->subject_raw.len ||
2020 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Hanno Beckerdc8751d2017-09-25 10:47:58 +01002021 child->subject_raw.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002024 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002025 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02002026#else
2027 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002028#endif
2029
Andres AG8136e822016-12-09 17:26:23 +00002030 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
2031 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
2032
2033 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
2034 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
2035
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002036 if( NULL != f_vrfy )
2037 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002038 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
2039 &ca_flags ) ) != 0 )
2040 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002041 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002042 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002043 }
2044 }
2045
2046 /* Call callback on top cert */
2047 if( NULL != f_vrfy )
2048 {
Paul Bakker66d5d072014-06-17 16:39:18 +02002049 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002050 return( ret );
2051 }
2052
2053 *flags |= ca_flags;
2054
2055 return( 0 );
2056}
2057
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002058/*
2059 * Verify a certificate with a parent inside the chain
2060 *
2061 * See comments for mbedtls_x509_crt_verify_with_profile()
2062 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002063static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002064 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
2065 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
2066 const mbedtls_x509_crt_profile *profile,
Janos Follath860f2392015-10-12 09:02:20 +02002067 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002068 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002069 void *p_vrfy )
2070{
2071 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002072 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2074 mbedtls_x509_crt *grandparent;
2075 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002076
Janos Follath860f2392015-10-12 09:02:20 +02002077 /* Counting intermediate self signed certificates */
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002078 if( ( path_cnt != 0 ) && x509_name_cmp( &child->issuer, &child->subject ) == 0 )
Janos Follath860f2392015-10-12 09:02:20 +02002079 self_cnt++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002081 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002083 {
Manuel Pégourié-Gonnardc3863172017-06-26 10:11:49 +02002084 /* return immediately as the goal is to avoid unbounded recursion */
2085 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002086 }
2087
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002088 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002089 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02002090
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002091 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002093
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002094 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2095 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2096
2097 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2098 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 md_info = mbedtls_md_info_from_type( child->sig_md );
Manuel Pégourié-Gonnardac54cea2018-03-07 09:41:20 +01002101 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002102 {
Manuel Pégourié-Gonnardac54cea2018-03-07 09:41:20 +01002103 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002105 }
2106 else
2107 {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002108 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2109 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2112 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002113 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002116 }
2117 }
2118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002120 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002121 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002122#endif
2123
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002124 /* Look for a grandparent in trusted CAs */
2125 for( grandparent = trust_ca;
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002126 grandparent != NULL;
2127 grandparent = grandparent->next )
2128 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002129 if( x509_crt_check_parent( parent, grandparent,
2130 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002131 break;
2132 }
2133
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002134 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002135 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002136 ret = x509_crt_verify_top( parent, grandparent, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002137 path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002138 if( ret != 0 )
2139 return( ret );
2140 }
2141 else
2142 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002143 /* Look for a grandparent upwards the chain */
2144 for( grandparent = parent->next;
2145 grandparent != NULL;
2146 grandparent = grandparent->next )
2147 {
Janos Follath860f2392015-10-12 09:02:20 +02002148 /* +2 because the current step is not yet accounted for
2149 * and because max_pathlen is one higher than it should be.
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002150 * Also self signed certificates do not count to the limit. */
Janos Follath860f2392015-10-12 09:02:20 +02002151 if( grandparent->max_pathlen > 0 &&
2152 grandparent->max_pathlen < 2 + path_cnt - self_cnt )
2153 {
2154 continue;
2155 }
2156
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002157 if( x509_crt_check_parent( parent, grandparent,
2158 0, path_cnt == 0 ) == 0 )
2159 break;
2160 }
2161
2162 /* Is our parent part of the chain or at the top? */
2163 if( grandparent != NULL )
2164 {
2165 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
Janos Follath860f2392015-10-12 09:02:20 +02002166 profile, path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002167 f_vrfy, p_vrfy );
2168 if( ret != 0 )
2169 return( ret );
2170 }
2171 else
2172 {
2173 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002174 path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002175 f_vrfy, p_vrfy );
2176 if( ret != 0 )
2177 return( ret );
2178 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002179 }
2180
2181 /* child is verified to be a child of the parent, call verify callback */
2182 if( NULL != f_vrfy )
2183 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2184 return( ret );
2185
2186 *flags |= parent_flags;
2187
2188 return( 0 );
2189}
2190
2191/*
2192 * Verify the certificate validity
2193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2195 mbedtls_x509_crt *trust_ca,
2196 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002197 const char *cn, uint32_t *flags,
2198 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002199 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002200{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002201 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002202 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002203}
2204
2205
2206/*
2207 * Verify the certificate validity, with profile
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002208 *
2209 * The chain building/verification is spread accross 4 functions:
2210 * - this one
2211 * - x509_crt_verify_child()
2212 * - x509_crt_verify_top()
2213 * - x509_crt_check_parent()
2214 *
2215 * There are five main cases to consider. Let's introduce some notation:
2216 * - E means the end-entity certificate
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01002217 * - I an intermediate CA
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002218 * - R the trusted root CA this chain anchors to
2219 * - T the list of trusted roots (R and possible some others)
2220 *
2221 * The main cases with the calling sequence of the crt_verify_xxx() are:
2222 * 1. E = R (explicitly trusted EE cert)
2223 * verify(E, T) -> verify_top(E, R)
2224 * 2. E -> R (EE signed by trusted root)
2225 * verify(E, T) -> verify_top(E, R)
2226 * 3. E -> I -> R (EE signed by intermediate signed by trusted root)
2227 * verify(E, T) -> verify_child(E, I, T) -> verify_top(I, R)
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01002228 * (plus variant with multiple intermediates)
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002229 * 4. E -> I (EE signed by intermediate that's not trusted)
2230 * verify(E, T) -> verify_child(E, I, T) -> verify_top(I, T)
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01002231 * (plus variant with multiple intermediates)
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002232 * 5. E (EE not trusted)
2233 * verify(E, T) -> verify_top(E, T)
Manuel Pégourié-Gonnard19d77b62018-03-07 09:36:30 +01002234 *
2235 * Note: this notation and case numbering is also used in x509_crt_verify_top()
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002236 */
2237int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2238 mbedtls_x509_crt *trust_ca,
2239 mbedtls_x509_crl *ca_crl,
2240 const mbedtls_x509_crt_profile *profile,
2241 const char *cn, uint32_t *flags,
2242 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2243 void *p_vrfy )
2244{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002245 size_t cn_len;
2246 int ret;
Janos Follath860f2392015-10-12 09:02:20 +02002247 int pathlen = 0, selfsigned = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 mbedtls_x509_crt *parent;
2249 mbedtls_x509_name *name;
2250 mbedtls_x509_sequence *cur = NULL;
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +02002251 mbedtls_pk_type_t pk_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002252
2253 *flags = 0;
2254
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002255 if( profile == NULL )
2256 {
2257 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2258 goto exit;
2259 }
2260
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002261 if( cn != NULL )
2262 {
2263 name = &crt->subject;
2264 cn_len = strlen( cn );
2265
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002266 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002267 {
2268 cur = &crt->subject_alt_names;
2269
2270 while( cur != NULL )
2271 {
2272 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002273 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002274 break;
2275
2276 if( cur->buf.len > 2 &&
2277 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002278 x509_check_wildcard( cn, &cur->buf ) == 0 )
2279 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002280 break;
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002281 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002282
2283 cur = cur->next;
2284 }
2285
2286 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002288 }
2289 else
2290 {
2291 while( name != NULL )
2292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002294 {
2295 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002296 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002297 break;
2298
2299 if( name->val.len > 2 &&
2300 memcmp( name->val.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002301 x509_check_wildcard( cn, &name->val ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002302 break;
2303 }
2304
2305 name = name->next;
2306 }
2307
2308 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002310 }
2311 }
2312
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +02002313 /* Check the type and size of the key */
2314 pk_type = mbedtls_pk_get_type( &crt->pk );
2315
2316 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
2317 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2318
2319 if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
2320 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2321
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002322 /* Look for a parent in trusted CAs */
2323 for( parent = trust_ca; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002324 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002325 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002326 break;
2327 }
2328
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002329 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002330 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002331 ret = x509_crt_verify_top( crt, parent, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002332 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002333 if( ret != 0 )
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002334 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002335 }
2336 else
2337 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002338 /* Look for a parent upwards the chain */
2339 for( parent = crt->next; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002340 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
2341 break;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002342
2343 /* Are we part of the chain or at the top? */
2344 if( parent != NULL )
2345 {
2346 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002347 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002348 if( ret != 0 )
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002349 goto exit;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002350 }
2351 else
2352 {
2353 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002354 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002355 if( ret != 0 )
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002356 goto exit;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002357 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002358 }
2359
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002360exit:
Manuel Pégourié-Gonnardcdb4dc92017-07-06 12:16:25 +02002361 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2362 * the SSL module for authmode optional, but non-zero return from the
2363 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnardc3863172017-06-26 10:11:49 +02002364 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2365 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2366
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002367 if( ret != 0 )
2368 {
2369 *flags = (uint32_t) -1;
2370 return( ret );
2371 }
2372
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002373 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002375
2376 return( 0 );
2377}
2378
2379/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002380 * Initialize a certificate chain
2381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002383{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002385}
2386
2387/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002388 * Unallocate all certificate data
2389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002391{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 mbedtls_x509_crt *cert_cur = crt;
2393 mbedtls_x509_crt *cert_prv;
2394 mbedtls_x509_name *name_cur;
2395 mbedtls_x509_name *name_prv;
2396 mbedtls_x509_sequence *seq_cur;
2397 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002398
2399 if( crt == NULL )
2400 return;
2401
2402 do
2403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2407 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002408#endif
2409
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002410 name_cur = cert_cur->issuer.next;
2411 while( name_cur != NULL )
2412 {
2413 name_prv = name_cur;
2414 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2416 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002417 }
2418
2419 name_cur = cert_cur->subject.next;
2420 while( name_cur != NULL )
2421 {
2422 name_prv = name_cur;
2423 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2425 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002426 }
2427
2428 seq_cur = cert_cur->ext_key_usage.next;
2429 while( seq_cur != NULL )
2430 {
2431 seq_prv = seq_cur;
2432 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2434 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002435 }
2436
2437 seq_cur = cert_cur->subject_alt_names.next;
2438 while( seq_cur != NULL )
2439 {
2440 seq_prv = seq_cur;
2441 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2443 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002444 }
2445
2446 if( cert_cur->raw.p != NULL )
2447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2449 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002450 }
2451
2452 cert_cur = cert_cur->next;
2453 }
2454 while( cert_cur != NULL );
2455
2456 cert_cur = crt;
2457 do
2458 {
2459 cert_prv = cert_cur;
2460 cert_cur = cert_cur->next;
2461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002463 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002465 }
2466 while( cert_cur != NULL );
2467}
2468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469#endif /* MBEDTLS_X509_CRT_PARSE_C */