blob: 9a416d382d54d58c4d13e8997bfe1b956432b75a [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{
155 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
156 return( 0 );
157
158 return( -1 );
159}
160
161/*
162 * Check pk_alg against profile
163 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
164 */
165static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
166 mbedtls_pk_type_t pk_alg )
167{
168 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
169 return( 0 );
170
171 return( -1 );
172}
173
174/*
175 * Check key against profile
176 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
177 */
178static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
179 mbedtls_pk_type_t pk_alg,
180 const mbedtls_pk_context *pk )
181{
182#if defined(MBEDTLS_RSA_C)
183 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
184 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200185 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200186 return( 0 );
187
188 return( -1 );
189 }
190#endif
191
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +0200192#if defined(MBEDTLS_ECP_C)
193 if( pk_alg == MBEDTLS_PK_ECDSA ||
194 pk_alg == MBEDTLS_PK_ECKEY ||
195 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200196 {
197 mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
198
199 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
200 return( 0 );
201
202 return( -1 );
203 }
204#endif
205
206 return( -1 );
207}
208
209/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200210 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
211 */
212static int x509_get_version( unsigned char **p,
213 const unsigned char *end,
214 int *ver )
215{
216 int ret;
217 size_t len;
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
220 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200223 {
224 *ver = 0;
225 return( 0 );
226 }
227
228 return( ret );
229 }
230
231 end = *p + len;
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
234 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200235
236 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 return( MBEDTLS_ERR_X509_INVALID_VERSION +
238 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200239
240 return( 0 );
241}
242
243/*
244 * Validity ::= SEQUENCE {
245 * notBefore Time,
246 * notAfter Time }
247 */
248static int x509_get_dates( unsigned char **p,
249 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_x509_time *from,
251 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200252{
253 int ret;
254 size_t len;
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
257 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
258 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200259
260 end = *p + len;
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200263 return( ret );
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200266 return( ret );
267
268 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 return( MBEDTLS_ERR_X509_INVALID_DATE +
270 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271
272 return( 0 );
273}
274
275/*
276 * X.509 v2/v3 unique identifier (not parsed)
277 */
278static int x509_get_uid( unsigned char **p,
279 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281{
282 int ret;
283
284 if( *p == end )
285 return( 0 );
286
287 uid->tag = **p;
288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
290 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293 return( 0 );
294
295 return( ret );
296 }
297
298 uid->p = *p;
299 *p += uid->len;
300
301 return( 0 );
302}
303
304static int x509_get_basic_constraints( unsigned char **p,
305 const unsigned char *end,
306 int *ca_istrue,
307 int *max_pathlen )
308{
309 int ret;
310 size_t len;
311
312 /*
313 * BasicConstraints ::= SEQUENCE {
314 * cA BOOLEAN DEFAULT FALSE,
315 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
316 */
317 *ca_istrue = 0; /* DEFAULT FALSE */
318 *max_pathlen = 0; /* endless */
319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
321 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
322 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323
324 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200325 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
330 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200331
332 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200334
335 if( *ca_istrue != 0 )
336 *ca_istrue = 1;
337 }
338
339 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200340 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
343 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344
345 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
347 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348
349 (*max_pathlen)++;
350
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200351 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352}
353
354static int x509_get_ns_cert_type( unsigned char **p,
355 const unsigned char *end,
356 unsigned char *ns_cert_type)
357{
358 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
362 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363
364 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
366 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367
368 /* Get actual bitstring */
369 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200370 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200371}
372
373static int x509_get_key_usage( unsigned char **p,
374 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100375 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376{
377 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200378 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
382 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383
384 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
386 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387
388 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200389 *key_usage = 0;
390 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
391 {
392 *key_usage |= (unsigned int) bs.p[i] << (8*i);
393 }
394
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200395 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396}
397
398/*
399 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
400 *
401 * KeyPurposeId ::= OBJECT IDENTIFIER
402 */
403static int x509_get_ext_key_usage( unsigned char **p,
404 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406{
407 int ret;
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
410 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411
412 /* Sequence length must be >= 1 */
413 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
415 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200417 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418}
419
420/*
421 * SubjectAltName ::= GeneralNames
422 *
423 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
424 *
425 * GeneralName ::= CHOICE {
426 * otherName [0] OtherName,
427 * rfc822Name [1] IA5String,
428 * dNSName [2] IA5String,
429 * x400Address [3] ORAddress,
430 * directoryName [4] Name,
431 * ediPartyName [5] EDIPartyName,
432 * uniformResourceIdentifier [6] IA5String,
433 * iPAddress [7] OCTET STRING,
434 * registeredID [8] OBJECT IDENTIFIER }
435 *
436 * OtherName ::= SEQUENCE {
437 * type-id OBJECT IDENTIFIER,
438 * value [0] EXPLICIT ANY DEFINED BY type-id }
439 *
440 * EDIPartyName ::= SEQUENCE {
441 * nameAssigner [0] DirectoryString OPTIONAL,
442 * partyName [1] DirectoryString }
443 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000444 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445 */
446static int x509_get_subject_alt_name( unsigned char **p,
447 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449{
450 int ret;
451 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455
456 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
458 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
459 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
461 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
463 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
465 while( *p < end )
466 {
467 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
469 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470
471 tag = **p;
472 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
474 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475
Andres Amaya Garciaf6a6b822017-08-25 17:13:12 +0100476 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
477 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
480 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Andres Amaya Garciaf6a6b822017-08-25 17:13:12 +0100481 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200483 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 {
486 *p += tag_len;
487 continue;
488 }
489
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200491 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100493 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100495
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200496 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497
498 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200500 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502 cur = cur->next;
503 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200504
505 buf = &(cur->buf);
506 buf->tag = tag;
507 buf->p = *p;
508 buf->len = tag_len;
509 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510 }
511
512 /* Set final sequence entry's next pointer to NULL */
513 cur->next = NULL;
514
515 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
517 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
519 return( 0 );
520}
521
522/*
523 * X.509 v3 extensions
524 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 */
526static int x509_get_crt_ext( unsigned char **p,
527 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529{
530 int ret;
531 size_t len;
532 unsigned char *end_ext_data, *end_ext_octet;
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537 return( 0 );
538
539 return( ret );
540 }
541
542 while( *p < end )
543 {
544 /*
545 * Extension ::= SEQUENCE {
546 * extnID OBJECT IDENTIFIER,
547 * critical BOOLEAN DEFAULT FALSE,
548 * extnValue OCTET STRING }
549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551 int is_critical = 0; /* DEFAULT FALSE */
552 int ext_type = 0;
553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
555 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
556 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557
558 end_ext_data = *p + len;
559
560 /* Get extension ID */
k-stachowiak2d2d80b2018-07-16 10:49:12 +0200561 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
562 MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564
k-stachowiak2d2d80b2018-07-16 10:49:12 +0200565 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566 extn_oid.p = *p;
567 *p += extn_oid.len;
568
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
571 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
572 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573
574 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
576 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
577 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578
579 end_ext_octet = *p + len;
580
581 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
583 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584
585 /*
586 * Detect supported extensions
587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589
590 if( ret != 0 )
591 {
592 /* No parser found, skip extension */
593 *p = end_ext_octet;
594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200596 if( is_critical )
597 {
598 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
600 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200601 }
602#endif
603 continue;
604 }
605
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100606 /* Forbid repeated extensions */
607 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100609
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610 crt->ext_types |= ext_type;
611
612 switch( ext_type )
613 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100614 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200615 /* Parse basic constraints */
616 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
617 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200618 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619 break;
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622 /* Parse key usage */
623 if( ( ret = x509_get_key_usage( p, end_ext_octet,
624 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200625 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 break;
627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629 /* Parse extended key usage */
630 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
631 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200632 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633 break;
634
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100635 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636 /* Parse subject alt name */
637 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
638 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200639 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 break;
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643 /* Parse netscape certificate type */
644 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
645 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200646 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647 break;
648
649 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651 }
652 }
653
654 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
656 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657
658 return( 0 );
659}
660
661/*
662 * Parse and fill a single X.509 certificate in DER format
663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200665 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666{
667 int ret;
668 size_t len;
669 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
673 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
674 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675
676 /*
677 * Check for valid input
678 */
679 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
Janos Follath16734f02016-02-17 14:34:12 +0000682 // Use the original buffer until we figure out actual length
683 p = (unsigned char*) buf;
684 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685 end = p + len;
686
687 /*
688 * Certificate ::= SEQUENCE {
689 * tbsCertificate TBSCertificate,
690 * signatureAlgorithm AlgorithmIdentifier,
691 * signatureValue BIT STRING }
692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
694 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_x509_crt_free( crt );
697 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698 }
699
700 if( len > (size_t) ( end - p ) )
701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_x509_crt_free( crt );
703 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
704 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705 }
706 crt_end = p + len;
707
Janos Follath16734f02016-02-17 14:34:12 +0000708 // Create and populate a new buffer for the raw field
709 crt->raw.len = crt_end - buf;
710 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
711 if( p == NULL )
712 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
713
714 memcpy( p, buf, crt->raw.len );
715
Hanno Beckerdc8751d2017-09-25 10:47:58 +0100716 // Direct pointers to the new buffer
Janos Follath16734f02016-02-17 14:34:12 +0000717 p += crt->raw.len - len;
718 end = crt_end = p + len;
719
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720 /*
721 * TBSCertificate ::= SEQUENCE {
722 */
723 crt->tbs.p = p;
724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
726 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_x509_crt_free( crt );
729 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730 }
731
732 end = p + len;
733 crt->tbs.len = end - crt->tbs.p;
734
735 /*
736 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
737 *
738 * CertificateSerialNumber ::= INTEGER
739 *
740 * signature AlgorithmIdentifier
741 */
742 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
744 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200745 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748 return( ret );
749 }
750
Andres AG1f06d9b2017-03-09 16:16:11 +0000751 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_x509_crt_free( crt );
754 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 }
756
Andres AG1f06d9b2017-03-09 16:16:11 +0000757 crt->version++;
758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200760 &crt->sig_md, &crt->sig_pk,
761 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764 return( ret );
765 }
766
767 /*
768 * issuer Name
769 */
770 crt->issuer_raw.p = p;
771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
773 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_x509_crt_free( crt );
776 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777 }
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200782 return( ret );
783 }
784
785 crt->issuer_raw.len = p - crt->issuer_raw.p;
786
787 /*
788 * Validity ::= SEQUENCE {
789 * notBefore Time,
790 * notAfter Time }
791 *
792 */
793 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
794 &crt->valid_to ) ) != 0 )
795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797 return( ret );
798 }
799
800 /*
801 * subject Name
802 */
803 crt->subject_raw.p = p;
804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
806 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_x509_crt_free( crt );
809 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200810 }
811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815 return( ret );
816 }
817
818 crt->subject_raw.len = p - crt->subject_raw.p;
819
820 /*
821 * SubjectPublicKeyInfo
822 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826 return( ret );
827 }
828
829 /*
830 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
831 * -- If present, version shall be v2 or v3
832 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
833 * -- If present, version shall be v2 or v3
834 * extensions [3] EXPLICIT Extensions OPTIONAL
835 * -- If present, version shall be v3
836 */
837 if( crt->version == 2 || crt->version == 3 )
838 {
839 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
840 if( ret != 0 )
841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200843 return( ret );
844 }
845 }
846
847 if( crt->version == 2 || crt->version == 3 )
848 {
849 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
850 if( ret != 0 )
851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200853 return( ret );
854 }
855 }
856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200858 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200859#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100860 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200861 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200862 if( ret != 0 )
863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200865 return( ret );
866 }
867 }
868
869 if( p != end )
870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_x509_crt_free( crt );
872 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
873 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874 }
875
876 end = crt_end;
877
878 /*
879 * }
880 * -- end of TBSCertificate
881 *
882 * signatureAlgorithm AlgorithmIdentifier,
883 * signatureValue BIT STRING
884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888 return( ret );
889 }
890
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100891 if( crt->sig_oid.len != sig_oid2.len ||
892 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200893 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200894 ( sig_params1.len != 0 &&
895 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 mbedtls_x509_crt_free( crt );
898 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 }
900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 return( ret );
905 }
906
907 if( p != end )
908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_x509_crt_free( crt );
910 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
911 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912 }
913
914 return( 0 );
915}
916
917/*
918 * Parse one X.509 certificate in DER format from a buffer and add them to a
919 * chained list
920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200922 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923{
924 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926
927 /*
928 * Check for valid input
929 */
930 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932
933 while( crt->version != 0 && crt->next != NULL )
934 {
935 prev = crt;
936 crt = crt->next;
937 }
938
939 /*
940 * Add new certificate on the end of the chain if needed.
941 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200942 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200944 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945
946 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200947 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948
949 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952 }
953
Paul Bakkerddf26b42013-09-18 13:46:23 +0200954 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 {
956 if( prev )
957 prev->next = NULL;
958
959 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961
962 return( ret );
963 }
964
965 return( 0 );
966}
967
968/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200969 * Parse one or more PEM certificates from a buffer and add them to the chained
970 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973{
974 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 int buf_format = MBEDTLS_X509_FORMAT_DER;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976
977 /*
978 * Check for valid input
979 */
980 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982
983 /*
984 * Determine buffer content. Buffer contains either one DER certificate or
985 * one or more PEM certificates.
986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +0200988 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200989 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200992 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993#endif
994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 if( buf_format == MBEDTLS_X509_FORMAT_DER )
996 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998#if defined(MBEDTLS_PEM_PARSE_C)
999 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000 {
1001 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001004 /* 1 rather than 0 since the terminating NULL byte is counted in */
1005 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006 {
1007 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001010 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012 "-----BEGIN CERTIFICATE-----",
1013 "-----END CERTIFICATE-----",
1014 buf, NULL, 0, &use_len );
1015
1016 if( ret == 0 )
1017 {
1018 /*
1019 * Was PEM encoded
1020 */
1021 buflen -= use_len;
1022 buf += use_len;
1023 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025 {
1026 return( ret );
1027 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031
1032 /*
1033 * PEM header and footer were found
1034 */
1035 buflen -= use_len;
1036 buf += use_len;
1037
1038 if( first_error == 0 )
1039 first_error = ret;
1040
Paul Bakker5a5fa922014-09-26 14:53:04 +02001041 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042 continue;
1043 }
1044 else
1045 break;
1046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050
1051 if( ret != 0 )
1052 {
1053 /*
1054 * Quit parsing on a memory error
1055 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001056 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001057 return( ret );
1058
1059 if( first_error == 0 )
1060 first_error = ret;
1061
1062 total_failed++;
1063 continue;
1064 }
1065
1066 success = 1;
1067 }
1068 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070
1071 if( success )
1072 return( total_failed );
1073 else if( first_error )
1074 return( first_error );
1075 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077}
1078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080/*
1081 * Load one or more certificates and add them to the chained list
1082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001084{
1085 int ret;
1086 size_t n;
1087 unsigned char *buf;
1088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090 return( ret );
1091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001094 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096
1097 return( ret );
1098}
1099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101{
1102 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001103#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104 int w_ret;
1105 WCHAR szDir[MAX_PATH];
1106 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001107 char *p;
Manuel Pégourié-Gonnard9dc66f42015-10-21 10:16:29 +02001108 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109
Paul Bakker9af723c2014-05-01 13:03:14 +02001110 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111 HANDLE hFind;
1112
1113 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001115
Paul Bakker9af723c2014-05-01 13:03:14 +02001116 memset( szDir, 0, sizeof(szDir) );
1117 memset( filename, 0, MAX_PATH );
1118 memcpy( filename, path, len );
1119 filename[len++] = '\\';
1120 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121 filename[len++] = '*';
1122
Simon B635f2152016-11-03 01:11:37 +00001123 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001124 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001125 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127
1128 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001129 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001131
1132 len = MAX_PATH - len;
1133 do
1134 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001135 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001136
1137 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1138 continue;
1139
Paul Bakker9af723c2014-05-01 13:03:14 +02001140 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001141 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard9dc66f42015-10-21 10:16:29 +02001142 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001143 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001144 if( w_ret == 0 )
Ron Eldor0fb3e0a2017-01-09 15:09:16 +02001145 {
1146 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1147 goto cleanup;
1148 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001151 if( w_ret < 0 )
1152 ret++;
1153 else
1154 ret += w_ret;
1155 }
1156 while( FindNextFileW( hFind, &file_data ) != 0 );
1157
Paul Bakker66d5d072014-06-17 16:39:18 +02001158 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160
Ron Eldor0fb3e0a2017-01-09 15:09:16 +02001161cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001163#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001164 int t_ret;
Andres AG879e6262016-09-02 14:06:04 +01001165 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001167 struct dirent *entry;
Andres AG879e6262016-09-02 14:06:04 +01001168 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169 DIR *dir = opendir( path );
1170
Paul Bakker66d5d072014-06-17 16:39:18 +02001171 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173
Ron Eldoree709f42017-01-09 19:27:59 +02001174#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001175 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001176 {
1177 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001178 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001179 }
Ron Eldoree709f42017-01-09 19:27:59 +02001180#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001181
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001182 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001183 {
Andres AG879e6262016-09-02 14:06:04 +01001184 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1185 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186
Andres AG879e6262016-09-02 14:06:04 +01001187 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001188 {
Andres AG879e6262016-09-02 14:06:04 +01001189 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1190 goto cleanup;
1191 }
1192 else if( stat( entry_name, &sb ) == -1 )
1193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001195 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001196 }
1197
1198 if( !S_ISREG( sb.st_mode ) )
1199 continue;
1200
1201 // Ignore parse errors
1202 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001204 if( t_ret < 0 )
1205 ret++;
1206 else
1207 ret += t_ret;
1208 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001209
1210cleanup:
Andres AG879e6262016-09-02 14:06:04 +01001211 closedir( dir );
1212
Ron Eldoree709f42017-01-09 19:27:59 +02001213#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001214 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldoree709f42017-01-09 19:27:59 +02001216#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001217
Paul Bakkerbe089b02013-10-14 15:51:50 +02001218#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219
1220 return( ret );
1221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001223
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001224static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001226{
1227 size_t i;
1228 size_t n = *size;
1229 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001231 const char *sep = "";
1232 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001233
1234 while( cur != NULL )
1235 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001236 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001237 {
1238 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001239 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001240 }
1241
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001242 n -= cur->buf.len + sep_len;
1243 for( i = 0; i < sep_len; i++ )
1244 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001245 for( i = 0; i < cur->buf.len; i++ )
1246 *p++ = cur->buf.p[i];
1247
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001248 sep = ", ";
1249 sep_len = 2;
1250
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001251 cur = cur->next;
1252 }
1253
1254 *p = '\0';
1255
1256 *size = n;
1257 *buf = p;
1258
1259 return( 0 );
1260}
1261
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001262#define PRINT_ITEM(i) \
1263 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001265 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001266 sep = ", "; \
1267 }
1268
1269#define CERT_TYPE(type,name) \
1270 if( ns_cert_type & type ) \
1271 PRINT_ITEM( name );
1272
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001273static int x509_info_cert_type( char **buf, size_t *size,
1274 unsigned char ns_cert_type )
1275{
1276 int ret;
1277 size_t n = *size;
1278 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001279 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001282 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1284 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1285 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001286 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1287 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1288 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001289
1290 *size = n;
1291 *buf = p;
1292
1293 return( 0 );
1294}
1295
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001296#define KEY_USAGE(code,name) \
1297 if( key_usage & code ) \
1298 PRINT_ITEM( name );
1299
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001300static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001301 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001302{
1303 int ret;
1304 size_t n = *size;
1305 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001306 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1309 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001310 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1311 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1312 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1314 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001315 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1316 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001317
1318 *size = n;
1319 *buf = p;
1320
1321 return( 0 );
1322}
1323
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001324static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001326{
1327 int ret;
1328 const char *desc;
1329 size_t n = *size;
1330 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001332 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001333
1334 while( cur != NULL )
1335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001337 desc = "???";
1338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001340 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001341
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001342 sep = ", ";
1343
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001344 cur = cur->next;
1345 }
1346
1347 *size = n;
1348 *buf = p;
1349
1350 return( 0 );
1351}
1352
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353/*
1354 * Return an informational string about the certificate.
1355 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001356#define BEFORE_COLON 18
1357#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1359 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360{
1361 int ret;
1362 size_t n;
1363 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364 char key_size_str[BEFORE_COLON];
1365
1366 p = buf;
1367 n = size;
1368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001371 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001374 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001377 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001380 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001382 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001385 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
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_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001390 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1391 crt->valid_from.year, crt->valid_from.mon,
1392 crt->valid_from.day, crt->valid_from.hour,
1393 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001394 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001397 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1398 crt->valid_to.year, crt->valid_to.mon,
1399 crt->valid_to.day, crt->valid_to.hour,
1400 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001401 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001404 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001407 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001408 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001409
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001410 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1412 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001413 {
1414 return( ret );
1415 }
1416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001418 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001419 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001421 /*
1422 * Optional extensions
1423 */
1424
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001425 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001428 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001429 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001430
1431 if( crt->max_pathlen > 0 )
1432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001434 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001435 }
1436 }
1437
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001438 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
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%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001441 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001442
1443 if( ( ret = x509_info_subject_alt_name( &p, &n,
1444 &crt->subject_alt_names ) ) != 0 )
1445 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001446 }
1447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001451 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001452
1453 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1454 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001455 }
1456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001460 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001461
1462 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1463 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001464 }
1465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001469 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001470
1471 if( ( ret = x509_info_ext_key_usage( &p, &n,
1472 &crt->ext_key_usage ) ) != 0 )
1473 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001474 }
1475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001477 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001478
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001479 return( (int) ( size - n ) );
1480}
1481
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001482struct x509_crt_verify_string {
1483 int code;
1484 const char *string;
1485};
1486
1487static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001488 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001489 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1490 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1491 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1492 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1493 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001494 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1495 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1496 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001497 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001498 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1499 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1500 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1501 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001502 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1503 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1504 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1505 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1506 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1507 { 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 +01001508 { 0, NULL }
1509};
1510
1511int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001512 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001513{
1514 int ret;
1515 const struct x509_crt_verify_string *cur;
1516 char *p = buf;
1517 size_t n = size;
1518
1519 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1520 {
1521 if( ( flags & cur->code ) == 0 )
1522 continue;
1523
1524 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001525 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001526 flags ^= cur->code;
1527 }
1528
1529 if( flags != 0 )
1530 {
1531 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1532 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001533 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001534 }
1535
1536 return( (int) ( size - n ) );
1537}
1538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001540int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1541 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001542{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001543 unsigned int usage_must, usage_may;
1544 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1545 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1546
1547 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1548 return( 0 );
1549
1550 usage_must = usage & ~may_mask;
1551
1552 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1553 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1554
1555 usage_may = usage & may_mask;
1556
1557 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001559
1560 return( 0 );
1561}
1562#endif
1563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1565int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001566 const char *usage_oid,
1567 size_t usage_len )
1568{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001570
1571 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001573 return( 0 );
1574
1575 /*
1576 * Look for the requested usage (or wildcard ANY) in our list
1577 */
1578 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001581
1582 if( cur_oid->len == usage_len &&
1583 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1584 {
1585 return( 0 );
1586 }
1587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001589 return( 0 );
1590 }
1591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001593}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597/*
1598 * Return 1 if the certificate is revoked, or 0 otherwise.
1599 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001600int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001601{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001603
1604 while( cur != NULL && cur->serial.len != 0 )
1605 {
1606 if( crt->serial.len == cur->serial.len &&
1607 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1608 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001609 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001610 return( 1 );
1611 }
1612
1613 cur = cur->next;
1614 }
1615
1616 return( 0 );
1617}
1618
1619/*
Manuel Pégourié-Gonnard78df7fc2018-03-05 13:22:59 +01001620 * Check that the given certificate is not revoked according to the CRL.
1621 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001624 mbedtls_x509_crl *crl_list,
1625 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001626{
1627 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1629 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630
1631 if( ca == NULL )
1632 return( flags );
1633
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634 while( crl_list != NULL )
1635 {
1636 if( crl_list->version == 0 ||
1637 crl_list->issuer_raw.len != ca->subject_raw.len ||
1638 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1639 crl_list->issuer_raw.len ) != 0 )
1640 {
1641 crl_list = crl_list->next;
1642 continue;
1643 }
1644
1645 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001646 * Check if the CA is configured to sign CRLs
1647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1649 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001652 break;
1653 }
1654#endif
1655
1656 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657 * Check if CRL is correctly signed by the trusted CA
1658 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001659 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1660 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1661
1662 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1663 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard081ed062017-06-26 12:22:17 +02001666 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001667 {
Manuel Pégourié-Gonnard081ed062017-06-26 12:22:17 +02001668 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670 break;
1671 }
1672
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001673 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1674 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1677 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001678 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001681 break;
1682 }
1683
1684 /*
1685 * Check for validity of CRL (Do not drop out)
1686 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001687 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001689
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001690 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001691 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001692
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001693 /*
1694 * Check if certificate is revoked
1695 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001696 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001699 break;
1700 }
1701
1702 crl_list = crl_list->next;
1703 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001704
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001705 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001706}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001708
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001709/*
1710 * Like memcmp, but case-insensitive and always returns -1 if different
1711 */
1712static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001713{
1714 size_t i;
1715 unsigned char diff;
1716 const unsigned char *n1 = s1, *n2 = s2;
1717
1718 for( i = 0; i < len; i++ )
1719 {
1720 diff = n1[i] ^ n2[i];
1721
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001722 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001723 continue;
1724
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001725 if( diff == 32 &&
1726 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1727 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1728 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001729 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001730 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001731
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001732 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001733 }
1734
1735 return( 0 );
1736}
1737
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001738/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001739 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001740 */
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001741static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001742{
1743 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001744 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745
1746 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1747 return( 0 );
1748
Paul Bakker14b16c62014-05-28 11:33:54 +02001749 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001750 {
1751 if( cn[i] == '.' )
1752 {
1753 cn_idx = i;
1754 break;
1755 }
1756 }
1757
1758 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001759 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001760
Paul Bakker14b16c62014-05-28 11:33:54 +02001761 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001762 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001763 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001764 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001765 }
1766
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001767 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768}
1769
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001770/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001771 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1772 * variations (but not all).
1773 *
1774 * Return 0 if equal, -1 otherwise.
1775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001777{
1778 if( a->tag == b->tag &&
1779 a->len == b->len &&
1780 memcmp( a->p, b->p, b->len ) == 0 )
1781 {
1782 return( 0 );
1783 }
1784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1786 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001787 a->len == b->len &&
1788 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1789 {
1790 return( 0 );
1791 }
1792
1793 return( -1 );
1794}
1795
1796/*
1797 * Compare two X.509 Names (aka rdnSequence).
1798 *
1799 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1800 * we sometimes return unequal when the full algorithm would return equal,
1801 * but never the other way. (In particular, we don't do Unicode normalisation
1802 * or space folding.)
1803 *
1804 * Return 0 if equal, -1 otherwise.
1805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001807{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001808 /* Avoid recursion, it might not be optimised by the compiler */
1809 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001810 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001811 if( a == NULL || b == NULL )
1812 return( -1 );
1813
1814 /* type */
1815 if( a->oid.tag != b->oid.tag ||
1816 a->oid.len != b->oid.len ||
1817 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1818 {
1819 return( -1 );
1820 }
1821
1822 /* value */
1823 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1824 return( -1 );
1825
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001826 /* structure of the list of sets */
1827 if( a->next_merged != b->next_merged )
1828 return( -1 );
1829
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001830 a = a->next;
1831 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001832 }
1833
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001834 /* a == NULL == b */
1835 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001836}
1837
1838/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001839 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1840 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001841 *
1842 * top means parent is a locally-trusted certificate
1843 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1846 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001847 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001848{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001849 int need_ca_bit;
1850
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001851 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001852 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001853 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001854
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001855 /* Parent must have the basicConstraints CA bit set as a general rule */
1856 need_ca_bit = 1;
1857
1858 /* Exception: v1/v2 certificates that are locally trusted. */
1859 if( top && parent->version < 3 )
1860 need_ca_bit = 0;
1861
1862 /* Exception: self-signed end-entity certs that are locally trusted. */
1863 if( top && bottom &&
1864 child->raw.len == parent->raw.len &&
1865 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1866 {
1867 need_ca_bit = 0;
1868 }
1869
1870 if( need_ca_bit && ! parent->ca_istrue )
1871 return( -1 );
1872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001874 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001876 {
1877 return( -1 );
1878 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001879#endif
1880
1881 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001882}
1883
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001884/*
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01001885 * Verify a certificate with no parent inside the chain
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001886 * (either the parent is a trusted root, or there is no parent)
1887 *
1888 * See comments for mbedtls_x509_crt_verify_with_profile()
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01001889 * (also for notation used below)
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001890 *
1891 * This function is called in two cases:
1892 * - child was found to have a parent in trusted roots, in which case we're
1893 * called with trust_ca pointing directly to that parent (not the full list)
Manuel Pégourié-Gonnard19d77b62018-03-07 09:36:30 +01001894 * - this is cases 1, 2 and 3 of the comment on verify_with_profile()
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001895 * - case 1 is special as child and trust_ca point to copies of the same
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01001896 * certificate then
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001897 * - child was found to have no parent either in the chain or in trusted CAs
Manuel Pégourié-Gonnard19d77b62018-03-07 09:36:30 +01001898 * - this is cases 4 and 5 of the comment on verify_with_profile()
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02001899 *
1900 * For historical reasons, the function currently does not assume that
1901 * trust_ca points directly to the right root in the first case, and it
1902 * doesn't know in which case it starts, so it always starts by searching for
1903 * a parent in trust_ca.
1904 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001905static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001907 mbedtls_x509_crl *ca_crl,
1908 const mbedtls_x509_crt_profile *profile,
Janos Follath860f2392015-10-12 09:02:20 +02001909 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001910 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001911 void *p_vrfy )
1912{
1913 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001914 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001915 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1917 const mbedtls_md_info_t *md_info;
Andres AG8136e822016-12-09 17:26:23 +00001918 mbedtls_x509_crt *future_past_ca = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001919
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001920 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001921 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001922
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001923 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001925
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001926 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
1927 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
1928
1929 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
1930 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
1931
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001932 /*
1933 * Child is the top of the chain. Check against the trust_ca list.
1934 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 md_info = mbedtls_md_info_from_type( child->sig_md );
Manuel Pégourié-Gonnard081ed062017-06-26 12:22:17 +02001938 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001939 {
Manuel Pégourié-Gonnard081ed062017-06-26 12:22:17 +02001940 /* Note: this can't happen except after an internal error */
1941 /* Cannot check signature, no need to try any CA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001942 trust_ca = NULL;
1943 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001944
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001945 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001946 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001947 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001948 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001949
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001950 check_path_cnt = path_cnt + 1;
1951
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001952 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001953 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001954 * the same as the trusted CA
1955 */
1956 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1957 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Hanno Beckerdc8751d2017-09-25 10:47:58 +01001958 child->subject_raw.len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001959 {
1960 check_path_cnt--;
1961 }
1962
Janos Follath860f2392015-10-12 09:02:20 +02001963 /* Self signed certificates do not count towards the limit */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001964 if( trust_ca->max_pathlen > 0 &&
Janos Follath860f2392015-10-12 09:02:20 +02001965 trust_ca->max_pathlen < check_path_cnt - self_cnt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001966 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001967 continue;
1968 }
1969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1971 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001972 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001973 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001974 continue;
1975 }
1976
Andres AG8136e822016-12-09 17:26:23 +00001977 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) ||
1978 mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
1979 {
1980 if ( future_past_ca == NULL )
1981 future_past_ca = trust_ca;
1982
1983 continue;
1984 }
1985
1986 break;
1987 }
1988
1989 if( trust_ca != NULL || ( trust_ca = future_past_ca ) != NULL )
1990 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001991 /*
1992 * Top of chain is signed by a trusted CA
1993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardfa67eba2015-06-27 14:41:38 +02001995
1996 if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
1997 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001998 }
1999
2000 /*
2001 * If top of chain is not the same as the trusted CA send a verify request
2002 * to the callback for any issues with validity and CRL presence for the
2003 * trusted CA certificate.
2004 */
2005 if( trust_ca != NULL &&
2006 ( child->subject_raw.len != trust_ca->subject_raw.len ||
2007 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Hanno Beckerdc8751d2017-09-25 10:47:58 +01002008 child->subject_raw.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002011 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002012 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02002013#else
2014 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002015#endif
2016
Andres AG8136e822016-12-09 17:26:23 +00002017 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
2018 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
2019
2020 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
2021 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
2022
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002023 if( NULL != f_vrfy )
2024 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002025 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
2026 &ca_flags ) ) != 0 )
2027 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002028 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002029 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002030 }
2031 }
2032
2033 /* Call callback on top cert */
2034 if( NULL != f_vrfy )
2035 {
Paul Bakker66d5d072014-06-17 16:39:18 +02002036 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002037 return( ret );
2038 }
2039
2040 *flags |= ca_flags;
2041
2042 return( 0 );
2043}
2044
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002045/*
2046 * Verify a certificate with a parent inside the chain
2047 *
2048 * See comments for mbedtls_x509_crt_verify_with_profile()
2049 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002050static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002051 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
2052 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
2053 const mbedtls_x509_crt_profile *profile,
Janos Follath860f2392015-10-12 09:02:20 +02002054 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002055 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002056 void *p_vrfy )
2057{
2058 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002059 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2061 mbedtls_x509_crt *grandparent;
2062 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002063
Janos Follath860f2392015-10-12 09:02:20 +02002064 /* Counting intermediate self signed certificates */
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002065 if( ( path_cnt != 0 ) && x509_name_cmp( &child->issuer, &child->subject ) == 0 )
Janos Follath860f2392015-10-12 09:02:20 +02002066 self_cnt++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002067
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002068 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002070 {
Manuel Pégourié-Gonnardc3863172017-06-26 10:11:49 +02002071 /* return immediately as the goal is to avoid unbounded recursion */
2072 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002073 }
2074
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002075 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002076 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02002077
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002078 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002081 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2082 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2083
2084 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2085 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 md_info = mbedtls_md_info_from_type( child->sig_md );
Manuel Pégourié-Gonnardac54cea2018-03-07 09:41:20 +01002088 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002089 {
Manuel Pégourié-Gonnardac54cea2018-03-07 09:41:20 +01002090 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002092 }
2093 else
2094 {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002095 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2096 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2099 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002100 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002103 }
2104 }
2105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002107 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002108 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002109#endif
2110
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002111 /* Look for a grandparent in trusted CAs */
2112 for( grandparent = trust_ca;
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002113 grandparent != NULL;
2114 grandparent = grandparent->next )
2115 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002116 if( x509_crt_check_parent( parent, grandparent,
2117 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002118 break;
2119 }
2120
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002121 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002122 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002123 ret = x509_crt_verify_top( parent, grandparent, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002124 path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002125 if( ret != 0 )
2126 return( ret );
2127 }
2128 else
2129 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002130 /* Look for a grandparent upwards the chain */
2131 for( grandparent = parent->next;
2132 grandparent != NULL;
2133 grandparent = grandparent->next )
2134 {
Janos Follath860f2392015-10-12 09:02:20 +02002135 /* +2 because the current step is not yet accounted for
2136 * and because max_pathlen is one higher than it should be.
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002137 * Also self signed certificates do not count to the limit. */
Janos Follath860f2392015-10-12 09:02:20 +02002138 if( grandparent->max_pathlen > 0 &&
2139 grandparent->max_pathlen < 2 + path_cnt - self_cnt )
2140 {
2141 continue;
2142 }
2143
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002144 if( x509_crt_check_parent( parent, grandparent,
2145 0, path_cnt == 0 ) == 0 )
2146 break;
2147 }
2148
2149 /* Is our parent part of the chain or at the top? */
2150 if( grandparent != NULL )
2151 {
2152 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
Janos Follath860f2392015-10-12 09:02:20 +02002153 profile, path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002154 f_vrfy, p_vrfy );
2155 if( ret != 0 )
2156 return( ret );
2157 }
2158 else
2159 {
2160 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002161 path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002162 f_vrfy, p_vrfy );
2163 if( ret != 0 )
2164 return( ret );
2165 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002166 }
2167
2168 /* child is verified to be a child of the parent, call verify callback */
2169 if( NULL != f_vrfy )
2170 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2171 return( ret );
2172
2173 *flags |= parent_flags;
2174
2175 return( 0 );
2176}
2177
2178/*
2179 * Verify the certificate validity
2180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2182 mbedtls_x509_crt *trust_ca,
2183 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002184 const char *cn, uint32_t *flags,
2185 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002186 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002187{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002188 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002189 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002190}
2191
2192
2193/*
2194 * Verify the certificate validity, with profile
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002195 *
2196 * The chain building/verification is spread accross 4 functions:
2197 * - this one
2198 * - x509_crt_verify_child()
2199 * - x509_crt_verify_top()
2200 * - x509_crt_check_parent()
2201 *
2202 * There are five main cases to consider. Let's introduce some notation:
2203 * - E means the end-entity certificate
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01002204 * - I an intermediate CA
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002205 * - R the trusted root CA this chain anchors to
2206 * - T the list of trusted roots (R and possible some others)
2207 *
2208 * The main cases with the calling sequence of the crt_verify_xxx() are:
2209 * 1. E = R (explicitly trusted EE cert)
2210 * verify(E, T) -> verify_top(E, R)
2211 * 2. E -> R (EE signed by trusted root)
2212 * verify(E, T) -> verify_top(E, R)
2213 * 3. E -> I -> R (EE signed by intermediate signed by trusted root)
2214 * verify(E, T) -> verify_child(E, I, T) -> verify_top(I, R)
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01002215 * (plus variant with multiple intermediates)
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002216 * 4. E -> I (EE signed by intermediate that's not trusted)
2217 * verify(E, T) -> verify_child(E, I, T) -> verify_top(I, T)
Manuel Pégourié-Gonnardb6d3e6d2018-03-06 10:34:11 +01002218 * (plus variant with multiple intermediates)
Manuel Pégourié-Gonnardafbbcf82017-06-29 10:45:25 +02002219 * 5. E (EE not trusted)
2220 * verify(E, T) -> verify_top(E, T)
Manuel Pégourié-Gonnard19d77b62018-03-07 09:36:30 +01002221 *
2222 * Note: this notation and case numbering is also used in x509_crt_verify_top()
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002223 */
2224int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2225 mbedtls_x509_crt *trust_ca,
2226 mbedtls_x509_crl *ca_crl,
2227 const mbedtls_x509_crt_profile *profile,
2228 const char *cn, uint32_t *flags,
2229 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2230 void *p_vrfy )
2231{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002232 size_t cn_len;
2233 int ret;
Janos Follath860f2392015-10-12 09:02:20 +02002234 int pathlen = 0, selfsigned = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 mbedtls_x509_crt *parent;
2236 mbedtls_x509_name *name;
2237 mbedtls_x509_sequence *cur = NULL;
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +02002238 mbedtls_pk_type_t pk_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002239
2240 *flags = 0;
2241
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002242 if( profile == NULL )
2243 {
2244 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2245 goto exit;
2246 }
2247
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002248 if( cn != NULL )
2249 {
2250 name = &crt->subject;
2251 cn_len = strlen( cn );
2252
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002253 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002254 {
2255 cur = &crt->subject_alt_names;
2256
2257 while( cur != NULL )
2258 {
2259 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002260 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002261 break;
2262
2263 if( cur->buf.len > 2 &&
2264 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002265 x509_check_wildcard( cn, &cur->buf ) == 0 )
2266 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002267 break;
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002268 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002269
2270 cur = cur->next;
2271 }
2272
2273 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002275 }
2276 else
2277 {
2278 while( name != NULL )
2279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002281 {
2282 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002283 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002284 break;
2285
2286 if( name->val.len > 2 &&
2287 memcmp( name->val.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002288 x509_check_wildcard( cn, &name->val ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002289 break;
2290 }
2291
2292 name = name->next;
2293 }
2294
2295 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002297 }
2298 }
2299
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +02002300 /* Check the type and size of the key */
2301 pk_type = mbedtls_pk_get_type( &crt->pk );
2302
2303 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
2304 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2305
2306 if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
2307 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2308
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002309 /* Look for a parent in trusted CAs */
2310 for( parent = trust_ca; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002311 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002312 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002313 break;
2314 }
2315
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002316 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002317 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002318 ret = x509_crt_verify_top( crt, parent, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002319 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002320 if( ret != 0 )
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002321 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002322 }
2323 else
2324 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002325 /* Look for a parent upwards the chain */
2326 for( parent = crt->next; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002327 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
2328 break;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002329
2330 /* Are we part of the chain or at the top? */
2331 if( parent != NULL )
2332 {
2333 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002334 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002335 if( ret != 0 )
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002336 goto exit;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002337 }
2338 else
2339 {
2340 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002341 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002342 if( ret != 0 )
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002343 goto exit;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002344 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002345 }
2346
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002347exit:
Manuel Pégourié-Gonnardcdb4dc92017-07-06 12:16:25 +02002348 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2349 * the SSL module for authmode optional, but non-zero return from the
2350 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnardc3863172017-06-26 10:11:49 +02002351 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2352 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2353
Manuel Pégourié-Gonnard489939f2017-06-22 12:19:27 +02002354 if( ret != 0 )
2355 {
2356 *flags = (uint32_t) -1;
2357 return( ret );
2358 }
2359
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002360 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002362
2363 return( 0 );
2364}
2365
2366/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002367 * Initialize a certificate chain
2368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002370{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002372}
2373
2374/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002375 * Unallocate all certificate data
2376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002378{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 mbedtls_x509_crt *cert_cur = crt;
2380 mbedtls_x509_crt *cert_prv;
2381 mbedtls_x509_name *name_cur;
2382 mbedtls_x509_name *name_prv;
2383 mbedtls_x509_sequence *seq_cur;
2384 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002385
2386 if( crt == NULL )
2387 return;
2388
2389 do
2390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2394 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002395#endif
2396
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002397 name_cur = cert_cur->issuer.next;
2398 while( name_cur != NULL )
2399 {
2400 name_prv = name_cur;
2401 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2403 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002404 }
2405
2406 name_cur = cert_cur->subject.next;
2407 while( name_cur != NULL )
2408 {
2409 name_prv = name_cur;
2410 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2412 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002413 }
2414
2415 seq_cur = cert_cur->ext_key_usage.next;
2416 while( seq_cur != NULL )
2417 {
2418 seq_prv = seq_cur;
2419 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2421 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002422 }
2423
2424 seq_cur = cert_cur->subject_alt_names.next;
2425 while( seq_cur != NULL )
2426 {
2427 seq_prv = seq_cur;
2428 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2430 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002431 }
2432
2433 if( cert_cur->raw.p != NULL )
2434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2436 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002437 }
2438
2439 cert_cur = cert_cur->next;
2440 }
2441 while( cert_cur != NULL );
2442
2443 cert_cur = crt;
2444 do
2445 {
2446 cert_prv = cert_cur;
2447 cert_cur = cert_cur->next;
2448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002450 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002452 }
2453 while( cert_cur != NULL );
2454}
2455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#endif /* MBEDTLS_X509_CRT_PARSE_C */