blob: 1a3eb2d965973ec9305a6e3f74a93bbf1565ae86 [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"
Rich Evans00ab4702015-02-06 13:43:58 +000044
45#include <stdio.h>
46#include <string.h>
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Rich Evans00ab4702015-02-06 13:43:58 +000055#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020057#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059#endif
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010063#endif
64
Paul Bakkerfa6a6202013-10-28 18:48:30 +010065#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066#include <windows.h>
67#else
68#include <time.h>
69#endif
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000072#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020073#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#include <sys/types.h>
75#include <sys/stat.h>
76#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000077#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078#endif
79
Paul Bakker34617722014-06-13 17:20:13 +020080/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020082 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
83}
84
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020086 * Default profile
87 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020088const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
89{
Gilles Peskine5d2511c2017-05-12 13:16:40 +020090#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +020091 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020092 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +020093#endif
94 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020095 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
96 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
97 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
98 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
99 0xFFFFFFF, /* Any PK alg */
100 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200101 2048,
102};
103
104/*
105 * Next-default profile
106 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200107const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
108{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200109 /* Hashes from SHA-256 and above */
110 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
111 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
112 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
113 0xFFFFFFF, /* Any PK alg */
114#if defined(MBEDTLS_ECP_C)
115 /* Curves at or above 128-bit security level */
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
120 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
121 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
122 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
123#else
124 0,
125#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200126 2048,
127};
128
129/*
130 * NSA Suite B Profile
131 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200132const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
133{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200134 /* Only SHA-256 and 384 */
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
136 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
137 /* Only ECDSA */
138 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ),
139#if defined(MBEDTLS_ECP_C)
140 /* Only NIST P-256 and P-384 */
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
142 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
143#else
144 0,
145#endif
146 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200147};
148
149/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200150 * Check md_alg against profile
151 * Return 0 if md_alg acceptable for this profile, -1 otherwise
152 */
153static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
154 mbedtls_md_type_t md_alg )
155{
156 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
157 return( 0 );
158
159 return( -1 );
160}
161
162/*
163 * Check pk_alg against profile
164 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
165 */
166static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
167 mbedtls_pk_type_t pk_alg )
168{
169 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
170 return( 0 );
171
172 return( -1 );
173}
174
175/*
176 * Check key against profile
177 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
178 */
179static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
180 mbedtls_pk_type_t pk_alg,
181 const mbedtls_pk_context *pk )
182{
183#if defined(MBEDTLS_RSA_C)
184 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
185 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200186 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200187 return( 0 );
188
189 return( -1 );
190 }
191#endif
192
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200193#if defined(MBEDTLS_ECP_C)
194 if( pk_alg == MBEDTLS_PK_ECDSA ||
195 pk_alg == MBEDTLS_PK_ECKEY ||
196 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200197 {
198 mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
199
200 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
201 return( 0 );
202
203 return( -1 );
204 }
205#endif
206
207 return( -1 );
208}
209
210/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200211 * Reset (init or clear) a verify_chain
212 */
213static void x509_crt_verify_chain_reset(
214 mbedtls_x509_crt_verify_chain *ver_chain )
215{
216 size_t i;
217
218 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
219 {
220 ver_chain->items[i].crt = NULL;
221 ver_chain->items[i].flags = -1;
222 }
223
224 ver_chain->len = 0;
225}
226
227/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200228 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
229 */
230static int x509_get_version( unsigned char **p,
231 const unsigned char *end,
232 int *ver )
233{
234 int ret;
235 size_t len;
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
238 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200241 {
242 *ver = 0;
243 return( 0 );
244 }
245
246 return( ret );
247 }
248
249 end = *p + len;
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
252 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200253
254 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 return( MBEDTLS_ERR_X509_INVALID_VERSION +
256 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200257
258 return( 0 );
259}
260
261/*
262 * Validity ::= SEQUENCE {
263 * notBefore Time,
264 * notAfter Time }
265 */
266static int x509_get_dates( unsigned char **p,
267 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_x509_time *from,
269 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200270{
271 int ret;
272 size_t len;
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
275 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
276 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200277
278 end = *p + len;
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281 return( ret );
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284 return( ret );
285
286 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 return( MBEDTLS_ERR_X509_INVALID_DATE +
288 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289
290 return( 0 );
291}
292
293/*
294 * X.509 v2/v3 unique identifier (not parsed)
295 */
296static int x509_get_uid( unsigned char **p,
297 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200299{
300 int ret;
301
302 if( *p == end )
303 return( 0 );
304
305 uid->tag = **p;
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
308 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311 return( 0 );
312
313 return( ret );
314 }
315
316 uid->p = *p;
317 *p += uid->len;
318
319 return( 0 );
320}
321
322static int x509_get_basic_constraints( unsigned char **p,
323 const unsigned char *end,
324 int *ca_istrue,
325 int *max_pathlen )
326{
327 int ret;
328 size_t len;
329
330 /*
331 * BasicConstraints ::= SEQUENCE {
332 * cA BOOLEAN DEFAULT FALSE,
333 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
334 */
335 *ca_istrue = 0; /* DEFAULT FALSE */
336 *max_pathlen = 0; /* endless */
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
339 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
340 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341
342 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200343 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
348 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349
350 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352
353 if( *ca_istrue != 0 )
354 *ca_istrue = 1;
355 }
356
357 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200358 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
361 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362
363 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
365 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366
367 (*max_pathlen)++;
368
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200369 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370}
371
372static int x509_get_ns_cert_type( unsigned char **p,
373 const unsigned char *end,
374 unsigned char *ns_cert_type)
375{
376 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
380 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381
382 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
384 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200385
386 /* Get actual bitstring */
387 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200388 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389}
390
391static int x509_get_key_usage( unsigned char **p,
392 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100393 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200394{
395 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200396 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
400 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401
402 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
404 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405
406 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200407 *key_usage = 0;
408 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
409 {
410 *key_usage |= (unsigned int) bs.p[i] << (8*i);
411 }
412
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200413 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414}
415
416/*
417 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
418 *
419 * KeyPurposeId ::= OBJECT IDENTIFIER
420 */
421static int x509_get_ext_key_usage( unsigned char **p,
422 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424{
425 int ret;
426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
428 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429
430 /* Sequence length must be >= 1 */
431 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
433 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200435 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436}
437
438/*
439 * SubjectAltName ::= GeneralNames
440 *
441 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
442 *
443 * GeneralName ::= CHOICE {
444 * otherName [0] OtherName,
445 * rfc822Name [1] IA5String,
446 * dNSName [2] IA5String,
447 * x400Address [3] ORAddress,
448 * directoryName [4] Name,
449 * ediPartyName [5] EDIPartyName,
450 * uniformResourceIdentifier [6] IA5String,
451 * iPAddress [7] OCTET STRING,
452 * registeredID [8] OBJECT IDENTIFIER }
453 *
454 * OtherName ::= SEQUENCE {
455 * type-id OBJECT IDENTIFIER,
456 * value [0] EXPLICIT ANY DEFINED BY type-id }
457 *
458 * EDIPartyName ::= SEQUENCE {
459 * nameAssigner [0] DirectoryString OPTIONAL,
460 * partyName [1] DirectoryString }
461 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000462 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463 */
464static int x509_get_subject_alt_name( unsigned char **p,
465 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467{
468 int ret;
469 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473
474 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
476 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
477 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478
479 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
481 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
483 while( *p < end )
484 {
485 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
487 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488
489 tag = **p;
490 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
492 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( ( tag & MBEDTLS_ASN1_CONTEXT_SPECIFIC ) != MBEDTLS_ASN1_CONTEXT_SPECIFIC )
495 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
496 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200498 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500 {
501 *p += tag_len;
502 continue;
503 }
504
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200506 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100508 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100510
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200511 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512
513 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200515 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517 cur = cur->next;
518 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200519
520 buf = &(cur->buf);
521 buf->tag = tag;
522 buf->p = *p;
523 buf->len = tag_len;
524 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 }
526
527 /* Set final sequence entry's next pointer to NULL */
528 cur->next = NULL;
529
530 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
532 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533
534 return( 0 );
535}
536
537/*
538 * X.509 v3 extensions
539 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540 */
541static int x509_get_crt_ext( unsigned char **p,
542 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544{
545 int ret;
546 size_t len;
547 unsigned char *end_ext_data, *end_ext_octet;
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552 return( 0 );
553
554 return( ret );
555 }
556
557 while( *p < end )
558 {
559 /*
560 * Extension ::= SEQUENCE {
561 * extnID OBJECT IDENTIFIER,
562 * critical BOOLEAN DEFAULT FALSE,
563 * extnValue OCTET STRING }
564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566 int is_critical = 0; /* DEFAULT FALSE */
567 int ext_type = 0;
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
570 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
571 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572
573 end_ext_data = *p + len;
574
575 /* Get extension ID */
576 extn_oid.tag = **p;
577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
579 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580
581 extn_oid.p = *p;
582 *p += extn_oid.len;
583
584 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
586 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587
588 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
590 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
591 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592
593 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
595 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
596 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597
598 end_ext_octet = *p + len;
599
600 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
602 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200603
604 /*
605 * Detect supported extensions
606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608
609 if( ret != 0 )
610 {
611 /* No parser found, skip extension */
612 *p = end_ext_octet;
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200615 if( is_critical )
616 {
617 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
619 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620 }
621#endif
622 continue;
623 }
624
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100625 /* Forbid repeated extensions */
626 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100628
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629 crt->ext_types |= ext_type;
630
631 switch( ext_type )
632 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100633 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634 /* Parse basic constraints */
635 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
636 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200637 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638 break;
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641 /* Parse key usage */
642 if( ( ret = x509_get_key_usage( p, end_ext_octet,
643 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200644 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645 break;
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 /* Parse extended key usage */
649 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
650 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200651 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652 break;
653
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100654 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 /* Parse subject alt name */
656 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
657 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200658 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659 break;
660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662 /* Parse netscape certificate type */
663 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
664 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200665 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666 break;
667
668 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670 }
671 }
672
673 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
675 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676
677 return( 0 );
678}
679
680/*
681 * Parse and fill a single X.509 certificate in DER format
682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200684 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685{
686 int ret;
687 size_t len;
688 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
692 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
693 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694
695 /*
696 * Check for valid input
697 */
698 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700
Janos Follathcc0e49d2016-02-17 14:34:12 +0000701 // Use the original buffer until we figure out actual length
702 p = (unsigned char*) buf;
703 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704 end = p + len;
705
706 /*
707 * Certificate ::= SEQUENCE {
708 * tbsCertificate TBSCertificate,
709 * signatureAlgorithm AlgorithmIdentifier,
710 * signatureValue BIT STRING }
711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
713 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_x509_crt_free( crt );
716 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717 }
718
719 if( len > (size_t) ( end - p ) )
720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_x509_crt_free( crt );
722 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
723 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724 }
725 crt_end = p + len;
726
Janos Follathcc0e49d2016-02-17 14:34:12 +0000727 // Create and populate a new buffer for the raw field
728 crt->raw.len = crt_end - buf;
729 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
730 if( p == NULL )
731 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
732
733 memcpy( p, buf, crt->raw.len );
734
735 // Direct pointers to the new buffer
736 p += crt->raw.len - len;
737 end = crt_end = p + len;
738
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 /*
740 * TBSCertificate ::= SEQUENCE {
741 */
742 crt->tbs.p = p;
743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
745 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_x509_crt_free( crt );
748 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200749 }
750
751 end = p + len;
752 crt->tbs.len = end - crt->tbs.p;
753
754 /*
755 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
756 *
757 * CertificateSerialNumber ::= INTEGER
758 *
759 * signature AlgorithmIdentifier
760 */
761 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
763 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200764 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200767 return( ret );
768 }
769
Andres AG7ca4a032017-03-09 16:16:11 +0000770 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_x509_crt_free( crt );
773 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774 }
775
Andres AG7ca4a032017-03-09 16:16:11 +0000776 crt->version++;
777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200779 &crt->sig_md, &crt->sig_pk,
780 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200783 return( ret );
784 }
785
786 /*
787 * issuer Name
788 */
789 crt->issuer_raw.p = p;
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
792 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_x509_crt_free( crt );
795 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796 }
797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801 return( ret );
802 }
803
804 crt->issuer_raw.len = p - crt->issuer_raw.p;
805
806 /*
807 * Validity ::= SEQUENCE {
808 * notBefore Time,
809 * notAfter Time }
810 *
811 */
812 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
813 &crt->valid_to ) ) != 0 )
814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 return( ret );
817 }
818
819 /*
820 * subject Name
821 */
822 crt->subject_raw.p = p;
823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
825 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_x509_crt_free( crt );
828 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200829 }
830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200834 return( ret );
835 }
836
837 crt->subject_raw.len = p - crt->subject_raw.p;
838
839 /*
840 * SubjectPublicKeyInfo
841 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845 return( ret );
846 }
847
848 /*
849 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
850 * -- If present, version shall be v2 or v3
851 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
852 * -- If present, version shall be v2 or v3
853 * extensions [3] EXPLICIT Extensions OPTIONAL
854 * -- If present, version shall be v3
855 */
856 if( crt->version == 2 || crt->version == 3 )
857 {
858 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
859 if( ret != 0 )
860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200862 return( ret );
863 }
864 }
865
866 if( crt->version == 2 || crt->version == 3 )
867 {
868 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
869 if( ret != 0 )
870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872 return( ret );
873 }
874 }
875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200877 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200878#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100879 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200880 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881 if( ret != 0 )
882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200884 return( ret );
885 }
886 }
887
888 if( p != end )
889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 mbedtls_x509_crt_free( crt );
891 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
892 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200893 }
894
895 end = crt_end;
896
897 /*
898 * }
899 * -- end of TBSCertificate
900 *
901 * signatureAlgorithm AlgorithmIdentifier,
902 * signatureValue BIT STRING
903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907 return( ret );
908 }
909
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100910 if( crt->sig_oid.len != sig_oid2.len ||
911 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200912 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200913 ( sig_params1.len != 0 &&
914 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_x509_crt_free( crt );
917 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918 }
919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923 return( ret );
924 }
925
926 if( p != end )
927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_x509_crt_free( crt );
929 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
930 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931 }
932
933 return( 0 );
934}
935
936/*
937 * Parse one X.509 certificate in DER format from a buffer and add them to a
938 * chained list
939 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200941 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942{
943 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945
946 /*
947 * Check for valid input
948 */
949 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951
952 while( crt->version != 0 && crt->next != NULL )
953 {
954 prev = crt;
955 crt = crt->next;
956 }
957
958 /*
959 * Add new certificate on the end of the chain if needed.
960 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200961 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200963 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964
965 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200966 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967
968 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971 }
972
Paul Bakkerddf26b42013-09-18 13:46:23 +0200973 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 {
975 if( prev )
976 prev->next = NULL;
977
978 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980
981 return( ret );
982 }
983
984 return( 0 );
985}
986
987/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200988 * Parse one or more PEM certificates from a buffer and add them to the chained
989 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992{
Janos Follath98e28a72016-05-31 14:03:54 +0100993#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +0000994 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +0100996#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997
998 /*
999 * Check for valid input
1000 */
1001 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003
1004 /*
1005 * Determine buffer content. Buffer contains either one DER certificate or
1006 * one or more PEM certificates.
1007 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001009 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001010 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001013 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1016 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001017#else
1018 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1019#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#if defined(MBEDTLS_PEM_PARSE_C)
1022 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 {
1024 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001027 /* 1 rather than 0 since the terminating NULL byte is counted in */
1028 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029 {
1030 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001033 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035 "-----BEGIN CERTIFICATE-----",
1036 "-----END CERTIFICATE-----",
1037 buf, NULL, 0, &use_len );
1038
1039 if( ret == 0 )
1040 {
1041 /*
1042 * Was PEM encoded
1043 */
1044 buflen -= use_len;
1045 buf += use_len;
1046 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 {
1049 return( ret );
1050 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001054
1055 /*
1056 * PEM header and footer were found
1057 */
1058 buflen -= use_len;
1059 buf += use_len;
1060
1061 if( first_error == 0 )
1062 first_error = ret;
1063
Paul Bakker5a5fa922014-09-26 14:53:04 +02001064 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065 continue;
1066 }
1067 else
1068 break;
1069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001073
1074 if( ret != 0 )
1075 {
1076 /*
1077 * Quit parsing on a memory error
1078 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001079 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080 return( ret );
1081
1082 if( first_error == 0 )
1083 first_error = ret;
1084
1085 total_failed++;
1086 continue;
1087 }
1088
1089 success = 1;
1090 }
1091 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092
1093 if( success )
1094 return( total_failed );
1095 else if( first_error )
1096 return( first_error );
1097 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001099#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100}
1101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103/*
1104 * Load one or more certificates and add them to the chained list
1105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001107{
1108 int ret;
1109 size_t n;
1110 unsigned char *buf;
1111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113 return( ret );
1114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001116
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001117 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119
1120 return( ret );
1121}
1122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124{
1125 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001126#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127 int w_ret;
1128 WCHAR szDir[MAX_PATH];
1129 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001130 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001131 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132
Paul Bakker9af723c2014-05-01 13:03:14 +02001133 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001134 HANDLE hFind;
1135
1136 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001138
Paul Bakker9af723c2014-05-01 13:03:14 +02001139 memset( szDir, 0, sizeof(szDir) );
1140 memset( filename, 0, MAX_PATH );
1141 memcpy( filename, path, len );
1142 filename[len++] = '\\';
1143 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001144 filename[len++] = '*';
1145
Simon B3c6b18d2016-11-03 01:11:37 +00001146 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001147 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001148 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150
1151 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001152 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154
1155 len = MAX_PATH - len;
1156 do
1157 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001158 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001159
1160 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1161 continue;
1162
Paul Bakker9af723c2014-05-01 13:03:14 +02001163 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001164 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001165 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001166 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001167 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001168 {
1169 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1170 goto cleanup;
1171 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 if( w_ret < 0 )
1175 ret++;
1176 else
1177 ret += w_ret;
1178 }
1179 while( FindNextFileW( hFind, &file_data ) != 0 );
1180
Paul Bakker66d5d072014-06-17 16:39:18 +02001181 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001183
Ron Eldor36d90422017-01-09 15:09:16 +02001184cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001185 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001186#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001187 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001188 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001190 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001191 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 DIR *dir = opendir( path );
1193
Paul Bakker66d5d072014-06-17 16:39:18 +02001194 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001196
Ron Eldor63140682017-01-09 19:27:59 +02001197#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001198 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001199 {
1200 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001201 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001202 }
Ron Eldor63140682017-01-09 19:27:59 +02001203#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001204
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001205 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206 {
Andres AGf9113192016-09-02 14:06:04 +01001207 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1208 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209
Andres AGf9113192016-09-02 14:06:04 +01001210 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001211 {
Andres AGf9113192016-09-02 14:06:04 +01001212 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1213 goto cleanup;
1214 }
1215 else if( stat( entry_name, &sb ) == -1 )
1216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001218 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219 }
1220
1221 if( !S_ISREG( sb.st_mode ) )
1222 continue;
1223
1224 // Ignore parse errors
1225 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227 if( t_ret < 0 )
1228 ret++;
1229 else
1230 ret += t_ret;
1231 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001232
1233cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001234 closedir( dir );
1235
Ron Eldor63140682017-01-09 19:27:59 +02001236#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001237 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001239#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001240
Paul Bakkerbe089b02013-10-14 15:51:50 +02001241#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242
1243 return( ret );
1244}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001246
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001247static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001249{
1250 size_t i;
1251 size_t n = *size;
1252 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001254 const char *sep = "";
1255 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001256
1257 while( cur != NULL )
1258 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001259 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001260 {
1261 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001262 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001263 }
1264
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001265 n -= cur->buf.len + sep_len;
1266 for( i = 0; i < sep_len; i++ )
1267 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001268 for( i = 0; i < cur->buf.len; i++ )
1269 *p++ = cur->buf.p[i];
1270
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001271 sep = ", ";
1272 sep_len = 2;
1273
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001274 cur = cur->next;
1275 }
1276
1277 *p = '\0';
1278
1279 *size = n;
1280 *buf = p;
1281
1282 return( 0 );
1283}
1284
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001285#define PRINT_ITEM(i) \
1286 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001288 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001289 sep = ", "; \
1290 }
1291
1292#define CERT_TYPE(type,name) \
1293 if( ns_cert_type & type ) \
1294 PRINT_ITEM( name );
1295
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001296static int x509_info_cert_type( char **buf, size_t *size,
1297 unsigned char ns_cert_type )
1298{
1299 int ret;
1300 size_t n = *size;
1301 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001302 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001305 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1307 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1308 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001309 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1310 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1311 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001312
1313 *size = n;
1314 *buf = p;
1315
1316 return( 0 );
1317}
1318
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001319#define KEY_USAGE(code,name) \
1320 if( key_usage & code ) \
1321 PRINT_ITEM( name );
1322
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001323static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001324 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001325{
1326 int ret;
1327 size_t n = *size;
1328 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001329 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1332 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001333 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1334 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1335 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1337 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001338 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1339 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001340
1341 *size = n;
1342 *buf = p;
1343
1344 return( 0 );
1345}
1346
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001347static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001349{
1350 int ret;
1351 const char *desc;
1352 size_t n = *size;
1353 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001355 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001356
1357 while( cur != NULL )
1358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001360 desc = "???";
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001363 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001364
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001365 sep = ", ";
1366
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001367 cur = cur->next;
1368 }
1369
1370 *size = n;
1371 *buf = p;
1372
1373 return( 0 );
1374}
1375
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376/*
1377 * Return an informational string about the certificate.
1378 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001379#define BEFORE_COLON 18
1380#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1382 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383{
1384 int ret;
1385 size_t n;
1386 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001387 char key_size_str[BEFORE_COLON];
1388
1389 p = buf;
1390 n = size;
1391
Janos Follath98e28a72016-05-31 14:03:54 +01001392 if( NULL == crt )
1393 {
1394 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
1395 MBEDTLS_X509_SAFE_SNPRINTF;
1396
1397 return( (int) ( size - n ) );
1398 }
1399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001401 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001402 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001404 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001405 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001408 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001411 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001413 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001416 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001418 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1422 crt->valid_from.year, crt->valid_from.mon,
1423 crt->valid_from.day, crt->valid_from.hour,
1424 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001425 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001428 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1429 crt->valid_to.year, crt->valid_to.mon,
1430 crt->valid_to.day, crt->valid_to.hour,
1431 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001432 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001435 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001438 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001439 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001441 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1443 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001444 {
1445 return( ret );
1446 }
1447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001449 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001450 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001452 /*
1453 * Optional extensions
1454 */
1455
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001456 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001459 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001460 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001461
1462 if( crt->max_pathlen > 0 )
1463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001465 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001466 }
1467 }
1468
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001469 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001472 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001473
1474 if( ( ret = x509_info_subject_alt_name( &p, &n,
1475 &crt->subject_alt_names ) ) != 0 )
1476 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001477 }
1478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001482 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001483
1484 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1485 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001486 }
1487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001491 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001492
1493 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1494 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001495 }
1496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001500 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001501
1502 if( ( ret = x509_info_ext_key_usage( &p, &n,
1503 &crt->ext_key_usage ) ) != 0 )
1504 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001505 }
1506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001508 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001509
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510 return( (int) ( size - n ) );
1511}
1512
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001513struct x509_crt_verify_string {
1514 int code;
1515 const char *string;
1516};
1517
1518static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001519 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001520 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1521 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1522 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1523 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1524 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001525 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1526 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1527 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001528 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001529 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1530 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1531 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1532 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001533 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1534 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1535 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1536 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1537 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1538 { 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 +01001539 { 0, NULL }
1540};
1541
1542int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001543 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001544{
1545 int ret;
1546 const struct x509_crt_verify_string *cur;
1547 char *p = buf;
1548 size_t n = size;
1549
1550 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1551 {
1552 if( ( flags & cur->code ) == 0 )
1553 continue;
1554
1555 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001556 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001557 flags ^= cur->code;
1558 }
1559
1560 if( flags != 0 )
1561 {
1562 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1563 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001564 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001565 }
1566
1567 return( (int) ( size - n ) );
1568}
1569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001571int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1572 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001573{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001574 unsigned int usage_must, usage_may;
1575 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1576 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1577
1578 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1579 return( 0 );
1580
1581 usage_must = usage & ~may_mask;
1582
1583 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1584 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1585
1586 usage_may = usage & may_mask;
1587
1588 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001590
1591 return( 0 );
1592}
1593#endif
1594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1596int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001597 const char *usage_oid,
1598 size_t usage_len )
1599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001601
1602 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001604 return( 0 );
1605
1606 /*
1607 * Look for the requested usage (or wildcard ANY) in our list
1608 */
1609 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001612
1613 if( cur_oid->len == usage_len &&
1614 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1615 {
1616 return( 0 );
1617 }
1618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001620 return( 0 );
1621 }
1622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001624}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628/*
1629 * Return 1 if the certificate is revoked, or 0 otherwise.
1630 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001631int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001632{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634
1635 while( cur != NULL && cur->serial.len != 0 )
1636 {
1637 if( crt->serial.len == cur->serial.len &&
1638 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1639 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001640 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641 return( 1 );
1642 }
1643
1644 cur = cur->next;
1645 }
1646
1647 return( 0 );
1648}
1649
1650/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01001651 * Check that the given certificate is not revoked according to the CRL.
1652 * Skip validation is no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001655 mbedtls_x509_crl *crl_list,
1656 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657{
1658 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1660 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661
1662 if( ca == NULL )
1663 return( flags );
1664
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001665 while( crl_list != NULL )
1666 {
1667 if( crl_list->version == 0 ||
1668 crl_list->issuer_raw.len != ca->subject_raw.len ||
1669 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1670 crl_list->issuer_raw.len ) != 0 )
1671 {
1672 crl_list = crl_list->next;
1673 continue;
1674 }
1675
1676 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001677 * Check if the CA is configured to sign CRLs
1678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1680 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001683 break;
1684 }
1685#endif
1686
1687 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001688 * Check if CRL is correctly signed by the trusted CA
1689 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001690 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1691 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1692
1693 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1694 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02001697 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001698 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02001699 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001701 break;
1702 }
1703
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001704 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1705 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1708 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001709 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001712 break;
1713 }
1714
1715 /*
1716 * Check for validity of CRL (Do not drop out)
1717 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001718 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001720
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001721 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001722 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001723
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001724 /*
1725 * Check if certificate is revoked
1726 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001727 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001730 break;
1731 }
1732
1733 crl_list = crl_list->next;
1734 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001735
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001736 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001737}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001739
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001740/*
1741 * Like memcmp, but case-insensitive and always returns -1 if different
1742 */
1743static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001744{
1745 size_t i;
1746 unsigned char diff;
1747 const unsigned char *n1 = s1, *n2 = s2;
1748
1749 for( i = 0; i < len; i++ )
1750 {
1751 diff = n1[i] ^ n2[i];
1752
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001753 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001754 continue;
1755
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001756 if( diff == 32 &&
1757 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1758 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1759 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001760 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001761 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001762
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001763 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001764 }
1765
1766 return( 0 );
1767}
1768
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001769/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001770 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001771 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02001772static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001773{
1774 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001775 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001776
1777 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1778 return( 0 );
1779
Paul Bakker14b16c62014-05-28 11:33:54 +02001780 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001781 {
1782 if( cn[i] == '.' )
1783 {
1784 cn_idx = i;
1785 break;
1786 }
1787 }
1788
1789 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001790 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001791
Paul Bakker14b16c62014-05-28 11:33:54 +02001792 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001793 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001794 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001795 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001796 }
1797
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001798 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001799}
1800
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001801/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001802 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1803 * variations (but not all).
1804 *
1805 * Return 0 if equal, -1 otherwise.
1806 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001808{
1809 if( a->tag == b->tag &&
1810 a->len == b->len &&
1811 memcmp( a->p, b->p, b->len ) == 0 )
1812 {
1813 return( 0 );
1814 }
1815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1817 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001818 a->len == b->len &&
1819 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1820 {
1821 return( 0 );
1822 }
1823
1824 return( -1 );
1825}
1826
1827/*
1828 * Compare two X.509 Names (aka rdnSequence).
1829 *
1830 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1831 * we sometimes return unequal when the full algorithm would return equal,
1832 * but never the other way. (In particular, we don't do Unicode normalisation
1833 * or space folding.)
1834 *
1835 * Return 0 if equal, -1 otherwise.
1836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001838{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001839 /* Avoid recursion, it might not be optimised by the compiler */
1840 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001841 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001842 if( a == NULL || b == NULL )
1843 return( -1 );
1844
1845 /* type */
1846 if( a->oid.tag != b->oid.tag ||
1847 a->oid.len != b->oid.len ||
1848 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1849 {
1850 return( -1 );
1851 }
1852
1853 /* value */
1854 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1855 return( -1 );
1856
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001857 /* structure of the list of sets */
1858 if( a->next_merged != b->next_merged )
1859 return( -1 );
1860
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001861 a = a->next;
1862 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001863 }
1864
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001865 /* a == NULL == b */
1866 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001867}
1868
1869/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001870 * Check the signature of a certificate by its parent
1871 */
1872static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02001873 mbedtls_x509_crt *parent,
1874 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001875{
1876 const mbedtls_md_info_t *md_info;
1877 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1878
1879 md_info = mbedtls_md_info_from_type( child->sig_md );
1880 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
1881 {
1882 /* Note: this can't happen except after an internal error */
1883 return( -1 );
1884 }
1885
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02001886 /* Skip expensive computation on obvious mismatch */
1887 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001888 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001889
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02001890#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02001891 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
1892 {
1893 return( mbedtls_pk_verify_restartable( &parent->pk,
1894 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001895 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02001896 }
1897#else
1898 (void) rs_ctx;
1899#endif
1900
1901 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1902 child->sig_md, hash, mbedtls_md_get_size( md_info ),
1903 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001904}
1905
1906/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001907 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1908 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001909 *
1910 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1913 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02001914 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001915{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001916 int need_ca_bit;
1917
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001918 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001919 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001920 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001921
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001922 /* Parent must have the basicConstraints CA bit set as a general rule */
1923 need_ca_bit = 1;
1924
1925 /* Exception: v1/v2 certificates that are locally trusted. */
1926 if( top && parent->version < 3 )
1927 need_ca_bit = 0;
1928
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001929 if( need_ca_bit && ! parent->ca_istrue )
1930 return( -1 );
1931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001933 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001935 {
1936 return( -1 );
1937 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001938#endif
1939
1940 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001941}
1942
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02001943/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001944 * Find a suitable parent for child in candidates, or return NULL.
1945 *
1946 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001947 * 1. subject name matches child's issuer
1948 * 2. if necessary, the CA bit is set and key usage allows signing certs
1949 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02001950 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001951 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001952 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02001953 * If there's a suitable candidate which is also time-valid, return the first
1954 * such. Otherwise, return the first suitable candidate (or NULL if there is
1955 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001956 *
1957 * The rationale for this rule is that someone could have a list of trusted
1958 * roots with two versions on the same root with different validity periods.
1959 * (At least one user reported having such a list and wanted it to just work.)
1960 * The reason we don't just require time-validity is that generally there is
1961 * only one version, and if it's expired we want the flags to state that
1962 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001963 *
1964 * The rationale for rule 3 (signature for trusted roots) is that users might
1965 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02001966 * way we select the correct one is by checking the signature (as we don't
1967 * rely on key identifier extensions). (This is one way users might choose to
1968 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02001969 *
1970 * Arguments:
1971 * [in] child: certificate for which we want a parent
1972 * [in] candidates: list of possible parents
1973 * [out] r_parent: parent found (or NULL)
1974 * [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
1975 * [in] top: 1 if candidates are locally trusted, or 0
1976 * [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
1977 * [in] self_cnt: number of self-signed certs in the chain so far
1978 * [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001979 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02001980static int x509_crt_find_parent_in(
1981 mbedtls_x509_crt *child,
1982 mbedtls_x509_crt *candidates,
1983 mbedtls_x509_crt **r_parent,
1984 int *r_signature_is_good,
1985 int top,
1986 int path_cnt,
1987 int self_cnt,
1988 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001989{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02001990 int ret;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02001991 mbedtls_x509_crt *parent, *fallback_parent;
1992 int signature_is_good, fallback_sign_good;
1993
1994#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02001995 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02001996 if( rs_ctx != NULL && rs_ctx->parent != NULL )
1997 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02001998 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02001999 parent = rs_ctx->parent;
2000 fallback_parent = rs_ctx->fallback_parent;
2001 fallback_sign_good = rs_ctx->fallback_sign_good;
2002
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002003 /* clear saved state */
2004 rs_ctx->parent = NULL;
2005 rs_ctx->fallback_parent = NULL;
2006 rs_ctx->fallback_sign_good = 0;
2007
2008 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002009 goto check_signature;
2010 }
2011#endif
2012
2013 fallback_parent = NULL;
2014 fallback_sign_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002015
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002016 for( parent = candidates; parent != NULL; parent = parent->next )
2017 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002018 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002019 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002020 continue;
2021
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002022 /* +1 because stored max_pathlen is 1 higher that the actual value */
2023 if( parent->max_pathlen > 0 &&
2024 parent->max_pathlen < 1 + path_cnt - self_cnt )
2025 {
2026 continue;
2027 }
2028
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002029 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002030#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2031check_signature:
2032#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002033 ret = x509_crt_check_signature( child, parent, rs_ctx );
2034
2035#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002036 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2037 {
2038 /* save state */
2039 rs_ctx->parent = parent;
2040 rs_ctx->fallback_parent = fallback_parent;
2041 rs_ctx->fallback_sign_good = fallback_sign_good;
2042
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002043 return( ret );
2044 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002045#else
2046 (void) ret;
2047#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002048
2049 signature_is_good = ret == 0;
2050 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002051 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002052
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002053 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002054 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2055 mbedtls_x509_time_is_future( &parent->valid_from ) )
2056 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002057 if( fallback_parent == NULL )
2058 {
2059 fallback_parent = parent;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002060 fallback_sign_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002061 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002062
2063 continue;
2064 }
2065
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002066 break;
2067 }
2068
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002069 if( parent != NULL )
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002070 {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002071 *r_parent = parent;
2072 *r_signature_is_good = signature_is_good;
2073 }
2074 else
2075 {
2076 *r_parent = fallback_parent;
2077 *r_signature_is_good = fallback_sign_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002078 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002079
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002080 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002081}
2082
2083/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002084 * Find a parent in trusted CAs or the provided chain, or return NULL.
2085 *
2086 * Searches in trusted CAs first, and return the first suitable parent found
2087 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002088 *
2089 * Arguments:
2090 * [in] child: certificate for which we want a parent,
2091 * possibly followed by a list of ancestors
2092 * [in] trust_ca: list of locally trusted certificates
2093 * [out] parent: parent found (or NULL)
2094 * [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2095 * [out] signature_is_good: 1 if child signature by parent is valid, or 0
2096 * [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2097 * [in] self_cnt: number of self-signed certs in the chain so far
2098 * [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002099 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002100static int x509_crt_find_parent(
2101 mbedtls_x509_crt *child,
2102 mbedtls_x509_crt *trust_ca,
2103 mbedtls_x509_crt **parent,
2104 int *parent_is_trusted,
2105 int *signature_is_good,
2106 int path_cnt,
2107 int self_cnt,
2108 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002109{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002110 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002111 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002112
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002113 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002114
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002115#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002116 /* restore then clear saved state if we have some stored */
2117 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2118 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002119 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002120 rs_ctx->parent_is_trusted = -1;
2121 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002122#endif
2123
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002124 while( 1 ) {
2125 search_list = *parent_is_trusted ? trust_ca : child->next;
2126
2127 ret = x509_crt_find_parent_in( child, search_list,
2128 parent, signature_is_good,
2129 *parent_is_trusted,
2130 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002131
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002132#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002133 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2134 {
2135 /* save state */
2136 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002137 return( ret );
2138 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002139#else
2140 (void) ret;
2141#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002142
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002143 /* stop here if found or already in second iteration */
2144 if( *parent != NULL || *parent_is_trusted == 0 )
2145 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002146
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002147 /* prepare second iteration */
2148 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002149 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002150
2151 /* extra precaution against mistakes in the caller */
2152 if( parent == NULL )
2153 {
2154 parent_is_trusted = 0;
2155 signature_is_good = 0;
2156 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002157
2158 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002159}
2160
2161/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002162 * Check if an end-entity certificate is locally trusted
2163 *
2164 * Currently we require such certificates to be self-signed (actually only
2165 * check for self-issued as self-signatures are not checked)
2166 */
2167static int x509_crt_check_ee_locally_trusted(
2168 mbedtls_x509_crt *crt,
2169 mbedtls_x509_crt *trust_ca )
2170{
2171 mbedtls_x509_crt *cur;
2172
2173 /* must be self-issued */
2174 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2175 return( -1 );
2176
2177 /* look for an exact match with trusted cert */
2178 for( cur = trust_ca; cur != NULL; cur = cur->next )
2179 {
2180 if( crt->raw.len == cur->raw.len &&
2181 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2182 {
2183 return( 0 );
2184 }
2185 }
2186
2187 /* too bad */
2188 return( -1 );
2189}
2190
2191/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002192 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002193 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002194 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2195 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002196 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002197 * such that every cert in the chain is a child of the next one,
2198 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002199 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002200 * Verify that chain and return it with flags for all issues found.
2201 *
2202 * Special cases:
2203 * - EE == Rj -> return a one-element list containing it
2204 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2205 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002206 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002207 * Tests for (aspects of) this function should include at least:
2208 * - trusted EE
2209 * - EE -> trusted root
2210 * - EE -> intermedate CA -> trusted root
2211 * - if relevant: EE untrusted
2212 * - if relevant: EE -> intermediate, untrusted
2213 * with the aspect under test checked at each relevant level (EE, int, root).
2214 * For some aspects longer chains are required, but usually length 2 is
2215 * enough (but length 1 is not in general).
2216 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002217 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002218 * - [in] crt: the cert list EE, C1, ..., Cn
2219 * - [in] trust_ca: the trusted list R1, ..., Rp
2220 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002221 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002222 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002223 *
2224 * Return value:
2225 * - non-zero if the chain could not be fully built and examined
2226 * - 0 is the chain was successfully built and examined,
2227 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002228 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002229static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002230 mbedtls_x509_crt *crt,
2231 mbedtls_x509_crt *trust_ca,
2232 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002233 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002234 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002235 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002236{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002237 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002238 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002239 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002240 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002241 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002242 int parent_is_trusted;
2243 int child_is_trusted;
2244 int signature_is_good;
2245 int self_cnt;
2246
2247#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2248 /* resume if we had an operation in progress */
2249 if( rs_ctx != NULL && rs_ctx->child != NULL )
2250 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002251 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002252 child = rs_ctx->child;
2253 self_cnt = rs_ctx->self_cnt;
2254 *ver_chain = rs_ctx->ver_chain;
2255
2256 cur = &ver_chain->items[ver_chain->len - 1];
2257 flags = &cur->flags;
2258
2259 goto find_parent;
2260 }
2261#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002262
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002263 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002264 self_cnt = 0;
2265 parent_is_trusted = 0;
2266 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002267
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002268 while( 1 ) {
2269 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002270 cur = &ver_chain->items[ver_chain->len];
2271 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002272 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002273 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002274 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002275
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002276 /* Check time-validity (all certificates) */
2277 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2278 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002279
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002280 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2281 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002282
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002283 /* Stop here for trusted roots (but not for trusted EE certs) */
2284 if( child_is_trusted )
2285 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002286
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002287 /* Check signature algorithm: MD & PK algs */
2288 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2289 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002290
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002291 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2292 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002293
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002294 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002295 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002296 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2297 {
2298 return( 0 );
2299 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002300
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002301#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2302find_parent:
2303#endif
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002304 /* Look for a parent in trusted CAs or up the chain */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002305 ret = x509_crt_find_parent( child, trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002306 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002307 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002308
2309#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002310 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2311 {
2312 /* save state */
2313 rs_ctx->child = child;
2314 rs_ctx->self_cnt = self_cnt;
2315 rs_ctx-> ver_chain = *ver_chain;
2316
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002317 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002318 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002319#else
2320 (void) ret;
2321#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002322
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002323 /* No parent? We're done here */
2324 if( parent == NULL )
2325 {
2326 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2327 return( 0 );
2328 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002329
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002330 /* Count intermediate self-issued (not necessarily self-signed) certs.
2331 * These can occur with some strategies for key rollover, see [SIRO],
2332 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002333 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002334 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2335 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002336 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002337 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002338
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002339 /* path_cnt is 0 for the first intermediate CA,
2340 * and if parent is trusted it's not an intermediate CA */
2341 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002342 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002343 {
2344 /* return immediately to avoid overflow the chain array */
2345 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2346 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002347
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002348 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002349 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002350 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2351
2352 /* check size of signing key */
2353 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2354 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002357 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002358 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002359#else
2360 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002361#endif
2362
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002363 /* prepare for next iteration */
2364 child = parent;
2365 parent = NULL;
2366 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002367 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002368 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002369}
2370
2371/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002372 * Check for CN match
2373 */
2374static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2375 const char *cn, size_t cn_len )
2376{
2377 /* try exact match */
2378 if( name->len == cn_len &&
2379 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2380 {
2381 return( 0 );
2382 }
2383
2384 /* try wildcard match */
2385 if( name->len > 2 &&
2386 memcmp( name->p, "*.", 2 ) == 0 &&
2387 x509_check_wildcard( cn, name ) == 0 )
2388 {
2389 return( 0 );
2390 }
2391
2392 return( -1 );
2393}
2394
2395/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002396 * Verify the requested CN - only call this if cn is not NULL!
2397 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002398static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002399 const char *cn,
2400 uint32_t *flags )
2401{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002402 const mbedtls_x509_name *name;
2403 const mbedtls_x509_sequence *cur;
2404 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002405
2406 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
2407 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002408 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002409 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002410 if( x509_crt_check_cn( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002411 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002412 }
2413
2414 if( cur == NULL )
2415 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2416 }
2417 else
2418 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002419 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002420 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002421 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
2422 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002423 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002424 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002425 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002426 }
2427
2428 if( name == NULL )
2429 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2430 }
2431}
2432
2433/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002434 * Merge the flags for all certs in the chain, after calling callback
2435 */
2436static int x509_crt_merge_flags_with_cb(
2437 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002438 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002439 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2440 void *p_vrfy )
2441{
2442 int ret;
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002443 size_t i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002444 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002445 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002446
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002447 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002448 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002449 cur = &ver_chain->items[i-1];
2450 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002451
2452 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002453 if( ( ret = f_vrfy( p_vrfy, cur->crt, i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002454 return( ret );
2455
2456 *flags |= cur_flags;
2457 }
2458
2459 return( 0 );
2460}
2461
2462/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002463 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2466 mbedtls_x509_crt *trust_ca,
2467 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002468 const char *cn, uint32_t *flags,
2469 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002470 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002471{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002472 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
2473 &mbedtls_x509_crt_profile_default, cn, flags,
2474 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002475}
2476
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002477/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002478 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002479 */
2480int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2481 mbedtls_x509_crt *trust_ca,
2482 mbedtls_x509_crl *ca_crl,
2483 const mbedtls_x509_crt_profile *profile,
2484 const char *cn, uint32_t *flags,
2485 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2486 void *p_vrfy )
2487{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002488 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
2489 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
2490}
2491
2492/*
2493 * Verify the certificate validity, with profile, restartable version
2494 *
2495 * This function:
2496 * - checks the requested CN (if any)
2497 * - checks the type and size of the EE cert's key,
2498 * as that isn't done as part of chain building/verification currently
2499 * - builds and verifies the chain
2500 * - then calls the callback and merges the flags
2501 */
2502int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
2503 mbedtls_x509_crt *trust_ca,
2504 mbedtls_x509_crl *ca_crl,
2505 const mbedtls_x509_crt_profile *profile,
2506 const char *cn, uint32_t *flags,
2507 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2508 void *p_vrfy,
2509 mbedtls_x509_crt_restart_ctx *rs_ctx )
2510{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002511 int ret;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002512 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002513 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002514 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002515
2516 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002517 ee_flags = 0;
2518 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002519
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002520 if( profile == NULL )
2521 {
2522 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2523 goto exit;
2524 }
2525
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002526 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002527 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002528 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002529
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002530 /* Check the type and size of the key */
2531 pk_type = mbedtls_pk_get_type( &crt->pk );
2532
2533 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002534 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002535
2536 if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002537 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002538
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002539 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002540 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002541 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002542
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002543 if( ret != 0 )
2544 goto exit;
2545
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002546 /* Merge end-entity flags */
2547 ver_chain.items[0].flags |= ee_flags;
2548
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002549 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002550 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002551
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002552exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002553#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2554 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2555 mbedtls_x509_crt_restart_free( rs_ctx );
2556#endif
2557
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02002558 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2559 * the SSL module for authmode optional, but non-zero return from the
2560 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002561 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2562 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2563
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002564 if( ret != 0 )
2565 {
2566 *flags = (uint32_t) -1;
2567 return( ret );
2568 }
2569
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002570 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002572
2573 return( 0 );
2574}
2575
2576/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002577 * Initialize a certificate chain
2578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002580{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002582}
2583
2584/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002585 * Unallocate all certificate data
2586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002588{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589 mbedtls_x509_crt *cert_cur = crt;
2590 mbedtls_x509_crt *cert_prv;
2591 mbedtls_x509_name *name_cur;
2592 mbedtls_x509_name *name_prv;
2593 mbedtls_x509_sequence *seq_cur;
2594 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002595
2596 if( crt == NULL )
2597 return;
2598
2599 do
2600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2604 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002605#endif
2606
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002607 name_cur = cert_cur->issuer.next;
2608 while( name_cur != NULL )
2609 {
2610 name_prv = name_cur;
2611 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2613 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002614 }
2615
2616 name_cur = cert_cur->subject.next;
2617 while( name_cur != NULL )
2618 {
2619 name_prv = name_cur;
2620 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2622 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002623 }
2624
2625 seq_cur = cert_cur->ext_key_usage.next;
2626 while( seq_cur != NULL )
2627 {
2628 seq_prv = seq_cur;
2629 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2631 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002632 }
2633
2634 seq_cur = cert_cur->subject_alt_names.next;
2635 while( seq_cur != NULL )
2636 {
2637 seq_prv = seq_cur;
2638 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2640 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002641 }
2642
2643 if( cert_cur->raw.p != NULL )
2644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2646 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002647 }
2648
2649 cert_cur = cert_cur->next;
2650 }
2651 while( cert_cur != NULL );
2652
2653 cert_cur = crt;
2654 do
2655 {
2656 cert_prv = cert_cur;
2657 cert_cur = cert_cur->next;
2658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002660 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002662 }
2663 while( cert_cur != NULL );
2664}
2665
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002666#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2667/*
2668 * Initialize a restart context
2669 */
2670void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
2671{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002672 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002673
2674 ctx->parent = NULL;
2675 ctx->fallback_parent = NULL;
2676 ctx->fallback_sign_good = 0;
2677
2678 ctx->parent_is_trusted = -1;
2679
2680 ctx->child = NULL;
2681 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002682 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002683}
2684
2685/*
2686 * Free the components of a restart context
2687 */
2688void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
2689{
2690 if( ctx == NULL )
2691 return;
2692
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002693 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002694
2695 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002696}
2697#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
2698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699#endif /* MBEDTLS_X509_CRT_PARSE_C */