blob: 89194b320f52d10f4e05c7cf9511fe9e8c6e660f [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
46#include <stdio.h>
47#include <string.h>
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051#endif
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#else
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 */
582 extn_oid.tag = **p;
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
585 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586
587 extn_oid.p = *p;
588 *p += extn_oid.len;
589
590 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
592 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593
594 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
596 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
597 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200598
599 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
601 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
602 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200603
604 end_ext_octet = *p + len;
605
606 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
608 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200609
610 /*
611 * Detect supported extensions
612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614
615 if( ret != 0 )
616 {
617 /* No parser found, skip extension */
618 *p = end_ext_octet;
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621 if( is_critical )
622 {
623 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
625 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 }
627#endif
628 continue;
629 }
630
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100631 /* Forbid repeated extensions */
632 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100634
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 crt->ext_types |= ext_type;
636
637 switch( ext_type )
638 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100639 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 /* Parse basic constraints */
641 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
642 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200643 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644 break;
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647 /* Parse key usage */
648 if( ( ret = x509_get_key_usage( p, end_ext_octet,
649 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200650 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651 break;
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654 /* Parse extended key usage */
655 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
656 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200657 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658 break;
659
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100660 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661 /* Parse subject alt name */
662 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
663 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200664 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665 break;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668 /* Parse netscape certificate type */
669 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
670 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200671 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672 break;
673
674 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676 }
677 }
678
679 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
681 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682
683 return( 0 );
684}
685
686/*
687 * Parse and fill a single X.509 certificate in DER format
688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200690 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691{
692 int ret;
693 size_t len;
694 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
698 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
699 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700
701 /*
702 * Check for valid input
703 */
704 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706
Janos Follathcc0e49d2016-02-17 14:34:12 +0000707 // Use the original buffer until we figure out actual length
708 p = (unsigned char*) buf;
709 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710 end = p + len;
711
712 /*
713 * Certificate ::= SEQUENCE {
714 * tbsCertificate TBSCertificate,
715 * signatureAlgorithm AlgorithmIdentifier,
716 * signatureValue BIT STRING }
717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
719 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_x509_crt_free( crt );
722 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200723 }
724
725 if( len > (size_t) ( end - p ) )
726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_x509_crt_free( crt );
728 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
729 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730 }
731 crt_end = p + len;
732
Janos Follathcc0e49d2016-02-17 14:34:12 +0000733 // Create and populate a new buffer for the raw field
734 crt->raw.len = crt_end - buf;
735 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
736 if( p == NULL )
737 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
738
739 memcpy( p, buf, crt->raw.len );
740
Darryl Green11999bb2018-03-13 15:22:58 +0000741 // Direct pointers to the new buffer
Janos Follathcc0e49d2016-02-17 14:34:12 +0000742 p += crt->raw.len - len;
743 end = crt_end = p + len;
744
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745 /*
746 * TBSCertificate ::= SEQUENCE {
747 */
748 crt->tbs.p = p;
749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
751 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_x509_crt_free( crt );
754 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 }
756
757 end = p + len;
758 crt->tbs.len = end - crt->tbs.p;
759
760 /*
761 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
762 *
763 * CertificateSerialNumber ::= INTEGER
764 *
765 * signature AlgorithmIdentifier
766 */
767 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
769 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200770 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773 return( ret );
774 }
775
Andres AG7ca4a032017-03-09 16:16:11 +0000776 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_x509_crt_free( crt );
779 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780 }
781
Andres AG7ca4a032017-03-09 16:16:11 +0000782 crt->version++;
783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200785 &crt->sig_md, &crt->sig_pk,
786 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789 return( ret );
790 }
791
792 /*
793 * issuer Name
794 */
795 crt->issuer_raw.p = p;
796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
798 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_x509_crt_free( crt );
801 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200802 }
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807 return( ret );
808 }
809
810 crt->issuer_raw.len = p - crt->issuer_raw.p;
811
812 /*
813 * Validity ::= SEQUENCE {
814 * notBefore Time,
815 * notAfter Time }
816 *
817 */
818 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
819 &crt->valid_to ) ) != 0 )
820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200822 return( ret );
823 }
824
825 /*
826 * subject Name
827 */
828 crt->subject_raw.p = p;
829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
831 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_x509_crt_free( crt );
834 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200835 }
836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200840 return( ret );
841 }
842
843 crt->subject_raw.len = p - crt->subject_raw.p;
844
845 /*
846 * SubjectPublicKeyInfo
847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851 return( ret );
852 }
853
854 /*
855 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
856 * -- If present, version shall be v2 or v3
857 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
858 * -- If present, version shall be v2 or v3
859 * extensions [3] EXPLICIT Extensions OPTIONAL
860 * -- If present, version shall be v3
861 */
862 if( crt->version == 2 || crt->version == 3 )
863 {
864 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
865 if( ret != 0 )
866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868 return( ret );
869 }
870 }
871
872 if( crt->version == 2 || crt->version == 3 )
873 {
874 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
875 if( ret != 0 )
876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878 return( ret );
879 }
880 }
881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200884#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100885 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200886 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 if( ret != 0 )
888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 return( ret );
891 }
892 }
893
894 if( p != end )
895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_x509_crt_free( crt );
897 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
898 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 }
900
901 end = crt_end;
902
903 /*
904 * }
905 * -- end of TBSCertificate
906 *
907 * signatureAlgorithm AlgorithmIdentifier,
908 * signatureValue BIT STRING
909 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913 return( ret );
914 }
915
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100916 if( crt->sig_oid.len != sig_oid2.len ||
917 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200918 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200919 ( sig_params1.len != 0 &&
920 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 mbedtls_x509_crt_free( crt );
923 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924 }
925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929 return( ret );
930 }
931
932 if( p != end )
933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 mbedtls_x509_crt_free( crt );
935 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
936 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937 }
938
939 return( 0 );
940}
941
942/*
943 * Parse one X.509 certificate in DER format from a buffer and add them to a
944 * chained list
945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200947 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948{
949 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951
952 /*
953 * Check for valid input
954 */
955 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957
958 while( crt->version != 0 && crt->next != NULL )
959 {
960 prev = crt;
961 crt = crt->next;
962 }
963
964 /*
965 * Add new certificate on the end of the chain if needed.
966 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200967 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200969 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970
971 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200972 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973
974 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977 }
978
Paul Bakkerddf26b42013-09-18 13:46:23 +0200979 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980 {
981 if( prev )
982 prev->next = NULL;
983
984 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986
987 return( ret );
988 }
989
990 return( 0 );
991}
992
993/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200994 * Parse one or more PEM certificates from a buffer and add them to the chained
995 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998{
Janos Follath98e28a72016-05-31 14:03:54 +0100999#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001000 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001002#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003
1004 /*
1005 * Check for valid input
1006 */
1007 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009
1010 /*
1011 * Determine buffer content. Buffer contains either one DER certificate or
1012 * one or more PEM certificates.
1013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001015 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001016 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001019 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1022 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001023#else
1024 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1025#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#if defined(MBEDTLS_PEM_PARSE_C)
1028 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029 {
1030 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001033 /* 1 rather than 0 since the terminating NULL byte is counted in */
1034 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035 {
1036 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001039 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041 "-----BEGIN CERTIFICATE-----",
1042 "-----END CERTIFICATE-----",
1043 buf, NULL, 0, &use_len );
1044
1045 if( ret == 0 )
1046 {
1047 /*
1048 * Was PEM encoded
1049 */
1050 buflen -= use_len;
1051 buf += use_len;
1052 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001054 {
1055 return( ret );
1056 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060
1061 /*
1062 * PEM header and footer were found
1063 */
1064 buflen -= use_len;
1065 buf += use_len;
1066
1067 if( first_error == 0 )
1068 first_error = ret;
1069
Paul Bakker5a5fa922014-09-26 14:53:04 +02001070 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071 continue;
1072 }
1073 else
1074 break;
1075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079
1080 if( ret != 0 )
1081 {
1082 /*
1083 * Quit parsing on a memory error
1084 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001085 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086 return( ret );
1087
1088 if( first_error == 0 )
1089 first_error = ret;
1090
1091 total_failed++;
1092 continue;
1093 }
1094
1095 success = 1;
1096 }
1097 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098
1099 if( success )
1100 return( total_failed );
1101 else if( first_error )
1102 return( first_error );
1103 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001105#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106}
1107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109/*
1110 * Load one or more certificates and add them to the chained list
1111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113{
1114 int ret;
1115 size_t n;
1116 unsigned char *buf;
1117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119 return( ret );
1120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001123 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001125
1126 return( ret );
1127}
1128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001130{
1131 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001132#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001133 int w_ret;
1134 WCHAR szDir[MAX_PATH];
1135 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001136 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001137 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001138
Paul Bakker9af723c2014-05-01 13:03:14 +02001139 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140 HANDLE hFind;
1141
1142 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001144
Paul Bakker9af723c2014-05-01 13:03:14 +02001145 memset( szDir, 0, sizeof(szDir) );
1146 memset( filename, 0, MAX_PATH );
1147 memcpy( filename, path, len );
1148 filename[len++] = '\\';
1149 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150 filename[len++] = '*';
1151
Simon B3c6b18d2016-11-03 01:11:37 +00001152 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001153 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001154 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156
1157 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001158 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160
1161 len = MAX_PATH - len;
1162 do
1163 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001164 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001165
1166 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1167 continue;
1168
Paul Bakker9af723c2014-05-01 13:03:14 +02001169 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001170 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001171 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001172 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001173 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001174 {
1175 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1176 goto cleanup;
1177 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 if( w_ret < 0 )
1181 ret++;
1182 else
1183 ret += w_ret;
1184 }
1185 while( FindNextFileW( hFind, &file_data ) != 0 );
1186
Paul Bakker66d5d072014-06-17 16:39:18 +02001187 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189
Ron Eldor36d90422017-01-09 15:09:16 +02001190cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001192#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001193 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001194 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001195 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001196 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001197 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001198 DIR *dir = opendir( path );
1199
Paul Bakker66d5d072014-06-17 16:39:18 +02001200 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001202
Ron Eldor63140682017-01-09 19:27:59 +02001203#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001204 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001205 {
1206 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001207 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001208 }
Ron Eldor63140682017-01-09 19:27:59 +02001209#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001210
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001211 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001212 {
Andres AGf9113192016-09-02 14:06:04 +01001213 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1214 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001215
Andres AGf9113192016-09-02 14:06:04 +01001216 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001217 {
Andres AGf9113192016-09-02 14:06:04 +01001218 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1219 goto cleanup;
1220 }
1221 else if( stat( entry_name, &sb ) == -1 )
1222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001224 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001225 }
1226
1227 if( !S_ISREG( sb.st_mode ) )
1228 continue;
1229
1230 // Ignore parse errors
1231 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001233 if( t_ret < 0 )
1234 ret++;
1235 else
1236 ret += t_ret;
1237 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001238
1239cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001240 closedir( dir );
1241
Ron Eldor63140682017-01-09 19:27:59 +02001242#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001243 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001245#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001246
Paul Bakkerbe089b02013-10-14 15:51:50 +02001247#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001248
1249 return( ret );
1250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001252
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001253static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001255{
1256 size_t i;
1257 size_t n = *size;
1258 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001260 const char *sep = "";
1261 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001262
1263 while( cur != NULL )
1264 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001265 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001266 {
1267 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001268 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001269 }
1270
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001271 n -= cur->buf.len + sep_len;
1272 for( i = 0; i < sep_len; i++ )
1273 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001274 for( i = 0; i < cur->buf.len; i++ )
1275 *p++ = cur->buf.p[i];
1276
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001277 sep = ", ";
1278 sep_len = 2;
1279
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001280 cur = cur->next;
1281 }
1282
1283 *p = '\0';
1284
1285 *size = n;
1286 *buf = p;
1287
1288 return( 0 );
1289}
1290
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001291#define PRINT_ITEM(i) \
1292 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001294 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001295 sep = ", "; \
1296 }
1297
1298#define CERT_TYPE(type,name) \
1299 if( ns_cert_type & type ) \
1300 PRINT_ITEM( name );
1301
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001302static int x509_info_cert_type( char **buf, size_t *size,
1303 unsigned char ns_cert_type )
1304{
1305 int ret;
1306 size_t n = *size;
1307 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001308 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001311 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1313 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1314 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001315 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1316 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1317 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001318
1319 *size = n;
1320 *buf = p;
1321
1322 return( 0 );
1323}
1324
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001325#define KEY_USAGE(code,name) \
1326 if( key_usage & code ) \
1327 PRINT_ITEM( name );
1328
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001329static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001330 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001331{
1332 int ret;
1333 size_t n = *size;
1334 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001335 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1338 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001339 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1340 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1341 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1343 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001344 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1345 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001346
1347 *size = n;
1348 *buf = p;
1349
1350 return( 0 );
1351}
1352
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001353static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001355{
1356 int ret;
1357 const char *desc;
1358 size_t n = *size;
1359 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001361 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001362
1363 while( cur != NULL )
1364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001366 desc = "???";
1367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001369 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001370
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001371 sep = ", ";
1372
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001373 cur = cur->next;
1374 }
1375
1376 *size = n;
1377 *buf = p;
1378
1379 return( 0 );
1380}
1381
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382/*
1383 * Return an informational string about the certificate.
1384 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001385#define BEFORE_COLON 18
1386#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1388 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389{
1390 int ret;
1391 size_t n;
1392 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001393 char key_size_str[BEFORE_COLON];
1394
1395 p = buf;
1396 n = size;
1397
Janos Follath98e28a72016-05-31 14:03:54 +01001398 if( NULL == crt )
1399 {
1400 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
1401 MBEDTLS_X509_SAFE_SNPRINTF;
1402
1403 return( (int) ( size - n ) );
1404 }
1405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001407 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001408 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001410 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001411 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001414 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001417 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001419 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001422 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001424 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1428 crt->valid_from.year, crt->valid_from.mon,
1429 crt->valid_from.day, crt->valid_from.hour,
1430 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001431 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001434 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1435 crt->valid_to.year, crt->valid_to.mon,
1436 crt->valid_to.day, crt->valid_to.hour,
1437 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001438 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001444 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001445 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001447 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1449 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001450 {
1451 return( ret );
1452 }
1453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001455 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001456 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001458 /*
1459 * Optional extensions
1460 */
1461
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001462 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001465 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001466 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001467
1468 if( crt->max_pathlen > 0 )
1469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001471 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001472 }
1473 }
1474
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001475 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001478 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001479
1480 if( ( ret = x509_info_subject_alt_name( &p, &n,
1481 &crt->subject_alt_names ) ) != 0 )
1482 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001483 }
1484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001488 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001489
1490 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1491 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001492 }
1493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001497 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001498
1499 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1500 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001501 }
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001506 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001507
1508 if( ( ret = x509_info_ext_key_usage( &p, &n,
1509 &crt->ext_key_usage ) ) != 0 )
1510 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001511 }
1512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001514 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001515
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516 return( (int) ( size - n ) );
1517}
1518
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001519struct x509_crt_verify_string {
1520 int code;
1521 const char *string;
1522};
1523
1524static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001525 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001526 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1527 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1528 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1529 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1530 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001531 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1532 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1533 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001534 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001535 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1536 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1537 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1538 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001539 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1540 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1541 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1542 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1543 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1544 { 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 +01001545 { 0, NULL }
1546};
1547
1548int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001549 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001550{
1551 int ret;
1552 const struct x509_crt_verify_string *cur;
1553 char *p = buf;
1554 size_t n = size;
1555
1556 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1557 {
1558 if( ( flags & cur->code ) == 0 )
1559 continue;
1560
1561 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001562 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001563 flags ^= cur->code;
1564 }
1565
1566 if( flags != 0 )
1567 {
1568 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1569 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001570 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001571 }
1572
1573 return( (int) ( size - n ) );
1574}
1575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001577int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1578 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001579{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001580 unsigned int usage_must, usage_may;
1581 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1582 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1583
1584 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1585 return( 0 );
1586
1587 usage_must = usage & ~may_mask;
1588
1589 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1590 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1591
1592 usage_may = usage & may_mask;
1593
1594 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001596
1597 return( 0 );
1598}
1599#endif
1600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1602int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001603 const char *usage_oid,
1604 size_t usage_len )
1605{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001607
1608 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001610 return( 0 );
1611
1612 /*
1613 * Look for the requested usage (or wildcard ANY) in our list
1614 */
1615 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001618
1619 if( cur_oid->len == usage_len &&
1620 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1621 {
1622 return( 0 );
1623 }
1624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001626 return( 0 );
1627 }
1628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001630}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634/*
1635 * Return 1 if the certificate is revoked, or 0 otherwise.
1636 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001637int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640
1641 while( cur != NULL && cur->serial.len != 0 )
1642 {
1643 if( crt->serial.len == cur->serial.len &&
1644 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1645 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001646 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001647 return( 1 );
1648 }
1649
1650 cur = cur->next;
1651 }
1652
1653 return( 0 );
1654}
1655
1656/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01001657 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02001658 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001661 mbedtls_x509_crl *crl_list,
1662 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001663{
1664 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1666 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001667
1668 if( ca == NULL )
1669 return( flags );
1670
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671 while( crl_list != NULL )
1672 {
1673 if( crl_list->version == 0 ||
1674 crl_list->issuer_raw.len != ca->subject_raw.len ||
1675 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1676 crl_list->issuer_raw.len ) != 0 )
1677 {
1678 crl_list = crl_list->next;
1679 continue;
1680 }
1681
1682 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001683 * Check if the CA is configured to sign CRLs
1684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1686 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001689 break;
1690 }
1691#endif
1692
1693 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001694 * Check if CRL is correctly signed by the trusted CA
1695 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001696 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1697 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1698
1699 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1700 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02001703 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001704 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02001705 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001707 break;
1708 }
1709
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02001710 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001711 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1714 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001715 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001718 break;
1719 }
1720
1721 /*
1722 * Check for validity of CRL (Do not drop out)
1723 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001724 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001726
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001727 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001728 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001729
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001730 /*
1731 * Check if certificate is revoked
1732 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001733 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736 break;
1737 }
1738
1739 crl_list = crl_list->next;
1740 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001741
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001742 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001743}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001746/*
1747 * Like memcmp, but case-insensitive and always returns -1 if different
1748 */
1749static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001750{
1751 size_t i;
1752 unsigned char diff;
1753 const unsigned char *n1 = s1, *n2 = s2;
1754
1755 for( i = 0; i < len; i++ )
1756 {
1757 diff = n1[i] ^ n2[i];
1758
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001759 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001760 continue;
1761
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001762 if( diff == 32 &&
1763 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1764 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1765 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001766 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001767 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001769 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001770 }
1771
1772 return( 0 );
1773}
1774
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001775/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001776 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001777 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02001778static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001779{
1780 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001781 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001782
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02001783 /* We can't have a match if there is no wildcard to match */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001784 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02001785 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001786
Paul Bakker14b16c62014-05-28 11:33:54 +02001787 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001788 {
1789 if( cn[i] == '.' )
1790 {
1791 cn_idx = i;
1792 break;
1793 }
1794 }
1795
1796 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001797 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001798
Paul Bakker14b16c62014-05-28 11:33:54 +02001799 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001800 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001801 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001802 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001803 }
1804
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001805 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001806}
1807
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001808/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001809 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1810 * variations (but not all).
1811 *
1812 * Return 0 if equal, -1 otherwise.
1813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001815{
1816 if( a->tag == b->tag &&
1817 a->len == b->len &&
1818 memcmp( a->p, b->p, b->len ) == 0 )
1819 {
1820 return( 0 );
1821 }
1822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1824 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001825 a->len == b->len &&
1826 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1827 {
1828 return( 0 );
1829 }
1830
1831 return( -1 );
1832}
1833
1834/*
1835 * Compare two X.509 Names (aka rdnSequence).
1836 *
1837 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1838 * we sometimes return unequal when the full algorithm would return equal,
1839 * but never the other way. (In particular, we don't do Unicode normalisation
1840 * or space folding.)
1841 *
1842 * Return 0 if equal, -1 otherwise.
1843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001845{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001846 /* Avoid recursion, it might not be optimised by the compiler */
1847 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001848 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001849 if( a == NULL || b == NULL )
1850 return( -1 );
1851
1852 /* type */
1853 if( a->oid.tag != b->oid.tag ||
1854 a->oid.len != b->oid.len ||
1855 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1856 {
1857 return( -1 );
1858 }
1859
1860 /* value */
1861 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1862 return( -1 );
1863
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001864 /* structure of the list of sets */
1865 if( a->next_merged != b->next_merged )
1866 return( -1 );
1867
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001868 a = a->next;
1869 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001870 }
1871
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001872 /* a == NULL == b */
1873 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001874}
1875
1876/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001877 * Check the signature of a certificate by its parent
1878 */
1879static int x509_crt_check_signature( const mbedtls_x509_crt *child,
1880 mbedtls_x509_crt *parent )
1881{
1882 const mbedtls_md_info_t *md_info;
1883 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1884
1885 md_info = mbedtls_md_info_from_type( child->sig_md );
1886 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
1887 {
1888 /* Note: this can't happen except after an internal error */
1889 return( -1 );
1890 }
1891
1892 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1893 child->sig_md, hash, mbedtls_md_get_size( md_info ),
1894 child->sig.p, child->sig.len ) != 0 )
1895 {
1896 return( -1 );
1897 }
1898
1899 return( 0 );
1900}
1901
1902/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001903 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1904 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001905 *
1906 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1909 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02001910 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001911{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001912 int need_ca_bit;
1913
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001914 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001915 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001916 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001917
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001918 /* Parent must have the basicConstraints CA bit set as a general rule */
1919 need_ca_bit = 1;
1920
1921 /* Exception: v1/v2 certificates that are locally trusted. */
1922 if( top && parent->version < 3 )
1923 need_ca_bit = 0;
1924
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001925 if( need_ca_bit && ! parent->ca_istrue )
1926 return( -1 );
1927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001929 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001931 {
1932 return( -1 );
1933 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001934#endif
1935
1936 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001937}
1938
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02001939/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001940 * Find a suitable parent for child in candidates, or return NULL.
1941 *
1942 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001943 * 1. subject name matches child's issuer
1944 * 2. if necessary, the CA bit is set and key usage allows signing certs
1945 * 3. for trusted roots, the signature is correct
1946 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001947 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02001948 * If there's a suitable candidate which is also time-valid, return the first
1949 * such. Otherwise, return the first suitable candidate (or NULL if there is
1950 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001951 *
1952 * The rationale for this rule is that someone could have a list of trusted
1953 * roots with two versions on the same root with different validity periods.
1954 * (At least one user reported having such a list and wanted it to just work.)
1955 * The reason we don't just require time-validity is that generally there is
1956 * only one version, and if it's expired we want the flags to state that
1957 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001958 *
1959 * The rationale for rule 3 (signature for trusted roots) is that users might
1960 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02001961 * way we select the correct one is by checking the signature (as we don't
1962 * rely on key identifier extensions). (This is one way users might choose to
1963 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01001964 *
1965 * Arguments:
1966 * - [in] child: certificate for which we're looking for a parent
1967 * - [in] candidates: chained list of potential parents
1968 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
1969 * of the chain, 0 otherwise
1970 * - [in] path_cnt: number of intermediates seen so far
1971 * - [in] self_cnt: number of self-signed intermediates seen so far
1972 * (will never be greater than path_cnt)
1973 *
1974 * Return value:
1975 * - the first suitable parent found (see above regarding time-validity)
1976 * - NULL if no suitable parent was found
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001977 */
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02001978static mbedtls_x509_crt *x509_crt_find_parent_in( mbedtls_x509_crt *child,
1979 mbedtls_x509_crt *candidates,
1980 int top,
Manuel Pégourié-Gonnard05c00ed2018-03-06 11:33:06 +01001981 size_t path_cnt,
1982 size_t self_cnt )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001983{
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001984 mbedtls_x509_crt *parent, *badtime_parent = NULL;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001985
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001986 for( parent = candidates; parent != NULL; parent = parent->next )
1987 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001988 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02001989 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001990 continue;
1991
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02001992 /* +1 because stored max_pathlen is 1 higher that the actual value */
1993 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnard05c00ed2018-03-06 11:33:06 +01001994 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02001995 {
1996 continue;
1997 }
1998
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001999 /* Signature */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002000 if( top && x509_crt_check_signature( child, parent ) != 0 )
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002001 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002002 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002003 }
2004
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002005 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002006 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2007 mbedtls_x509_time_is_future( &parent->valid_from ) )
2008 {
2009 if( badtime_parent == NULL )
2010 badtime_parent = parent;
2011
2012 continue;
2013 }
2014
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002015 break;
2016 }
2017
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002018 if( parent == NULL )
2019 parent = badtime_parent;
2020
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002021 return( parent );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002022}
2023
2024/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002025 * Find a parent in trusted CAs or the provided chain, or return NULL.
2026 *
2027 * Searches in trusted CAs first, and return the first suitable parent found
2028 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002029 *
2030 * Arguments:
2031 * - [in] child: certificate for which we're looking for a parent, followed
2032 * by a chain of possible intermediates
2033 * - [in] trust_ca: locally trusted CAs
2034 * - [out] 1 if parent was found in trust_ca, 0 if found in provided chain
2035 * - [in] path_cnt: number of intermediates seen so far
2036 * - [in] self_cnt: number of self-signed intermediates seen so far
2037 * (will always be no greater than path_cnt)
2038 *
2039 * Return value:
2040 * - the first suitable parent found (see find_parent_in() for "suitable")
2041 * - NULL if no suitable parent was found
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002042 */
2043static mbedtls_x509_crt *x509_crt_find_parent( mbedtls_x509_crt *child,
2044 mbedtls_x509_crt *trust_ca,
2045 int *parent_is_trusted,
Manuel Pégourié-Gonnardf5bb7812018-03-05 12:48:53 +01002046 size_t path_cnt,
2047 size_t self_cnt )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002048{
2049 mbedtls_x509_crt *parent;
2050
2051 /* Look for a parent in trusted CAs */
2052 *parent_is_trusted = 1;
2053 parent = x509_crt_find_parent_in( child, trust_ca, 1, path_cnt, self_cnt );
2054
2055 if( parent != NULL )
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002056 return( parent );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002057
2058 /* Look for a parent upwards the chain */
2059 *parent_is_trusted = 0;
2060 return( x509_crt_find_parent_in( child, child->next, 0, path_cnt, self_cnt ) );
2061}
2062
2063/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002064 * Check if an end-entity certificate is locally trusted
2065 *
2066 * Currently we require such certificates to be self-signed (actually only
2067 * check for self-issued as self-signatures are not checked)
2068 */
2069static int x509_crt_check_ee_locally_trusted(
2070 mbedtls_x509_crt *crt,
2071 mbedtls_x509_crt *trust_ca )
2072{
2073 mbedtls_x509_crt *cur;
2074
2075 /* must be self-issued */
2076 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2077 return( -1 );
2078
2079 /* look for an exact match with trusted cert */
2080 for( cur = trust_ca; cur != NULL; cur = cur->next )
2081 {
2082 if( crt->raw.len == cur->raw.len &&
2083 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2084 {
2085 return( 0 );
2086 }
2087 }
2088
2089 /* too bad */
2090 return( -1 );
2091}
2092
2093/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002094 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002095 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002096 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2097 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002098 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002099 * such that every cert in the chain is a child of the next one,
2100 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002101 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002102 * Verify that chain and return it with flags for all issues found.
2103 *
2104 * Special cases:
2105 * - EE == Rj -> return a one-element list containing it
2106 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2107 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002108 *
2109 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002110 * - [in] crt: the cert list EE, C1, ..., Cn
2111 * - [in] trust_ca: the trusted list R1, ..., Rp
2112 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002113 * - [out] ver_chain, chain_len: the built and verified chain
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002114 *
2115 * Return value:
2116 * - non-zero if the chain could not be fully built and examined
2117 * - 0 is the chain was successfully built and examined,
2118 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002119 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002120static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002121 mbedtls_x509_crt *crt,
2122 mbedtls_x509_crt *trust_ca,
2123 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002124 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002125 x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE],
2126 size_t *chain_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002127{
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002128 uint32_t *flags;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002129 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002130 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard6e786742017-07-03 23:47:44 +02002131 int parent_is_trusted = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002132 int child_is_trusted = 0;
Manuel Pégourié-Gonnardf5bb7812018-03-05 12:48:53 +01002133 size_t self_cnt = 0;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002134
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002135 child = crt;
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002136 *chain_len = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002137
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002138 while( 1 ) {
2139 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002140 ver_chain[*chain_len].crt = child;
2141 flags = &ver_chain[*chain_len].flags;
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002142 ++*chain_len;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002143
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002144 /* Check time-validity (all certificates) */
2145 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2146 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002147
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002148 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2149 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002150
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002151 /* Stop here for trusted roots (but not for trusted EE certs) */
2152 if( child_is_trusted )
2153 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002154
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002155 /* Check signature algorithm: MD & PK algs */
2156 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2157 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002158
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002159 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2160 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002161
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002162 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002163 if( *chain_len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002164 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2165 {
2166 return( 0 );
2167 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002168
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002169 /* Look for a parent in trusted CAs or up the chain */
2170 parent = x509_crt_find_parent( child, trust_ca, &parent_is_trusted,
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002171 *chain_len - 1, self_cnt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002172
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002173 /* No parent? We're done here */
2174 if( parent == NULL )
2175 {
2176 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2177 return( 0 );
2178 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002179
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002180 /* Count intermediate self-issued (not necessarily self-signed) certs.
2181 * These can occur with some strategies for key rollover, see [SIRO],
2182 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002183 if( *chain_len != 1 &&
2184 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2185 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002186 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002187 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002188
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002189 /* path_cnt is 0 for the first intermediate CA,
2190 * and if parent is trusted it's not an intermediate CA */
2191 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002192 *chain_len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002193 {
2194 /* return immediately to avoid overflow the chain array */
2195 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2196 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002197
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002198 /* if parent is trusted, the signature was checked by find_parent() */
2199 if( ! parent_is_trusted && x509_crt_check_signature( child, parent ) != 0 )
2200 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2201
2202 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002203 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002204 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002207 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002208 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002209#else
2210 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002211#endif
2212
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002213 /* prepare for next iteration */
2214 child = parent;
2215 parent = NULL;
2216 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002217 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002218}
2219
2220/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002221 * Check for CN match
2222 */
2223static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2224 const char *cn, size_t cn_len )
2225{
2226 /* try exact match */
2227 if( name->len == cn_len &&
2228 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2229 {
2230 return( 0 );
2231 }
2232
2233 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002234 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002235 {
2236 return( 0 );
2237 }
2238
2239 return( -1 );
2240}
2241
2242/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002243 * Verify the requested CN - only call this if cn is not NULL!
2244 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002245static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002246 const char *cn,
2247 uint32_t *flags )
2248{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002249 const mbedtls_x509_name *name;
2250 const mbedtls_x509_sequence *cur;
2251 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002252
2253 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
2254 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002255 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002256 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002257 if( x509_crt_check_cn( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002258 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002259 }
2260
2261 if( cur == NULL )
2262 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2263 }
2264 else
2265 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002266 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002267 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002268 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
2269 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002270 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002271 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002272 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002273 }
2274
2275 if( name == NULL )
2276 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2277 }
2278}
2279
2280/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002281 * Merge the flags for all certs in the chain, after calling callback
2282 */
2283static int x509_crt_merge_flags_with_cb(
2284 uint32_t *flags,
2285 x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE],
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002286 size_t chain_len,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002287 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2288 void *p_vrfy )
2289{
2290 int ret;
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002291 size_t i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002292 uint32_t cur_flags;
2293
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002294 for( i = chain_len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002295 {
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002296 cur_flags = ver_chain[i-1].flags;
2297
2298 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardf5bb7812018-03-05 12:48:53 +01002299 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 +02002300 return( ret );
2301
2302 *flags |= cur_flags;
2303 }
2304
2305 return( 0 );
2306}
2307
2308/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002309 * Verify the certificate validity
2310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2312 mbedtls_x509_crt *trust_ca,
2313 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002314 const char *cn, uint32_t *flags,
2315 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002316 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002317{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002318 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002319 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002320}
2321
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002322/*
2323 * Verify the certificate validity, with profile
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002324 *
Manuel Pégourié-Gonnard66a36b02017-07-12 12:23:06 +02002325 * This function:
2326 * - checks the requested CN (if any)
2327 * - checks the type and size of the EE cert's key,
2328 * as that isn't done as part of chain building/verification currently
2329 * - builds and verifies the chain
2330 * - then calls the callback and merges the flags
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002331 */
2332int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2333 mbedtls_x509_crt *trust_ca,
2334 mbedtls_x509_crl *ca_crl,
2335 const mbedtls_x509_crt_profile *profile,
2336 const char *cn, uint32_t *flags,
2337 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2338 void *p_vrfy )
2339{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002340 int ret;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002341 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002342 x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002343 size_t chain_len;
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002344 uint32_t *ee_flags = &ver_chain[0].flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002345
2346 *flags = 0;
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002347 memset( ver_chain, 0, sizeof( ver_chain ) );
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002348 chain_len = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002349
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002350 if( profile == NULL )
2351 {
2352 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2353 goto exit;
2354 }
2355
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002356 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002357 if( cn != NULL )
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002358 x509_crt_verify_name( crt, cn, ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002359
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002360 /* Check the type and size of the key */
2361 pk_type = mbedtls_pk_get_type( &crt->pk );
2362
2363 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002364 *ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002365
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002366 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002367 *ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002368
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002369 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002370 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
2371 ver_chain, &chain_len );
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002372 if( ret != 0 )
2373 goto exit;
2374
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002375 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002376 ret = x509_crt_merge_flags_with_cb( flags,
2377 ver_chain, chain_len, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002378
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002379exit:
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02002380 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2381 * the SSL module for authmode optional, but non-zero return from the
2382 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002383 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2384 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2385
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002386 if( ret != 0 )
2387 {
2388 *flags = (uint32_t) -1;
2389 return( ret );
2390 }
2391
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002392 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002394
2395 return( 0 );
2396}
2397
2398/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002399 * Initialize a certificate chain
2400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002402{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002404}
2405
2406/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002407 * Unallocate all certificate data
2408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002410{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 mbedtls_x509_crt *cert_cur = crt;
2412 mbedtls_x509_crt *cert_prv;
2413 mbedtls_x509_name *name_cur;
2414 mbedtls_x509_name *name_prv;
2415 mbedtls_x509_sequence *seq_cur;
2416 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002417
2418 if( crt == NULL )
2419 return;
2420
2421 do
2422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2426 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002427#endif
2428
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002429 name_cur = cert_cur->issuer.next;
2430 while( name_cur != NULL )
2431 {
2432 name_prv = name_cur;
2433 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002434 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002436 }
2437
2438 name_cur = cert_cur->subject.next;
2439 while( name_cur != NULL )
2440 {
2441 name_prv = name_cur;
2442 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002443 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002445 }
2446
2447 seq_cur = cert_cur->ext_key_usage.next;
2448 while( seq_cur != NULL )
2449 {
2450 seq_prv = seq_cur;
2451 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002452 mbedtls_platform_zeroize( seq_prv,
2453 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002455 }
2456
2457 seq_cur = cert_cur->subject_alt_names.next;
2458 while( seq_cur != NULL )
2459 {
2460 seq_prv = seq_cur;
2461 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002462 mbedtls_platform_zeroize( seq_prv,
2463 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002465 }
2466
2467 if( cert_cur->raw.p != NULL )
2468 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002469 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002471 }
2472
2473 cert_cur = cert_cur->next;
2474 }
2475 while( cert_cur != NULL );
2476
2477 cert_cur = crt;
2478 do
2479 {
2480 cert_prv = cert_cur;
2481 cert_cur = cert_cur->next;
2482
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002483 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002484 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002486 }
2487 while( cert_cur != NULL );
2488}
2489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490#endif /* MBEDTLS_X509_CRT_PARSE_C */