blob: bbc0f3c0871a8101adc6a8e87272406b1ebebb88 [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
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200164 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200165 */
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
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200177 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200178 */
179static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
180 mbedtls_pk_type_t pk_alg )
181{
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
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200190 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200191 */
192static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200193 const mbedtls_pk_context *pk )
194{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200195 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200196
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200197#if defined(MBEDTLS_RSA_C)
198 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
199 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200200 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200201 return( 0 );
202
203 return( -1 );
204 }
205#endif
206
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200207#if defined(MBEDTLS_ECP_C)
208 if( pk_alg == MBEDTLS_PK_ECDSA ||
209 pk_alg == MBEDTLS_PK_ECKEY ||
210 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200211 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200212 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200213
214 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
215 return( 0 );
216
217 return( -1 );
218 }
219#endif
220
221 return( -1 );
222}
223
224/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200225 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
226 */
227static int x509_get_version( unsigned char **p,
228 const unsigned char *end,
229 int *ver )
230{
231 int ret;
232 size_t len;
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
235 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200238 {
239 *ver = 0;
240 return( 0 );
241 }
242
243 return( ret );
244 }
245
246 end = *p + len;
247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
249 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200250
251 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 return( MBEDTLS_ERR_X509_INVALID_VERSION +
253 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254
255 return( 0 );
256}
257
258/*
259 * Validity ::= SEQUENCE {
260 * notBefore Time,
261 * notAfter Time }
262 */
263static int x509_get_dates( unsigned char **p,
264 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_x509_time *from,
266 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200267{
268 int ret;
269 size_t len;
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
272 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
273 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200274
275 end = *p + len;
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278 return( ret );
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281 return( ret );
282
283 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 return( MBEDTLS_ERR_X509_INVALID_DATE +
285 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286
287 return( 0 );
288}
289
290/*
291 * X.509 v2/v3 unique identifier (not parsed)
292 */
293static int x509_get_uid( unsigned char **p,
294 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296{
297 int ret;
298
299 if( *p == end )
300 return( 0 );
301
302 uid->tag = **p;
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
305 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200308 return( 0 );
309
310 return( ret );
311 }
312
313 uid->p = *p;
314 *p += uid->len;
315
316 return( 0 );
317}
318
319static int x509_get_basic_constraints( unsigned char **p,
320 const unsigned char *end,
321 int *ca_istrue,
322 int *max_pathlen )
323{
324 int ret;
325 size_t len;
326
327 /*
328 * BasicConstraints ::= SEQUENCE {
329 * cA BOOLEAN DEFAULT FALSE,
330 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
331 */
332 *ca_istrue = 0; /* DEFAULT FALSE */
333 *max_pathlen = 0; /* endless */
334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
336 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
337 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200338
339 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200340 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
345 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200346
347 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349
350 if( *ca_istrue != 0 )
351 *ca_istrue = 1;
352 }
353
354 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200355 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
358 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359
360 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
362 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363
364 (*max_pathlen)++;
365
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200366 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367}
368
369static int x509_get_ns_cert_type( unsigned char **p,
370 const unsigned char *end,
371 unsigned char *ns_cert_type)
372{
373 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
377 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378
379 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
381 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382
383 /* Get actual bitstring */
384 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200385 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386}
387
388static int x509_get_key_usage( unsigned char **p,
389 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100390 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391{
392 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200393 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
397 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398
399 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
401 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402
403 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200404 *key_usage = 0;
405 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
406 {
407 *key_usage |= (unsigned int) bs.p[i] << (8*i);
408 }
409
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200410 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411}
412
413/*
414 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
415 *
416 * KeyPurposeId ::= OBJECT IDENTIFIER
417 */
418static int x509_get_ext_key_usage( unsigned char **p,
419 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421{
422 int ret;
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
425 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426
427 /* Sequence length must be >= 1 */
428 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
430 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200432 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433}
434
435/*
436 * SubjectAltName ::= GeneralNames
437 *
438 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
439 *
440 * GeneralName ::= CHOICE {
441 * otherName [0] OtherName,
442 * rfc822Name [1] IA5String,
443 * dNSName [2] IA5String,
444 * x400Address [3] ORAddress,
445 * directoryName [4] Name,
446 * ediPartyName [5] EDIPartyName,
447 * uniformResourceIdentifier [6] IA5String,
448 * iPAddress [7] OCTET STRING,
449 * registeredID [8] OBJECT IDENTIFIER }
450 *
451 * OtherName ::= SEQUENCE {
452 * type-id OBJECT IDENTIFIER,
453 * value [0] EXPLICIT ANY DEFINED BY type-id }
454 *
455 * EDIPartyName ::= SEQUENCE {
456 * nameAssigner [0] DirectoryString OPTIONAL,
457 * partyName [1] DirectoryString }
458 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000459 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460 */
461static int x509_get_subject_alt_name( unsigned char **p,
462 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464{
465 int ret;
466 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470
471 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
473 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
474 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475
476 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
478 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479
480 while( *p < end )
481 {
482 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
484 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485
486 tag = **p;
487 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
489 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 if( ( tag & MBEDTLS_ASN1_CONTEXT_SPECIFIC ) != MBEDTLS_ASN1_CONTEXT_SPECIFIC )
492 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
493 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200495 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 {
498 *p += tag_len;
499 continue;
500 }
501
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200503 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100505 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100507
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200508 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509
510 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200512 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514 cur = cur->next;
515 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200516
517 buf = &(cur->buf);
518 buf->tag = tag;
519 buf->p = *p;
520 buf->len = tag_len;
521 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522 }
523
524 /* Set final sequence entry's next pointer to NULL */
525 cur->next = NULL;
526
527 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
529 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530
531 return( 0 );
532}
533
534/*
535 * X.509 v3 extensions
536 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537 */
538static int x509_get_crt_ext( unsigned char **p,
539 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541{
542 int ret;
543 size_t len;
544 unsigned char *end_ext_data, *end_ext_octet;
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549 return( 0 );
550
551 return( ret );
552 }
553
554 while( *p < end )
555 {
556 /*
557 * Extension ::= SEQUENCE {
558 * extnID OBJECT IDENTIFIER,
559 * critical BOOLEAN DEFAULT FALSE,
560 * extnValue OCTET STRING }
561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563 int is_critical = 0; /* DEFAULT FALSE */
564 int ext_type = 0;
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
567 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
568 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569
570 end_ext_data = *p + len;
571
572 /* Get extension ID */
573 extn_oid.tag = **p;
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
576 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577
578 extn_oid.p = *p;
579 *p += extn_oid.len;
580
581 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
583 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584
585 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
587 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
588 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589
590 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
592 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
593 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594
595 end_ext_octet = *p + len;
596
597 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
599 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200600
601 /*
602 * Detect supported extensions
603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605
606 if( ret != 0 )
607 {
608 /* No parser found, skip extension */
609 *p = end_ext_octet;
610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612 if( is_critical )
613 {
614 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
616 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617 }
618#endif
619 continue;
620 }
621
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100622 /* Forbid repeated extensions */
623 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100625
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 crt->ext_types |= ext_type;
627
628 switch( ext_type )
629 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100630 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631 /* Parse basic constraints */
632 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
633 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200634 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 break;
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638 /* Parse key usage */
639 if( ( ret = x509_get_key_usage( p, end_ext_octet,
640 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200641 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 break;
643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645 /* Parse extended key usage */
646 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
647 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200648 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649 break;
650
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100651 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652 /* Parse subject alt name */
653 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
654 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200655 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656 break;
657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659 /* Parse netscape certificate type */
660 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
661 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200662 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663 break;
664
665 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200667 }
668 }
669
670 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
672 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673
674 return( 0 );
675}
676
677/*
678 * Parse and fill a single X.509 certificate in DER format
679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200681 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682{
683 int ret;
684 size_t len;
685 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
689 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
690 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691
692 /*
693 * Check for valid input
694 */
695 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697
Janos Follathcc0e49d2016-02-17 14:34:12 +0000698 // Use the original buffer until we figure out actual length
699 p = (unsigned char*) buf;
700 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701 end = p + len;
702
703 /*
704 * Certificate ::= SEQUENCE {
705 * tbsCertificate TBSCertificate,
706 * signatureAlgorithm AlgorithmIdentifier,
707 * signatureValue BIT STRING }
708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
710 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_x509_crt_free( crt );
713 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714 }
715
716 if( len > (size_t) ( end - p ) )
717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_x509_crt_free( crt );
719 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
720 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721 }
722 crt_end = p + len;
723
Janos Follathcc0e49d2016-02-17 14:34:12 +0000724 // Create and populate a new buffer for the raw field
725 crt->raw.len = crt_end - buf;
726 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
727 if( p == NULL )
728 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
729
730 memcpy( p, buf, crt->raw.len );
731
732 // Direct pointers to the new buffer
733 p += crt->raw.len - len;
734 end = crt_end = p + len;
735
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736 /*
737 * TBSCertificate ::= SEQUENCE {
738 */
739 crt->tbs.p = p;
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
742 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_x509_crt_free( crt );
745 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746 }
747
748 end = p + len;
749 crt->tbs.len = end - crt->tbs.p;
750
751 /*
752 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
753 *
754 * CertificateSerialNumber ::= INTEGER
755 *
756 * signature AlgorithmIdentifier
757 */
758 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
760 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200761 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764 return( ret );
765 }
766
Andres AG7ca4a032017-03-09 16:16:11 +0000767 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_x509_crt_free( crt );
770 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 }
772
Andres AG7ca4a032017-03-09 16:16:11 +0000773 crt->version++;
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200776 &crt->sig_md, &crt->sig_pk,
777 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780 return( ret );
781 }
782
783 /*
784 * issuer Name
785 */
786 crt->issuer_raw.p = p;
787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
789 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_x509_crt_free( crt );
792 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793 }
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798 return( ret );
799 }
800
801 crt->issuer_raw.len = p - crt->issuer_raw.p;
802
803 /*
804 * Validity ::= SEQUENCE {
805 * notBefore Time,
806 * notAfter Time }
807 *
808 */
809 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
810 &crt->valid_to ) ) != 0 )
811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813 return( ret );
814 }
815
816 /*
817 * subject Name
818 */
819 crt->subject_raw.p = p;
820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
822 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 mbedtls_x509_crt_free( crt );
825 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826 }
827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200831 return( ret );
832 }
833
834 crt->subject_raw.len = p - crt->subject_raw.p;
835
836 /*
837 * SubjectPublicKeyInfo
838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 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 /*
846 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
847 * -- If present, version shall be v2 or v3
848 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
849 * -- If present, version shall be v2 or v3
850 * extensions [3] EXPLICIT Extensions OPTIONAL
851 * -- If present, version shall be v3
852 */
853 if( crt->version == 2 || crt->version == 3 )
854 {
855 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
856 if( ret != 0 )
857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200859 return( ret );
860 }
861 }
862
863 if( crt->version == 2 || crt->version == 3 )
864 {
865 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
866 if( ret != 0 )
867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869 return( ret );
870 }
871 }
872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200875#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100876 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200877 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878 if( ret != 0 )
879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881 return( ret );
882 }
883 }
884
885 if( p != end )
886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_x509_crt_free( crt );
888 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
889 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 }
891
892 end = crt_end;
893
894 /*
895 * }
896 * -- end of TBSCertificate
897 *
898 * signatureAlgorithm AlgorithmIdentifier,
899 * signatureValue BIT STRING
900 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 return( ret );
905 }
906
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100907 if( crt->sig_oid.len != sig_oid2.len ||
908 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200909 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200910 ( sig_params1.len != 0 &&
911 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_x509_crt_free( crt );
914 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915 }
916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920 return( ret );
921 }
922
923 if( p != end )
924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_x509_crt_free( crt );
926 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
927 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928 }
929
930 return( 0 );
931}
932
933/*
934 * Parse one X.509 certificate in DER format from a buffer and add them to a
935 * chained list
936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200938 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939{
940 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942
943 /*
944 * Check for valid input
945 */
946 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948
949 while( crt->version != 0 && crt->next != NULL )
950 {
951 prev = crt;
952 crt = crt->next;
953 }
954
955 /*
956 * Add new certificate on the end of the chain if needed.
957 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200958 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200960 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961
962 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200963 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964
965 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968 }
969
Paul Bakkerddf26b42013-09-18 13:46:23 +0200970 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971 {
972 if( prev )
973 prev->next = NULL;
974
975 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977
978 return( ret );
979 }
980
981 return( 0 );
982}
983
984/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200985 * Parse one or more PEM certificates from a buffer and add them to the chained
986 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989{
Janos Follath98e28a72016-05-31 14:03:54 +0100990#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +0000991 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +0100993#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994
995 /*
996 * Check for valid input
997 */
998 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000
1001 /*
1002 * Determine buffer content. Buffer contains either one DER certificate or
1003 * one or more PEM certificates.
1004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001006 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001007 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001010 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1013 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001014#else
1015 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1016#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#if defined(MBEDTLS_PEM_PARSE_C)
1019 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 {
1021 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001024 /* 1 rather than 0 since the terminating NULL byte is counted in */
1025 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 {
1027 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001030 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032 "-----BEGIN CERTIFICATE-----",
1033 "-----END CERTIFICATE-----",
1034 buf, NULL, 0, &use_len );
1035
1036 if( ret == 0 )
1037 {
1038 /*
1039 * Was PEM encoded
1040 */
1041 buflen -= use_len;
1042 buf += use_len;
1043 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045 {
1046 return( ret );
1047 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051
1052 /*
1053 * PEM header and footer were found
1054 */
1055 buflen -= use_len;
1056 buf += use_len;
1057
1058 if( first_error == 0 )
1059 first_error = ret;
1060
Paul Bakker5a5fa922014-09-26 14:53:04 +02001061 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062 continue;
1063 }
1064 else
1065 break;
1066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070
1071 if( ret != 0 )
1072 {
1073 /*
1074 * Quit parsing on a memory error
1075 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001076 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077 return( ret );
1078
1079 if( first_error == 0 )
1080 first_error = ret;
1081
1082 total_failed++;
1083 continue;
1084 }
1085
1086 success = 1;
1087 }
1088 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089
1090 if( success )
1091 return( total_failed );
1092 else if( first_error )
1093 return( first_error );
1094 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001096#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097}
1098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100/*
1101 * Load one or more certificates and add them to the chained list
1102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104{
1105 int ret;
1106 size_t n;
1107 unsigned char *buf;
1108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110 return( ret );
1111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001114 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001116
1117 return( ret );
1118}
1119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121{
1122 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001123#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124 int w_ret;
1125 WCHAR szDir[MAX_PATH];
1126 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001127 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001128 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129
Paul Bakker9af723c2014-05-01 13:03:14 +02001130 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001131 HANDLE hFind;
1132
1133 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001135
Paul Bakker9af723c2014-05-01 13:03:14 +02001136 memset( szDir, 0, sizeof(szDir) );
1137 memset( filename, 0, MAX_PATH );
1138 memcpy( filename, path, len );
1139 filename[len++] = '\\';
1140 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001141 filename[len++] = '*';
1142
Simon B3c6b18d2016-11-03 01:11:37 +00001143 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001144 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001145 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001147
1148 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001149 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001151
1152 len = MAX_PATH - len;
1153 do
1154 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001155 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156
1157 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1158 continue;
1159
Paul Bakker9af723c2014-05-01 13:03:14 +02001160 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001161 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001162 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001163 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001164 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001165 {
1166 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1167 goto cleanup;
1168 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 if( w_ret < 0 )
1172 ret++;
1173 else
1174 ret += w_ret;
1175 }
1176 while( FindNextFileW( hFind, &file_data ) != 0 );
1177
Paul Bakker66d5d072014-06-17 16:39:18 +02001178 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180
Ron Eldor36d90422017-01-09 15:09:16 +02001181cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001183#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001184 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001185 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001187 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001188 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189 DIR *dir = opendir( path );
1190
Paul Bakker66d5d072014-06-17 16:39:18 +02001191 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001193
Ron Eldor63140682017-01-09 19:27:59 +02001194#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001195 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001196 {
1197 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001198 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001199 }
Ron Eldor63140682017-01-09 19:27:59 +02001200#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001201
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001202 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203 {
Andres AGf9113192016-09-02 14:06:04 +01001204 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1205 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206
Andres AGf9113192016-09-02 14:06:04 +01001207 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001208 {
Andres AGf9113192016-09-02 14:06:04 +01001209 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1210 goto cleanup;
1211 }
1212 else if( stat( entry_name, &sb ) == -1 )
1213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001215 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001216 }
1217
1218 if( !S_ISREG( sb.st_mode ) )
1219 continue;
1220
1221 // Ignore parse errors
1222 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001224 if( t_ret < 0 )
1225 ret++;
1226 else
1227 ret += t_ret;
1228 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001229
1230cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001231 closedir( dir );
1232
Ron Eldor63140682017-01-09 19:27:59 +02001233#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001234 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001236#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001237
Paul Bakkerbe089b02013-10-14 15:51:50 +02001238#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001239
1240 return( ret );
1241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001243
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001244static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001246{
1247 size_t i;
1248 size_t n = *size;
1249 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001251 const char *sep = "";
1252 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001253
1254 while( cur != NULL )
1255 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001256 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001257 {
1258 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001259 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001260 }
1261
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001262 n -= cur->buf.len + sep_len;
1263 for( i = 0; i < sep_len; i++ )
1264 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001265 for( i = 0; i < cur->buf.len; i++ )
1266 *p++ = cur->buf.p[i];
1267
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001268 sep = ", ";
1269 sep_len = 2;
1270
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001271 cur = cur->next;
1272 }
1273
1274 *p = '\0';
1275
1276 *size = n;
1277 *buf = p;
1278
1279 return( 0 );
1280}
1281
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001282#define PRINT_ITEM(i) \
1283 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001285 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001286 sep = ", "; \
1287 }
1288
1289#define CERT_TYPE(type,name) \
1290 if( ns_cert_type & type ) \
1291 PRINT_ITEM( name );
1292
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001293static int x509_info_cert_type( char **buf, size_t *size,
1294 unsigned char ns_cert_type )
1295{
1296 int ret;
1297 size_t n = *size;
1298 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001299 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001302 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1304 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1305 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001306 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1307 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1308 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001309
1310 *size = n;
1311 *buf = p;
1312
1313 return( 0 );
1314}
1315
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001316#define KEY_USAGE(code,name) \
1317 if( key_usage & code ) \
1318 PRINT_ITEM( name );
1319
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001320static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001321 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001322{
1323 int ret;
1324 size_t n = *size;
1325 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001326 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1329 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001330 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1331 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1332 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1334 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001335 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1336 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001337
1338 *size = n;
1339 *buf = p;
1340
1341 return( 0 );
1342}
1343
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001344static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001346{
1347 int ret;
1348 const char *desc;
1349 size_t n = *size;
1350 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001352 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001353
1354 while( cur != NULL )
1355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001357 desc = "???";
1358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001360 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001361
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001362 sep = ", ";
1363
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001364 cur = cur->next;
1365 }
1366
1367 *size = n;
1368 *buf = p;
1369
1370 return( 0 );
1371}
1372
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373/*
1374 * Return an informational string about the certificate.
1375 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001376#define BEFORE_COLON 18
1377#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1379 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380{
1381 int ret;
1382 size_t n;
1383 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001384 char key_size_str[BEFORE_COLON];
1385
1386 p = buf;
1387 n = size;
1388
Janos Follath98e28a72016-05-31 14:03:54 +01001389 if( NULL == crt )
1390 {
1391 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
1392 MBEDTLS_X509_SAFE_SNPRINTF;
1393
1394 return( (int) ( size - n ) );
1395 }
1396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001398 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001399 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001401 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001402 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001405 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001408 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001410 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001413 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001415 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001418 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1419 crt->valid_from.year, crt->valid_from.mon,
1420 crt->valid_from.day, crt->valid_from.hour,
1421 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001422 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1426 crt->valid_to.year, crt->valid_to.mon,
1427 crt->valid_to.day, crt->valid_to.hour,
1428 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001429 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001432 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001435 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001436 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001437
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001438 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1440 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441 {
1442 return( ret );
1443 }
1444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001446 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
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 /*
1450 * Optional extensions
1451 */
1452
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001453 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001456 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001457 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001458
1459 if( crt->max_pathlen > 0 )
1460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001462 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001463 }
1464 }
1465
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001466 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001469 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001470
1471 if( ( ret = x509_info_subject_alt_name( &p, &n,
1472 &crt->subject_alt_names ) ) != 0 )
1473 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001474 }
1475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001479 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001480
1481 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1482 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001483 }
1484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001488 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001489
1490 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1491 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001492 }
1493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001497 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001498
1499 if( ( ret = x509_info_ext_key_usage( &p, &n,
1500 &crt->ext_key_usage ) ) != 0 )
1501 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001502 }
1503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001505 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001506
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507 return( (int) ( size - n ) );
1508}
1509
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001510struct x509_crt_verify_string {
1511 int code;
1512 const char *string;
1513};
1514
1515static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001516 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001517 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1518 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1519 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1520 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1521 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001522 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1523 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1524 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001525 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001526 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1527 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1528 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1529 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001530 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1531 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1532 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1533 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1534 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1535 { 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 +01001536 { 0, NULL }
1537};
1538
1539int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001540 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001541{
1542 int ret;
1543 const struct x509_crt_verify_string *cur;
1544 char *p = buf;
1545 size_t n = size;
1546
1547 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1548 {
1549 if( ( flags & cur->code ) == 0 )
1550 continue;
1551
1552 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001553 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001554 flags ^= cur->code;
1555 }
1556
1557 if( flags != 0 )
1558 {
1559 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1560 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001561 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001562 }
1563
1564 return( (int) ( size - n ) );
1565}
1566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001568int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1569 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001570{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001571 unsigned int usage_must, usage_may;
1572 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1573 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1574
1575 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1576 return( 0 );
1577
1578 usage_must = usage & ~may_mask;
1579
1580 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1581 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1582
1583 usage_may = usage & may_mask;
1584
1585 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001587
1588 return( 0 );
1589}
1590#endif
1591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1593int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001594 const char *usage_oid,
1595 size_t usage_len )
1596{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001598
1599 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001601 return( 0 );
1602
1603 /*
1604 * Look for the requested usage (or wildcard ANY) in our list
1605 */
1606 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001609
1610 if( cur_oid->len == usage_len &&
1611 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1612 {
1613 return( 0 );
1614 }
1615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001617 return( 0 );
1618 }
1619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001621}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625/*
1626 * Return 1 if the certificate is revoked, or 0 otherwise.
1627 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001628int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001629{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631
1632 while( cur != NULL && cur->serial.len != 0 )
1633 {
1634 if( crt->serial.len == cur->serial.len &&
1635 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1636 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001637 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638 return( 1 );
1639 }
1640
1641 cur = cur->next;
1642 }
1643
1644 return( 0 );
1645}
1646
1647/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01001648 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02001649 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001652 mbedtls_x509_crl *crl_list,
1653 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001654{
1655 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1657 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001658
1659 if( ca == NULL )
1660 return( flags );
1661
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001662 while( crl_list != NULL )
1663 {
1664 if( crl_list->version == 0 ||
1665 crl_list->issuer_raw.len != ca->subject_raw.len ||
1666 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1667 crl_list->issuer_raw.len ) != 0 )
1668 {
1669 crl_list = crl_list->next;
1670 continue;
1671 }
1672
1673 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001674 * Check if the CA is configured to sign CRLs
1675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1677 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001680 break;
1681 }
1682#endif
1683
1684 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001685 * Check if CRL is correctly signed by the trusted CA
1686 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001687 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1688 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1689
1690 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1691 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02001694 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001695 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02001696 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001698 break;
1699 }
1700
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02001701 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001702 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1705 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001706 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001707 {
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
1712 /*
1713 * Check for validity of CRL (Do not drop out)
1714 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001715 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001717
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001718 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001719 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001720
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721 /*
1722 * Check if certificate is revoked
1723 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001724 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001727 break;
1728 }
1729
1730 crl_list = crl_list->next;
1731 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001732
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001733 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001734}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001737/*
1738 * Like memcmp, but case-insensitive and always returns -1 if different
1739 */
1740static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001741{
1742 size_t i;
1743 unsigned char diff;
1744 const unsigned char *n1 = s1, *n2 = s2;
1745
1746 for( i = 0; i < len; i++ )
1747 {
1748 diff = n1[i] ^ n2[i];
1749
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001750 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001751 continue;
1752
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001753 if( diff == 32 &&
1754 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1755 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1756 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001757 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001758 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001759
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001760 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001761 }
1762
1763 return( 0 );
1764}
1765
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001766/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001767 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001768 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02001769static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001770{
1771 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001772 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001773
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02001774 /* We can't have a match if there is no wildcard to match */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001775 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02001776 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001777
Paul Bakker14b16c62014-05-28 11:33:54 +02001778 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001779 {
1780 if( cn[i] == '.' )
1781 {
1782 cn_idx = i;
1783 break;
1784 }
1785 }
1786
1787 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001788 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001789
Paul Bakker14b16c62014-05-28 11:33:54 +02001790 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001791 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001792 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001793 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001794 }
1795
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001796 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001797}
1798
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001799/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001800 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1801 * variations (but not all).
1802 *
1803 * Return 0 if equal, -1 otherwise.
1804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001806{
1807 if( a->tag == b->tag &&
1808 a->len == b->len &&
1809 memcmp( a->p, b->p, b->len ) == 0 )
1810 {
1811 return( 0 );
1812 }
1813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1815 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001816 a->len == b->len &&
1817 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1818 {
1819 return( 0 );
1820 }
1821
1822 return( -1 );
1823}
1824
1825/*
1826 * Compare two X.509 Names (aka rdnSequence).
1827 *
1828 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1829 * we sometimes return unequal when the full algorithm would return equal,
1830 * but never the other way. (In particular, we don't do Unicode normalisation
1831 * or space folding.)
1832 *
1833 * Return 0 if equal, -1 otherwise.
1834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001836{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001837 /* Avoid recursion, it might not be optimised by the compiler */
1838 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001839 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001840 if( a == NULL || b == NULL )
1841 return( -1 );
1842
1843 /* type */
1844 if( a->oid.tag != b->oid.tag ||
1845 a->oid.len != b->oid.len ||
1846 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1847 {
1848 return( -1 );
1849 }
1850
1851 /* value */
1852 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1853 return( -1 );
1854
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001855 /* structure of the list of sets */
1856 if( a->next_merged != b->next_merged )
1857 return( -1 );
1858
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001859 a = a->next;
1860 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001861 }
1862
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001863 /* a == NULL == b */
1864 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001865}
1866
1867/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001868 * Check the signature of a certificate by its parent
1869 */
1870static int x509_crt_check_signature( const mbedtls_x509_crt *child,
1871 mbedtls_x509_crt *parent )
1872{
1873 const mbedtls_md_info_t *md_info;
1874 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1875
1876 md_info = mbedtls_md_info_from_type( child->sig_md );
1877 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
1878 {
1879 /* Note: this can't happen except after an internal error */
1880 return( -1 );
1881 }
1882
1883 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1884 child->sig_md, hash, mbedtls_md_get_size( md_info ),
1885 child->sig.p, child->sig.len ) != 0 )
1886 {
1887 return( -1 );
1888 }
1889
1890 return( 0 );
1891}
1892
1893/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001894 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1895 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001896 *
1897 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1900 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02001901 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001902{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001903 int need_ca_bit;
1904
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001905 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001906 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001907 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001908
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001909 /* Parent must have the basicConstraints CA bit set as a general rule */
1910 need_ca_bit = 1;
1911
1912 /* Exception: v1/v2 certificates that are locally trusted. */
1913 if( top && parent->version < 3 )
1914 need_ca_bit = 0;
1915
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001916 if( need_ca_bit && ! parent->ca_istrue )
1917 return( -1 );
1918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001920 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001922 {
1923 return( -1 );
1924 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001925#endif
1926
1927 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001928}
1929
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02001930/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001931 * Find a suitable parent for child in candidates, or return NULL.
1932 *
1933 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001934 * 1. subject name matches child's issuer
1935 * 2. if necessary, the CA bit is set and key usage allows signing certs
1936 * 3. for trusted roots, the signature is correct
1937 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001938 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02001939 * If there's a suitable candidate which is also time-valid, return the first
1940 * such. Otherwise, return the first suitable candidate (or NULL if there is
1941 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001942 *
1943 * The rationale for this rule is that someone could have a list of trusted
1944 * roots with two versions on the same root with different validity periods.
1945 * (At least one user reported having such a list and wanted it to just work.)
1946 * The reason we don't just require time-validity is that generally there is
1947 * only one version, and if it's expired we want the flags to state that
1948 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001949 *
1950 * The rationale for rule 3 (signature for trusted roots) is that users might
1951 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02001952 * way we select the correct one is by checking the signature (as we don't
1953 * rely on key identifier extensions). (This is one way users might choose to
1954 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001955 */
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02001956static mbedtls_x509_crt *x509_crt_find_parent_in( mbedtls_x509_crt *child,
1957 mbedtls_x509_crt *candidates,
1958 int top,
1959 int path_cnt,
1960 int self_cnt )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001961{
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001962 mbedtls_x509_crt *parent, *badtime_parent = NULL;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001963
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001964 for( parent = candidates; parent != NULL; parent = parent->next )
1965 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001966 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02001967 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001968 continue;
1969
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02001970 /* +1 because stored max_pathlen is 1 higher that the actual value */
1971 if( parent->max_pathlen > 0 &&
1972 parent->max_pathlen < 1 + path_cnt - self_cnt )
1973 {
1974 continue;
1975 }
1976
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001977 /* Signature */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001978 if( top && x509_crt_check_signature( child, parent ) != 0 )
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001979 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001980 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001981 }
1982
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02001983 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001984 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
1985 mbedtls_x509_time_is_future( &parent->valid_from ) )
1986 {
1987 if( badtime_parent == NULL )
1988 badtime_parent = parent;
1989
1990 continue;
1991 }
1992
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02001993 break;
1994 }
1995
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001996 if( parent == NULL )
1997 parent = badtime_parent;
1998
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02001999 return( parent );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002000}
2001
2002/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002003 * Find a parent in trusted CAs or the provided chain, or return NULL.
2004 *
2005 * Searches in trusted CAs first, and return the first suitable parent found
2006 * (see find_parent_in() for definition of suitable).
2007 */
2008static mbedtls_x509_crt *x509_crt_find_parent( mbedtls_x509_crt *child,
2009 mbedtls_x509_crt *trust_ca,
2010 int *parent_is_trusted,
2011 int path_cnt,
2012 int self_cnt )
2013{
2014 mbedtls_x509_crt *parent;
2015
2016 /* Look for a parent in trusted CAs */
2017 *parent_is_trusted = 1;
2018 parent = x509_crt_find_parent_in( child, trust_ca, 1, path_cnt, self_cnt );
2019
2020 if( parent != NULL )
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002021 return( parent );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002022
2023 /* Look for a parent upwards the chain */
2024 *parent_is_trusted = 0;
2025 return( x509_crt_find_parent_in( child, child->next, 0, path_cnt, self_cnt ) );
2026}
2027
2028/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002029 * Check if an end-entity certificate is locally trusted
2030 *
2031 * Currently we require such certificates to be self-signed (actually only
2032 * check for self-issued as self-signatures are not checked)
2033 */
2034static int x509_crt_check_ee_locally_trusted(
2035 mbedtls_x509_crt *crt,
2036 mbedtls_x509_crt *trust_ca )
2037{
2038 mbedtls_x509_crt *cur;
2039
2040 /* must be self-issued */
2041 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2042 return( -1 );
2043
2044 /* look for an exact match with trusted cert */
2045 for( cur = trust_ca; cur != NULL; cur = cur->next )
2046 {
2047 if( crt->raw.len == cur->raw.len &&
2048 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2049 {
2050 return( 0 );
2051 }
2052 }
2053
2054 /* too bad */
2055 return( -1 );
2056}
2057
2058/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002059 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002060 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002061 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2062 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002063 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002064 * such that every cert in the chain is a child of the next one,
2065 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002066 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002067 * Verify that chain and return it with flags for all issues found.
2068 *
2069 * Special cases:
2070 * - EE == Rj -> return a one-element list containing it
2071 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2072 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002073 *
2074 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002075 * - [in] crt: the cert list EE, C1, ..., Cn
2076 * - [in] trust_ca: the trusted list R1, ..., Rp
2077 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002078 * - [out] ver_chain, chain_len: the built and verified chain
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002079 *
2080 * Return value:
2081 * - non-zero if the chain could not be fully built and examined
2082 * - 0 is the chain was successfully built and examined,
2083 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002084 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002085static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002086 mbedtls_x509_crt *crt,
2087 mbedtls_x509_crt *trust_ca,
2088 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002089 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002090 x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE],
2091 size_t *chain_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002092{
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002093 uint32_t *flags;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002094 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002095 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard6e786742017-07-03 23:47:44 +02002096 int parent_is_trusted = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002097 int child_is_trusted = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002098 int self_cnt = 0;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002099
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002100 child = crt;
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002101 *chain_len = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002102
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002103 while( 1 ) {
2104 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002105 ver_chain[*chain_len].crt = child;
2106 flags = &ver_chain[*chain_len].flags;
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002107 ++*chain_len;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002108
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002109 /* Check time-validity (all certificates) */
2110 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2111 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002112
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002113 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2114 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002115
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002116 /* Stop here for trusted roots (but not for trusted EE certs) */
2117 if( child_is_trusted )
2118 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002119
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002120 /* Check signature algorithm: MD & PK algs */
2121 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2122 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002123
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002124 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2125 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002126
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002127 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002128 if( *chain_len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002129 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2130 {
2131 return( 0 );
2132 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002133
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002134 /* Look for a parent in trusted CAs or up the chain */
2135 parent = x509_crt_find_parent( child, trust_ca, &parent_is_trusted,
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002136 *chain_len - 1, self_cnt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002137
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002138 /* No parent? We're done here */
2139 if( parent == NULL )
2140 {
2141 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2142 return( 0 );
2143 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002144
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002145 /* Count intermediate self-issued (not necessarily self-signed) certs.
2146 * These can occur with some strategies for key rollover, see [SIRO],
2147 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002148 if( *chain_len != 1 &&
2149 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2150 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002151 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002152 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002153
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002154 /* path_cnt is 0 for the first intermediate CA,
2155 * and if parent is trusted it's not an intermediate CA */
2156 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002157 *chain_len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002158 {
2159 /* return immediately to avoid overflow the chain array */
2160 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2161 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002162
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002163 /* if parent is trusted, the signature was checked by find_parent() */
2164 if( ! parent_is_trusted && x509_crt_check_signature( child, parent ) != 0 )
2165 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2166
2167 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002168 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002169 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002172 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002173 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002174#else
2175 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002176#endif
2177
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002178 /* prepare for next iteration */
2179 child = parent;
2180 parent = NULL;
2181 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002182 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002183}
2184
2185/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002186 * Check for CN match
2187 */
2188static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2189 const char *cn, size_t cn_len )
2190{
2191 /* try exact match */
2192 if( name->len == cn_len &&
2193 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2194 {
2195 return( 0 );
2196 }
2197
2198 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002199 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002200 {
2201 return( 0 );
2202 }
2203
2204 return( -1 );
2205}
2206
2207/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002208 * Verify the requested CN - only call this if cn is not NULL!
2209 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002210static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002211 const char *cn,
2212 uint32_t *flags )
2213{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002214 const mbedtls_x509_name *name;
2215 const mbedtls_x509_sequence *cur;
2216 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002217
2218 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
2219 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002220 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002221 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002222 if( x509_crt_check_cn( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002223 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002224 }
2225
2226 if( cur == NULL )
2227 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2228 }
2229 else
2230 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002231 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002232 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002233 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
2234 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002235 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002236 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002237 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002238 }
2239
2240 if( name == NULL )
2241 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2242 }
2243}
2244
2245/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002246 * Merge the flags for all certs in the chain, after calling callback
2247 */
2248static int x509_crt_merge_flags_with_cb(
2249 uint32_t *flags,
2250 x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE],
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002251 size_t chain_len,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002252 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2253 void *p_vrfy )
2254{
2255 int ret;
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002256 size_t i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002257 uint32_t cur_flags;
2258
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002259 for( i = chain_len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002260 {
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002261 cur_flags = ver_chain[i-1].flags;
2262
2263 if( NULL != f_vrfy )
2264 if( ( ret = f_vrfy( p_vrfy, ver_chain[i-1].crt, i-1, &cur_flags ) ) != 0 )
2265 return( ret );
2266
2267 *flags |= cur_flags;
2268 }
2269
2270 return( 0 );
2271}
2272
2273/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002274 * Verify the certificate validity
2275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2277 mbedtls_x509_crt *trust_ca,
2278 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002279 const char *cn, uint32_t *flags,
2280 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002281 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002282{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002283 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002284 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002285}
2286
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002287/*
2288 * Verify the certificate validity, with profile
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002289 *
Manuel Pégourié-Gonnard66a36b02017-07-12 12:23:06 +02002290 * This function:
2291 * - checks the requested CN (if any)
2292 * - checks the type and size of the EE cert's key,
2293 * as that isn't done as part of chain building/verification currently
2294 * - builds and verifies the chain
2295 * - then calls the callback and merges the flags
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002296 */
2297int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2298 mbedtls_x509_crt *trust_ca,
2299 mbedtls_x509_crl *ca_crl,
2300 const mbedtls_x509_crt_profile *profile,
2301 const char *cn, uint32_t *flags,
2302 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2303 void *p_vrfy )
2304{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002305 int ret;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002306 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002307 x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE];
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002308 size_t chain_len;
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002309 uint32_t *ee_flags = &ver_chain[0].flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002310
2311 *flags = 0;
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002312 memset( ver_chain, 0, sizeof( ver_chain ) );
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002313 chain_len = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002314
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002315 if( profile == NULL )
2316 {
2317 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2318 goto exit;
2319 }
2320
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002321 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002322 if( cn != NULL )
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002323 x509_crt_verify_name( crt, cn, ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002324
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002325 /* Check the type and size of the key */
2326 pk_type = mbedtls_pk_get_type( &crt->pk );
2327
2328 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002329 *ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002330
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002331 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002332 *ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002333
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002334 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002335 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
2336 ver_chain, &chain_len );
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002337 if( ret != 0 )
2338 goto exit;
2339
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002340 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002341 ret = x509_crt_merge_flags_with_cb( flags,
2342 ver_chain, chain_len, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002343
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002344exit:
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02002345 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2346 * the SSL module for authmode optional, but non-zero return from the
2347 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002348 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2349 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2350
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002351 if( ret != 0 )
2352 {
2353 *flags = (uint32_t) -1;
2354 return( ret );
2355 }
2356
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002357 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002359
2360 return( 0 );
2361}
2362
2363/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002364 * Initialize a certificate chain
2365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002369}
2370
2371/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002372 * Unallocate all certificate data
2373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002375{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 mbedtls_x509_crt *cert_cur = crt;
2377 mbedtls_x509_crt *cert_prv;
2378 mbedtls_x509_name *name_cur;
2379 mbedtls_x509_name *name_prv;
2380 mbedtls_x509_sequence *seq_cur;
2381 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002382
2383 if( crt == NULL )
2384 return;
2385
2386 do
2387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2391 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002392#endif
2393
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002394 name_cur = cert_cur->issuer.next;
2395 while( name_cur != NULL )
2396 {
2397 name_prv = name_cur;
2398 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2400 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002401 }
2402
2403 name_cur = cert_cur->subject.next;
2404 while( name_cur != NULL )
2405 {
2406 name_prv = name_cur;
2407 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2409 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002410 }
2411
2412 seq_cur = cert_cur->ext_key_usage.next;
2413 while( seq_cur != NULL )
2414 {
2415 seq_prv = seq_cur;
2416 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2418 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002419 }
2420
2421 seq_cur = cert_cur->subject_alt_names.next;
2422 while( seq_cur != NULL )
2423 {
2424 seq_prv = seq_cur;
2425 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2427 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002428 }
2429
2430 if( cert_cur->raw.p != NULL )
2431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2433 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002434 }
2435
2436 cert_cur = cert_cur->next;
2437 }
2438 while( cert_cur != NULL );
2439
2440 cert_cur = crt;
2441 do
2442 {
2443 cert_prv = cert_cur;
2444 cert_cur = cert_cur->next;
2445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002447 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002449 }
2450 while( cert_cur != NULL );
2451}
2452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453#endif /* MBEDTLS_X509_CRT_PARSE_C */