blob: 8f8f6930cff12d1a521c14b8a4f7c6b6b007dcc5 [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
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020080/*
81 * Item in a verification chain: cert and flags for it
82 */
83typedef struct {
84 mbedtls_x509_crt *crt;
85 uint32_t flags;
86} x509_crt_verify_chain_item;
87
88/*
89 * Max size of verification chain: end-entity + intermediates + trusted root
90 */
91#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
92
Paul Bakker34617722014-06-13 17:20:13 +020093/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020095 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
96}
97
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020099 * Default profile
100 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200101const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
102{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200103#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200104 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200105 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200106#endif
107 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200108 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
110 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
111 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
112 0xFFFFFFF, /* Any PK alg */
113 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200114 2048,
115};
116
117/*
118 * Next-default profile
119 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200120const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
121{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200122 /* Hashes from SHA-256 and above */
123 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
124 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
125 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
126 0xFFFFFFF, /* Any PK alg */
127#if defined(MBEDTLS_ECP_C)
128 /* Curves at or above 128-bit security level */
129 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
130 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
136#else
137 0,
138#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200139 2048,
140};
141
142/*
143 * NSA Suite B Profile
144 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200145const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
146{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200147 /* Only SHA-256 and 384 */
148 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
149 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
150 /* Only ECDSA */
151 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ),
152#if defined(MBEDTLS_ECP_C)
153 /* Only NIST P-256 and P-384 */
154 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
155 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
156#else
157 0,
158#endif
159 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200160};
161
162/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200163 * Check md_alg against profile
164 * Return 0 if md_alg acceptable for this profile, -1 otherwise
165 */
166static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
167 mbedtls_md_type_t md_alg )
168{
169 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
170 return( 0 );
171
172 return( -1 );
173}
174
175/*
176 * Check pk_alg against profile
177 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
178 */
179static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
180 mbedtls_pk_type_t pk_alg )
181{
182 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
183 return( 0 );
184
185 return( -1 );
186}
187
188/*
189 * Check key against profile
190 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
191 */
192static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
193 mbedtls_pk_type_t pk_alg,
194 const mbedtls_pk_context *pk )
195{
196#if defined(MBEDTLS_RSA_C)
197 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
198 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200199 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200200 return( 0 );
201
202 return( -1 );
203 }
204#endif
205
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200206#if defined(MBEDTLS_ECP_C)
207 if( pk_alg == MBEDTLS_PK_ECDSA ||
208 pk_alg == MBEDTLS_PK_ECKEY ||
209 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200210 {
Manuel Pégourié-Gonnard08c36632017-10-18 14:57:11 +0200211 mbedtls_ecp_group_id gid;
212 mbedtls_pk_type_t pk_type;
213
214 /* Avoid calling pk_ec() if this is not an EC key */
215 pk_type = mbedtls_pk_get_type( pk );
216 if( pk_type != MBEDTLS_PK_ECDSA &&
217 pk_type != MBEDTLS_PK_ECKEY &&
218 pk_type != MBEDTLS_PK_ECKEY_DH )
219 {
220 return( -1 );
221 }
222
223 gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200224
225 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
226 return( 0 );
227
228 return( -1 );
229 }
230#endif
231
232 return( -1 );
233}
234
235/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
237 */
238static int x509_get_version( unsigned char **p,
239 const unsigned char *end,
240 int *ver )
241{
242 int ret;
243 size_t len;
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
246 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249 {
250 *ver = 0;
251 return( 0 );
252 }
253
254 return( ret );
255 }
256
257 end = *p + len;
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
260 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261
262 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 return( MBEDTLS_ERR_X509_INVALID_VERSION +
264 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265
266 return( 0 );
267}
268
269/*
270 * Validity ::= SEQUENCE {
271 * notBefore Time,
272 * notAfter Time }
273 */
274static int x509_get_dates( unsigned char **p,
275 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_x509_time *from,
277 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278{
279 int ret;
280 size_t len;
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
283 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
284 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285
286 end = *p + len;
287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289 return( ret );
290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200292 return( ret );
293
294 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 return( MBEDTLS_ERR_X509_INVALID_DATE +
296 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200297
298 return( 0 );
299}
300
301/*
302 * X.509 v2/v3 unique identifier (not parsed)
303 */
304static int x509_get_uid( unsigned char **p,
305 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307{
308 int ret;
309
310 if( *p == end )
311 return( 0 );
312
313 uid->tag = **p;
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
316 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200319 return( 0 );
320
321 return( ret );
322 }
323
324 uid->p = *p;
325 *p += uid->len;
326
327 return( 0 );
328}
329
330static int x509_get_basic_constraints( unsigned char **p,
331 const unsigned char *end,
332 int *ca_istrue,
333 int *max_pathlen )
334{
335 int ret;
336 size_t len;
337
338 /*
339 * BasicConstraints ::= SEQUENCE {
340 * cA BOOLEAN DEFAULT FALSE,
341 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
342 */
343 *ca_istrue = 0; /* DEFAULT FALSE */
344 *max_pathlen = 0; /* endless */
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
347 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
348 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349
350 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200351 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
356 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357
358 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360
361 if( *ca_istrue != 0 )
362 *ca_istrue = 1;
363 }
364
365 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200366 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
369 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370
371 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
373 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374
375 (*max_pathlen)++;
376
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200377 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378}
379
380static int x509_get_ns_cert_type( unsigned char **p,
381 const unsigned char *end,
382 unsigned char *ns_cert_type)
383{
384 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
388 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389
390 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
392 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393
394 /* Get actual bitstring */
395 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200396 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397}
398
399static int x509_get_key_usage( unsigned char **p,
400 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100401 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402{
403 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200404 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
408 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409
410 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
412 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413
414 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200415 *key_usage = 0;
416 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
417 {
418 *key_usage |= (unsigned int) bs.p[i] << (8*i);
419 }
420
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200421 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422}
423
424/*
425 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
426 *
427 * KeyPurposeId ::= OBJECT IDENTIFIER
428 */
429static int x509_get_ext_key_usage( unsigned char **p,
430 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432{
433 int ret;
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
436 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437
438 /* Sequence length must be >= 1 */
439 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
441 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200443 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444}
445
446/*
447 * SubjectAltName ::= GeneralNames
448 *
449 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
450 *
451 * GeneralName ::= CHOICE {
452 * otherName [0] OtherName,
453 * rfc822Name [1] IA5String,
454 * dNSName [2] IA5String,
455 * x400Address [3] ORAddress,
456 * directoryName [4] Name,
457 * ediPartyName [5] EDIPartyName,
458 * uniformResourceIdentifier [6] IA5String,
459 * iPAddress [7] OCTET STRING,
460 * registeredID [8] OBJECT IDENTIFIER }
461 *
462 * OtherName ::= SEQUENCE {
463 * type-id OBJECT IDENTIFIER,
464 * value [0] EXPLICIT ANY DEFINED BY type-id }
465 *
466 * EDIPartyName ::= SEQUENCE {
467 * nameAssigner [0] DirectoryString OPTIONAL,
468 * partyName [1] DirectoryString }
469 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000470 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471 */
472static int x509_get_subject_alt_name( unsigned char **p,
473 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475{
476 int ret;
477 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481
482 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
484 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
485 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486
487 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
489 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490
491 while( *p < end )
492 {
493 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
495 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496
497 tag = **p;
498 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
500 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 if( ( tag & MBEDTLS_ASN1_CONTEXT_SPECIFIC ) != MBEDTLS_ASN1_CONTEXT_SPECIFIC )
503 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
504 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200506 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508 {
509 *p += tag_len;
510 continue;
511 }
512
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200514 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100516 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100518
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200519 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520
521 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200523 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 cur = cur->next;
526 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200527
528 buf = &(cur->buf);
529 buf->tag = tag;
530 buf->p = *p;
531 buf->len = tag_len;
532 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533 }
534
535 /* Set final sequence entry's next pointer to NULL */
536 cur->next = NULL;
537
538 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
540 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541
542 return( 0 );
543}
544
545/*
546 * X.509 v3 extensions
547 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548 */
549static int x509_get_crt_ext( unsigned char **p,
550 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552{
553 int ret;
554 size_t len;
555 unsigned char *end_ext_data, *end_ext_octet;
556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560 return( 0 );
561
562 return( ret );
563 }
564
565 while( *p < end )
566 {
567 /*
568 * Extension ::= SEQUENCE {
569 * extnID OBJECT IDENTIFIER,
570 * critical BOOLEAN DEFAULT FALSE,
571 * extnValue OCTET STRING }
572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574 int is_critical = 0; /* DEFAULT FALSE */
575 int ext_type = 0;
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
578 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
579 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580
581 end_ext_data = *p + len;
582
583 /* Get extension ID */
584 extn_oid.tag = **p;
585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
587 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588
589 extn_oid.p = *p;
590 *p += extn_oid.len;
591
592 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
594 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595
596 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
598 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
599 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200600
601 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
603 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
604 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605
606 end_ext_octet = *p + len;
607
608 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
610 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611
612 /*
613 * Detect supported extensions
614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200616
617 if( ret != 0 )
618 {
619 /* No parser found, skip extension */
620 *p = end_ext_octet;
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623 if( is_critical )
624 {
625 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
627 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628 }
629#endif
630 continue;
631 }
632
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100633 /* Forbid repeated extensions */
634 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100636
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637 crt->ext_types |= ext_type;
638
639 switch( ext_type )
640 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100641 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 /* Parse basic constraints */
643 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
644 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200645 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646 break;
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649 /* Parse key usage */
650 if( ( ret = x509_get_key_usage( p, end_ext_octet,
651 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200652 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653 break;
654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656 /* Parse extended key usage */
657 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
658 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200659 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660 break;
661
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100662 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663 /* Parse subject alt name */
664 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
665 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200666 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200667 break;
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670 /* Parse netscape certificate type */
671 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
672 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200673 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 break;
675
676 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200678 }
679 }
680
681 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
683 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684
685 return( 0 );
686}
687
688/*
689 * Parse and fill a single X.509 certificate in DER format
690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200692 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693{
694 int ret;
695 size_t len;
696 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
700 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
701 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
703 /*
704 * Check for valid input
705 */
706 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708
Janos Follathcc0e49d2016-02-17 14:34:12 +0000709 // Use the original buffer until we figure out actual length
710 p = (unsigned char*) buf;
711 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200712 end = p + len;
713
714 /*
715 * Certificate ::= SEQUENCE {
716 * tbsCertificate TBSCertificate,
717 * signatureAlgorithm AlgorithmIdentifier,
718 * signatureValue BIT STRING }
719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
721 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_x509_crt_free( crt );
724 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725 }
726
727 if( len > (size_t) ( end - p ) )
728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_x509_crt_free( crt );
730 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
731 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200732 }
733 crt_end = p + len;
734
Janos Follathcc0e49d2016-02-17 14:34:12 +0000735 // Create and populate a new buffer for the raw field
736 crt->raw.len = crt_end - buf;
737 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
738 if( p == NULL )
739 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
740
741 memcpy( p, buf, crt->raw.len );
742
743 // Direct pointers to the new buffer
744 p += crt->raw.len - len;
745 end = crt_end = p + len;
746
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200747 /*
748 * TBSCertificate ::= SEQUENCE {
749 */
750 crt->tbs.p = p;
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
753 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 mbedtls_x509_crt_free( crt );
756 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757 }
758
759 end = p + len;
760 crt->tbs.len = end - crt->tbs.p;
761
762 /*
763 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
764 *
765 * CertificateSerialNumber ::= INTEGER
766 *
767 * signature AlgorithmIdentifier
768 */
769 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
771 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200772 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775 return( ret );
776 }
777
Andres AG7ca4a032017-03-09 16:16:11 +0000778 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_x509_crt_free( crt );
781 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200782 }
783
Andres AG7ca4a032017-03-09 16:16:11 +0000784 crt->version++;
785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200787 &crt->sig_md, &crt->sig_pk,
788 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791 return( ret );
792 }
793
794 /*
795 * issuer Name
796 */
797 crt->issuer_raw.p = p;
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
800 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_x509_crt_free( crt );
803 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804 }
805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200809 return( ret );
810 }
811
812 crt->issuer_raw.len = p - crt->issuer_raw.p;
813
814 /*
815 * Validity ::= SEQUENCE {
816 * notBefore Time,
817 * notAfter Time }
818 *
819 */
820 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
821 &crt->valid_to ) ) != 0 )
822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824 return( ret );
825 }
826
827 /*
828 * subject Name
829 */
830 crt->subject_raw.p = p;
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
833 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_x509_crt_free( crt );
836 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837 }
838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200842 return( ret );
843 }
844
845 crt->subject_raw.len = p - crt->subject_raw.p;
846
847 /*
848 * SubjectPublicKeyInfo
849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200853 return( ret );
854 }
855
856 /*
857 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
858 * -- If present, version shall be v2 or v3
859 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
860 * -- If present, version shall be v2 or v3
861 * extensions [3] EXPLICIT Extensions OPTIONAL
862 * -- If present, version shall be v3
863 */
864 if( crt->version == 2 || crt->version == 3 )
865 {
866 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
867 if( ret != 0 )
868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870 return( ret );
871 }
872 }
873
874 if( crt->version == 2 || crt->version == 3 )
875 {
876 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
877 if( ret != 0 )
878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 return( ret );
881 }
882 }
883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200886#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100887 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200888 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 if( ret != 0 )
890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 return( ret );
893 }
894 }
895
896 if( p != end )
897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_x509_crt_free( crt );
899 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
900 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901 }
902
903 end = crt_end;
904
905 /*
906 * }
907 * -- end of TBSCertificate
908 *
909 * signatureAlgorithm AlgorithmIdentifier,
910 * signatureValue BIT STRING
911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915 return( ret );
916 }
917
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100918 if( crt->sig_oid.len != sig_oid2.len ||
919 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200920 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200921 ( sig_params1.len != 0 &&
922 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_x509_crt_free( crt );
925 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926 }
927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931 return( ret );
932 }
933
934 if( p != end )
935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_x509_crt_free( crt );
937 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
938 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939 }
940
941 return( 0 );
942}
943
944/*
945 * Parse one X.509 certificate in DER format from a buffer and add them to a
946 * chained list
947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200949 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950{
951 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953
954 /*
955 * Check for valid input
956 */
957 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959
960 while( crt->version != 0 && crt->next != NULL )
961 {
962 prev = crt;
963 crt = crt->next;
964 }
965
966 /*
967 * Add new certificate on the end of the chain if needed.
968 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200969 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200971 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972
973 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200974 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975
976 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979 }
980
Paul Bakkerddf26b42013-09-18 13:46:23 +0200981 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 {
983 if( prev )
984 prev->next = NULL;
985
986 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988
989 return( ret );
990 }
991
992 return( 0 );
993}
994
995/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200996 * Parse one or more PEM certificates from a buffer and add them to the chained
997 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000{
Janos Follath98e28a72016-05-31 14:03:54 +01001001#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001002 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001004#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005
1006 /*
1007 * Check for valid input
1008 */
1009 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011
1012 /*
1013 * Determine buffer content. Buffer contains either one DER certificate or
1014 * one or more PEM certificates.
1015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001017 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001018 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001021 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1024 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001025#else
1026 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1027#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029#if defined(MBEDTLS_PEM_PARSE_C)
1030 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 {
1032 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001035 /* 1 rather than 0 since the terminating NULL byte is counted in */
1036 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037 {
1038 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001041 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 "-----BEGIN CERTIFICATE-----",
1044 "-----END CERTIFICATE-----",
1045 buf, NULL, 0, &use_len );
1046
1047 if( ret == 0 )
1048 {
1049 /*
1050 * Was PEM encoded
1051 */
1052 buflen -= use_len;
1053 buf += use_len;
1054 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056 {
1057 return( ret );
1058 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062
1063 /*
1064 * PEM header and footer were found
1065 */
1066 buflen -= use_len;
1067 buf += use_len;
1068
1069 if( first_error == 0 )
1070 first_error = ret;
1071
Paul Bakker5a5fa922014-09-26 14:53:04 +02001072 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001073 continue;
1074 }
1075 else
1076 break;
1077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081
1082 if( ret != 0 )
1083 {
1084 /*
1085 * Quit parsing on a memory error
1086 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001087 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088 return( ret );
1089
1090 if( first_error == 0 )
1091 first_error = ret;
1092
1093 total_failed++;
1094 continue;
1095 }
1096
1097 success = 1;
1098 }
1099 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100
1101 if( success )
1102 return( total_failed );
1103 else if( first_error )
1104 return( first_error );
1105 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001107#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108}
1109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111/*
1112 * Load one or more certificates and add them to the chained list
1113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001115{
1116 int ret;
1117 size_t n;
1118 unsigned char *buf;
1119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121 return( ret );
1122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001125 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127
1128 return( ret );
1129}
1130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132{
1133 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001134#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001135 int w_ret;
1136 WCHAR szDir[MAX_PATH];
1137 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001138 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001139 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140
Paul Bakker9af723c2014-05-01 13:03:14 +02001141 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142 HANDLE hFind;
1143
1144 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001146
Paul Bakker9af723c2014-05-01 13:03:14 +02001147 memset( szDir, 0, sizeof(szDir) );
1148 memset( filename, 0, MAX_PATH );
1149 memcpy( filename, path, len );
1150 filename[len++] = '\\';
1151 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152 filename[len++] = '*';
1153
Simon B3c6b18d2016-11-03 01:11:37 +00001154 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001155 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001156 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158
1159 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001160 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162
1163 len = MAX_PATH - len;
1164 do
1165 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001166 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001167
1168 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1169 continue;
1170
Paul Bakker9af723c2014-05-01 13:03:14 +02001171 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001172 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001173 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001174 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001175 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001176 {
1177 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1178 goto cleanup;
1179 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 if( w_ret < 0 )
1183 ret++;
1184 else
1185 ret += w_ret;
1186 }
1187 while( FindNextFileW( hFind, &file_data ) != 0 );
1188
Paul Bakker66d5d072014-06-17 16:39:18 +02001189 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191
Ron Eldor36d90422017-01-09 15:09:16 +02001192cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001193 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001194#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001195 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001196 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001197 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001198 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001199 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001200 DIR *dir = opendir( path );
1201
Paul Bakker66d5d072014-06-17 16:39:18 +02001202 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001204
Ron Eldor63140682017-01-09 19:27:59 +02001205#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001206 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001207 {
1208 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001209 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001210 }
Ron Eldor63140682017-01-09 19:27:59 +02001211#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001212
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001213 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001214 {
Andres AGf9113192016-09-02 14:06:04 +01001215 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1216 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001217
Andres AGf9113192016-09-02 14:06:04 +01001218 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219 {
Andres AGf9113192016-09-02 14:06:04 +01001220 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1221 goto cleanup;
1222 }
1223 else if( stat( entry_name, &sb ) == -1 )
1224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001226 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227 }
1228
1229 if( !S_ISREG( sb.st_mode ) )
1230 continue;
1231
1232 // Ignore parse errors
1233 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235 if( t_ret < 0 )
1236 ret++;
1237 else
1238 ret += t_ret;
1239 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001240
1241cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001242 closedir( dir );
1243
Ron Eldor63140682017-01-09 19:27:59 +02001244#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001245 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001247#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001248
Paul Bakkerbe089b02013-10-14 15:51:50 +02001249#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001250
1251 return( ret );
1252}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001254
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001255static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001257{
1258 size_t i;
1259 size_t n = *size;
1260 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001262 const char *sep = "";
1263 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001264
1265 while( cur != NULL )
1266 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001267 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001268 {
1269 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001270 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001271 }
1272
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001273 n -= cur->buf.len + sep_len;
1274 for( i = 0; i < sep_len; i++ )
1275 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001276 for( i = 0; i < cur->buf.len; i++ )
1277 *p++ = cur->buf.p[i];
1278
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001279 sep = ", ";
1280 sep_len = 2;
1281
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001282 cur = cur->next;
1283 }
1284
1285 *p = '\0';
1286
1287 *size = n;
1288 *buf = p;
1289
1290 return( 0 );
1291}
1292
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001293#define PRINT_ITEM(i) \
1294 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001296 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001297 sep = ", "; \
1298 }
1299
1300#define CERT_TYPE(type,name) \
1301 if( ns_cert_type & type ) \
1302 PRINT_ITEM( name );
1303
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001304static int x509_info_cert_type( char **buf, size_t *size,
1305 unsigned char ns_cert_type )
1306{
1307 int ret;
1308 size_t n = *size;
1309 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001310 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001313 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1315 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1316 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001317 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1318 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1319 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001320
1321 *size = n;
1322 *buf = p;
1323
1324 return( 0 );
1325}
1326
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001327#define KEY_USAGE(code,name) \
1328 if( key_usage & code ) \
1329 PRINT_ITEM( name );
1330
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001331static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001332 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001333{
1334 int ret;
1335 size_t n = *size;
1336 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001337 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1340 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001341 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1342 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1343 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1345 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001346 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1347 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001348
1349 *size = n;
1350 *buf = p;
1351
1352 return( 0 );
1353}
1354
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001355static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001357{
1358 int ret;
1359 const char *desc;
1360 size_t n = *size;
1361 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001363 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001364
1365 while( cur != NULL )
1366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001368 desc = "???";
1369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001371 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001372
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001373 sep = ", ";
1374
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001375 cur = cur->next;
1376 }
1377
1378 *size = n;
1379 *buf = p;
1380
1381 return( 0 );
1382}
1383
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001384/*
1385 * Return an informational string about the certificate.
1386 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001387#define BEFORE_COLON 18
1388#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1390 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001391{
1392 int ret;
1393 size_t n;
1394 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001395 char key_size_str[BEFORE_COLON];
1396
1397 p = buf;
1398 n = size;
1399
Janos Follath98e28a72016-05-31 14:03:54 +01001400 if( NULL == crt )
1401 {
1402 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
1403 MBEDTLS_X509_SAFE_SNPRINTF;
1404
1405 return( (int) ( size - n ) );
1406 }
1407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001409 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001410 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001412 prefix );
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_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001416 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001419 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001421 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001424 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001426 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1430 crt->valid_from.year, crt->valid_from.mon,
1431 crt->valid_from.day, crt->valid_from.hour,
1432 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001433 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1437 crt->valid_to.year, crt->valid_to.mon,
1438 crt->valid_to.day, crt->valid_to.hour,
1439 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001440 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001443 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001446 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001447 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001448
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001449 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1451 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452 {
1453 return( ret );
1454 }
1455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001457 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001458 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001459
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001460 /*
1461 * Optional extensions
1462 */
1463
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001464 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001467 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001468 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001469
1470 if( crt->max_pathlen > 0 )
1471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001473 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001474 }
1475 }
1476
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001477 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001480 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001481
1482 if( ( ret = x509_info_subject_alt_name( &p, &n,
1483 &crt->subject_alt_names ) ) != 0 )
1484 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001485 }
1486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001490 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001491
1492 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1493 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001494 }
1495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001499 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001500
1501 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1502 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001503 }
1504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001508 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001509
1510 if( ( ret = x509_info_ext_key_usage( &p, &n,
1511 &crt->ext_key_usage ) ) != 0 )
1512 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001513 }
1514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001516 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001517
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001518 return( (int) ( size - n ) );
1519}
1520
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001521struct x509_crt_verify_string {
1522 int code;
1523 const char *string;
1524};
1525
1526static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001527 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001528 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1529 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1530 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1531 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1532 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001533 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1534 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1535 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001536 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001537 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1538 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1539 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1540 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001541 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1542 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1543 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1544 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1545 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1546 { 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 +01001547 { 0, NULL }
1548};
1549
1550int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001551 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001552{
1553 int ret;
1554 const struct x509_crt_verify_string *cur;
1555 char *p = buf;
1556 size_t n = size;
1557
1558 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1559 {
1560 if( ( flags & cur->code ) == 0 )
1561 continue;
1562
1563 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001564 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001565 flags ^= cur->code;
1566 }
1567
1568 if( flags != 0 )
1569 {
1570 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1571 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001572 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001573 }
1574
1575 return( (int) ( size - n ) );
1576}
1577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001579int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1580 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001581{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001582 unsigned int usage_must, usage_may;
1583 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1584 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1585
1586 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1587 return( 0 );
1588
1589 usage_must = usage & ~may_mask;
1590
1591 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1592 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1593
1594 usage_may = usage & may_mask;
1595
1596 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001598
1599 return( 0 );
1600}
1601#endif
1602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1604int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001605 const char *usage_oid,
1606 size_t usage_len )
1607{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001609
1610 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001612 return( 0 );
1613
1614 /*
1615 * Look for the requested usage (or wildcard ANY) in our list
1616 */
1617 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001620
1621 if( cur_oid->len == usage_len &&
1622 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1623 {
1624 return( 0 );
1625 }
1626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001628 return( 0 );
1629 }
1630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636/*
1637 * Return 1 if the certificate is revoked, or 0 otherwise.
1638 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001639int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001642
1643 while( cur != NULL && cur->serial.len != 0 )
1644 {
1645 if( crt->serial.len == cur->serial.len &&
1646 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1647 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001648 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001649 return( 1 );
1650 }
1651
1652 cur = cur->next;
1653 }
1654
1655 return( 0 );
1656}
1657
1658/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01001659 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02001660 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001663 mbedtls_x509_crl *crl_list,
1664 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001665{
1666 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1668 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001669
1670 if( ca == NULL )
1671 return( flags );
1672
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001673 while( crl_list != NULL )
1674 {
1675 if( crl_list->version == 0 ||
1676 crl_list->issuer_raw.len != ca->subject_raw.len ||
1677 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1678 crl_list->issuer_raw.len ) != 0 )
1679 {
1680 crl_list = crl_list->next;
1681 continue;
1682 }
1683
1684 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001685 * Check if the CA is configured to sign CRLs
1686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1688 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001691 break;
1692 }
1693#endif
1694
1695 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001696 * Check if CRL is correctly signed by the trusted CA
1697 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001698 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1699 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1700
1701 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1702 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02001705 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001706 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02001707 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001709 break;
1710 }
1711
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001712 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1713 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1716 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001717 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001720 break;
1721 }
1722
1723 /*
1724 * Check for validity of CRL (Do not drop out)
1725 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001726 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001728
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001729 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001730 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001731
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001732 /*
1733 * Check if certificate is revoked
1734 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001735 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001738 break;
1739 }
1740
1741 crl_list = crl_list->next;
1742 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001743
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001744 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001747
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001748/*
1749 * Like memcmp, but case-insensitive and always returns -1 if different
1750 */
1751static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001752{
1753 size_t i;
1754 unsigned char diff;
1755 const unsigned char *n1 = s1, *n2 = s2;
1756
1757 for( i = 0; i < len; i++ )
1758 {
1759 diff = n1[i] ^ n2[i];
1760
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001761 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001762 continue;
1763
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001764 if( diff == 32 &&
1765 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1766 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1767 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001769 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001770
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001771 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001772 }
1773
1774 return( 0 );
1775}
1776
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001777/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001778 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001779 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02001780static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001781{
1782 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001783 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001784
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02001785 /* We can't have a match if there is no wildcard to match */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001786 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02001787 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001788
Paul Bakker14b16c62014-05-28 11:33:54 +02001789 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001790 {
1791 if( cn[i] == '.' )
1792 {
1793 cn_idx = i;
1794 break;
1795 }
1796 }
1797
1798 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001799 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001800
Paul Bakker14b16c62014-05-28 11:33:54 +02001801 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001802 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001803 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001804 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001805 }
1806
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001807 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808}
1809
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001810/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001811 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1812 * variations (but not all).
1813 *
1814 * Return 0 if equal, -1 otherwise.
1815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001817{
1818 if( a->tag == b->tag &&
1819 a->len == b->len &&
1820 memcmp( a->p, b->p, b->len ) == 0 )
1821 {
1822 return( 0 );
1823 }
1824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1826 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001827 a->len == b->len &&
1828 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1829 {
1830 return( 0 );
1831 }
1832
1833 return( -1 );
1834}
1835
1836/*
1837 * Compare two X.509 Names (aka rdnSequence).
1838 *
1839 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1840 * we sometimes return unequal when the full algorithm would return equal,
1841 * but never the other way. (In particular, we don't do Unicode normalisation
1842 * or space folding.)
1843 *
1844 * Return 0 if equal, -1 otherwise.
1845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001847{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001848 /* Avoid recursion, it might not be optimised by the compiler */
1849 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001850 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001851 if( a == NULL || b == NULL )
1852 return( -1 );
1853
1854 /* type */
1855 if( a->oid.tag != b->oid.tag ||
1856 a->oid.len != b->oid.len ||
1857 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1858 {
1859 return( -1 );
1860 }
1861
1862 /* value */
1863 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1864 return( -1 );
1865
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001866 /* structure of the list of sets */
1867 if( a->next_merged != b->next_merged )
1868 return( -1 );
1869
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001870 a = a->next;
1871 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001872 }
1873
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001874 /* a == NULL == b */
1875 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001876}
1877
1878/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001879 * Check the signature of a certificate by its parent
1880 */
1881static int x509_crt_check_signature( const mbedtls_x509_crt *child,
1882 mbedtls_x509_crt *parent )
1883{
1884 const mbedtls_md_info_t *md_info;
1885 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1886
1887 md_info = mbedtls_md_info_from_type( child->sig_md );
1888 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
1889 {
1890 /* Note: this can't happen except after an internal error */
1891 return( -1 );
1892 }
1893
1894 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1895 child->sig_md, hash, mbedtls_md_get_size( md_info ),
1896 child->sig.p, child->sig.len ) != 0 )
1897 {
1898 return( -1 );
1899 }
1900
1901 return( 0 );
1902}
1903
1904/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001905 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1906 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001907 *
1908 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001909 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1911 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02001912 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001913{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001914 int need_ca_bit;
1915
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001916 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001917 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001918 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001919
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001920 /* Parent must have the basicConstraints CA bit set as a general rule */
1921 need_ca_bit = 1;
1922
1923 /* Exception: v1/v2 certificates that are locally trusted. */
1924 if( top && parent->version < 3 )
1925 need_ca_bit = 0;
1926
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001927 if( need_ca_bit && ! parent->ca_istrue )
1928 return( -1 );
1929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001931 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001933 {
1934 return( -1 );
1935 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001936#endif
1937
1938 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001939}
1940
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02001941/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001942 * Find a suitable parent for child in candidates, or return NULL.
1943 *
1944 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001945 * 1. subject name matches child's issuer
1946 * 2. if necessary, the CA bit is set and key usage allows signing certs
1947 * 3. for trusted roots, the signature is correct
1948 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001949 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02001950 * If there's a suitable candidate which is also time-valid, return the first
1951 * such. Otherwise, return the first suitable candidate (or NULL if there is
1952 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001953 *
1954 * The rationale for this rule is that someone could have a list of trusted
1955 * roots with two versions on the same root with different validity periods.
1956 * (At least one user reported having such a list and wanted it to just work.)
1957 * The reason we don't just require time-validity is that generally there is
1958 * only one version, and if it's expired we want the flags to state that
1959 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001960 *
1961 * The rationale for rule 3 (signature for trusted roots) is that users might
1962 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02001963 * way we select the correct one is by checking the signature (as we don't
1964 * rely on key identifier extensions). (This is one way users might choose to
1965 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001966 */
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02001967static mbedtls_x509_crt *x509_crt_find_parent_in( mbedtls_x509_crt *child,
1968 mbedtls_x509_crt *candidates,
1969 int top,
1970 int path_cnt,
1971 int self_cnt )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001972{
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001973 mbedtls_x509_crt *parent, *badtime_parent = NULL;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001974
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001975 for( parent = candidates; parent != NULL; parent = parent->next )
1976 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001977 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02001978 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001979 continue;
1980
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02001981 /* +1 because stored max_pathlen is 1 higher that the actual value */
1982 if( parent->max_pathlen > 0 &&
1983 parent->max_pathlen < 1 + path_cnt - self_cnt )
1984 {
1985 continue;
1986 }
1987
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001988 /* Signature */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001989 if( top && x509_crt_check_signature( child, parent ) != 0 )
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001990 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001991 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001992 }
1993
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02001994 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001995 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
1996 mbedtls_x509_time_is_future( &parent->valid_from ) )
1997 {
1998 if( badtime_parent == NULL )
1999 badtime_parent = parent;
2000
2001 continue;
2002 }
2003
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002004 break;
2005 }
2006
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002007 if( parent == NULL )
2008 parent = badtime_parent;
2009
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002010 return( parent );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002011}
2012
2013/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002014 * Find a parent in trusted CAs or the provided chain, or return NULL.
2015 *
2016 * Searches in trusted CAs first, and return the first suitable parent found
2017 * (see find_parent_in() for definition of suitable).
2018 */
2019static mbedtls_x509_crt *x509_crt_find_parent( mbedtls_x509_crt *child,
2020 mbedtls_x509_crt *trust_ca,
2021 int *parent_is_trusted,
2022 int path_cnt,
2023 int self_cnt )
2024{
2025 mbedtls_x509_crt *parent;
2026
2027 /* Look for a parent in trusted CAs */
2028 *parent_is_trusted = 1;
2029 parent = x509_crt_find_parent_in( child, trust_ca, 1, path_cnt, self_cnt );
2030
2031 if( parent != NULL )
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002032 return( parent );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002033
2034 /* Look for a parent upwards the chain */
2035 *parent_is_trusted = 0;
2036 return( x509_crt_find_parent_in( child, child->next, 0, path_cnt, self_cnt ) );
2037}
2038
2039/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002040 * Check if an end-entity certificate is locally trusted
2041 *
2042 * Currently we require such certificates to be self-signed (actually only
2043 * check for self-issued as self-signatures are not checked)
2044 */
2045static int x509_crt_check_ee_locally_trusted(
2046 mbedtls_x509_crt *crt,
2047 mbedtls_x509_crt *trust_ca )
2048{
2049 mbedtls_x509_crt *cur;
2050
2051 /* must be self-issued */
2052 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2053 return( -1 );
2054
2055 /* look for an exact match with trusted cert */
2056 for( cur = trust_ca; cur != NULL; cur = cur->next )
2057 {
2058 if( crt->raw.len == cur->raw.len &&
2059 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2060 {
2061 return( 0 );
2062 }
2063 }
2064
2065 /* too bad */
2066 return( -1 );
2067}
2068
2069/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002070 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002071 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002072 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2073 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002074 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002075 * such that every cert in the chain is a child of the next one,
2076 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002077 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002078 * Verify that chain and return it with flags for all issues found.
2079 *
2080 * Special cases:
2081 * - EE == Rj -> return a one-element list containing it
2082 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2083 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002084 *
2085 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002086 * - [in] crt: the cert list EE, C1, ..., Cn
2087 * - [in] trust_ca: the trusted list R1, ..., Rp
2088 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002089 * - [out] ver_chain, chain_len: the built and verified chain
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002090 *
2091 * Return value:
2092 * - non-zero if the chain could not be fully built and examined
2093 * - 0 is the chain was successfully built and examined,
2094 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002095 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002096static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002097 mbedtls_x509_crt *crt,
2098 mbedtls_x509_crt *trust_ca,
2099 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002100 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002101 x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE],
2102 size_t *chain_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002103{
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002104 uint32_t *flags;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002105 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002106 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard6e786742017-07-03 23:47:44 +02002107 int parent_is_trusted = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002108 int child_is_trusted = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002109 int self_cnt = 0;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002110
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002111 child = crt;
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002112 *chain_len = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002113
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002114 while( 1 ) {
2115 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002116 ver_chain[*chain_len].crt = child;
2117 flags = &ver_chain[*chain_len].flags;
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002118 ++*chain_len;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002119
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002120 /* Check time-validity (all certificates) */
2121 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2122 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002123
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002124 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2125 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002126
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002127 /* Stop here for trusted roots (but not for trusted EE certs) */
2128 if( child_is_trusted )
2129 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002130
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002131 /* Check signature algorithm: MD & PK algs */
2132 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2133 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002134
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002135 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2136 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002137
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002138 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002139 if( *chain_len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002140 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2141 {
2142 return( 0 );
2143 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002144
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002145 /* Look for a parent in trusted CAs or up the chain */
2146 parent = x509_crt_find_parent( child, trust_ca, &parent_is_trusted,
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002147 *chain_len - 1, self_cnt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002148
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002149 /* No parent? We're done here */
2150 if( parent == NULL )
2151 {
2152 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2153 return( 0 );
2154 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002155
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002156 /* Count intermediate self-issued (not necessarily self-signed) certs.
2157 * These can occur with some strategies for key rollover, see [SIRO],
2158 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002159 if( *chain_len != 1 &&
2160 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2161 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002162 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002163 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002164
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002165 /* path_cnt is 0 for the first intermediate CA,
2166 * and if parent is trusted it's not an intermediate CA */
2167 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002168 *chain_len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002169 {
2170 /* return immediately to avoid overflow the chain array */
2171 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2172 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002173
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002174 /* if parent is trusted, the signature was checked by find_parent() */
2175 if( ! parent_is_trusted && x509_crt_check_signature( child, parent ) != 0 )
2176 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2177
2178 /* check size of signing key */
2179 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2180 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002183 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002184 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002185#else
2186 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002187#endif
2188
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002189 /* prepare for next iteration */
2190 child = parent;
2191 parent = NULL;
2192 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002193 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002194}
2195
2196/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002197 * Check for CN match
2198 */
2199static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2200 const char *cn, size_t cn_len )
2201{
2202 /* try exact match */
2203 if( name->len == cn_len &&
2204 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2205 {
2206 return( 0 );
2207 }
2208
2209 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002210 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002211 {
2212 return( 0 );
2213 }
2214
2215 return( -1 );
2216}
2217
2218/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002219 * Verify the requested CN - only call this if cn is not NULL!
2220 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002221static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002222 const char *cn,
2223 uint32_t *flags )
2224{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002225 const mbedtls_x509_name *name;
2226 const mbedtls_x509_sequence *cur;
2227 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002228
2229 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
2230 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002231 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002232 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002233 if( x509_crt_check_cn( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002234 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002235 }
2236
2237 if( cur == NULL )
2238 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2239 }
2240 else
2241 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002242 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002243 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002244 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
2245 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002246 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002247 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002248 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002249 }
2250
2251 if( name == NULL )
2252 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2253 }
2254}
2255
2256/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002257 * Merge the flags for all certs in the chain, after calling callback
2258 */
2259static int x509_crt_merge_flags_with_cb(
2260 uint32_t *flags,
2261 x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE],
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002262 size_t chain_len,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002263 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2264 void *p_vrfy )
2265{
2266 int ret;
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002267 size_t i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002268 uint32_t cur_flags;
2269
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002270 for( i = chain_len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002271 {
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002272 cur_flags = ver_chain[i-1].flags;
2273
2274 if( NULL != f_vrfy )
2275 if( ( ret = f_vrfy( p_vrfy, ver_chain[i-1].crt, i-1, &cur_flags ) ) != 0 )
2276 return( ret );
2277
2278 *flags |= cur_flags;
2279 }
2280
2281 return( 0 );
2282}
2283
2284/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002285 * Verify the certificate validity
2286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2288 mbedtls_x509_crt *trust_ca,
2289 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002290 const char *cn, uint32_t *flags,
2291 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002292 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002293{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002294 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002295 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002296}
2297
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002298/*
2299 * Verify the certificate validity, with profile
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002300 *
Manuel Pégourié-Gonnard66a36b02017-07-12 12:23:06 +02002301 * This function:
2302 * - checks the requested CN (if any)
2303 * - checks the type and size of the EE cert's key,
2304 * as that isn't done as part of chain building/verification currently
2305 * - builds and verifies the chain
2306 * - then calls the callback and merges the flags
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002307 */
2308int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2309 mbedtls_x509_crt *trust_ca,
2310 mbedtls_x509_crl *ca_crl,
2311 const mbedtls_x509_crt_profile *profile,
2312 const char *cn, uint32_t *flags,
2313 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2314 void *p_vrfy )
2315{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002316 int ret;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002317 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002318 x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002319 size_t chain_len;
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002320 uint32_t *ee_flags = &ver_chain[0].flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002321
2322 *flags = 0;
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002323 memset( ver_chain, 0, sizeof( ver_chain ) );
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002324 chain_len = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002325
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002326 if( profile == NULL )
2327 {
2328 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2329 goto exit;
2330 }
2331
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002332 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002333 if( cn != NULL )
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002334 x509_crt_verify_name( crt, cn, ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002335
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002336 /* Check the type and size of the key */
2337 pk_type = mbedtls_pk_get_type( &crt->pk );
2338
2339 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002340 *ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002341
2342 if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002343 *ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002344
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002345 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002346 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
2347 ver_chain, &chain_len );
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002348 if( ret != 0 )
2349 goto exit;
2350
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002351 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002352 ret = x509_crt_merge_flags_with_cb( flags,
2353 ver_chain, chain_len, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002354
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002355exit:
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02002356 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2357 * the SSL module for authmode optional, but non-zero return from the
2358 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002359 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2360 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2361
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002362 if( ret != 0 )
2363 {
2364 *flags = (uint32_t) -1;
2365 return( ret );
2366 }
2367
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002368 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002370
2371 return( 0 );
2372}
2373
2374/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002375 * Initialize a certificate chain
2376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002378{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002380}
2381
2382/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002383 * Unallocate all certificate data
2384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002386{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 mbedtls_x509_crt *cert_cur = crt;
2388 mbedtls_x509_crt *cert_prv;
2389 mbedtls_x509_name *name_cur;
2390 mbedtls_x509_name *name_prv;
2391 mbedtls_x509_sequence *seq_cur;
2392 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002393
2394 if( crt == NULL )
2395 return;
2396
2397 do
2398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2402 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002403#endif
2404
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002405 name_cur = cert_cur->issuer.next;
2406 while( name_cur != NULL )
2407 {
2408 name_prv = name_cur;
2409 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2411 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002412 }
2413
2414 name_cur = cert_cur->subject.next;
2415 while( name_cur != NULL )
2416 {
2417 name_prv = name_cur;
2418 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2420 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002421 }
2422
2423 seq_cur = cert_cur->ext_key_usage.next;
2424 while( seq_cur != NULL )
2425 {
2426 seq_prv = seq_cur;
2427 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2429 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002430 }
2431
2432 seq_cur = cert_cur->subject_alt_names.next;
2433 while( seq_cur != NULL )
2434 {
2435 seq_prv = seq_cur;
2436 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2438 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002439 }
2440
2441 if( cert_cur->raw.p != NULL )
2442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2444 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002445 }
2446
2447 cert_cur = cert_cur->next;
2448 }
2449 while( cert_cur != NULL );
2450
2451 cert_cur = crt;
2452 do
2453 {
2454 cert_prv = cert_cur;
2455 cert_cur = cert_cur->next;
2456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002458 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002460 }
2461 while( cert_cur != NULL );
2462}
2463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464#endif /* MBEDTLS_X509_CRT_PARSE_C */