blob: a828a8618dc6dd252090086622c0421a2794adb9 [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
30 */
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/x509_crt.h"
41#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000042
43#include <stdio.h>
44#include <string.h>
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020048#endif
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052#else
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020055#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010061#endif
62
Paul Bakkerfa6a6202013-10-28 18:48:30 +010063#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064#include <windows.h>
65#else
66#include <time.h>
67#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000070#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020071#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#include <sys/types.h>
73#include <sys/stat.h>
74#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000075#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#endif
77
Paul Bakker34617722014-06-13 17:20:13 +020078/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020080 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
81}
82
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020084 * Default profile
85 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020086const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
87{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020088 /* Hashes from SHA-1 and above */
89 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
90 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
91 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
92 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
93 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
94 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
95 0xFFFFFFF, /* Any PK alg */
96 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020097 2048,
98};
99
100/*
101 * Next-default profile
102 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200103const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
104{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200105 /* Hashes from SHA-256 and above */
106 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
107 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
108 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
109 0xFFFFFFF, /* Any PK alg */
110#if defined(MBEDTLS_ECP_C)
111 /* Curves at or above 128-bit security level */
112 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
113 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
119#else
120 0,
121#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200122 2048,
123};
124
125/*
126 * NSA Suite B Profile
127 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200128const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
129{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200130 /* Only SHA-256 and 384 */
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
133 /* Only ECDSA */
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ),
135#if defined(MBEDTLS_ECP_C)
136 /* Only NIST P-256 and P-384 */
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
138 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
139#else
140 0,
141#endif
142 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200143};
144
145/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200146 * Check md_alg against profile
147 * Return 0 if md_alg acceptable for this profile, -1 otherwise
148 */
149static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
150 mbedtls_md_type_t md_alg )
151{
152 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
153 return( 0 );
154
155 return( -1 );
156}
157
158/*
159 * Check pk_alg against profile
160 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
161 */
162static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
163 mbedtls_pk_type_t pk_alg )
164{
165 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
166 return( 0 );
167
168 return( -1 );
169}
170
171/*
172 * Check key against profile
173 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
174 */
175static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
176 mbedtls_pk_type_t pk_alg,
177 const mbedtls_pk_context *pk )
178{
179#if defined(MBEDTLS_RSA_C)
180 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
181 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200182 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200183 return( 0 );
184
185 return( -1 );
186 }
187#endif
188
189#if defined(MBEDTLS_ECDSA_C)
190 if( pk_alg == MBEDTLS_PK_ECDSA )
191 {
192 mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
193
194 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
195 return( 0 );
196
197 return( -1 );
198 }
199#endif
200
201 return( -1 );
202}
203
204/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200205 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
206 */
207static int x509_get_version( unsigned char **p,
208 const unsigned char *end,
209 int *ver )
210{
211 int ret;
212 size_t len;
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
215 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200218 {
219 *ver = 0;
220 return( 0 );
221 }
222
223 return( ret );
224 }
225
226 end = *p + len;
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
229 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200230
231 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 return( MBEDTLS_ERR_X509_INVALID_VERSION +
233 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200234
235 return( 0 );
236}
237
238/*
239 * Validity ::= SEQUENCE {
240 * notBefore Time,
241 * notAfter Time }
242 */
243static int x509_get_dates( unsigned char **p,
244 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_x509_time *from,
246 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247{
248 int ret;
249 size_t len;
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
252 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
253 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254
255 end = *p + len;
256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200258 return( ret );
259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261 return( ret );
262
263 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 return( MBEDTLS_ERR_X509_INVALID_DATE +
265 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200266
267 return( 0 );
268}
269
270/*
271 * X.509 v2/v3 unique identifier (not parsed)
272 */
273static int x509_get_uid( unsigned char **p,
274 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200276{
277 int ret;
278
279 if( *p == end )
280 return( 0 );
281
282 uid->tag = **p;
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
285 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 return( 0 );
289
290 return( ret );
291 }
292
293 uid->p = *p;
294 *p += uid->len;
295
296 return( 0 );
297}
298
299static int x509_get_basic_constraints( unsigned char **p,
300 const unsigned char *end,
301 int *ca_istrue,
302 int *max_pathlen )
303{
304 int ret;
305 size_t len;
306
307 /*
308 * BasicConstraints ::= SEQUENCE {
309 * cA BOOLEAN DEFAULT FALSE,
310 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
311 */
312 *ca_istrue = 0; /* DEFAULT FALSE */
313 *max_pathlen = 0; /* endless */
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
316 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
317 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200318
319 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200320 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
325 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200326
327 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329
330 if( *ca_istrue != 0 )
331 *ca_istrue = 1;
332 }
333
334 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200335 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
338 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339
340 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
342 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343
344 (*max_pathlen)++;
345
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200346 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347}
348
349static int x509_get_ns_cert_type( unsigned char **p,
350 const unsigned char *end,
351 unsigned char *ns_cert_type)
352{
353 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
357 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358
359 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
361 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362
363 /* Get actual bitstring */
364 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200365 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366}
367
368static int x509_get_key_usage( unsigned char **p,
369 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100370 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200371{
372 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200373 size_t i;
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 */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200384 *key_usage = 0;
385 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
386 {
387 *key_usage |= (unsigned int) bs.p[i] << (8*i);
388 }
389
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200390 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391}
392
393/*
394 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
395 *
396 * KeyPurposeId ::= OBJECT IDENTIFIER
397 */
398static int x509_get_ext_key_usage( unsigned char **p,
399 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401{
402 int ret;
403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
405 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406
407 /* Sequence length must be >= 1 */
408 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
410 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200412 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413}
414
415/*
416 * SubjectAltName ::= GeneralNames
417 *
418 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
419 *
420 * GeneralName ::= CHOICE {
421 * otherName [0] OtherName,
422 * rfc822Name [1] IA5String,
423 * dNSName [2] IA5String,
424 * x400Address [3] ORAddress,
425 * directoryName [4] Name,
426 * ediPartyName [5] EDIPartyName,
427 * uniformResourceIdentifier [6] IA5String,
428 * iPAddress [7] OCTET STRING,
429 * registeredID [8] OBJECT IDENTIFIER }
430 *
431 * OtherName ::= SEQUENCE {
432 * type-id OBJECT IDENTIFIER,
433 * value [0] EXPLICIT ANY DEFINED BY type-id }
434 *
435 * EDIPartyName ::= SEQUENCE {
436 * nameAssigner [0] DirectoryString OPTIONAL,
437 * partyName [1] DirectoryString }
438 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000439 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440 */
441static int x509_get_subject_alt_name( unsigned char **p,
442 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444{
445 int ret;
446 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450
451 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
453 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
454 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455
456 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
458 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459
460 while( *p < end )
461 {
462 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
464 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200465
466 tag = **p;
467 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
469 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 if( ( tag & MBEDTLS_ASN1_CONTEXT_SPECIFIC ) != MBEDTLS_ASN1_CONTEXT_SPECIFIC )
472 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
473 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200475 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477 {
478 *p += tag_len;
479 continue;
480 }
481
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200483 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100485 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100487
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200488 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489
490 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnarda0d3a262014-04-11 15:31:02 +0200492 MBEDTLS_ERR_ASN1_ALLOC_FAILED ); // LCOV_EXCL_LINE
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494 cur = cur->next;
495 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200496
497 buf = &(cur->buf);
498 buf->tag = tag;
499 buf->p = *p;
500 buf->len = tag_len;
501 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502 }
503
504 /* Set final sequence entry's next pointer to NULL */
505 cur->next = NULL;
506
507 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
509 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510
511 return( 0 );
512}
513
514/*
515 * X.509 v3 extensions
516 *
517 * TODO: Perform all of the basic constraints tests required by the RFC
518 * TODO: Set values for undetected extensions to a sane default?
519 *
520 */
521static int x509_get_crt_ext( unsigned char **p,
522 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524{
525 int ret;
526 size_t len;
527 unsigned char *end_ext_data, *end_ext_octet;
528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532 return( 0 );
533
534 return( ret );
535 }
536
537 while( *p < end )
538 {
539 /*
540 * Extension ::= SEQUENCE {
541 * extnID OBJECT IDENTIFIER,
542 * critical BOOLEAN DEFAULT FALSE,
543 * extnValue OCTET STRING }
544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546 int is_critical = 0; /* DEFAULT FALSE */
547 int ext_type = 0;
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
550 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
551 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552
553 end_ext_data = *p + len;
554
555 /* Get extension ID */
556 extn_oid.tag = **p;
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
559 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560
561 extn_oid.p = *p;
562 *p += extn_oid.len;
563
564 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
566 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
568 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
570 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
571 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572
573 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
575 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
576 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577
578 end_ext_octet = *p + len;
579
580 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
582 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583
584 /*
585 * Detect supported extensions
586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588
589 if( ret != 0 )
590 {
591 /* No parser found, skip extension */
592 *p = end_ext_octet;
593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595 if( is_critical )
596 {
597 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
599 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200600 }
601#endif
602 continue;
603 }
604
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100605 /* Forbid repeated extensions */
606 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100608
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200609 crt->ext_types |= ext_type;
610
611 switch( ext_type )
612 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100613 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614 /* Parse basic constraints */
615 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
616 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200617 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618 break;
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621 /* Parse key usage */
622 if( ( ret = x509_get_key_usage( p, end_ext_octet,
623 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200624 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625 break;
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628 /* Parse extended key usage */
629 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
630 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200631 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632 break;
633
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100634 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 /* Parse subject alt name */
636 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
637 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200638 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639 break;
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 /* Parse netscape certificate type */
643 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
644 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200645 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646 break;
647
648 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650 }
651 }
652
653 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
655 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656
657 return( 0 );
658}
659
660/*
661 * Parse and fill a single X.509 certificate in DER format
662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200664 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665{
666 int ret;
667 size_t len;
668 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
672 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
673 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674
675 /*
676 * Check for valid input
677 */
678 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200680
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200681 p = mbedtls_calloc( 1, len = buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682 if( p == NULL )
Manuel Pégourié-Gonnarda0d3a262014-04-11 15:31:02 +0200683 return( MBEDTLS_ERR_X509_ALLOC_FAILED ); // LCOV_EXCL_LINE
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684
685 memcpy( p, buf, buflen );
686
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687 crt->raw.p = p;
688 crt->raw.len = len;
689 end = p + len;
690
691 /*
692 * Certificate ::= SEQUENCE {
693 * tbsCertificate TBSCertificate,
694 * signatureAlgorithm AlgorithmIdentifier,
695 * signatureValue BIT STRING }
696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
698 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_x509_crt_free( crt );
701 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702 }
703
704 if( len > (size_t) ( end - p ) )
705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_x509_crt_free( crt );
707 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
708 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709 }
710 crt_end = p + len;
711
712 /*
713 * TBSCertificate ::= SEQUENCE {
714 */
715 crt->tbs.p = p;
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
718 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_x509_crt_free( crt );
721 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722 }
723
724 end = p + len;
725 crt->tbs.len = end - crt->tbs.p;
726
727 /*
728 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
729 *
730 * CertificateSerialNumber ::= INTEGER
731 *
732 * signature AlgorithmIdentifier
733 */
734 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
736 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200737 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740 return( ret );
741 }
742
743 crt->version++;
744
745 if( crt->version > 3 )
746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_x509_crt_free( crt );
748 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200749 }
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200752 &crt->sig_md, &crt->sig_pk,
753 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200756 return( ret );
757 }
758
759 /*
760 * issuer Name
761 */
762 crt->issuer_raw.p = p;
763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
765 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_x509_crt_free( crt );
768 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 }
770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774 return( ret );
775 }
776
777 crt->issuer_raw.len = p - crt->issuer_raw.p;
778
779 /*
780 * Validity ::= SEQUENCE {
781 * notBefore Time,
782 * notAfter Time }
783 *
784 */
785 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
786 &crt->valid_to ) ) != 0 )
787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789 return( ret );
790 }
791
792 /*
793 * subject Name
794 */
795 crt->subject_raw.p = p;
796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
798 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_x509_crt_free( crt );
801 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200802 }
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807 return( ret );
808 }
809
810 crt->subject_raw.len = p - crt->subject_raw.p;
811
812 /*
813 * SubjectPublicKeyInfo
814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818 return( ret );
819 }
820
821 /*
822 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
823 * -- If present, version shall be v2 or v3
824 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
825 * -- If present, version shall be v2 or v3
826 * extensions [3] EXPLICIT Extensions OPTIONAL
827 * -- If present, version shall be v3
828 */
829 if( crt->version == 2 || crt->version == 3 )
830 {
831 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
832 if( ret != 0 )
833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200835 return( ret );
836 }
837 }
838
839 if( crt->version == 2 || crt->version == 3 )
840 {
841 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
842 if( ret != 0 )
843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845 return( ret );
846 }
847 }
848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200850 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200851#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100852 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200853 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200854 if( ret != 0 )
855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200857 return( ret );
858 }
859 }
860
861 if( p != end )
862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 mbedtls_x509_crt_free( crt );
864 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
865 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866 }
867
868 end = crt_end;
869
870 /*
871 * }
872 * -- end of TBSCertificate
873 *
874 * signatureAlgorithm AlgorithmIdentifier,
875 * signatureValue BIT STRING
876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 return( ret );
881 }
882
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100883 if( crt->sig_oid.len != sig_oid2.len ||
884 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200885 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200886 ( sig_params1.len != 0 &&
887 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_x509_crt_free( crt );
890 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200891 }
892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896 return( ret );
897 }
898
899 if( p != end )
900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_x509_crt_free( crt );
902 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
903 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 }
905
906 return( 0 );
907}
908
909/*
910 * Parse one X.509 certificate in DER format from a buffer and add them to a
911 * chained list
912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200914 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915{
916 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918
919 /*
920 * Check for valid input
921 */
922 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924
925 while( crt->version != 0 && crt->next != NULL )
926 {
927 prev = crt;
928 crt = crt->next;
929 }
930
931 /*
932 * Add new certificate on the end of the chain if needed.
933 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200934 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200936 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937
938 if( crt->next == NULL )
Manuel Pégourié-Gonnarda0d3a262014-04-11 15:31:02 +0200939 return( MBEDTLS_ERR_X509_ALLOC_FAILED ); // LCOV_EXCL_LINE
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940
941 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944 }
945
Paul Bakkerddf26b42013-09-18 13:46:23 +0200946 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947 {
948 if( prev )
949 prev->next = NULL;
950
951 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953
954 return( ret );
955 }
956
957 return( 0 );
958}
959
960/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200961 * Parse one or more PEM certificates from a buffer and add them to the chained
962 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965{
966 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 int buf_format = MBEDTLS_X509_FORMAT_DER;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968
969 /*
970 * Check for valid input
971 */
972 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974
975 /*
976 * Determine buffer content. Buffer contains either one DER certificate or
977 * one or more PEM certificates.
978 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +0200980 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200981 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200984 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985#endif
986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 if( buf_format == MBEDTLS_X509_FORMAT_DER )
988 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#if defined(MBEDTLS_PEM_PARSE_C)
991 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 {
993 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200996 /* 1 rather than 0 since the terminating NULL byte is counted in */
997 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998 {
999 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001002 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004 "-----BEGIN CERTIFICATE-----",
1005 "-----END CERTIFICATE-----",
1006 buf, NULL, 0, &use_len );
1007
1008 if( ret == 0 )
1009 {
1010 /*
1011 * Was PEM encoded
1012 */
1013 buflen -= use_len;
1014 buf += use_len;
1015 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017 {
1018 return( ret );
1019 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023
1024 /*
1025 * PEM header and footer were found
1026 */
1027 buflen -= use_len;
1028 buf += use_len;
1029
1030 if( first_error == 0 )
1031 first_error = ret;
1032
Paul Bakker5a5fa922014-09-26 14:53:04 +02001033 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 continue;
1035 }
1036 else
1037 break;
1038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042
1043 if( ret != 0 )
1044 {
1045 /*
1046 * Quit parsing on a memory error
1047 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001048 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Manuel Pégourié-Gonnarda0d3a262014-04-11 15:31:02 +02001049 return( ret ); // LCOV_EXCL_LINE
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050
1051 if( first_error == 0 )
1052 first_error = ret;
1053
1054 total_failed++;
1055 continue;
1056 }
1057
1058 success = 1;
1059 }
1060 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062
1063 if( success )
1064 return( total_failed );
1065 else if( first_error )
1066 return( first_error );
1067 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001069}
1070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072/*
1073 * Load one or more certificates and add them to the chained list
1074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076{
1077 int ret;
1078 size_t n;
1079 unsigned char *buf;
1080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001082 return( ret );
1083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001086 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088
1089 return( ret );
1090}
1091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093{
1094 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001095#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096 int w_ret;
1097 WCHAR szDir[MAX_PATH];
1098 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001099 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001100 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101
Paul Bakker9af723c2014-05-01 13:03:14 +02001102 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103 HANDLE hFind;
1104
1105 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001107
Paul Bakker9af723c2014-05-01 13:03:14 +02001108 memset( szDir, 0, sizeof(szDir) );
1109 memset( filename, 0, MAX_PATH );
1110 memcpy( filename, path, len );
1111 filename[len++] = '\\';
1112 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113 filename[len++] = '*';
1114
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001115 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
1116 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001117 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119
1120 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001121 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123
1124 len = MAX_PATH - len;
1125 do
1126 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001127 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128
1129 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1130 continue;
1131
Paul Bakker9af723c2014-05-01 13:03:14 +02001132 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001133 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001134 p, len - 1,
1135 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001136 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140 if( w_ret < 0 )
1141 ret++;
1142 else
1143 ret += w_ret;
1144 }
1145 while( FindNextFileW( hFind, &file_data ) != 0 );
1146
Paul Bakker66d5d072014-06-17 16:39:18 +02001147 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001151#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001152 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001153 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001154 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155 char entry_name[255];
1156 DIR *dir = opendir( path );
1157
Paul Bakker66d5d072014-06-17 16:39:18 +02001158 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001162 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001163 {
1164 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001165 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001166 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001167#endif
1168
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001169 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 mbedtls_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001173 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 {
1175 closedir( dir );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001177 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001178 }
1179
1180 if( !S_ISREG( sb.st_mode ) )
1181 continue;
1182
1183 // Ignore parse errors
1184 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186 if( t_ret < 0 )
1187 ret++;
1188 else
1189 ret += t_ret;
1190 }
1191 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001192
1193cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001195 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001197#endif
1198
Paul Bakkerbe089b02013-10-14 15:51:50 +02001199#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001200
1201 return( ret );
1202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001204
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001205static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001207{
1208 size_t i;
1209 size_t n = *size;
1210 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001212 const char *sep = "";
1213 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001214
1215 while( cur != NULL )
1216 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001217 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001218 {
1219 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001220 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001221 }
1222
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001223 n -= cur->buf.len + sep_len;
1224 for( i = 0; i < sep_len; i++ )
1225 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001226 for( i = 0; i < cur->buf.len; i++ )
1227 *p++ = cur->buf.p[i];
1228
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001229 sep = ", ";
1230 sep_len = 2;
1231
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001232 cur = cur->next;
1233 }
1234
1235 *p = '\0';
1236
1237 *size = n;
1238 *buf = p;
1239
1240 return( 0 );
1241}
1242
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001243#define PRINT_ITEM(i) \
1244 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001246 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001247 sep = ", "; \
1248 }
1249
1250#define CERT_TYPE(type,name) \
1251 if( ns_cert_type & type ) \
1252 PRINT_ITEM( name );
1253
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001254static int x509_info_cert_type( char **buf, size_t *size,
1255 unsigned char ns_cert_type )
1256{
1257 int ret;
1258 size_t n = *size;
1259 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001260 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001263 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1265 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1266 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001267 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1268 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1269 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001270
1271 *size = n;
1272 *buf = p;
1273
1274 return( 0 );
1275}
1276
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001277#define KEY_USAGE(code,name) \
1278 if( key_usage & code ) \
1279 PRINT_ITEM( name );
1280
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001281static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001282 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001283{
1284 int ret;
1285 size_t n = *size;
1286 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001287 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1290 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001291 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1292 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1293 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1295 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001296 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1297 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001298
1299 *size = n;
1300 *buf = p;
1301
1302 return( 0 );
1303}
1304
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001305static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001307{
1308 int ret;
1309 const char *desc;
1310 size_t n = *size;
1311 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001313 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001314
1315 while( cur != NULL )
1316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001318 desc = "???";
1319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001321 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001322
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001323 sep = ", ";
1324
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001325 cur = cur->next;
1326 }
1327
1328 *size = n;
1329 *buf = p;
1330
1331 return( 0 );
1332}
1333
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001334/*
1335 * Return an informational string about the certificate.
1336 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001337#define BEFORE_COLON 18
1338#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1340 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001341{
1342 int ret;
1343 size_t n;
1344 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001345 char key_size_str[BEFORE_COLON];
1346
1347 p = buf;
1348 n = size;
1349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001351 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001352 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001354 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001355 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001358 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001361 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001363 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001366 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001368 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001371 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1372 crt->valid_from.year, crt->valid_from.mon,
1373 crt->valid_from.day, crt->valid_from.hour,
1374 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001375 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1379 crt->valid_to.year, crt->valid_to.mon,
1380 crt->valid_to.day, crt->valid_to.hour,
1381 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001382 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001385 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001388 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001389 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001390
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001391 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1393 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001394 {
1395 return( ret );
1396 }
1397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001399 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001400 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001401
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001402 /*
1403 * Optional extensions
1404 */
1405
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001406 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001409 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001410 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001411
1412 if( crt->max_pathlen > 0 )
1413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001415 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001416 }
1417 }
1418
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001419 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001422 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001423
1424 if( ( ret = x509_info_subject_alt_name( &p, &n,
1425 &crt->subject_alt_names ) ) != 0 )
1426 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001427 }
1428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001432 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001433
1434 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1435 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001436 }
1437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001441 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001442
1443 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1444 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001445 }
1446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001450 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001451
1452 if( ( ret = x509_info_ext_key_usage( &p, &n,
1453 &crt->ext_key_usage ) ) != 0 )
1454 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001455 }
1456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001458 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001459
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460 return( (int) ( size - n ) );
1461}
1462
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001463struct x509_crt_verify_string {
1464 int code;
1465 const char *string;
1466};
1467
1468static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001469 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001470 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1471 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1472 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1473 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1474 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001475 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1476 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1477 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001478 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001479 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1480 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1481 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1482 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001483 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1484 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1485 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1486 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1487 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1488 { 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 +01001489 { 0, NULL }
1490};
1491
1492int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001493 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001494{
1495 int ret;
1496 const struct x509_crt_verify_string *cur;
1497 char *p = buf;
1498 size_t n = size;
1499
1500 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1501 {
1502 if( ( flags & cur->code ) == 0 )
1503 continue;
1504
1505 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001506 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001507 flags ^= cur->code;
1508 }
1509
1510 if( flags != 0 )
1511 {
1512 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1513 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001514 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001515 }
1516
1517 return( (int) ( size - n ) );
1518}
1519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001521int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1522 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001523{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001524 unsigned int usage_must, usage_may;
1525 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1526 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1527
1528 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1529 return( 0 );
1530
1531 usage_must = usage & ~may_mask;
1532
1533 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1534 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1535
1536 usage_may = usage & may_mask;
1537
1538 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001540
1541 return( 0 );
1542}
1543#endif
1544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1546int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001547 const char *usage_oid,
1548 size_t usage_len )
1549{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001551
1552 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001554 return( 0 );
1555
1556 /*
1557 * Look for the requested usage (or wildcard ANY) in our list
1558 */
1559 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001562
1563 if( cur_oid->len == usage_len &&
1564 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1565 {
1566 return( 0 );
1567 }
1568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001570 return( 0 );
1571 }
1572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578/*
1579 * Return 1 if the certificate is revoked, or 0 otherwise.
1580 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001581int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001584
1585 while( cur != NULL && cur->serial.len != 0 )
1586 {
1587 if( crt->serial.len == cur->serial.len &&
1588 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1589 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001590 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591 return( 1 );
1592 }
1593
1594 cur = cur->next;
1595 }
1596
1597 return( 0 );
1598}
1599
1600/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001601 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001604 mbedtls_x509_crl *crl_list,
1605 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001606{
1607 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1609 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001610
1611 if( ca == NULL )
1612 return( flags );
1613
1614 /*
1615 * TODO: What happens if no CRL is present?
1616 * Suggestion: Revocation state should be unknown if no CRL is present.
1617 * For backwards compatibility this is not yet implemented.
1618 */
1619
1620 while( crl_list != NULL )
1621 {
1622 if( crl_list->version == 0 ||
1623 crl_list->issuer_raw.len != ca->subject_raw.len ||
1624 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1625 crl_list->issuer_raw.len ) != 0 )
1626 {
1627 crl_list = crl_list->next;
1628 continue;
1629 }
1630
1631 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001632 * Check if the CA is configured to sign CRLs
1633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1635 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001638 break;
1639 }
1640#endif
1641
1642 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001643 * Check if CRL is correctly signed by the trusted CA
1644 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001645 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1646 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1647
1648 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1649 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001652 if( md_info == NULL )
1653 {
1654 /*
1655 * Cannot check 'unknown' hash
1656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001658 break;
1659 }
1660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001662
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001663 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1664 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1667 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001668 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671 break;
1672 }
1673
1674 /*
1675 * Check for validity of CRL (Do not drop out)
1676 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001677 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001680 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001681 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001682
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001683 /*
1684 * Check if certificate is revoked
1685 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001686 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001689 break;
1690 }
1691
1692 crl_list = crl_list->next;
1693 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001694
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001695 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001696}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001698
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001699/*
1700 * Like memcmp, but case-insensitive and always returns -1 if different
1701 */
1702static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001703{
1704 size_t i;
1705 unsigned char diff;
1706 const unsigned char *n1 = s1, *n2 = s2;
1707
1708 for( i = 0; i < len; i++ )
1709 {
1710 diff = n1[i] ^ n2[i];
1711
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001712 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001713 continue;
1714
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001715 if( diff == 32 &&
1716 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1717 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1718 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001719 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001720 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001722 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001723 }
1724
1725 return( 0 );
1726}
1727
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001728/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001729 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001730 */
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001731static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001732{
1733 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001734 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001735
1736 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1737 return( 0 );
1738
Paul Bakker14b16c62014-05-28 11:33:54 +02001739 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001740 {
1741 if( cn[i] == '.' )
1742 {
1743 cn_idx = i;
1744 break;
1745 }
1746 }
1747
1748 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001749 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001750
Paul Bakker14b16c62014-05-28 11:33:54 +02001751 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001752 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001753 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001754 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001755 }
1756
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001757 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001758}
1759
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001760/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001761 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1762 * variations (but not all).
1763 *
1764 * Return 0 if equal, -1 otherwise.
1765 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001767{
1768 if( a->tag == b->tag &&
1769 a->len == b->len &&
1770 memcmp( a->p, b->p, b->len ) == 0 )
1771 {
1772 return( 0 );
1773 }
1774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1776 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001777 a->len == b->len &&
1778 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1779 {
1780 return( 0 );
1781 }
1782
1783 return( -1 );
1784}
1785
1786/*
1787 * Compare two X.509 Names (aka rdnSequence).
1788 *
1789 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1790 * we sometimes return unequal when the full algorithm would return equal,
1791 * but never the other way. (In particular, we don't do Unicode normalisation
1792 * or space folding.)
1793 *
1794 * Return 0 if equal, -1 otherwise.
1795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001797{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001798 /* Avoid recursion, it might not be optimised by the compiler */
1799 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001800 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001801 if( a == NULL || b == NULL )
1802 return( -1 );
1803
1804 /* type */
1805 if( a->oid.tag != b->oid.tag ||
1806 a->oid.len != b->oid.len ||
1807 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1808 {
1809 return( -1 );
1810 }
1811
1812 /* value */
1813 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1814 return( -1 );
1815
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001816 /* structure of the list of sets */
1817 if( a->next_merged != b->next_merged )
1818 return( -1 );
1819
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001820 a = a->next;
1821 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001822 }
1823
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001824 /* a == NULL == b */
1825 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001826}
1827
1828/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001829 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1830 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001831 *
1832 * top means parent is a locally-trusted certificate
1833 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1836 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001837 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001838{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001839 int need_ca_bit;
1840
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001841 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001842 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001843 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001844
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001845 /* Parent must have the basicConstraints CA bit set as a general rule */
1846 need_ca_bit = 1;
1847
1848 /* Exception: v1/v2 certificates that are locally trusted. */
1849 if( top && parent->version < 3 )
1850 need_ca_bit = 0;
1851
1852 /* Exception: self-signed end-entity certs that are locally trusted. */
1853 if( top && bottom &&
1854 child->raw.len == parent->raw.len &&
1855 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1856 {
1857 need_ca_bit = 0;
1858 }
1859
1860 if( need_ca_bit && ! parent->ca_istrue )
1861 return( -1 );
1862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001864 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001866 {
1867 return( -1 );
1868 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001869#endif
1870
1871 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001872}
1873
Paul Bakkerddf26b42013-09-18 13:46:23 +02001874static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001876 mbedtls_x509_crl *ca_crl,
1877 const mbedtls_x509_crt_profile *profile,
1878 int path_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001879 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001880 void *p_vrfy )
1881{
1882 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001883 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001884 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1886 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001887
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001888 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001889 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001890
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001891 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001893
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001894 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
1895 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
1896
1897 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
1898 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
1899
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001900 /*
1901 * Child is the top of the chain. Check against the trust_ca list.
1902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001906 if( md_info == NULL )
1907 {
1908 /*
1909 * Cannot check 'unknown', no need to try any CA
1910 */
1911 trust_ca = NULL;
1912 }
1913 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001915
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001916 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001917 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001918 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001919 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001920
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001921 check_path_cnt = path_cnt + 1;
1922
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001923 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001924 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001925 * the same as the trusted CA
1926 */
1927 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1928 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1929 child->issuer_raw.len ) == 0 )
1930 {
1931 check_path_cnt--;
1932 }
1933
1934 if( trust_ca->max_pathlen > 0 &&
1935 trust_ca->max_pathlen < check_path_cnt )
1936 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001937 continue;
1938 }
1939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1941 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001942 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001943 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001944 continue;
1945 }
1946
1947 /*
1948 * Top of chain is signed by a trusted CA
1949 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardfa67eba2015-06-27 14:41:38 +02001951
1952 if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
1953 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
1954
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001955 break;
1956 }
1957
1958 /*
1959 * If top of chain is not the same as the trusted CA send a verify request
1960 * to the callback for any issues with validity and CRL presence for the
1961 * trusted CA certificate.
1962 */
1963 if( trust_ca != NULL &&
1964 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1965 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1966 child->issuer_raw.len ) != 0 ) )
1967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001969 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001970 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001971#else
1972 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001973#endif
1974
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001975 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001976 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001977
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001978 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001980
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001981 if( NULL != f_vrfy )
1982 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001983 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1984 &ca_flags ) ) != 0 )
1985 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001986 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001987 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001988 }
1989 }
1990
1991 /* Call callback on top cert */
1992 if( NULL != f_vrfy )
1993 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001994 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001995 return( ret );
1996 }
1997
1998 *flags |= ca_flags;
1999
2000 return( 0 );
2001}
2002
Paul Bakkerddf26b42013-09-18 13:46:23 +02002003static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002004 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
2005 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
2006 const mbedtls_x509_crt_profile *profile,
2007 int path_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002008 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002009 void *p_vrfy )
2010{
2011 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002012 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2014 mbedtls_x509_crt *grandparent;
2015 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002016
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002017 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2021 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002022 }
2023
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002024 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002025 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02002026
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002027 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002029
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002030 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2031 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2032
2033 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2034 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002037 if( md_info == NULL )
2038 {
2039 /*
2040 * Cannot check 'unknown' hash
2041 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002043 }
2044 else
2045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002047
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002048 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2049 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2052 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002053 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002056 }
2057 }
2058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002060 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002061 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002062#endif
2063
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002064 /* Look for a grandparent in trusted CAs */
2065 for( grandparent = trust_ca;
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002066 grandparent != NULL;
2067 grandparent = grandparent->next )
2068 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002069 if( x509_crt_check_parent( parent, grandparent,
2070 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002071 break;
2072 }
2073
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002074 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002075 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002076 ret = x509_crt_verify_top( parent, grandparent, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002077 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002078 if( ret != 0 )
2079 return( ret );
2080 }
2081 else
2082 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002083 /* Look for a grandparent upwards the chain */
2084 for( grandparent = parent->next;
2085 grandparent != NULL;
2086 grandparent = grandparent->next )
2087 {
2088 if( x509_crt_check_parent( parent, grandparent,
2089 0, path_cnt == 0 ) == 0 )
2090 break;
2091 }
2092
2093 /* Is our parent part of the chain or at the top? */
2094 if( grandparent != NULL )
2095 {
2096 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
2097 profile, path_cnt + 1, &parent_flags,
2098 f_vrfy, p_vrfy );
2099 if( ret != 0 )
2100 return( ret );
2101 }
2102 else
2103 {
2104 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
2105 path_cnt + 1, &parent_flags,
2106 f_vrfy, p_vrfy );
2107 if( ret != 0 )
2108 return( ret );
2109 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002110 }
2111
2112 /* child is verified to be a child of the parent, call verify callback */
2113 if( NULL != f_vrfy )
2114 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2115 return( ret );
2116
2117 *flags |= parent_flags;
2118
2119 return( 0 );
2120}
2121
2122/*
2123 * Verify the certificate validity
2124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2126 mbedtls_x509_crt *trust_ca,
2127 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002128 const char *cn, uint32_t *flags,
2129 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002130 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002132 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002133 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002134}
2135
2136
2137/*
2138 * Verify the certificate validity, with profile
2139 */
2140int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2141 mbedtls_x509_crt *trust_ca,
2142 mbedtls_x509_crl *ca_crl,
2143 const mbedtls_x509_crt_profile *profile,
2144 const char *cn, uint32_t *flags,
2145 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2146 void *p_vrfy )
2147{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002148 size_t cn_len;
2149 int ret;
2150 int pathlen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 mbedtls_x509_crt *parent;
2152 mbedtls_x509_name *name;
2153 mbedtls_x509_sequence *cur = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002154
Manuel Pégourié-Gonnarda83e4e22015-06-17 11:53:48 +02002155 if( profile == NULL )
2156 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2157
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002158 *flags = 0;
2159
2160 if( cn != NULL )
2161 {
2162 name = &crt->subject;
2163 cn_len = strlen( cn );
2164
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002165 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002166 {
2167 cur = &crt->subject_alt_names;
2168
2169 while( cur != NULL )
2170 {
2171 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002172 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002173 break;
2174
2175 if( cur->buf.len > 2 &&
2176 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002177 x509_check_wildcard( cn, &cur->buf ) == 0 )
2178 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002179 break;
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002180 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002181
2182 cur = cur->next;
2183 }
2184
2185 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002187 }
2188 else
2189 {
2190 while( name != NULL )
2191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002193 {
2194 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002195 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002196 break;
2197
2198 if( name->val.len > 2 &&
2199 memcmp( name->val.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002200 x509_check_wildcard( cn, &name->val ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002201 break;
2202 }
2203
2204 name = name->next;
2205 }
2206
2207 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002209 }
2210 }
2211
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002212 /* Look for a parent in trusted CAs */
2213 for( parent = trust_ca; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002214 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002215 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002216 break;
2217 }
2218
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002219 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002220 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002221 ret = x509_crt_verify_top( crt, parent, ca_crl, profile,
2222 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002223 if( ret != 0 )
2224 return( ret );
2225 }
2226 else
2227 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002228 /* Look for a parent upwards the chain */
2229 for( parent = crt->next; parent != NULL; parent = parent->next )
2230 {
2231 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
2232 break;
2233 }
2234
2235 /* Are we part of the chain or at the top? */
2236 if( parent != NULL )
2237 {
2238 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
2239 pathlen, flags, f_vrfy, p_vrfy );
2240 if( ret != 0 )
2241 return( ret );
2242 }
2243 else
2244 {
2245 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
2246 pathlen, flags, f_vrfy, p_vrfy );
2247 if( ret != 0 )
2248 return( ret );
2249 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002250 }
2251
2252 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002254
2255 return( 0 );
2256}
2257
2258/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002259 * Initialize a certificate chain
2260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002262{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002264}
2265
2266/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002267 * Unallocate all certificate data
2268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002270{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 mbedtls_x509_crt *cert_cur = crt;
2272 mbedtls_x509_crt *cert_prv;
2273 mbedtls_x509_name *name_cur;
2274 mbedtls_x509_name *name_prv;
2275 mbedtls_x509_sequence *seq_cur;
2276 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002277
2278 if( crt == NULL )
2279 return;
2280
2281 do
2282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2286 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002287#endif
2288
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002289 name_cur = cert_cur->issuer.next;
2290 while( name_cur != NULL )
2291 {
2292 name_prv = name_cur;
2293 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2295 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002296 }
2297
2298 name_cur = cert_cur->subject.next;
2299 while( name_cur != NULL )
2300 {
2301 name_prv = name_cur;
2302 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2304 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002305 }
2306
2307 seq_cur = cert_cur->ext_key_usage.next;
2308 while( seq_cur != NULL )
2309 {
2310 seq_prv = seq_cur;
2311 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2313 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002314 }
2315
2316 seq_cur = cert_cur->subject_alt_names.next;
2317 while( seq_cur != NULL )
2318 {
2319 seq_prv = seq_cur;
2320 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2322 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002323 }
2324
2325 if( cert_cur->raw.p != NULL )
2326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2328 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002329 }
2330
2331 cert_cur = cert_cur->next;
2332 }
2333 while( cert_cur != NULL );
2334
2335 cert_cur = crt;
2336 do
2337 {
2338 cert_prv = cert_cur;
2339 cert_cur = cert_cur->next;
2340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002342 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002344 }
2345 while( cert_cur != NULL );
2346}
2347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348#endif /* MBEDTLS_X509_CRT_PARSE_C */