blob: a1ce2544ec77d4d20eec6fbcd5730afde989df41 [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
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +0200189#if defined(MBEDTLS_ECP_C)
190 if( pk_alg == MBEDTLS_PK_ECDSA ||
191 pk_alg == MBEDTLS_PK_ECKEY ||
192 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200193 {
194 mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
195
196 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
197 return( 0 );
198
199 return( -1 );
200 }
201#endif
202
203 return( -1 );
204}
205
206/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200207 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
208 */
209static int x509_get_version( unsigned char **p,
210 const unsigned char *end,
211 int *ver )
212{
213 int ret;
214 size_t len;
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
217 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200220 {
221 *ver = 0;
222 return( 0 );
223 }
224
225 return( ret );
226 }
227
228 end = *p + len;
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
231 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200232
233 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 return( MBEDTLS_ERR_X509_INVALID_VERSION +
235 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236
237 return( 0 );
238}
239
240/*
241 * Validity ::= SEQUENCE {
242 * notBefore Time,
243 * notAfter Time }
244 */
245static int x509_get_dates( unsigned char **p,
246 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_x509_time *from,
248 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249{
250 int ret;
251 size_t len;
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
254 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
255 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256
257 end = *p + len;
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260 return( ret );
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200263 return( ret );
264
265 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 return( MBEDTLS_ERR_X509_INVALID_DATE +
267 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268
269 return( 0 );
270}
271
272/*
273 * X.509 v2/v3 unique identifier (not parsed)
274 */
275static int x509_get_uid( unsigned char **p,
276 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278{
279 int ret;
280
281 if( *p == end )
282 return( 0 );
283
284 uid->tag = **p;
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
287 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290 return( 0 );
291
292 return( ret );
293 }
294
295 uid->p = *p;
296 *p += uid->len;
297
298 return( 0 );
299}
300
301static int x509_get_basic_constraints( unsigned char **p,
302 const unsigned char *end,
303 int *ca_istrue,
304 int *max_pathlen )
305{
306 int ret;
307 size_t len;
308
309 /*
310 * BasicConstraints ::= SEQUENCE {
311 * cA BOOLEAN DEFAULT FALSE,
312 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
313 */
314 *ca_istrue = 0; /* DEFAULT FALSE */
315 *max_pathlen = 0; /* endless */
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
318 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
319 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200320
321 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200322 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
327 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328
329 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200331
332 if( *ca_istrue != 0 )
333 *ca_istrue = 1;
334 }
335
336 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200337 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
340 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341
342 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
344 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345
346 (*max_pathlen)++;
347
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200348 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349}
350
351static int x509_get_ns_cert_type( unsigned char **p,
352 const unsigned char *end,
353 unsigned char *ns_cert_type)
354{
355 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
359 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360
361 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
363 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364
365 /* Get actual bitstring */
366 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200367 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368}
369
370static int x509_get_key_usage( unsigned char **p,
371 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100372 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200373{
374 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200375 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
379 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380
381 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
383 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384
385 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200386 *key_usage = 0;
387 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
388 {
389 *key_usage |= (unsigned int) bs.p[i] << (8*i);
390 }
391
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200392 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393}
394
395/*
396 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
397 *
398 * KeyPurposeId ::= OBJECT IDENTIFIER
399 */
400static int x509_get_ext_key_usage( unsigned char **p,
401 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403{
404 int ret;
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
407 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408
409 /* Sequence length must be >= 1 */
410 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
412 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200414 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415}
416
417/*
418 * SubjectAltName ::= GeneralNames
419 *
420 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
421 *
422 * GeneralName ::= CHOICE {
423 * otherName [0] OtherName,
424 * rfc822Name [1] IA5String,
425 * dNSName [2] IA5String,
426 * x400Address [3] ORAddress,
427 * directoryName [4] Name,
428 * ediPartyName [5] EDIPartyName,
429 * uniformResourceIdentifier [6] IA5String,
430 * iPAddress [7] OCTET STRING,
431 * registeredID [8] OBJECT IDENTIFIER }
432 *
433 * OtherName ::= SEQUENCE {
434 * type-id OBJECT IDENTIFIER,
435 * value [0] EXPLICIT ANY DEFINED BY type-id }
436 *
437 * EDIPartyName ::= SEQUENCE {
438 * nameAssigner [0] DirectoryString OPTIONAL,
439 * partyName [1] DirectoryString }
440 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000441 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442 */
443static int x509_get_subject_alt_name( unsigned char **p,
444 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446{
447 int ret;
448 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452
453 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
455 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
456 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457
458 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
460 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461
462 while( *p < end )
463 {
464 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
466 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467
468 tag = **p;
469 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
471 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 if( ( tag & MBEDTLS_ASN1_CONTEXT_SPECIFIC ) != MBEDTLS_ASN1_CONTEXT_SPECIFIC )
474 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
475 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200477 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479 {
480 *p += tag_len;
481 continue;
482 }
483
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200485 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100487 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100489
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200490 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491
492 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200494 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496 cur = cur->next;
497 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200498
499 buf = &(cur->buf);
500 buf->tag = tag;
501 buf->p = *p;
502 buf->len = tag_len;
503 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504 }
505
506 /* Set final sequence entry's next pointer to NULL */
507 cur->next = NULL;
508
509 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
511 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512
513 return( 0 );
514}
515
516/*
517 * X.509 v3 extensions
518 *
519 * TODO: Perform all of the basic constraints tests required by the RFC
520 * TODO: Set values for undetected extensions to a sane default?
521 *
522 */
523static int x509_get_crt_ext( unsigned char **p,
524 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526{
527 int ret;
528 size_t len;
529 unsigned char *end_ext_data, *end_ext_octet;
530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534 return( 0 );
535
536 return( ret );
537 }
538
539 while( *p < end )
540 {
541 /*
542 * Extension ::= SEQUENCE {
543 * extnID OBJECT IDENTIFIER,
544 * critical BOOLEAN DEFAULT FALSE,
545 * extnValue OCTET STRING }
546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548 int is_critical = 0; /* DEFAULT FALSE */
549 int ext_type = 0;
550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
552 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
553 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554
555 end_ext_data = *p + len;
556
557 /* Get extension ID */
558 extn_oid.tag = **p;
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
561 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562
563 extn_oid.p = *p;
564 *p += extn_oid.len;
565
566 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
568 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569
570 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
572 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
573 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574
575 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
577 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
578 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579
580 end_ext_octet = *p + len;
581
582 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
584 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585
586 /*
587 * Detect supported extensions
588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590
591 if( ret != 0 )
592 {
593 /* No parser found, skip extension */
594 *p = end_ext_octet;
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597 if( is_critical )
598 {
599 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
601 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602 }
603#endif
604 continue;
605 }
606
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100607 /* Forbid repeated extensions */
608 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100610
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611 crt->ext_types |= ext_type;
612
613 switch( ext_type )
614 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100615 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200616 /* Parse basic constraints */
617 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
618 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200619 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620 break;
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623 /* Parse key usage */
624 if( ( ret = x509_get_key_usage( p, end_ext_octet,
625 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200626 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627 break;
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 /* Parse extended key usage */
631 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
632 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200633 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634 break;
635
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100636 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637 /* Parse subject alt name */
638 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
639 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200640 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641 break;
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644 /* Parse netscape certificate type */
645 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
646 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200647 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 break;
649
650 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652 }
653 }
654
655 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
657 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658
659 return( 0 );
660}
661
662/*
663 * Parse and fill a single X.509 certificate in DER format
664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200666 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200667{
668 int ret;
669 size_t len;
670 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
674 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
675 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676
677 /*
678 * Check for valid input
679 */
680 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682
Janos Follath16734f02016-02-17 14:34:12 +0000683 // Use the original buffer until we figure out actual length
684 p = (unsigned char*) buf;
685 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686 end = p + len;
687
688 /*
689 * Certificate ::= SEQUENCE {
690 * tbsCertificate TBSCertificate,
691 * signatureAlgorithm AlgorithmIdentifier,
692 * signatureValue BIT STRING }
693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
695 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_x509_crt_free( crt );
698 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699 }
700
701 if( len > (size_t) ( end - p ) )
702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_x509_crt_free( crt );
704 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
705 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706 }
707 crt_end = p + len;
708
Janos Follath16734f02016-02-17 14:34:12 +0000709 // Create and populate a new buffer for the raw field
710 crt->raw.len = crt_end - buf;
711 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
712 if( p == NULL )
713 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
714
715 memcpy( p, buf, crt->raw.len );
716
717 // Direct pointers to the new buffer
718 p += crt->raw.len - len;
719 end = crt_end = p + len;
720
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721 /*
722 * TBSCertificate ::= SEQUENCE {
723 */
724 crt->tbs.p = p;
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
727 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_x509_crt_free( crt );
730 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731 }
732
733 end = p + len;
734 crt->tbs.len = end - crt->tbs.p;
735
736 /*
737 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
738 *
739 * CertificateSerialNumber ::= INTEGER
740 *
741 * signature AlgorithmIdentifier
742 */
743 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
745 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200746 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200749 return( ret );
750 }
751
752 crt->version++;
753
754 if( crt->version > 3 )
755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_x509_crt_free( crt );
757 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200758 }
759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200761 &crt->sig_md, &crt->sig_pk,
762 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765 return( ret );
766 }
767
768 /*
769 * issuer Name
770 */
771 crt->issuer_raw.p = p;
772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
774 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_x509_crt_free( crt );
777 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200778 }
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200783 return( ret );
784 }
785
786 crt->issuer_raw.len = p - crt->issuer_raw.p;
787
788 /*
789 * Validity ::= SEQUENCE {
790 * notBefore Time,
791 * notAfter Time }
792 *
793 */
794 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
795 &crt->valid_to ) ) != 0 )
796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798 return( ret );
799 }
800
801 /*
802 * subject Name
803 */
804 crt->subject_raw.p = p;
805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
807 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 mbedtls_x509_crt_free( crt );
810 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811 }
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 return( ret );
817 }
818
819 crt->subject_raw.len = p - crt->subject_raw.p;
820
821 /*
822 * SubjectPublicKeyInfo
823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827 return( ret );
828 }
829
830 /*
831 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
832 * -- If present, version shall be v2 or v3
833 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
834 * -- If present, version shall be v2 or v3
835 * extensions [3] EXPLICIT Extensions OPTIONAL
836 * -- If present, version shall be v3
837 */
838 if( crt->version == 2 || crt->version == 3 )
839 {
840 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
841 if( ret != 0 )
842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200844 return( ret );
845 }
846 }
847
848 if( crt->version == 2 || crt->version == 3 )
849 {
850 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
851 if( ret != 0 )
852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200854 return( ret );
855 }
856 }
857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200859 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200860#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100861 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200862 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200863 if( ret != 0 )
864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866 return( ret );
867 }
868 }
869
870 if( p != end )
871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_x509_crt_free( crt );
873 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
874 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200875 }
876
877 end = crt_end;
878
879 /*
880 * }
881 * -- end of TBSCertificate
882 *
883 * signatureAlgorithm AlgorithmIdentifier,
884 * signatureValue BIT STRING
885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 return( ret );
890 }
891
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100892 if( crt->sig_oid.len != sig_oid2.len ||
893 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200894 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200895 ( sig_params1.len != 0 &&
896 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_x509_crt_free( crt );
899 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900 }
901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 return( ret );
906 }
907
908 if( p != end )
909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_x509_crt_free( crt );
911 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
912 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913 }
914
915 return( 0 );
916}
917
918/*
919 * Parse one X.509 certificate in DER format from a buffer and add them to a
920 * chained list
921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200923 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924{
925 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927
928 /*
929 * Check for valid input
930 */
931 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
934 while( crt->version != 0 && crt->next != NULL )
935 {
936 prev = crt;
937 crt = crt->next;
938 }
939
940 /*
941 * Add new certificate on the end of the chain if needed.
942 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200943 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200945 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946
947 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200948 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949
950 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953 }
954
Paul Bakkerddf26b42013-09-18 13:46:23 +0200955 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956 {
957 if( prev )
958 prev->next = NULL;
959
960 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962
963 return( ret );
964 }
965
966 return( 0 );
967}
968
969/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200970 * Parse one or more PEM certificates from a buffer and add them to the chained
971 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974{
975 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 int buf_format = MBEDTLS_X509_FORMAT_DER;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977
978 /*
979 * Check for valid input
980 */
981 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983
984 /*
985 * Determine buffer content. Buffer contains either one DER certificate or
986 * one or more PEM certificates.
987 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +0200989 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200990 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200993 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994#endif
995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 if( buf_format == MBEDTLS_X509_FORMAT_DER )
997 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999#if defined(MBEDTLS_PEM_PARSE_C)
1000 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001 {
1002 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001005 /* 1 rather than 0 since the terminating NULL byte is counted in */
1006 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007 {
1008 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001011 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 "-----BEGIN CERTIFICATE-----",
1014 "-----END CERTIFICATE-----",
1015 buf, NULL, 0, &use_len );
1016
1017 if( ret == 0 )
1018 {
1019 /*
1020 * Was PEM encoded
1021 */
1022 buflen -= use_len;
1023 buf += use_len;
1024 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 {
1027 return( ret );
1028 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032
1033 /*
1034 * PEM header and footer were found
1035 */
1036 buflen -= use_len;
1037 buf += use_len;
1038
1039 if( first_error == 0 )
1040 first_error = ret;
1041
Paul Bakker5a5fa922014-09-26 14:53:04 +02001042 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 continue;
1044 }
1045 else
1046 break;
1047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051
1052 if( ret != 0 )
1053 {
1054 /*
1055 * Quit parsing on a memory error
1056 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001057 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001058 return( ret );
1059
1060 if( first_error == 0 )
1061 first_error = ret;
1062
1063 total_failed++;
1064 continue;
1065 }
1066
1067 success = 1;
1068 }
1069 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071
1072 if( success )
1073 return( total_failed );
1074 else if( first_error )
1075 return( first_error );
1076 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078}
1079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081/*
1082 * Load one or more certificates and add them to the chained list
1083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085{
1086 int ret;
1087 size_t n;
1088 unsigned char *buf;
1089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091 return( ret );
1092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001095 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097
1098 return( ret );
1099}
1100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102{
1103 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001104#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105 int w_ret;
1106 WCHAR szDir[MAX_PATH];
1107 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001108 char *p;
Manuel Pégourié-Gonnard9dc66f42015-10-21 10:16:29 +02001109 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110
Paul Bakker9af723c2014-05-01 13:03:14 +02001111 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112 HANDLE hFind;
1113
1114 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001116
Paul Bakker9af723c2014-05-01 13:03:14 +02001117 memset( szDir, 0, sizeof(szDir) );
1118 memset( filename, 0, MAX_PATH );
1119 memcpy( filename, path, len );
1120 filename[len++] = '\\';
1121 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122 filename[len++] = '*';
1123
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001124 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
1125 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001126 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128
1129 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001130 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132
1133 len = MAX_PATH - len;
1134 do
1135 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001136 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001137
1138 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1139 continue;
1140
Paul Bakker9af723c2014-05-01 13:03:14 +02001141 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001142 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard9dc66f42015-10-21 10:16:29 +02001143 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001144 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001145 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149 if( w_ret < 0 )
1150 ret++;
1151 else
1152 ret += w_ret;
1153 }
1154 while( FindNextFileW( hFind, &file_data ) != 0 );
1155
Paul Bakker66d5d072014-06-17 16:39:18 +02001156 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001159 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001160#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001161 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001163 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164 char entry_name[255];
1165 DIR *dir = opendir( path );
1166
Paul Bakker66d5d072014-06-17 16:39:18 +02001167 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001171 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001172 {
1173 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001174 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001175 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001176#endif
1177
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001178 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 mbedtls_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001182 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001183 {
1184 closedir( dir );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001186 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001187 }
1188
1189 if( !S_ISREG( sb.st_mode ) )
1190 continue;
1191
1192 // Ignore parse errors
1193 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001195 if( t_ret < 0 )
1196 ret++;
1197 else
1198 ret += t_ret;
1199 }
1200 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001201
1202cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001204 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001206#endif
1207
Paul Bakkerbe089b02013-10-14 15:51:50 +02001208#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209
1210 return( ret );
1211}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001213
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001214static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001216{
1217 size_t i;
1218 size_t n = *size;
1219 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001221 const char *sep = "";
1222 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001223
1224 while( cur != NULL )
1225 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001226 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001227 {
1228 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001229 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001230 }
1231
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001232 n -= cur->buf.len + sep_len;
1233 for( i = 0; i < sep_len; i++ )
1234 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001235 for( i = 0; i < cur->buf.len; i++ )
1236 *p++ = cur->buf.p[i];
1237
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001238 sep = ", ";
1239 sep_len = 2;
1240
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001241 cur = cur->next;
1242 }
1243
1244 *p = '\0';
1245
1246 *size = n;
1247 *buf = p;
1248
1249 return( 0 );
1250}
1251
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001252#define PRINT_ITEM(i) \
1253 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001255 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001256 sep = ", "; \
1257 }
1258
1259#define CERT_TYPE(type,name) \
1260 if( ns_cert_type & type ) \
1261 PRINT_ITEM( name );
1262
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001263static int x509_info_cert_type( char **buf, size_t *size,
1264 unsigned char ns_cert_type )
1265{
1266 int ret;
1267 size_t n = *size;
1268 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001269 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001272 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1274 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1275 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001276 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1277 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1278 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001279
1280 *size = n;
1281 *buf = p;
1282
1283 return( 0 );
1284}
1285
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001286#define KEY_USAGE(code,name) \
1287 if( key_usage & code ) \
1288 PRINT_ITEM( name );
1289
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001290static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001291 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001292{
1293 int ret;
1294 size_t n = *size;
1295 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001296 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1299 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001300 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1301 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1302 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1304 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001305 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1306 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001307
1308 *size = n;
1309 *buf = p;
1310
1311 return( 0 );
1312}
1313
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001314static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001316{
1317 int ret;
1318 const char *desc;
1319 size_t n = *size;
1320 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001322 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001323
1324 while( cur != NULL )
1325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001327 desc = "???";
1328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001330 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001331
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001332 sep = ", ";
1333
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001334 cur = cur->next;
1335 }
1336
1337 *size = n;
1338 *buf = p;
1339
1340 return( 0 );
1341}
1342
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343/*
1344 * Return an informational string about the certificate.
1345 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001346#define BEFORE_COLON 18
1347#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1349 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001350{
1351 int ret;
1352 size_t n;
1353 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001354 char key_size_str[BEFORE_COLON];
1355
1356 p = buf;
1357 n = size;
1358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360 prefix, crt->version );
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_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001364 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001367 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001370 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001372 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001375 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001377 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1381 crt->valid_from.year, crt->valid_from.mon,
1382 crt->valid_from.day, crt->valid_from.hour,
1383 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001384 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001387 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1388 crt->valid_to.year, crt->valid_to.mon,
1389 crt->valid_to.day, crt->valid_to.hour,
1390 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001391 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001394 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001397 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001398 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001399
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001400 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1402 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001403 {
1404 return( ret );
1405 }
1406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001408 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001409 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001410
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001411 /*
1412 * Optional extensions
1413 */
1414
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001415 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001418 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001419 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001420
1421 if( crt->max_pathlen > 0 )
1422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001424 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001425 }
1426 }
1427
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001428 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001431 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001432
1433 if( ( ret = x509_info_subject_alt_name( &p, &n,
1434 &crt->subject_alt_names ) ) != 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_NS_CERT_TYPE )
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%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001441 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001442
1443 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 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_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%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001450 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001451
1452 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1453 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001454 }
1455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001459 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001460
1461 if( ( ret = x509_info_ext_key_usage( &p, &n,
1462 &crt->ext_key_usage ) ) != 0 )
1463 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001464 }
1465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001467 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001468
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001469 return( (int) ( size - n ) );
1470}
1471
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001472struct x509_crt_verify_string {
1473 int code;
1474 const char *string;
1475};
1476
1477static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001478 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001479 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1480 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1481 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1482 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1483 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001484 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1485 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1486 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001487 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001488 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1489 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1490 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1491 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001492 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1493 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1494 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1495 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1496 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1497 { 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 +01001498 { 0, NULL }
1499};
1500
1501int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001502 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001503{
1504 int ret;
1505 const struct x509_crt_verify_string *cur;
1506 char *p = buf;
1507 size_t n = size;
1508
1509 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1510 {
1511 if( ( flags & cur->code ) == 0 )
1512 continue;
1513
1514 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001515 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001516 flags ^= cur->code;
1517 }
1518
1519 if( flags != 0 )
1520 {
1521 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1522 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001523 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001524 }
1525
1526 return( (int) ( size - n ) );
1527}
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001530int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1531 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001532{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001533 unsigned int usage_must, usage_may;
1534 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1535 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1536
1537 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1538 return( 0 );
1539
1540 usage_must = usage & ~may_mask;
1541
1542 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1543 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1544
1545 usage_may = usage & may_mask;
1546
1547 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001549
1550 return( 0 );
1551}
1552#endif
1553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1555int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001556 const char *usage_oid,
1557 size_t usage_len )
1558{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001560
1561 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001563 return( 0 );
1564
1565 /*
1566 * Look for the requested usage (or wildcard ANY) in our list
1567 */
1568 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001571
1572 if( cur_oid->len == usage_len &&
1573 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1574 {
1575 return( 0 );
1576 }
1577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001579 return( 0 );
1580 }
1581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587/*
1588 * Return 1 if the certificate is revoked, or 0 otherwise.
1589 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001590int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593
1594 while( cur != NULL && cur->serial.len != 0 )
1595 {
1596 if( crt->serial.len == cur->serial.len &&
1597 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1598 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001599 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600 return( 1 );
1601 }
1602
1603 cur = cur->next;
1604 }
1605
1606 return( 0 );
1607}
1608
1609/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001610 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001613 mbedtls_x509_crl *crl_list,
1614 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615{
1616 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1618 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619
1620 if( ca == NULL )
1621 return( flags );
1622
1623 /*
1624 * TODO: What happens if no CRL is present?
1625 * Suggestion: Revocation state should be unknown if no CRL is present.
1626 * For backwards compatibility this is not yet implemented.
1627 */
1628
1629 while( crl_list != NULL )
1630 {
1631 if( crl_list->version == 0 ||
1632 crl_list->issuer_raw.len != ca->subject_raw.len ||
1633 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1634 crl_list->issuer_raw.len ) != 0 )
1635 {
1636 crl_list = crl_list->next;
1637 continue;
1638 }
1639
1640 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001641 * Check if the CA is configured to sign CRLs
1642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1644 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001647 break;
1648 }
1649#endif
1650
1651 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001652 * Check if CRL is correctly signed by the trusted CA
1653 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001654 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1655 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1656
1657 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1658 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661 if( md_info == NULL )
1662 {
1663 /*
1664 * Cannot check 'unknown' hash
1665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001667 break;
1668 }
1669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001672 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1673 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1676 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001677 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680 break;
1681 }
1682
1683 /*
1684 * Check for validity of CRL (Do not drop out)
1685 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001686 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001688
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001689 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001690 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001691
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001692 /*
1693 * Check if certificate is revoked
1694 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001695 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001698 break;
1699 }
1700
1701 crl_list = crl_list->next;
1702 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001703
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001704 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001707
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001708/*
1709 * Like memcmp, but case-insensitive and always returns -1 if different
1710 */
1711static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001712{
1713 size_t i;
1714 unsigned char diff;
1715 const unsigned char *n1 = s1, *n2 = s2;
1716
1717 for( i = 0; i < len; i++ )
1718 {
1719 diff = n1[i] ^ n2[i];
1720
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001721 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001722 continue;
1723
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001724 if( diff == 32 &&
1725 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1726 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1727 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001728 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001729 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001730
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001731 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001732 }
1733
1734 return( 0 );
1735}
1736
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001737/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001738 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001739 */
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001740static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001741{
1742 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001743 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001744
1745 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1746 return( 0 );
1747
Paul Bakker14b16c62014-05-28 11:33:54 +02001748 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001749 {
1750 if( cn[i] == '.' )
1751 {
1752 cn_idx = i;
1753 break;
1754 }
1755 }
1756
1757 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001758 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001759
Paul Bakker14b16c62014-05-28 11:33:54 +02001760 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001761 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001762 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001763 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001764 }
1765
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001766 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001767}
1768
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001769/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001770 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1771 * variations (but not all).
1772 *
1773 * Return 0 if equal, -1 otherwise.
1774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001776{
1777 if( a->tag == b->tag &&
1778 a->len == b->len &&
1779 memcmp( a->p, b->p, b->len ) == 0 )
1780 {
1781 return( 0 );
1782 }
1783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1785 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001786 a->len == b->len &&
1787 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1788 {
1789 return( 0 );
1790 }
1791
1792 return( -1 );
1793}
1794
1795/*
1796 * Compare two X.509 Names (aka rdnSequence).
1797 *
1798 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1799 * we sometimes return unequal when the full algorithm would return equal,
1800 * but never the other way. (In particular, we don't do Unicode normalisation
1801 * or space folding.)
1802 *
1803 * Return 0 if equal, -1 otherwise.
1804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001806{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001807 /* Avoid recursion, it might not be optimised by the compiler */
1808 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001809 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001810 if( a == NULL || b == NULL )
1811 return( -1 );
1812
1813 /* type */
1814 if( a->oid.tag != b->oid.tag ||
1815 a->oid.len != b->oid.len ||
1816 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1817 {
1818 return( -1 );
1819 }
1820
1821 /* value */
1822 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1823 return( -1 );
1824
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001825 /* structure of the list of sets */
1826 if( a->next_merged != b->next_merged )
1827 return( -1 );
1828
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001829 a = a->next;
1830 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001831 }
1832
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001833 /* a == NULL == b */
1834 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001835}
1836
1837/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001838 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1839 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001840 *
1841 * top means parent is a locally-trusted certificate
1842 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1845 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001846 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001847{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001848 int need_ca_bit;
1849
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001850 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001851 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001852 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001853
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001854 /* Parent must have the basicConstraints CA bit set as a general rule */
1855 need_ca_bit = 1;
1856
1857 /* Exception: v1/v2 certificates that are locally trusted. */
1858 if( top && parent->version < 3 )
1859 need_ca_bit = 0;
1860
1861 /* Exception: self-signed end-entity certs that are locally trusted. */
1862 if( top && bottom &&
1863 child->raw.len == parent->raw.len &&
1864 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1865 {
1866 need_ca_bit = 0;
1867 }
1868
1869 if( need_ca_bit && ! parent->ca_istrue )
1870 return( -1 );
1871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001873 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001875 {
1876 return( -1 );
1877 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001878#endif
1879
1880 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001881}
1882
Paul Bakkerddf26b42013-09-18 13:46:23 +02001883static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001885 mbedtls_x509_crl *ca_crl,
1886 const mbedtls_x509_crt_profile *profile,
Janos Follath860f2392015-10-12 09:02:20 +02001887 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001888 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001889 void *p_vrfy )
1890{
1891 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001892 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001893 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1895 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001896
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001897 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001898 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001899
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001900 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001902
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001903 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
1904 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
1905
1906 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
1907 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
1908
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001909 /*
1910 * Child is the top of the chain. Check against the trust_ca list.
1911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001915 if( md_info == NULL )
1916 {
1917 /*
1918 * Cannot check 'unknown', no need to try any CA
1919 */
1920 trust_ca = NULL;
1921 }
1922 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001924
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001925 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001926 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001927 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001928 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001929
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001930 check_path_cnt = path_cnt + 1;
1931
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001932 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001933 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001934 * the same as the trusted CA
1935 */
1936 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1937 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1938 child->issuer_raw.len ) == 0 )
1939 {
1940 check_path_cnt--;
1941 }
1942
Janos Follath860f2392015-10-12 09:02:20 +02001943 /* Self signed certificates do not count towards the limit */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001944 if( trust_ca->max_pathlen > 0 &&
Janos Follath860f2392015-10-12 09:02:20 +02001945 trust_ca->max_pathlen < check_path_cnt - self_cnt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001946 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001947 continue;
1948 }
1949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1951 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001952 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001953 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001954 continue;
1955 }
1956
1957 /*
1958 * Top of chain is signed by a trusted CA
1959 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardfa67eba2015-06-27 14:41:38 +02001961
1962 if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
1963 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
1964
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001965 break;
1966 }
1967
1968 /*
1969 * If top of chain is not the same as the trusted CA send a verify request
1970 * to the callback for any issues with validity and CRL presence for the
1971 * trusted CA certificate.
1972 */
1973 if( trust_ca != NULL &&
1974 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1975 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1976 child->issuer_raw.len ) != 0 ) )
1977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001979 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001980 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001981#else
1982 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001983#endif
1984
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001985 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001986 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001987
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001988 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001990
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001991 if( NULL != f_vrfy )
1992 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001993 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1994 &ca_flags ) ) != 0 )
1995 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001996 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001997 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001998 }
1999 }
2000
2001 /* Call callback on top cert */
2002 if( NULL != f_vrfy )
2003 {
Paul Bakker66d5d072014-06-17 16:39:18 +02002004 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002005 return( ret );
2006 }
2007
2008 *flags |= ca_flags;
2009
2010 return( 0 );
2011}
2012
Paul Bakkerddf26b42013-09-18 13:46:23 +02002013static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002014 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
2015 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
2016 const mbedtls_x509_crt_profile *profile,
Janos Follath860f2392015-10-12 09:02:20 +02002017 int path_cnt, int self_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002018 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002019 void *p_vrfy )
2020{
2021 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002022 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2024 mbedtls_x509_crt *grandparent;
2025 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002026
Janos Follath860f2392015-10-12 09:02:20 +02002027 /* Counting intermediate self signed certificates */
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002028 if( ( path_cnt != 0 ) && x509_name_cmp( &child->issuer, &child->subject ) == 0 )
Janos Follath860f2392015-10-12 09:02:20 +02002029 self_cnt++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002030
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002031 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2035 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002036 }
2037
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002038 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002039 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02002040
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002041 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002043
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002044 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2045 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2046
2047 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2048 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002051 if( md_info == NULL )
2052 {
2053 /*
2054 * Cannot check 'unknown' hash
2055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002057 }
2058 else
2059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002061
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002062 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2063 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2066 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002067 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002070 }
2071 }
2072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002074 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002075 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002076#endif
2077
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002078 /* Look for a grandparent in trusted CAs */
2079 for( grandparent = trust_ca;
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002080 grandparent != NULL;
2081 grandparent = grandparent->next )
2082 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002083 if( x509_crt_check_parent( parent, grandparent,
2084 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002085 break;
2086 }
2087
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002088 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002089 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002090 ret = x509_crt_verify_top( parent, grandparent, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002091 path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002092 if( ret != 0 )
2093 return( ret );
2094 }
2095 else
2096 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002097 /* Look for a grandparent upwards the chain */
2098 for( grandparent = parent->next;
2099 grandparent != NULL;
2100 grandparent = grandparent->next )
2101 {
Janos Follath860f2392015-10-12 09:02:20 +02002102 /* +2 because the current step is not yet accounted for
2103 * and because max_pathlen is one higher than it should be.
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +01002104 * Also self signed certificates do not count to the limit. */
Janos Follath860f2392015-10-12 09:02:20 +02002105 if( grandparent->max_pathlen > 0 &&
2106 grandparent->max_pathlen < 2 + path_cnt - self_cnt )
2107 {
2108 continue;
2109 }
2110
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002111 if( x509_crt_check_parent( parent, grandparent,
2112 0, path_cnt == 0 ) == 0 )
2113 break;
2114 }
2115
2116 /* Is our parent part of the chain or at the top? */
2117 if( grandparent != NULL )
2118 {
2119 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
Janos Follath860f2392015-10-12 09:02:20 +02002120 profile, path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002121 f_vrfy, p_vrfy );
2122 if( ret != 0 )
2123 return( ret );
2124 }
2125 else
2126 {
2127 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002128 path_cnt + 1, self_cnt, &parent_flags,
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002129 f_vrfy, p_vrfy );
2130 if( ret != 0 )
2131 return( ret );
2132 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002133 }
2134
2135 /* child is verified to be a child of the parent, call verify callback */
2136 if( NULL != f_vrfy )
2137 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2138 return( ret );
2139
2140 *flags |= parent_flags;
2141
2142 return( 0 );
2143}
2144
2145/*
2146 * Verify the certificate validity
2147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2149 mbedtls_x509_crt *trust_ca,
2150 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002151 const char *cn, uint32_t *flags,
2152 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002153 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002154{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002155 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002156 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002157}
2158
2159
2160/*
2161 * Verify the certificate validity, with profile
2162 */
2163int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2164 mbedtls_x509_crt *trust_ca,
2165 mbedtls_x509_crl *ca_crl,
2166 const mbedtls_x509_crt_profile *profile,
2167 const char *cn, uint32_t *flags,
2168 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2169 void *p_vrfy )
2170{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002171 size_t cn_len;
2172 int ret;
Janos Follath860f2392015-10-12 09:02:20 +02002173 int pathlen = 0, selfsigned = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 mbedtls_x509_crt *parent;
2175 mbedtls_x509_name *name;
2176 mbedtls_x509_sequence *cur = NULL;
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +02002177 mbedtls_pk_type_t pk_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002178
Manuel Pégourié-Gonnarda83e4e22015-06-17 11:53:48 +02002179 if( profile == NULL )
2180 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2181
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002182 *flags = 0;
2183
2184 if( cn != NULL )
2185 {
2186 name = &crt->subject;
2187 cn_len = strlen( cn );
2188
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002189 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002190 {
2191 cur = &crt->subject_alt_names;
2192
2193 while( cur != NULL )
2194 {
2195 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002196 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002197 break;
2198
2199 if( cur->buf.len > 2 &&
2200 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002201 x509_check_wildcard( cn, &cur->buf ) == 0 )
2202 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002203 break;
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002204 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002205
2206 cur = cur->next;
2207 }
2208
2209 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002211 }
2212 else
2213 {
2214 while( name != NULL )
2215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002217 {
2218 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002219 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002220 break;
2221
2222 if( name->val.len > 2 &&
2223 memcmp( name->val.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002224 x509_check_wildcard( cn, &name->val ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002225 break;
2226 }
2227
2228 name = name->next;
2229 }
2230
2231 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002233 }
2234 }
2235
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +02002236 /* Check the type and size of the key */
2237 pk_type = mbedtls_pk_get_type( &crt->pk );
2238
2239 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
2240 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2241
2242 if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
2243 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2244
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002245 /* Look for a parent in trusted CAs */
2246 for( parent = trust_ca; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002247 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002248 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002249 break;
2250 }
2251
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002252 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002253 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002254 ret = x509_crt_verify_top( crt, parent, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002255 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002256 if( ret != 0 )
2257 return( ret );
2258 }
2259 else
2260 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002261 /* Look for a parent upwards the chain */
2262 for( parent = crt->next; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002263 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
2264 break;
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002265
2266 /* Are we part of the chain or at the top? */
2267 if( parent != NULL )
2268 {
2269 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002270 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002271 if( ret != 0 )
2272 return( ret );
2273 }
2274 else
2275 {
2276 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
Janos Follath860f2392015-10-12 09:02:20 +02002277 pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002278 if( ret != 0 )
2279 return( ret );
2280 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002281 }
2282
2283 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002285
2286 return( 0 );
2287}
2288
2289/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002290 * Initialize a certificate chain
2291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002293{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002295}
2296
2297/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002298 * Unallocate all certificate data
2299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002301{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 mbedtls_x509_crt *cert_cur = crt;
2303 mbedtls_x509_crt *cert_prv;
2304 mbedtls_x509_name *name_cur;
2305 mbedtls_x509_name *name_prv;
2306 mbedtls_x509_sequence *seq_cur;
2307 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002308
2309 if( crt == NULL )
2310 return;
2311
2312 do
2313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2317 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002318#endif
2319
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002320 name_cur = cert_cur->issuer.next;
2321 while( name_cur != NULL )
2322 {
2323 name_prv = name_cur;
2324 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2326 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002327 }
2328
2329 name_cur = cert_cur->subject.next;
2330 while( name_cur != NULL )
2331 {
2332 name_prv = name_cur;
2333 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2335 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002336 }
2337
2338 seq_cur = cert_cur->ext_key_usage.next;
2339 while( seq_cur != NULL )
2340 {
2341 seq_prv = seq_cur;
2342 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2344 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002345 }
2346
2347 seq_cur = cert_cur->subject_alt_names.next;
2348 while( seq_cur != NULL )
2349 {
2350 seq_prv = seq_cur;
2351 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2353 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002354 }
2355
2356 if( cert_cur->raw.p != NULL )
2357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2359 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002360 }
2361
2362 cert_cur = cert_cur->next;
2363 }
2364 while( cert_cur != NULL );
2365
2366 cert_cur = crt;
2367 do
2368 {
2369 cert_prv = cert_cur;
2370 cert_cur = cert_cur->next;
2371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002373 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002375 }
2376 while( cert_cur != NULL );
2377}
2378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379#endif /* MBEDTLS_X509_CRT_PARSE_C */