blob: 14e5d94cd76b9be9d2c3e47824c250e7e10d144f [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
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200683 p = mbedtls_calloc( 1, len = buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684 if( p == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200685 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686
687 memcpy( p, buf, buflen );
688
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689 crt->raw.p = p;
690 crt->raw.len = len;
691 end = p + len;
692
693 /*
694 * Certificate ::= SEQUENCE {
695 * tbsCertificate TBSCertificate,
696 * signatureAlgorithm AlgorithmIdentifier,
697 * signatureValue BIT STRING }
698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
700 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_x509_crt_free( crt );
703 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704 }
705
706 if( len > (size_t) ( end - p ) )
707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_x509_crt_free( crt );
709 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
710 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711 }
712 crt_end = p + len;
713
714 /*
715 * TBSCertificate ::= SEQUENCE {
716 */
717 crt->tbs.p = p;
718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
720 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 mbedtls_x509_crt_free( crt );
723 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724 }
725
726 end = p + len;
727 crt->tbs.len = end - crt->tbs.p;
728
729 /*
730 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
731 *
732 * CertificateSerialNumber ::= INTEGER
733 *
734 * signature AlgorithmIdentifier
735 */
736 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
738 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200739 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200742 return( ret );
743 }
744
745 crt->version++;
746
747 if( crt->version > 3 )
748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_x509_crt_free( crt );
750 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200751 }
752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200754 &crt->sig_md, &crt->sig_pk,
755 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200758 return( ret );
759 }
760
761 /*
762 * issuer Name
763 */
764 crt->issuer_raw.p = p;
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
767 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_x509_crt_free( crt );
770 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 }
772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200776 return( ret );
777 }
778
779 crt->issuer_raw.len = p - crt->issuer_raw.p;
780
781 /*
782 * Validity ::= SEQUENCE {
783 * notBefore Time,
784 * notAfter Time }
785 *
786 */
787 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
788 &crt->valid_to ) ) != 0 )
789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791 return( ret );
792 }
793
794 /*
795 * subject Name
796 */
797 crt->subject_raw.p = p;
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
800 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_x509_crt_free( crt );
803 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804 }
805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200809 return( ret );
810 }
811
812 crt->subject_raw.len = p - crt->subject_raw.p;
813
814 /*
815 * SubjectPublicKeyInfo
816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820 return( ret );
821 }
822
823 /*
824 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
825 * -- If present, version shall be v2 or v3
826 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
827 * -- If present, version shall be v2 or v3
828 * extensions [3] EXPLICIT Extensions OPTIONAL
829 * -- If present, version shall be v3
830 */
831 if( crt->version == 2 || crt->version == 3 )
832 {
833 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
834 if( ret != 0 )
835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837 return( ret );
838 }
839 }
840
841 if( crt->version == 2 || crt->version == 3 )
842 {
843 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
844 if( ret != 0 )
845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200847 return( ret );
848 }
849 }
850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200852 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200853#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100854 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200855 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856 if( ret != 0 )
857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200859 return( ret );
860 }
861 }
862
863 if( p != end )
864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_x509_crt_free( crt );
866 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
867 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868 }
869
870 end = crt_end;
871
872 /*
873 * }
874 * -- end of TBSCertificate
875 *
876 * signatureAlgorithm AlgorithmIdentifier,
877 * signatureValue BIT STRING
878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882 return( ret );
883 }
884
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100885 if( crt->sig_oid.len != sig_oid2.len ||
886 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200887 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200888 ( sig_params1.len != 0 &&
889 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 mbedtls_x509_crt_free( crt );
892 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200893 }
894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 return( ret );
899 }
900
901 if( p != end )
902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_x509_crt_free( crt );
904 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
905 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906 }
907
908 return( 0 );
909}
910
911/*
912 * Parse one X.509 certificate in DER format from a buffer and add them to a
913 * chained list
914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200916 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917{
918 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920
921 /*
922 * Check for valid input
923 */
924 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926
927 while( crt->version != 0 && crt->next != NULL )
928 {
929 prev = crt;
930 crt = crt->next;
931 }
932
933 /*
934 * Add new certificate on the end of the chain if needed.
935 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200936 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200938 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939
940 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200941 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942
943 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946 }
947
Paul Bakkerddf26b42013-09-18 13:46:23 +0200948 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949 {
950 if( prev )
951 prev->next = NULL;
952
953 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955
956 return( ret );
957 }
958
959 return( 0 );
960}
961
962/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200963 * Parse one or more PEM certificates from a buffer and add them to the chained
964 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967{
968 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 int buf_format = MBEDTLS_X509_FORMAT_DER;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970
971 /*
972 * Check for valid input
973 */
974 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976
977 /*
978 * Determine buffer content. Buffer contains either one DER certificate or
979 * one or more PEM certificates.
980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +0200982 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200983 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200986 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987#endif
988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 if( buf_format == MBEDTLS_X509_FORMAT_DER )
990 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992#if defined(MBEDTLS_PEM_PARSE_C)
993 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994 {
995 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200998 /* 1 rather than 0 since the terminating NULL byte is counted in */
999 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000 {
1001 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001004 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006 "-----BEGIN CERTIFICATE-----",
1007 "-----END CERTIFICATE-----",
1008 buf, NULL, 0, &use_len );
1009
1010 if( ret == 0 )
1011 {
1012 /*
1013 * Was PEM encoded
1014 */
1015 buflen -= use_len;
1016 buf += use_len;
1017 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 {
1020 return( ret );
1021 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025
1026 /*
1027 * PEM header and footer were found
1028 */
1029 buflen -= use_len;
1030 buf += use_len;
1031
1032 if( first_error == 0 )
1033 first_error = ret;
1034
Paul Bakker5a5fa922014-09-26 14:53:04 +02001035 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036 continue;
1037 }
1038 else
1039 break;
1040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044
1045 if( ret != 0 )
1046 {
1047 /*
1048 * Quit parsing on a memory error
1049 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001050 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051 return( ret );
1052
1053 if( first_error == 0 )
1054 first_error = ret;
1055
1056 total_failed++;
1057 continue;
1058 }
1059
1060 success = 1;
1061 }
1062 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001064
1065 if( success )
1066 return( total_failed );
1067 else if( first_error )
1068 return( first_error );
1069 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071}
1072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074/*
1075 * Load one or more certificates and add them to the chained list
1076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078{
1079 int ret;
1080 size_t n;
1081 unsigned char *buf;
1082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001084 return( ret );
1085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001087
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001088 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090
1091 return( ret );
1092}
1093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095{
1096 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001097#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 int w_ret;
1099 WCHAR szDir[MAX_PATH];
1100 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001101 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001102 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103
Paul Bakker9af723c2014-05-01 13:03:14 +02001104 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105 HANDLE hFind;
1106
1107 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109
Paul Bakker9af723c2014-05-01 13:03:14 +02001110 memset( szDir, 0, sizeof(szDir) );
1111 memset( filename, 0, MAX_PATH );
1112 memcpy( filename, path, len );
1113 filename[len++] = '\\';
1114 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001115 filename[len++] = '*';
1116
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001117 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
1118 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001119 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121
1122 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001123 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001125
1126 len = MAX_PATH - len;
1127 do
1128 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001129 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001130
1131 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1132 continue;
1133
Paul Bakker9af723c2014-05-01 13:03:14 +02001134 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001135 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001136 p, len - 1,
1137 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001138 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142 if( w_ret < 0 )
1143 ret++;
1144 else
1145 ret += w_ret;
1146 }
1147 while( FindNextFileW( hFind, &file_data ) != 0 );
1148
Paul Bakker66d5d072014-06-17 16:39:18 +02001149 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001151
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001153#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001154 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001156 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 char entry_name[255];
1158 DIR *dir = opendir( path );
1159
Paul Bakker66d5d072014-06-17 16:39:18 +02001160 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001164 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001165 {
1166 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001167 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001168 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001169#endif
1170
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001171 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 mbedtls_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001175 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176 {
1177 closedir( dir );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001179 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 }
1181
1182 if( !S_ISREG( sb.st_mode ) )
1183 continue;
1184
1185 // Ignore parse errors
1186 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001188 if( t_ret < 0 )
1189 ret++;
1190 else
1191 ret += t_ret;
1192 }
1193 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001194
1195cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001197 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001199#endif
1200
Paul Bakkerbe089b02013-10-14 15:51:50 +02001201#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001202
1203 return( ret );
1204}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001207static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001209{
1210 size_t i;
1211 size_t n = *size;
1212 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001214 const char *sep = "";
1215 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001216
1217 while( cur != NULL )
1218 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001219 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001220 {
1221 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001222 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001223 }
1224
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001225 n -= cur->buf.len + sep_len;
1226 for( i = 0; i < sep_len; i++ )
1227 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001228 for( i = 0; i < cur->buf.len; i++ )
1229 *p++ = cur->buf.p[i];
1230
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001231 sep = ", ";
1232 sep_len = 2;
1233
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001234 cur = cur->next;
1235 }
1236
1237 *p = '\0';
1238
1239 *size = n;
1240 *buf = p;
1241
1242 return( 0 );
1243}
1244
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001245#define PRINT_ITEM(i) \
1246 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001248 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001249 sep = ", "; \
1250 }
1251
1252#define CERT_TYPE(type,name) \
1253 if( ns_cert_type & type ) \
1254 PRINT_ITEM( name );
1255
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001256static int x509_info_cert_type( char **buf, size_t *size,
1257 unsigned char ns_cert_type )
1258{
1259 int ret;
1260 size_t n = *size;
1261 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001262 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001265 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1267 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1268 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001269 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1270 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1271 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001272
1273 *size = n;
1274 *buf = p;
1275
1276 return( 0 );
1277}
1278
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001279#define KEY_USAGE(code,name) \
1280 if( key_usage & code ) \
1281 PRINT_ITEM( name );
1282
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001283static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001284 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001285{
1286 int ret;
1287 size_t n = *size;
1288 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001289 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1292 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001293 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1294 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1295 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1297 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001298 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1299 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001300
1301 *size = n;
1302 *buf = p;
1303
1304 return( 0 );
1305}
1306
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001307static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001309{
1310 int ret;
1311 const char *desc;
1312 size_t n = *size;
1313 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001315 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001316
1317 while( cur != NULL )
1318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001320 desc = "???";
1321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001323 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001324
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001325 sep = ", ";
1326
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001327 cur = cur->next;
1328 }
1329
1330 *size = n;
1331 *buf = p;
1332
1333 return( 0 );
1334}
1335
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001336/*
1337 * Return an informational string about the certificate.
1338 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001339#define BEFORE_COLON 18
1340#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1342 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343{
1344 int ret;
1345 size_t n;
1346 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001347 char key_size_str[BEFORE_COLON];
1348
1349 p = buf;
1350 n = size;
1351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001354 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001356 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001357 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001360 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001363 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001365 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001368 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001370 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1374 crt->valid_from.year, crt->valid_from.mon,
1375 crt->valid_from.day, crt->valid_from.hour,
1376 crt->valid_from.min, crt->valid_from.sec );
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%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1381 crt->valid_to.year, crt->valid_to.mon,
1382 crt->valid_to.day, crt->valid_to.hour,
1383 crt->valid_to.min, crt->valid_to.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%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001387 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001390 crt->sig_md, crt->sig_opts );
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é-Gonnardb28487d2014-04-01 12:19:09 +02001393 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1395 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001396 {
1397 return( ret );
1398 }
1399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001401 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001402 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001403
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001404 /*
1405 * Optional extensions
1406 */
1407
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001408 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001411 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001412 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001413
1414 if( crt->max_pathlen > 0 )
1415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001417 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001418 }
1419 }
1420
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001421 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001424 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001425
1426 if( ( ret = x509_info_subject_alt_name( &p, &n,
1427 &crt->subject_alt_names ) ) != 0 )
1428 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001429 }
1430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001434 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001435
1436 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1437 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001438 }
1439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001443 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001444
1445 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1446 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001447 }
1448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001452 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001453
1454 if( ( ret = x509_info_ext_key_usage( &p, &n,
1455 &crt->ext_key_usage ) ) != 0 )
1456 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001457 }
1458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001460 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001461
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001462 return( (int) ( size - n ) );
1463}
1464
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001465struct x509_crt_verify_string {
1466 int code;
1467 const char *string;
1468};
1469
1470static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001471 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001472 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1473 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1474 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1475 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1476 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001477 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1478 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1479 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001480 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001481 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1482 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1483 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1484 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001485 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1486 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1487 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1488 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1489 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1490 { 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 +01001491 { 0, NULL }
1492};
1493
1494int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001495 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001496{
1497 int ret;
1498 const struct x509_crt_verify_string *cur;
1499 char *p = buf;
1500 size_t n = size;
1501
1502 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1503 {
1504 if( ( flags & cur->code ) == 0 )
1505 continue;
1506
1507 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001508 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001509 flags ^= cur->code;
1510 }
1511
1512 if( flags != 0 )
1513 {
1514 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1515 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001516 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001517 }
1518
1519 return( (int) ( size - n ) );
1520}
1521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001523int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1524 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001525{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001526 unsigned int usage_must, usage_may;
1527 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1528 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1529
1530 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1531 return( 0 );
1532
1533 usage_must = usage & ~may_mask;
1534
1535 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1536 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1537
1538 usage_may = usage & may_mask;
1539
1540 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001542
1543 return( 0 );
1544}
1545#endif
1546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1548int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001549 const char *usage_oid,
1550 size_t usage_len )
1551{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001553
1554 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001556 return( 0 );
1557
1558 /*
1559 * Look for the requested usage (or wildcard ANY) in our list
1560 */
1561 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001564
1565 if( cur_oid->len == usage_len &&
1566 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1567 {
1568 return( 0 );
1569 }
1570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001572 return( 0 );
1573 }
1574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001576}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580/*
1581 * Return 1 if the certificate is revoked, or 0 otherwise.
1582 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001583int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001584{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001586
1587 while( cur != NULL && cur->serial.len != 0 )
1588 {
1589 if( crt->serial.len == cur->serial.len &&
1590 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1591 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001592 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593 return( 1 );
1594 }
1595
1596 cur = cur->next;
1597 }
1598
1599 return( 0 );
1600}
1601
1602/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001603 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001606 mbedtls_x509_crl *crl_list,
1607 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001608{
1609 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1611 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612
1613 if( ca == NULL )
1614 return( flags );
1615
1616 /*
1617 * TODO: What happens if no CRL is present?
1618 * Suggestion: Revocation state should be unknown if no CRL is present.
1619 * For backwards compatibility this is not yet implemented.
1620 */
1621
1622 while( crl_list != NULL )
1623 {
1624 if( crl_list->version == 0 ||
1625 crl_list->issuer_raw.len != ca->subject_raw.len ||
1626 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1627 crl_list->issuer_raw.len ) != 0 )
1628 {
1629 crl_list = crl_list->next;
1630 continue;
1631 }
1632
1633 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001634 * Check if the CA is configured to sign CRLs
1635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1637 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001640 break;
1641 }
1642#endif
1643
1644 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001645 * Check if CRL is correctly signed by the trusted CA
1646 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001647 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1648 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1649
1650 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1651 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001654 if( md_info == NULL )
1655 {
1656 /*
1657 * Cannot check 'unknown' hash
1658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660 break;
1661 }
1662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001664
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001665 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1666 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1669 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001670 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001673 break;
1674 }
1675
1676 /*
1677 * Check for validity of CRL (Do not drop out)
1678 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001679 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001681
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001682 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001683 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001684
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001685 /*
1686 * Check if certificate is revoked
1687 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001688 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001691 break;
1692 }
1693
1694 crl_list = crl_list->next;
1695 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001696
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001697 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001698}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001700
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001701/*
1702 * Like memcmp, but case-insensitive and always returns -1 if different
1703 */
1704static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705{
1706 size_t i;
1707 unsigned char diff;
1708 const unsigned char *n1 = s1, *n2 = s2;
1709
1710 for( i = 0; i < len; i++ )
1711 {
1712 diff = n1[i] ^ n2[i];
1713
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001714 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001715 continue;
1716
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001717 if( diff == 32 &&
1718 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1719 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1720 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001722 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001723
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001724 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001725 }
1726
1727 return( 0 );
1728}
1729
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001730/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001731 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001732 */
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001733static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001734{
1735 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001736 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001737
1738 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1739 return( 0 );
1740
Paul Bakker14b16c62014-05-28 11:33:54 +02001741 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001742 {
1743 if( cn[i] == '.' )
1744 {
1745 cn_idx = i;
1746 break;
1747 }
1748 }
1749
1750 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001751 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001752
Paul Bakker14b16c62014-05-28 11:33:54 +02001753 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001754 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001755 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001756 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001757 }
1758
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001759 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001760}
1761
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001762/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001763 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1764 * variations (but not all).
1765 *
1766 * Return 0 if equal, -1 otherwise.
1767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001769{
1770 if( a->tag == b->tag &&
1771 a->len == b->len &&
1772 memcmp( a->p, b->p, b->len ) == 0 )
1773 {
1774 return( 0 );
1775 }
1776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1778 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001779 a->len == b->len &&
1780 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1781 {
1782 return( 0 );
1783 }
1784
1785 return( -1 );
1786}
1787
1788/*
1789 * Compare two X.509 Names (aka rdnSequence).
1790 *
1791 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1792 * we sometimes return unequal when the full algorithm would return equal,
1793 * but never the other way. (In particular, we don't do Unicode normalisation
1794 * or space folding.)
1795 *
1796 * Return 0 if equal, -1 otherwise.
1797 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001799{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001800 /* Avoid recursion, it might not be optimised by the compiler */
1801 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001802 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001803 if( a == NULL || b == NULL )
1804 return( -1 );
1805
1806 /* type */
1807 if( a->oid.tag != b->oid.tag ||
1808 a->oid.len != b->oid.len ||
1809 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1810 {
1811 return( -1 );
1812 }
1813
1814 /* value */
1815 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1816 return( -1 );
1817
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001818 /* structure of the list of sets */
1819 if( a->next_merged != b->next_merged )
1820 return( -1 );
1821
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001822 a = a->next;
1823 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001824 }
1825
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001826 /* a == NULL == b */
1827 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001828}
1829
1830/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001831 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1832 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001833 *
1834 * top means parent is a locally-trusted certificate
1835 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1838 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001839 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001840{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001841 int need_ca_bit;
1842
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001843 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001844 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001845 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001846
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001847 /* Parent must have the basicConstraints CA bit set as a general rule */
1848 need_ca_bit = 1;
1849
1850 /* Exception: v1/v2 certificates that are locally trusted. */
1851 if( top && parent->version < 3 )
1852 need_ca_bit = 0;
1853
1854 /* Exception: self-signed end-entity certs that are locally trusted. */
1855 if( top && bottom &&
1856 child->raw.len == parent->raw.len &&
1857 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1858 {
1859 need_ca_bit = 0;
1860 }
1861
1862 if( need_ca_bit && ! parent->ca_istrue )
1863 return( -1 );
1864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001866 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001868 {
1869 return( -1 );
1870 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001871#endif
1872
1873 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001874}
1875
Paul Bakkerddf26b42013-09-18 13:46:23 +02001876static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001878 mbedtls_x509_crl *ca_crl,
1879 const mbedtls_x509_crt_profile *profile,
1880 int path_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001881 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001882 void *p_vrfy )
1883{
1884 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001885 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001886 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1888 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001889
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001890 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001891 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001892
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001893 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001895
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001896 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
1897 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
1898
1899 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
1900 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
1901
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001902 /*
1903 * Child is the top of the chain. Check against the trust_ca list.
1904 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001908 if( md_info == NULL )
1909 {
1910 /*
1911 * Cannot check 'unknown', no need to try any CA
1912 */
1913 trust_ca = NULL;
1914 }
1915 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001917
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001918 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001919 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001920 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001921 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001922
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001923 check_path_cnt = path_cnt + 1;
1924
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001925 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001926 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001927 * the same as the trusted CA
1928 */
1929 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1930 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1931 child->issuer_raw.len ) == 0 )
1932 {
1933 check_path_cnt--;
1934 }
1935
1936 if( trust_ca->max_pathlen > 0 &&
1937 trust_ca->max_pathlen < check_path_cnt )
1938 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001939 continue;
1940 }
1941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1943 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001944 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001945 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001946 continue;
1947 }
1948
1949 /*
1950 * Top of chain is signed by a trusted CA
1951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardfa67eba2015-06-27 14:41:38 +02001953
1954 if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
1955 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
1956
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001957 break;
1958 }
1959
1960 /*
1961 * If top of chain is not the same as the trusted CA send a verify request
1962 * to the callback for any issues with validity and CRL presence for the
1963 * trusted CA certificate.
1964 */
1965 if( trust_ca != NULL &&
1966 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1967 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1968 child->issuer_raw.len ) != 0 ) )
1969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001971 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001972 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001973#else
1974 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001975#endif
1976
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001977 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001978 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001979
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001980 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001982
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001983 if( NULL != f_vrfy )
1984 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001985 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1986 &ca_flags ) ) != 0 )
1987 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001988 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001989 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001990 }
1991 }
1992
1993 /* Call callback on top cert */
1994 if( NULL != f_vrfy )
1995 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001996 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001997 return( ret );
1998 }
1999
2000 *flags |= ca_flags;
2001
2002 return( 0 );
2003}
2004
Paul Bakkerddf26b42013-09-18 13:46:23 +02002005static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002006 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
2007 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
2008 const mbedtls_x509_crt_profile *profile,
2009 int path_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002010 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002011 void *p_vrfy )
2012{
2013 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002014 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2016 mbedtls_x509_crt *grandparent;
2017 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002018
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002019 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2023 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002024 }
2025
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002026 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002027 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02002028
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002029 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002031
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002032 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2033 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2034
2035 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2036 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002039 if( md_info == NULL )
2040 {
2041 /*
2042 * Cannot check 'unknown' hash
2043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002045 }
2046 else
2047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002049
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002050 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2051 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2054 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002055 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002058 }
2059 }
2060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002062 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002063 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002064#endif
2065
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002066 /* Look for a grandparent in trusted CAs */
2067 for( grandparent = trust_ca;
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002068 grandparent != NULL;
2069 grandparent = grandparent->next )
2070 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002071 if( x509_crt_check_parent( parent, grandparent,
2072 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002073 break;
2074 }
2075
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002076 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002077 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002078 ret = x509_crt_verify_top( parent, grandparent, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002079 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080 if( ret != 0 )
2081 return( ret );
2082 }
2083 else
2084 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002085 /* Look for a grandparent upwards the chain */
2086 for( grandparent = parent->next;
2087 grandparent != NULL;
2088 grandparent = grandparent->next )
2089 {
2090 if( x509_crt_check_parent( parent, grandparent,
2091 0, path_cnt == 0 ) == 0 )
2092 break;
2093 }
2094
2095 /* Is our parent part of the chain or at the top? */
2096 if( grandparent != NULL )
2097 {
2098 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
2099 profile, path_cnt + 1, &parent_flags,
2100 f_vrfy, p_vrfy );
2101 if( ret != 0 )
2102 return( ret );
2103 }
2104 else
2105 {
2106 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
2107 path_cnt + 1, &parent_flags,
2108 f_vrfy, p_vrfy );
2109 if( ret != 0 )
2110 return( ret );
2111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112 }
2113
2114 /* child is verified to be a child of the parent, call verify callback */
2115 if( NULL != f_vrfy )
2116 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2117 return( ret );
2118
2119 *flags |= parent_flags;
2120
2121 return( 0 );
2122}
2123
2124/*
2125 * Verify the certificate validity
2126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2128 mbedtls_x509_crt *trust_ca,
2129 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002130 const char *cn, uint32_t *flags,
2131 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002132 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002133{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002134 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002135 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002136}
2137
2138
2139/*
2140 * Verify the certificate validity, with profile
2141 */
2142int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2143 mbedtls_x509_crt *trust_ca,
2144 mbedtls_x509_crl *ca_crl,
2145 const mbedtls_x509_crt_profile *profile,
2146 const char *cn, uint32_t *flags,
2147 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2148 void *p_vrfy )
2149{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002150 size_t cn_len;
2151 int ret;
2152 int pathlen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 mbedtls_x509_crt *parent;
2154 mbedtls_x509_name *name;
2155 mbedtls_x509_sequence *cur = NULL;
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +02002156 mbedtls_pk_type_t pk_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002157
Manuel Pégourié-Gonnarda83e4e22015-06-17 11:53:48 +02002158 if( profile == NULL )
2159 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2160
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002161 *flags = 0;
2162
2163 if( cn != NULL )
2164 {
2165 name = &crt->subject;
2166 cn_len = strlen( cn );
2167
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002168 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002169 {
2170 cur = &crt->subject_alt_names;
2171
2172 while( cur != NULL )
2173 {
2174 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002175 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002176 break;
2177
2178 if( cur->buf.len > 2 &&
2179 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002180 x509_check_wildcard( cn, &cur->buf ) == 0 )
2181 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002182 break;
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002183 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002184
2185 cur = cur->next;
2186 }
2187
2188 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002190 }
2191 else
2192 {
2193 while( name != NULL )
2194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002196 {
2197 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002198 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002199 break;
2200
2201 if( name->val.len > 2 &&
2202 memcmp( name->val.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002203 x509_check_wildcard( cn, &name->val ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002204 break;
2205 }
2206
2207 name = name->next;
2208 }
2209
2210 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002212 }
2213 }
2214
Manuel Pégourié-Gonnard93080df2015-10-23 14:08:48 +02002215 /* Check the type and size of the key */
2216 pk_type = mbedtls_pk_get_type( &crt->pk );
2217
2218 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
2219 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2220
2221 if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
2222 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2223
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002224 /* Look for a parent in trusted CAs */
2225 for( parent = trust_ca; parent != NULL; parent = parent->next )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002226 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002227 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002228 break;
2229 }
2230
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002231 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002232 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002233 ret = x509_crt_verify_top( crt, parent, ca_crl, profile,
2234 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002235 if( ret != 0 )
2236 return( ret );
2237 }
2238 else
2239 {
Manuel Pégourié-Gonnardfdbdd722015-09-01 16:35:00 +02002240 /* Look for a parent upwards the chain */
2241 for( parent = crt->next; parent != NULL; parent = parent->next )
2242 {
2243 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
2244 break;
2245 }
2246
2247 /* Are we part of the chain or at the top? */
2248 if( parent != NULL )
2249 {
2250 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
2251 pathlen, flags, f_vrfy, p_vrfy );
2252 if( ret != 0 )
2253 return( ret );
2254 }
2255 else
2256 {
2257 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
2258 pathlen, flags, f_vrfy, p_vrfy );
2259 if( ret != 0 )
2260 return( ret );
2261 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002262 }
2263
2264 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002266
2267 return( 0 );
2268}
2269
2270/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002271 * Initialize a certificate chain
2272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002274{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002276}
2277
2278/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002279 * Unallocate all certificate data
2280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002282{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 mbedtls_x509_crt *cert_cur = crt;
2284 mbedtls_x509_crt *cert_prv;
2285 mbedtls_x509_name *name_cur;
2286 mbedtls_x509_name *name_prv;
2287 mbedtls_x509_sequence *seq_cur;
2288 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002289
2290 if( crt == NULL )
2291 return;
2292
2293 do
2294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2298 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002299#endif
2300
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002301 name_cur = cert_cur->issuer.next;
2302 while( name_cur != NULL )
2303 {
2304 name_prv = name_cur;
2305 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2307 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002308 }
2309
2310 name_cur = cert_cur->subject.next;
2311 while( name_cur != NULL )
2312 {
2313 name_prv = name_cur;
2314 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2316 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002317 }
2318
2319 seq_cur = cert_cur->ext_key_usage.next;
2320 while( seq_cur != NULL )
2321 {
2322 seq_prv = seq_cur;
2323 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2325 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002326 }
2327
2328 seq_cur = cert_cur->subject_alt_names.next;
2329 while( seq_cur != NULL )
2330 {
2331 seq_prv = seq_cur;
2332 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2334 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002335 }
2336
2337 if( cert_cur->raw.p != NULL )
2338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2340 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002341 }
2342
2343 cert_cur = cert_cur->next;
2344 }
2345 while( cert_cur != NULL );
2346
2347 cert_cur = crt;
2348 do
2349 {
2350 cert_prv = cert_cur;
2351 cert_cur = cert_cur->next;
2352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002354 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002356 }
2357 while( cert_cur != NULL );
2358}
2359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360#endif /* MBEDTLS_X509_CRT_PARSE_C */