blob: e67c32bae2309472408006ab2650c5bd0b090211 [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
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020030 *
31 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032 */
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/x509_crt.h"
43#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050044#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000045
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <string.h>
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Simon Butcherd2642582018-10-03 15:11:19 +010055#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010064#endif
65
Paul Bakkerfa6a6202013-10-28 18:48:30 +010066#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#include <windows.h>
68#else
69#include <time.h>
70#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000073#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020074#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <sys/types.h>
76#include <sys/stat.h>
77#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000078#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#endif
80
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020081/*
82 * Item in a verification chain: cert and flags for it
83 */
84typedef struct {
85 mbedtls_x509_crt *crt;
86 uint32_t flags;
87} x509_crt_verify_chain_item;
88
89/*
90 * Max size of verification chain: end-entity + intermediates + trusted root
91 */
92#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
93
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020095 * Default profile
96 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020097const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
98{
Gilles Peskine5d2511c2017-05-12 13:16:40 +020099#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200100 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200101 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200102#endif
103 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200104 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
105 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
106 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
107 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
108 0xFFFFFFF, /* Any PK alg */
109 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200110 2048,
111};
112
113/*
114 * Next-default profile
115 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200116const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
117{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200118 /* Hashes from SHA-256 and above */
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
120 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
121 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
122 0xFFFFFFF, /* Any PK alg */
123#if defined(MBEDTLS_ECP_C)
124 /* Curves at or above 128-bit security level */
125 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
126 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
127 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
128 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
129 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
130 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
132#else
133 0,
134#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200135 2048,
136};
137
138/*
139 * NSA Suite B Profile
140 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200141const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
142{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200143 /* Only SHA-256 and 384 */
144 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
145 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
146 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200147 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
148 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200149#if defined(MBEDTLS_ECP_C)
150 /* Only NIST P-256 and P-384 */
151 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
152 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
153#else
154 0,
155#endif
156 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200157};
158
159/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200160 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200161 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200162 */
163static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
164 mbedtls_md_type_t md_alg )
165{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200166 if( md_alg == MBEDTLS_MD_NONE )
167 return( -1 );
168
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200169 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
170 return( 0 );
171
172 return( -1 );
173}
174
175/*
176 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200177 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200178 */
179static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
180 mbedtls_pk_type_t pk_alg )
181{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200182 if( pk_alg == MBEDTLS_PK_NONE )
183 return( -1 );
184
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200185 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
186 return( 0 );
187
188 return( -1 );
189}
190
191/*
192 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200193 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200194 */
195static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200196 const mbedtls_pk_context *pk )
197{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200198 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200199
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200200#if defined(MBEDTLS_RSA_C)
201 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
202 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200203 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200204 return( 0 );
205
206 return( -1 );
207 }
208#endif
209
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200210#if defined(MBEDTLS_ECP_C)
211 if( pk_alg == MBEDTLS_PK_ECDSA ||
212 pk_alg == MBEDTLS_PK_ECKEY ||
213 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200214 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200215 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200216
Philippe Antoineb5b25432018-05-11 11:06:29 +0200217 if( gid == MBEDTLS_ECP_DP_NONE )
218 return( -1 );
219
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200220 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
221 return( 0 );
222
223 return( -1 );
224 }
225#endif
226
227 return( -1 );
228}
229
230/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200231 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
232 */
233static int x509_get_version( unsigned char **p,
234 const unsigned char *end,
235 int *ver )
236{
237 int ret;
238 size_t len;
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
241 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200244 {
245 *ver = 0;
246 return( 0 );
247 }
248
249 return( ret );
250 }
251
252 end = *p + len;
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
255 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256
257 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 return( MBEDTLS_ERR_X509_INVALID_VERSION +
259 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260
261 return( 0 );
262}
263
264/*
265 * Validity ::= SEQUENCE {
266 * notBefore Time,
267 * notAfter Time }
268 */
269static int x509_get_dates( unsigned char **p,
270 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_x509_time *from,
272 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200273{
274 int ret;
275 size_t len;
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
278 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
279 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280
281 end = *p + len;
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284 return( ret );
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287 return( ret );
288
289 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 return( MBEDTLS_ERR_X509_INVALID_DATE +
291 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200292
293 return( 0 );
294}
295
296/*
297 * X.509 v2/v3 unique identifier (not parsed)
298 */
299static int x509_get_uid( unsigned char **p,
300 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200302{
303 int ret;
304
305 if( *p == end )
306 return( 0 );
307
308 uid->tag = **p;
309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
311 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200314 return( 0 );
315
316 return( ret );
317 }
318
319 uid->p = *p;
320 *p += uid->len;
321
322 return( 0 );
323}
324
325static int x509_get_basic_constraints( unsigned char **p,
326 const unsigned char *end,
327 int *ca_istrue,
328 int *max_pathlen )
329{
330 int ret;
331 size_t len;
332
333 /*
334 * BasicConstraints ::= SEQUENCE {
335 * cA BOOLEAN DEFAULT FALSE,
336 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
337 */
338 *ca_istrue = 0; /* DEFAULT FALSE */
339 *max_pathlen = 0; /* endless */
340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
342 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
343 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344
345 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200346 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
351 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352
353 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355
356 if( *ca_istrue != 0 )
357 *ca_istrue = 1;
358 }
359
360 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200361 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
364 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365
366 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
368 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369
370 (*max_pathlen)++;
371
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200372 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200373}
374
375static int x509_get_ns_cert_type( unsigned char **p,
376 const unsigned char *end,
377 unsigned char *ns_cert_type)
378{
379 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
383 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384
385 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
387 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388
389 /* Get actual bitstring */
390 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200391 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392}
393
394static int x509_get_key_usage( unsigned char **p,
395 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100396 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397{
398 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200399 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
403 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404
405 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
407 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408
409 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200410 *key_usage = 0;
411 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
412 {
413 *key_usage |= (unsigned int) bs.p[i] << (8*i);
414 }
415
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200416 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417}
418
419/*
420 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
421 *
422 * KeyPurposeId ::= OBJECT IDENTIFIER
423 */
424static int x509_get_ext_key_usage( unsigned char **p,
425 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427{
428 int ret;
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
431 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433 /* Sequence length must be >= 1 */
434 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
436 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200438 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439}
440
441/*
442 * SubjectAltName ::= GeneralNames
443 *
444 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
445 *
446 * GeneralName ::= CHOICE {
447 * otherName [0] OtherName,
448 * rfc822Name [1] IA5String,
449 * dNSName [2] IA5String,
450 * x400Address [3] ORAddress,
451 * directoryName [4] Name,
452 * ediPartyName [5] EDIPartyName,
453 * uniformResourceIdentifier [6] IA5String,
454 * iPAddress [7] OCTET STRING,
455 * registeredID [8] OBJECT IDENTIFIER }
456 *
457 * OtherName ::= SEQUENCE {
458 * type-id OBJECT IDENTIFIER,
459 * value [0] EXPLICIT ANY DEFINED BY type-id }
460 *
461 * EDIPartyName ::= SEQUENCE {
462 * nameAssigner [0] DirectoryString OPTIONAL,
463 * partyName [1] DirectoryString }
464 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000465 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466 */
467static int x509_get_subject_alt_name( unsigned char **p,
468 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470{
471 int ret;
472 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476
477 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
479 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
480 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481
482 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
484 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485
486 while( *p < end )
487 {
488 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
490 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491
492 tag = **p;
493 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
495 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100497 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
498 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
501 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100502 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200504 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506 {
507 *p += tag_len;
508 continue;
509 }
510
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200512 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100514 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100516
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200517 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
519 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200521 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523 cur = cur->next;
524 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200525
526 buf = &(cur->buf);
527 buf->tag = tag;
528 buf->p = *p;
529 buf->len = tag_len;
530 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531 }
532
533 /* Set final sequence entry's next pointer to NULL */
534 cur->next = NULL;
535
536 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
538 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539
540 return( 0 );
541}
542
543/*
544 * X.509 v3 extensions
545 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546 */
547static int x509_get_crt_ext( unsigned char **p,
548 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550{
551 int ret;
552 size_t len;
553 unsigned char *end_ext_data, *end_ext_octet;
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558 return( 0 );
559
560 return( ret );
561 }
562
563 while( *p < end )
564 {
565 /*
566 * Extension ::= SEQUENCE {
567 * extnID OBJECT IDENTIFIER,
568 * critical BOOLEAN DEFAULT FALSE,
569 * extnValue OCTET STRING }
570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572 int is_critical = 0; /* DEFAULT FALSE */
573 int ext_type = 0;
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
576 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
577 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578
579 end_ext_data = *p + len;
580
581 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200582 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200583 MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585
k-stachowiak463928a2018-07-24 12:50:59 +0200586 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587 extn_oid.p = *p;
588 *p += extn_oid.len;
589
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
592 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
593 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594
595 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
597 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
598 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599
600 end_ext_octet = *p + len;
601
602 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
604 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605
606 /*
607 * Detect supported extensions
608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610
611 if( ret != 0 )
612 {
613 /* No parser found, skip extension */
614 *p = end_ext_octet;
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617 if( is_critical )
618 {
619 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
621 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622 }
623#endif
624 continue;
625 }
626
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100627 /* Forbid repeated extensions */
628 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100630
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631 crt->ext_types |= ext_type;
632
633 switch( ext_type )
634 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100635 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636 /* Parse basic constraints */
637 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
638 &crt->ca_istrue, &crt->max_pathlen ) ) != 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_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643 /* Parse key usage */
644 if( ( ret = x509_get_key_usage( p, end_ext_octet,
645 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200646 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647 break;
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650 /* Parse extended key usage */
651 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
652 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200653 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654 break;
655
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100656 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657 /* Parse subject alt name */
658 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
659 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200660 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661 break;
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664 /* Parse netscape certificate type */
665 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
666 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200667 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668 break;
669
670 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672 }
673 }
674
675 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
677 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200678
679 return( 0 );
680}
681
682/*
683 * Parse and fill a single X.509 certificate in DER format
684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200686 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687{
688 int ret;
689 size_t len;
690 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
694 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
695 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696
697 /*
698 * Check for valid input
699 */
700 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
Janos Follathcc0e49d2016-02-17 14:34:12 +0000703 // Use the original buffer until we figure out actual length
704 p = (unsigned char*) buf;
705 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706 end = p + len;
707
708 /*
709 * Certificate ::= SEQUENCE {
710 * tbsCertificate TBSCertificate,
711 * signatureAlgorithm AlgorithmIdentifier,
712 * signatureValue BIT STRING }
713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
715 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_x509_crt_free( crt );
718 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719 }
720
721 if( len > (size_t) ( end - p ) )
722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_x509_crt_free( crt );
724 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
725 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726 }
727 crt_end = p + len;
728
Janos Follathcc0e49d2016-02-17 14:34:12 +0000729 // Create and populate a new buffer for the raw field
730 crt->raw.len = crt_end - buf;
731 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
732 if( p == NULL )
733 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
734
735 memcpy( p, buf, crt->raw.len );
736
Darryl Green11999bb2018-03-13 15:22:58 +0000737 // Direct pointers to the new buffer
Janos Follathcc0e49d2016-02-17 14:34:12 +0000738 p += crt->raw.len - len;
739 end = crt_end = p + len;
740
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741 /*
742 * TBSCertificate ::= SEQUENCE {
743 */
744 crt->tbs.p = p;
745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
747 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_x509_crt_free( crt );
750 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200751 }
752
753 end = p + len;
754 crt->tbs.len = end - crt->tbs.p;
755
756 /*
757 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
758 *
759 * CertificateSerialNumber ::= INTEGER
760 *
761 * signature AlgorithmIdentifier
762 */
763 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
765 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200766 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 return( ret );
770 }
771
Andres AG7ca4a032017-03-09 16:16:11 +0000772 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_x509_crt_free( crt );
775 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200776 }
777
Andres AG7ca4a032017-03-09 16:16:11 +0000778 crt->version++;
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200781 &crt->sig_md, &crt->sig_pk,
782 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785 return( ret );
786 }
787
788 /*
789 * issuer Name
790 */
791 crt->issuer_raw.p = p;
792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
794 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_x509_crt_free( crt );
797 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798 }
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803 return( ret );
804 }
805
806 crt->issuer_raw.len = p - crt->issuer_raw.p;
807
808 /*
809 * Validity ::= SEQUENCE {
810 * notBefore Time,
811 * notAfter Time }
812 *
813 */
814 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
815 &crt->valid_to ) ) != 0 )
816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818 return( ret );
819 }
820
821 /*
822 * subject Name
823 */
824 crt->subject_raw.p = p;
825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
827 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_x509_crt_free( crt );
830 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200831 }
832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836 return( ret );
837 }
838
839 crt->subject_raw.len = p - crt->subject_raw.p;
840
841 /*
842 * SubjectPublicKeyInfo
843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200847 return( ret );
848 }
849
850 /*
851 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
852 * -- If present, version shall be v2 or v3
853 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
854 * -- If present, version shall be v2 or v3
855 * extensions [3] EXPLICIT Extensions OPTIONAL
856 * -- If present, version shall be v3
857 */
858 if( crt->version == 2 || crt->version == 3 )
859 {
860 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
861 if( ret != 0 )
862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200864 return( ret );
865 }
866 }
867
868 if( crt->version == 2 || crt->version == 3 )
869 {
870 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
871 if( ret != 0 )
872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874 return( ret );
875 }
876 }
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200880#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100881 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200882 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883 if( ret != 0 )
884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886 return( ret );
887 }
888 }
889
890 if( p != end )
891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 mbedtls_x509_crt_free( crt );
893 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
894 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895 }
896
897 end = crt_end;
898
899 /*
900 * }
901 * -- end of TBSCertificate
902 *
903 * signatureAlgorithm AlgorithmIdentifier,
904 * signatureValue BIT STRING
905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909 return( ret );
910 }
911
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100912 if( crt->sig_oid.len != sig_oid2.len ||
913 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200914 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200915 ( sig_params1.len != 0 &&
916 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_x509_crt_free( crt );
919 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920 }
921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925 return( ret );
926 }
927
928 if( p != end )
929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_x509_crt_free( crt );
931 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
932 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 }
934
935 return( 0 );
936}
937
938/*
939 * Parse one X.509 certificate in DER format from a buffer and add them to a
940 * chained list
941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200943 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944{
945 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947
948 /*
949 * Check for valid input
950 */
951 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953
954 while( crt->version != 0 && crt->next != NULL )
955 {
956 prev = crt;
957 crt = crt->next;
958 }
959
960 /*
961 * Add new certificate on the end of the chain if needed.
962 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200963 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200965 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966
967 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200968 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969
970 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973 }
974
Paul Bakkerddf26b42013-09-18 13:46:23 +0200975 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976 {
977 if( prev )
978 prev->next = NULL;
979
980 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982
983 return( ret );
984 }
985
986 return( 0 );
987}
988
989/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200990 * Parse one or more PEM certificates from a buffer and add them to the chained
991 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994{
Janos Follath98e28a72016-05-31 14:03:54 +0100995#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +0000996 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +0100998#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999
1000 /*
1001 * Check for valid input
1002 */
1003 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005
1006 /*
1007 * Determine buffer content. Buffer contains either one DER certificate or
1008 * one or more PEM certificates.
1009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001011 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001012 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001015 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1018 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001019#else
1020 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1021#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_PEM_PARSE_C)
1024 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025 {
1026 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001029 /* 1 rather than 0 since the terminating NULL byte is counted in */
1030 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 {
1032 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001035 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037 "-----BEGIN CERTIFICATE-----",
1038 "-----END CERTIFICATE-----",
1039 buf, NULL, 0, &use_len );
1040
1041 if( ret == 0 )
1042 {
1043 /*
1044 * Was PEM encoded
1045 */
1046 buflen -= use_len;
1047 buf += use_len;
1048 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050 {
1051 return( ret );
1052 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056
1057 /*
1058 * PEM header and footer were found
1059 */
1060 buflen -= use_len;
1061 buf += use_len;
1062
1063 if( first_error == 0 )
1064 first_error = ret;
1065
Paul Bakker5a5fa922014-09-26 14:53:04 +02001066 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067 continue;
1068 }
1069 else
1070 break;
1071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075
1076 if( ret != 0 )
1077 {
1078 /*
1079 * Quit parsing on a memory error
1080 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001081 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001082 return( ret );
1083
1084 if( first_error == 0 )
1085 first_error = ret;
1086
1087 total_failed++;
1088 continue;
1089 }
1090
1091 success = 1;
1092 }
1093 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094
1095 if( success )
1096 return( total_failed );
1097 else if( first_error )
1098 return( first_error );
1099 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001101#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102}
1103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105/*
1106 * Load one or more certificates and add them to the chained list
1107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109{
1110 int ret;
1111 size_t n;
1112 unsigned char *buf;
1113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001115 return( ret );
1116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001119 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121
1122 return( ret );
1123}
1124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001126{
1127 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001128#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129 int w_ret;
1130 WCHAR szDir[MAX_PATH];
1131 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001132 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001133 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001134
Paul Bakker9af723c2014-05-01 13:03:14 +02001135 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001136 HANDLE hFind;
1137
1138 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140
Paul Bakker9af723c2014-05-01 13:03:14 +02001141 memset( szDir, 0, sizeof(szDir) );
1142 memset( filename, 0, MAX_PATH );
1143 memcpy( filename, path, len );
1144 filename[len++] = '\\';
1145 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001146 filename[len++] = '*';
1147
Simon B3c6b18d2016-11-03 01:11:37 +00001148 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001149 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001150 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152
1153 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001154 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156
1157 len = MAX_PATH - len;
1158 do
1159 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001160 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001161
1162 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1163 continue;
1164
Paul Bakker9af723c2014-05-01 13:03:14 +02001165 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001166 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001167 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001168 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001169 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001170 {
1171 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1172 goto cleanup;
1173 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176 if( w_ret < 0 )
1177 ret++;
1178 else
1179 ret += w_ret;
1180 }
1181 while( FindNextFileW( hFind, &file_data ) != 0 );
1182
Paul Bakker66d5d072014-06-17 16:39:18 +02001183 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001185
Ron Eldor36d90422017-01-09 15:09:16 +02001186cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001187 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001188#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001189 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001190 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001192 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001193 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 DIR *dir = opendir( path );
1195
Paul Bakker66d5d072014-06-17 16:39:18 +02001196 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001198
Ron Eldor63140682017-01-09 19:27:59 +02001199#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001200 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001201 {
1202 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001203 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001204 }
Ron Eldor63140682017-01-09 19:27:59 +02001205#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001206
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001207 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001208 {
Andres AGf9113192016-09-02 14:06:04 +01001209 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1210 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001211
Andres AGf9113192016-09-02 14:06:04 +01001212 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001213 {
Andres AGf9113192016-09-02 14:06:04 +01001214 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1215 goto cleanup;
1216 }
1217 else if( stat( entry_name, &sb ) == -1 )
1218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001220 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001221 }
1222
1223 if( !S_ISREG( sb.st_mode ) )
1224 continue;
1225
1226 // Ignore parse errors
1227 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229 if( t_ret < 0 )
1230 ret++;
1231 else
1232 ret += t_ret;
1233 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001234
1235cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001236 closedir( dir );
1237
Ron Eldor63140682017-01-09 19:27:59 +02001238#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001239 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001241#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001242
Paul Bakkerbe089b02013-10-14 15:51:50 +02001243#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001244
1245 return( ret );
1246}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001248
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001249static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001251{
1252 size_t i;
1253 size_t n = *size;
1254 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001256 const char *sep = "";
1257 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001258
1259 while( cur != NULL )
1260 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001261 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001262 {
1263 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001264 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001265 }
1266
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001267 n -= cur->buf.len + sep_len;
1268 for( i = 0; i < sep_len; i++ )
1269 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001270 for( i = 0; i < cur->buf.len; i++ )
1271 *p++ = cur->buf.p[i];
1272
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001273 sep = ", ";
1274 sep_len = 2;
1275
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001276 cur = cur->next;
1277 }
1278
1279 *p = '\0';
1280
1281 *size = n;
1282 *buf = p;
1283
1284 return( 0 );
1285}
1286
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001287#define PRINT_ITEM(i) \
1288 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001290 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001291 sep = ", "; \
1292 }
1293
1294#define CERT_TYPE(type,name) \
1295 if( ns_cert_type & type ) \
1296 PRINT_ITEM( name );
1297
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001298static int x509_info_cert_type( char **buf, size_t *size,
1299 unsigned char ns_cert_type )
1300{
1301 int ret;
1302 size_t n = *size;
1303 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001304 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001307 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1309 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1310 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001311 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1312 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1313 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001314
1315 *size = n;
1316 *buf = p;
1317
1318 return( 0 );
1319}
1320
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001321#define KEY_USAGE(code,name) \
1322 if( key_usage & code ) \
1323 PRINT_ITEM( name );
1324
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001325static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001326 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001327{
1328 int ret;
1329 size_t n = *size;
1330 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001331 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1334 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001335 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1336 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1337 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1339 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001340 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1341 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001342
1343 *size = n;
1344 *buf = p;
1345
1346 return( 0 );
1347}
1348
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001349static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001351{
1352 int ret;
1353 const char *desc;
1354 size_t n = *size;
1355 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001357 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001358
1359 while( cur != NULL )
1360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001362 desc = "???";
1363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001365 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001366
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001367 sep = ", ";
1368
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001369 cur = cur->next;
1370 }
1371
1372 *size = n;
1373 *buf = p;
1374
1375 return( 0 );
1376}
1377
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378/*
1379 * Return an informational string about the certificate.
1380 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001381#define BEFORE_COLON 18
1382#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1384 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001385{
1386 int ret;
1387 size_t n;
1388 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389 char key_size_str[BEFORE_COLON];
1390
1391 p = buf;
1392 n = size;
1393
Janos Follath98e28a72016-05-31 14:03:54 +01001394 if( NULL == crt )
1395 {
1396 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
1397 MBEDTLS_X509_SAFE_SNPRINTF;
1398
1399 return( (int) ( size - n ) );
1400 }
1401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001403 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001404 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001407 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001410 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001413 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001415 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001418 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001420 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1424 crt->valid_from.year, crt->valid_from.mon,
1425 crt->valid_from.day, crt->valid_from.hour,
1426 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001427 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001430 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1431 crt->valid_to.year, crt->valid_to.mon,
1432 crt->valid_to.day, crt->valid_to.hour,
1433 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001434 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001437 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001440 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001441 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001443 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1445 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446 {
1447 return( ret );
1448 }
1449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001451 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001452 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001454 /*
1455 * Optional extensions
1456 */
1457
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001458 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001461 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001462 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001463
1464 if( crt->max_pathlen > 0 )
1465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001467 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001468 }
1469 }
1470
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001471 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001474 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001475
1476 if( ( ret = x509_info_subject_alt_name( &p, &n,
1477 &crt->subject_alt_names ) ) != 0 )
1478 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001479 }
1480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001484 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001485
1486 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1487 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001488 }
1489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001493 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001494
1495 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1496 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001497 }
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001502 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001503
1504 if( ( ret = x509_info_ext_key_usage( &p, &n,
1505 &crt->ext_key_usage ) ) != 0 )
1506 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001507 }
1508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001510 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001511
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512 return( (int) ( size - n ) );
1513}
1514
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001515struct x509_crt_verify_string {
1516 int code;
1517 const char *string;
1518};
1519
1520static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001521 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001522 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1523 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1524 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1525 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1526 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001527 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1528 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1529 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001530 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001531 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1532 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1533 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1534 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001535 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1536 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1537 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1538 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1539 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1540 { 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 +01001541 { 0, NULL }
1542};
1543
1544int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001545 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001546{
1547 int ret;
1548 const struct x509_crt_verify_string *cur;
1549 char *p = buf;
1550 size_t n = size;
1551
1552 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1553 {
1554 if( ( flags & cur->code ) == 0 )
1555 continue;
1556
1557 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001558 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001559 flags ^= cur->code;
1560 }
1561
1562 if( flags != 0 )
1563 {
1564 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1565 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001566 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001567 }
1568
1569 return( (int) ( size - n ) );
1570}
1571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001573int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1574 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001575{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001576 unsigned int usage_must, usage_may;
1577 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1578 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1579
1580 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1581 return( 0 );
1582
1583 usage_must = usage & ~may_mask;
1584
1585 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1586 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1587
1588 usage_may = usage & may_mask;
1589
1590 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001592
1593 return( 0 );
1594}
1595#endif
1596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1598int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001599 const char *usage_oid,
1600 size_t usage_len )
1601{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001603
1604 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001606 return( 0 );
1607
1608 /*
1609 * Look for the requested usage (or wildcard ANY) in our list
1610 */
1611 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001614
1615 if( cur_oid->len == usage_len &&
1616 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1617 {
1618 return( 0 );
1619 }
1620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001622 return( 0 );
1623 }
1624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001626}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630/*
1631 * Return 1 if the certificate is revoked, or 0 otherwise.
1632 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001633int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636
1637 while( cur != NULL && cur->serial.len != 0 )
1638 {
1639 if( crt->serial.len == cur->serial.len &&
1640 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1641 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001642 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001643 return( 1 );
1644 }
1645
1646 cur = cur->next;
1647 }
1648
1649 return( 0 );
1650}
1651
1652/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01001653 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02001654 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001657 mbedtls_x509_crl *crl_list,
1658 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659{
1660 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1662 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001663
1664 if( ca == NULL )
1665 return( flags );
1666
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001667 while( crl_list != NULL )
1668 {
1669 if( crl_list->version == 0 ||
1670 crl_list->issuer_raw.len != ca->subject_raw.len ||
1671 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1672 crl_list->issuer_raw.len ) != 0 )
1673 {
1674 crl_list = crl_list->next;
1675 continue;
1676 }
1677
1678 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001679 * Check if the CA is configured to sign CRLs
1680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1682 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001685 break;
1686 }
1687#endif
1688
1689 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001690 * Check if CRL is correctly signed by the trusted CA
1691 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001692 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1693 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1694
1695 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1696 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02001699 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001700 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02001701 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001703 break;
1704 }
1705
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02001706 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001707 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1710 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001711 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001714 break;
1715 }
1716
1717 /*
1718 * Check for validity of CRL (Do not drop out)
1719 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001720 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001722
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001723 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001724 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001725
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001726 /*
1727 * Check if certificate is revoked
1728 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001729 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001732 break;
1733 }
1734
1735 crl_list = crl_list->next;
1736 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001737
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001738 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001739}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001741
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001742/*
1743 * Like memcmp, but case-insensitive and always returns -1 if different
1744 */
1745static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001746{
1747 size_t i;
1748 unsigned char diff;
1749 const unsigned char *n1 = s1, *n2 = s2;
1750
1751 for( i = 0; i < len; i++ )
1752 {
1753 diff = n1[i] ^ n2[i];
1754
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001755 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001756 continue;
1757
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001758 if( diff == 32 &&
1759 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1760 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1761 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001762 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001763 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001764
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001765 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001766 }
1767
1768 return( 0 );
1769}
1770
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001771/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001772 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001773 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02001774static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001775{
1776 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001777 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001778
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02001779 /* We can't have a match if there is no wildcard to match */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001780 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02001781 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001782
Paul Bakker14b16c62014-05-28 11:33:54 +02001783 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001784 {
1785 if( cn[i] == '.' )
1786 {
1787 cn_idx = i;
1788 break;
1789 }
1790 }
1791
1792 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001793 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001794
Paul Bakker14b16c62014-05-28 11:33:54 +02001795 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001796 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001797 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001798 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001799 }
1800
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001801 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001802}
1803
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001804/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001805 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1806 * variations (but not all).
1807 *
1808 * Return 0 if equal, -1 otherwise.
1809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001811{
1812 if( a->tag == b->tag &&
1813 a->len == b->len &&
1814 memcmp( a->p, b->p, b->len ) == 0 )
1815 {
1816 return( 0 );
1817 }
1818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1820 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001821 a->len == b->len &&
1822 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1823 {
1824 return( 0 );
1825 }
1826
1827 return( -1 );
1828}
1829
1830/*
1831 * Compare two X.509 Names (aka rdnSequence).
1832 *
1833 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1834 * we sometimes return unequal when the full algorithm would return equal,
1835 * but never the other way. (In particular, we don't do Unicode normalisation
1836 * or space folding.)
1837 *
1838 * Return 0 if equal, -1 otherwise.
1839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001841{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001842 /* Avoid recursion, it might not be optimised by the compiler */
1843 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001844 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001845 if( a == NULL || b == NULL )
1846 return( -1 );
1847
1848 /* type */
1849 if( a->oid.tag != b->oid.tag ||
1850 a->oid.len != b->oid.len ||
1851 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1852 {
1853 return( -1 );
1854 }
1855
1856 /* value */
1857 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1858 return( -1 );
1859
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001860 /* structure of the list of sets */
1861 if( a->next_merged != b->next_merged )
1862 return( -1 );
1863
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001864 a = a->next;
1865 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001866 }
1867
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001868 /* a == NULL == b */
1869 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001870}
1871
1872/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001873 * Check the signature of a certificate by its parent
1874 */
1875static int x509_crt_check_signature( const mbedtls_x509_crt *child,
1876 mbedtls_x509_crt *parent )
1877{
1878 const mbedtls_md_info_t *md_info;
1879 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1880
1881 md_info = mbedtls_md_info_from_type( child->sig_md );
1882 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
1883 {
1884 /* Note: this can't happen except after an internal error */
1885 return( -1 );
1886 }
1887
1888 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1889 child->sig_md, hash, mbedtls_md_get_size( md_info ),
1890 child->sig.p, child->sig.len ) != 0 )
1891 {
1892 return( -1 );
1893 }
1894
1895 return( 0 );
1896}
1897
1898/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001899 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1900 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001901 *
1902 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1905 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02001906 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001907{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001908 int need_ca_bit;
1909
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001910 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001911 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001912 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001913
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001914 /* Parent must have the basicConstraints CA bit set as a general rule */
1915 need_ca_bit = 1;
1916
1917 /* Exception: v1/v2 certificates that are locally trusted. */
1918 if( top && parent->version < 3 )
1919 need_ca_bit = 0;
1920
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001921 if( need_ca_bit && ! parent->ca_istrue )
1922 return( -1 );
1923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001925 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001927 {
1928 return( -1 );
1929 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001930#endif
1931
1932 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001933}
1934
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02001935/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001936 * Find a suitable parent for child in candidates, or return NULL.
1937 *
1938 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001939 * 1. subject name matches child's issuer
1940 * 2. if necessary, the CA bit is set and key usage allows signing certs
1941 * 3. for trusted roots, the signature is correct
1942 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001943 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02001944 * If there's a suitable candidate which is also time-valid, return the first
1945 * such. Otherwise, return the first suitable candidate (or NULL if there is
1946 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001947 *
1948 * The rationale for this rule is that someone could have a list of trusted
1949 * roots with two versions on the same root with different validity periods.
1950 * (At least one user reported having such a list and wanted it to just work.)
1951 * The reason we don't just require time-validity is that generally there is
1952 * only one version, and if it's expired we want the flags to state that
1953 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001954 *
1955 * The rationale for rule 3 (signature for trusted roots) is that users might
1956 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02001957 * way we select the correct one is by checking the signature (as we don't
1958 * rely on key identifier extensions). (This is one way users might choose to
1959 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01001960 *
1961 * Arguments:
1962 * - [in] child: certificate for which we're looking for a parent
1963 * - [in] candidates: chained list of potential parents
1964 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
1965 * of the chain, 0 otherwise
1966 * - [in] path_cnt: number of intermediates seen so far
1967 * - [in] self_cnt: number of self-signed intermediates seen so far
1968 * (will never be greater than path_cnt)
1969 *
1970 * Return value:
1971 * - the first suitable parent found (see above regarding time-validity)
1972 * - NULL if no suitable parent was found
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001973 */
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02001974static mbedtls_x509_crt *x509_crt_find_parent_in( mbedtls_x509_crt *child,
1975 mbedtls_x509_crt *candidates,
1976 int top,
Manuel Pégourié-Gonnard05c00ed2018-03-06 11:33:06 +01001977 size_t path_cnt,
1978 size_t self_cnt )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001979{
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001980 mbedtls_x509_crt *parent, *badtime_parent = NULL;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001981
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001982 for( parent = candidates; parent != NULL; parent = parent->next )
1983 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001984 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02001985 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001986 continue;
1987
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02001988 /* +1 because stored max_pathlen is 1 higher that the actual value */
1989 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnard05c00ed2018-03-06 11:33:06 +01001990 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02001991 {
1992 continue;
1993 }
1994
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001995 /* Signature */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001996 if( top && x509_crt_check_signature( child, parent ) != 0 )
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001997 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001998 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001999 }
2000
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002001 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002002 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2003 mbedtls_x509_time_is_future( &parent->valid_from ) )
2004 {
2005 if( badtime_parent == NULL )
2006 badtime_parent = parent;
2007
2008 continue;
2009 }
2010
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002011 break;
2012 }
2013
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002014 if( parent == NULL )
2015 parent = badtime_parent;
2016
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002017 return( parent );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002018}
2019
2020/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002021 * Find a parent in trusted CAs or the provided chain, or return NULL.
2022 *
2023 * Searches in trusted CAs first, and return the first suitable parent found
2024 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002025 *
2026 * Arguments:
2027 * - [in] child: certificate for which we're looking for a parent, followed
2028 * by a chain of possible intermediates
2029 * - [in] trust_ca: locally trusted CAs
2030 * - [out] 1 if parent was found in trust_ca, 0 if found in provided chain
2031 * - [in] path_cnt: number of intermediates seen so far
2032 * - [in] self_cnt: number of self-signed intermediates seen so far
2033 * (will always be no greater than path_cnt)
2034 *
2035 * Return value:
2036 * - the first suitable parent found (see find_parent_in() for "suitable")
2037 * - NULL if no suitable parent was found
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002038 */
2039static mbedtls_x509_crt *x509_crt_find_parent( mbedtls_x509_crt *child,
2040 mbedtls_x509_crt *trust_ca,
2041 int *parent_is_trusted,
Manuel Pégourié-Gonnardf5bb7812018-03-05 12:48:53 +01002042 size_t path_cnt,
2043 size_t self_cnt )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002044{
2045 mbedtls_x509_crt *parent;
2046
2047 /* Look for a parent in trusted CAs */
2048 *parent_is_trusted = 1;
2049 parent = x509_crt_find_parent_in( child, trust_ca, 1, path_cnt, self_cnt );
2050
2051 if( parent != NULL )
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002052 return( parent );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002053
2054 /* Look for a parent upwards the chain */
2055 *parent_is_trusted = 0;
2056 return( x509_crt_find_parent_in( child, child->next, 0, path_cnt, self_cnt ) );
2057}
2058
2059/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002060 * Check if an end-entity certificate is locally trusted
2061 *
2062 * Currently we require such certificates to be self-signed (actually only
2063 * check for self-issued as self-signatures are not checked)
2064 */
2065static int x509_crt_check_ee_locally_trusted(
2066 mbedtls_x509_crt *crt,
2067 mbedtls_x509_crt *trust_ca )
2068{
2069 mbedtls_x509_crt *cur;
2070
2071 /* must be self-issued */
2072 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2073 return( -1 );
2074
2075 /* look for an exact match with trusted cert */
2076 for( cur = trust_ca; cur != NULL; cur = cur->next )
2077 {
2078 if( crt->raw.len == cur->raw.len &&
2079 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2080 {
2081 return( 0 );
2082 }
2083 }
2084
2085 /* too bad */
2086 return( -1 );
2087}
2088
2089/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002090 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002091 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002092 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2093 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002094 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002095 * such that every cert in the chain is a child of the next one,
2096 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002097 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002098 * Verify that chain and return it with flags for all issues found.
2099 *
2100 * Special cases:
2101 * - EE == Rj -> return a one-element list containing it
2102 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2103 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002104 *
2105 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002106 * - [in] crt: the cert list EE, C1, ..., Cn
2107 * - [in] trust_ca: the trusted list R1, ..., Rp
2108 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002109 * - [out] ver_chain, chain_len: the built and verified chain
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002110 *
2111 * Return value:
2112 * - non-zero if the chain could not be fully built and examined
2113 * - 0 is the chain was successfully built and examined,
2114 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002115 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002116static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002117 mbedtls_x509_crt *crt,
2118 mbedtls_x509_crt *trust_ca,
2119 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002120 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002121 x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE],
2122 size_t *chain_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002123{
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002124 uint32_t *flags;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002125 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002126 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard6e786742017-07-03 23:47:44 +02002127 int parent_is_trusted = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002128 int child_is_trusted = 0;
Manuel Pégourié-Gonnardf5bb7812018-03-05 12:48:53 +01002129 size_t self_cnt = 0;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002130
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002131 child = crt;
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002132 *chain_len = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002133
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002134 while( 1 ) {
2135 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002136 ver_chain[*chain_len].crt = child;
2137 flags = &ver_chain[*chain_len].flags;
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002138 ++*chain_len;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002139
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002140 /* Check time-validity (all certificates) */
2141 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2142 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002143
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002144 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2145 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002146
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002147 /* Stop here for trusted roots (but not for trusted EE certs) */
2148 if( child_is_trusted )
2149 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002150
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002151 /* Check signature algorithm: MD & PK algs */
2152 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2153 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002154
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002155 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2156 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002157
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002158 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002159 if( *chain_len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002160 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2161 {
2162 return( 0 );
2163 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002164
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002165 /* Look for a parent in trusted CAs or up the chain */
2166 parent = x509_crt_find_parent( child, trust_ca, &parent_is_trusted,
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002167 *chain_len - 1, self_cnt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002168
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002169 /* No parent? We're done here */
2170 if( parent == NULL )
2171 {
2172 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2173 return( 0 );
2174 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002175
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002176 /* Count intermediate self-issued (not necessarily self-signed) certs.
2177 * These can occur with some strategies for key rollover, see [SIRO],
2178 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002179 if( *chain_len != 1 &&
2180 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2181 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002182 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002183 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002184
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002185 /* path_cnt is 0 for the first intermediate CA,
2186 * and if parent is trusted it's not an intermediate CA */
2187 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002188 *chain_len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002189 {
2190 /* return immediately to avoid overflow the chain array */
2191 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2192 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002193
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002194 /* if parent is trusted, the signature was checked by find_parent() */
2195 if( ! parent_is_trusted && x509_crt_check_signature( child, parent ) != 0 )
2196 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2197
2198 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002199 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002200 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002203 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002204 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002205#else
2206 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002207#endif
2208
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002209 /* prepare for next iteration */
2210 child = parent;
2211 parent = NULL;
2212 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002213 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002214}
2215
2216/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002217 * Check for CN match
2218 */
2219static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2220 const char *cn, size_t cn_len )
2221{
2222 /* try exact match */
2223 if( name->len == cn_len &&
2224 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2225 {
2226 return( 0 );
2227 }
2228
2229 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002230 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002231 {
2232 return( 0 );
2233 }
2234
2235 return( -1 );
2236}
2237
2238/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002239 * Verify the requested CN - only call this if cn is not NULL!
2240 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002241static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002242 const char *cn,
2243 uint32_t *flags )
2244{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002245 const mbedtls_x509_name *name;
2246 const mbedtls_x509_sequence *cur;
2247 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002248
2249 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
2250 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002251 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002252 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002253 if( x509_crt_check_cn( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002254 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002255 }
2256
2257 if( cur == NULL )
2258 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2259 }
2260 else
2261 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002262 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002263 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002264 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
2265 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002266 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002267 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002268 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002269 }
2270
2271 if( name == NULL )
2272 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2273 }
2274}
2275
2276/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002277 * Merge the flags for all certs in the chain, after calling callback
2278 */
2279static int x509_crt_merge_flags_with_cb(
2280 uint32_t *flags,
2281 x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE],
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002282 size_t chain_len,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002283 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2284 void *p_vrfy )
2285{
2286 int ret;
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002287 size_t i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002288 uint32_t cur_flags;
2289
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002290 for( i = chain_len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002291 {
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002292 cur_flags = ver_chain[i-1].flags;
2293
2294 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardf5bb7812018-03-05 12:48:53 +01002295 if( ( ret = f_vrfy( p_vrfy, ver_chain[i-1].crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002296 return( ret );
2297
2298 *flags |= cur_flags;
2299 }
2300
2301 return( 0 );
2302}
2303
2304/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002305 * Verify the certificate validity
2306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2308 mbedtls_x509_crt *trust_ca,
2309 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002310 const char *cn, uint32_t *flags,
2311 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002312 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002313{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002314 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002315 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002316}
2317
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002318/*
2319 * Verify the certificate validity, with profile
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002320 *
Manuel Pégourié-Gonnard66a36b02017-07-12 12:23:06 +02002321 * This function:
2322 * - checks the requested CN (if any)
2323 * - checks the type and size of the EE cert's key,
2324 * as that isn't done as part of chain building/verification currently
2325 * - builds and verifies the chain
2326 * - then calls the callback and merges the flags
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002327 */
2328int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2329 mbedtls_x509_crt *trust_ca,
2330 mbedtls_x509_crl *ca_crl,
2331 const mbedtls_x509_crt_profile *profile,
2332 const char *cn, uint32_t *flags,
2333 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2334 void *p_vrfy )
2335{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002336 int ret;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002337 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002338 x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002339 size_t chain_len;
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002340 uint32_t *ee_flags = &ver_chain[0].flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002341
2342 *flags = 0;
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002343 memset( ver_chain, 0, sizeof( ver_chain ) );
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002344 chain_len = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002345
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002346 if( profile == NULL )
2347 {
2348 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2349 goto exit;
2350 }
2351
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002352 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002353 if( cn != NULL )
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002354 x509_crt_verify_name( crt, cn, ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002355
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002356 /* Check the type and size of the key */
2357 pk_type = mbedtls_pk_get_type( &crt->pk );
2358
2359 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002360 *ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002361
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002362 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002363 *ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002364
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002365 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002366 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
2367 ver_chain, &chain_len );
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002368 if( ret != 0 )
2369 goto exit;
2370
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002371 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002372 ret = x509_crt_merge_flags_with_cb( flags,
2373 ver_chain, chain_len, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002374
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002375exit:
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02002376 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2377 * the SSL module for authmode optional, but non-zero return from the
2378 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002379 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2380 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2381
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002382 if( ret != 0 )
2383 {
2384 *flags = (uint32_t) -1;
2385 return( ret );
2386 }
2387
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002388 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002390
2391 return( 0 );
2392}
2393
2394/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002395 * Initialize a certificate chain
2396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002400}
2401
2402/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002403 * Unallocate all certificate data
2404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002406{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 mbedtls_x509_crt *cert_cur = crt;
2408 mbedtls_x509_crt *cert_prv;
2409 mbedtls_x509_name *name_cur;
2410 mbedtls_x509_name *name_prv;
2411 mbedtls_x509_sequence *seq_cur;
2412 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002413
2414 if( crt == NULL )
2415 return;
2416
2417 do
2418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2422 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002423#endif
2424
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002425 name_cur = cert_cur->issuer.next;
2426 while( name_cur != NULL )
2427 {
2428 name_prv = name_cur;
2429 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002430 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002432 }
2433
2434 name_cur = cert_cur->subject.next;
2435 while( name_cur != NULL )
2436 {
2437 name_prv = name_cur;
2438 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002439 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002441 }
2442
2443 seq_cur = cert_cur->ext_key_usage.next;
2444 while( seq_cur != NULL )
2445 {
2446 seq_prv = seq_cur;
2447 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002448 mbedtls_platform_zeroize( seq_prv,
2449 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002451 }
2452
2453 seq_cur = cert_cur->subject_alt_names.next;
2454 while( seq_cur != NULL )
2455 {
2456 seq_prv = seq_cur;
2457 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002458 mbedtls_platform_zeroize( seq_prv,
2459 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002461 }
2462
2463 if( cert_cur->raw.p != NULL )
2464 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002465 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002467 }
2468
2469 cert_cur = cert_cur->next;
2470 }
2471 while( cert_cur != NULL );
2472
2473 cert_cur = crt;
2474 do
2475 {
2476 cert_prv = cert_cur;
2477 cert_cur = cert_cur->next;
2478
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002479 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002480 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002482 }
2483 while( cert_cur != NULL );
2484}
2485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#endif /* MBEDTLS_X509_CRT_PARSE_C */