blob: 329b7e296bc004f0264abb0bb4f862e4bcafb5c3 [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
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047 */
48/*
49 * The ITU-T X.509 standard defines a certificate format for PKI.
50 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020051 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
52 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
53 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054 *
55 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
56 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020057 *
58 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059 */
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020063#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020065#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/x509_crt.h"
70#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050071#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000072
Rich Evans00ab4702015-02-06 13:43:58 +000073#include <string.h>
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000076#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#endif
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081#else
Simon Butcherd2642582018-10-03 15:11:19 +010082#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000083#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020085#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087#endif
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000090#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010091#endif
92
Paul Bakkerfa6a6202013-10-28 18:48:30 +010093#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094#include <windows.h>
95#else
96#include <time.h>
97#endif
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +0000100#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +0200101#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102#include <sys/types.h>
103#include <sys/stat.h>
104#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +0000105#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200106#endif
107
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +0200108/*
109 * Item in a verification chain: cert and flags for it
110 */
111typedef struct {
112 mbedtls_x509_crt *crt;
113 uint32_t flags;
114} x509_crt_verify_chain_item;
115
116/*
117 * Max size of verification chain: end-entity + intermediates + trusted root
118 */
119#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200120
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200121/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200122 * Default profile
123 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200124const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
125{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200126#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200127 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200128 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200129#endif
130 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200131 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
135 0xFFFFFFF, /* Any PK alg */
136 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200137 2048,
138};
139
140/*
141 * Next-default profile
142 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200143const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
144{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200145 /* Hashes from SHA-256 and above */
146 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
147 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
148 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
149 0xFFFFFFF, /* Any PK alg */
150#if defined(MBEDTLS_ECP_C)
151 /* Curves at or above 128-bit security level */
152 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
153 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
154 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
155 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
156 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
157 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
158 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
159#else
160 0,
161#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200162 2048,
163};
164
165/*
166 * NSA Suite B Profile
167 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200168const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
169{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200170 /* Only SHA-256 and 384 */
171 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
172 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
173 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200174 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
175 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200176#if defined(MBEDTLS_ECP_C)
177 /* Only NIST P-256 and P-384 */
178 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
179 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
180#else
181 0,
182#endif
183 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200184};
185
186/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200187 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200188 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200189 */
190static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
191 mbedtls_md_type_t md_alg )
192{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200193 if( md_alg == MBEDTLS_MD_NONE )
194 return( -1 );
195
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200196 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
197 return( 0 );
198
199 return( -1 );
200}
201
202/*
203 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200204 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200205 */
206static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
207 mbedtls_pk_type_t pk_alg )
208{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200209 if( pk_alg == MBEDTLS_PK_NONE )
210 return( -1 );
211
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200212 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
213 return( 0 );
214
215 return( -1 );
216}
217
218/*
219 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200220 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200221 */
222static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200223 const mbedtls_pk_context *pk )
224{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200225 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200226
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200227#if defined(MBEDTLS_RSA_C)
228 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
229 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200230 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200231 return( 0 );
232
233 return( -1 );
234 }
235#endif
236
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200237#if defined(MBEDTLS_ECP_C)
238 if( pk_alg == MBEDTLS_PK_ECDSA ||
239 pk_alg == MBEDTLS_PK_ECKEY ||
240 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200241 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200242 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200243
Philippe Antoineb5b25432018-05-11 11:06:29 +0200244 if( gid == MBEDTLS_ECP_DP_NONE )
245 return( -1 );
246
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200247 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
248 return( 0 );
249
250 return( -1 );
251 }
252#endif
253
254 return( -1 );
255}
256
257/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000258 * Like memcmp, but case-insensitive and always returns -1 if different
259 */
260static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
261{
262 size_t i;
263 unsigned char diff;
264 const unsigned char *n1 = s1, *n2 = s2;
265
266 for( i = 0; i < len; i++ )
267 {
268 diff = n1[i] ^ n2[i];
269
270 if( diff == 0 )
271 continue;
272
273 if( diff == 32 &&
274 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
275 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
276 {
277 continue;
278 }
279
280 return( -1 );
281 }
282
283 return( 0 );
284}
285
286/*
287 * Return 0 if name matches wildcard, -1 otherwise
288 */
289static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
290{
291 size_t i;
292 size_t cn_idx = 0, cn_len = strlen( cn );
293
294 /* We can't have a match if there is no wildcard to match */
295 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
296 return( -1 );
297
298 for( i = 0; i < cn_len; ++i )
299 {
300 if( cn[i] == '.' )
301 {
302 cn_idx = i;
303 break;
304 }
305 }
306
307 if( cn_idx == 0 )
308 return( -1 );
309
310 if( cn_len - cn_idx == name->len - 1 &&
311 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
312 {
313 return( 0 );
314 }
315
316 return( -1 );
317}
318
319/*
320 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
321 * variations (but not all).
322 *
323 * Return 0 if equal, -1 otherwise.
324 */
325static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
326{
327 if( a->tag == b->tag &&
328 a->len == b->len &&
329 memcmp( a->p, b->p, b->len ) == 0 )
330 {
331 return( 0 );
332 }
333
334 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
335 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
336 a->len == b->len &&
337 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
338 {
339 return( 0 );
340 }
341
342 return( -1 );
343}
344
345/*
346 * Compare two X.509 Names (aka rdnSequence).
347 *
348 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
349 * we sometimes return unequal when the full algorithm would return equal,
350 * but never the other way. (In particular, we don't do Unicode normalisation
351 * or space folding.)
352 *
353 * Return 0 if equal, -1 otherwise.
354 */
355static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
356{
357 /* Avoid recursion, it might not be optimised by the compiler */
358 while( a != NULL || b != NULL )
359 {
360 if( a == NULL || b == NULL )
361 return( -1 );
362
363 /* type */
364 if( a->oid.tag != b->oid.tag ||
365 a->oid.len != b->oid.len ||
366 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
367 {
368 return( -1 );
369 }
370
371 /* value */
372 if( x509_string_cmp( &a->val, &b->val ) != 0 )
373 return( -1 );
374
375 /* structure of the list of sets */
376 if( a->next_merged != b->next_merged )
377 return( -1 );
378
379 a = a->next;
380 b = b->next;
381 }
382
383 /* a == NULL == b */
384 return( 0 );
385}
386
387/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200388 * Reset (init or clear) a verify_chain
389 */
390static void x509_crt_verify_chain_reset(
391 mbedtls_x509_crt_verify_chain *ver_chain )
392{
393 size_t i;
394
395 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
396 {
397 ver_chain->items[i].crt = NULL;
Hanno Beckerd6ddcd62019-01-10 09:19:26 +0000398 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200399 }
400
401 ver_chain->len = 0;
402}
403
404/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
406 */
407static int x509_get_version( unsigned char **p,
408 const unsigned char *end,
409 int *ver )
410{
411 int ret;
412 size_t len;
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
415 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 {
419 *ver = 0;
420 return( 0 );
421 }
422
Hanno Becker2f472142019-02-12 11:52:10 +0000423 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424 }
425
426 end = *p + len;
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
429 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430
431 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 return( MBEDTLS_ERR_X509_INVALID_VERSION +
433 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434
435 return( 0 );
436}
437
438/*
439 * Validity ::= SEQUENCE {
440 * notBefore Time,
441 * notAfter Time }
442 */
443static int x509_get_dates( unsigned char **p,
444 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_x509_time *from,
446 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447{
448 int ret;
449 size_t len;
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
452 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
453 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454
455 end = *p + len;
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458 return( ret );
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461 return( ret );
462
463 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 return( MBEDTLS_ERR_X509_INVALID_DATE +
465 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
467 return( 0 );
468}
469
470/*
471 * X.509 v2/v3 unique identifier (not parsed)
472 */
473static int x509_get_uid( unsigned char **p,
474 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476{
477 int ret;
478
479 if( *p == end )
480 return( 0 );
481
482 uid->tag = **p;
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
485 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488 return( 0 );
489
Hanno Becker2f472142019-02-12 11:52:10 +0000490 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 }
492
493 uid->p = *p;
494 *p += uid->len;
495
496 return( 0 );
497}
498
499static int x509_get_basic_constraints( unsigned char **p,
500 const unsigned char *end,
501 int *ca_istrue,
502 int *max_pathlen )
503{
504 int ret;
505 size_t len;
506
507 /*
508 * BasicConstraints ::= SEQUENCE {
509 * cA BOOLEAN DEFAULT FALSE,
510 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
511 */
512 *ca_istrue = 0; /* DEFAULT FALSE */
513 *max_pathlen = 0; /* endless */
514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
516 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
517 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
519 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200520 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
525 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526
527 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529
530 if( *ca_istrue != 0 )
531 *ca_istrue = 1;
532 }
533
534 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200535 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
538 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539
540 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
542 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543
Andrzej Kurekacf7f2c2020-04-14 09:49:52 -0400544 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
545 * overflow, which is an undefined behavior. */
546 if( *max_pathlen == INT_MAX )
547 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
548 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
549
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550 (*max_pathlen)++;
551
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200552 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553}
554
555static int x509_get_ns_cert_type( unsigned char **p,
556 const unsigned char *end,
557 unsigned char *ns_cert_type)
558{
559 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
563 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564
565 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
567 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568
569 /* Get actual bitstring */
570 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200571 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572}
573
574static int x509_get_key_usage( unsigned char **p,
575 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100576 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577{
578 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200579 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
583 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584
585 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
587 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588
589 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200590 *key_usage = 0;
591 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
592 {
593 *key_usage |= (unsigned int) bs.p[i] << (8*i);
594 }
595
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200596 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597}
598
599/*
600 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
601 *
602 * KeyPurposeId ::= OBJECT IDENTIFIER
603 */
604static int x509_get_ext_key_usage( unsigned char **p,
605 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200607{
608 int ret;
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
611 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612
613 /* Sequence length must be >= 1 */
614 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
616 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200618 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619}
620
621/*
622 * SubjectAltName ::= GeneralNames
623 *
624 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
625 *
626 * GeneralName ::= CHOICE {
627 * otherName [0] OtherName,
628 * rfc822Name [1] IA5String,
629 * dNSName [2] IA5String,
630 * x400Address [3] ORAddress,
631 * directoryName [4] Name,
632 * ediPartyName [5] EDIPartyName,
633 * uniformResourceIdentifier [6] IA5String,
634 * iPAddress [7] OCTET STRING,
635 * registeredID [8] OBJECT IDENTIFIER }
636 *
637 * OtherName ::= SEQUENCE {
638 * type-id OBJECT IDENTIFIER,
639 * value [0] EXPLICIT ANY DEFINED BY type-id }
640 *
641 * EDIPartyName ::= SEQUENCE {
642 * nameAssigner [0] DirectoryString OPTIONAL,
643 * partyName [1] DirectoryString }
644 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000645 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646 */
647static int x509_get_subject_alt_name( unsigned char **p,
648 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650{
651 int ret;
652 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656
657 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
659 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
660 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661
662 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
664 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665
666 while( *p < end )
667 {
668 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
670 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671
672 tag = **p;
673 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
675 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100677 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
678 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
681 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100682 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200684 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686 {
687 *p += tag_len;
688 continue;
689 }
690
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200692 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100694 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100696
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200697 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698
699 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200701 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703 cur = cur->next;
704 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200705
706 buf = &(cur->buf);
707 buf->tag = tag;
708 buf->p = *p;
709 buf->len = tag_len;
710 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711 }
712
713 /* Set final sequence entry's next pointer to NULL */
714 cur->next = NULL;
715
716 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
718 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719
720 return( 0 );
721}
722
723/*
724 * X.509 v3 extensions
725 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726 */
727static int x509_get_crt_ext( unsigned char **p,
728 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730{
731 int ret;
732 size_t len;
733 unsigned char *end_ext_data, *end_ext_octet;
734
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000735 if( *p == end )
736 return( 0 );
737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000741 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200742 while( *p < end )
743 {
744 /*
745 * Extension ::= SEQUENCE {
746 * extnID OBJECT IDENTIFIER,
747 * critical BOOLEAN DEFAULT FALSE,
748 * extnValue OCTET STRING }
749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200751 int is_critical = 0; /* DEFAULT FALSE */
752 int ext_type = 0;
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
755 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
756 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757
758 end_ext_data = *p + len;
759
760 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200761 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200762 MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764
k-stachowiak463928a2018-07-24 12:50:59 +0200765 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766 extn_oid.p = *p;
767 *p += extn_oid.len;
768
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
771 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
772 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773
774 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
776 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
777 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200778
779 end_ext_octet = *p + len;
780
781 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
783 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784
785 /*
786 * Detect supported extensions
787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789
790 if( ret != 0 )
791 {
792 /* No parser found, skip extension */
793 *p = end_ext_octet;
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796 if( is_critical )
797 {
798 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
800 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801 }
802#endif
803 continue;
804 }
805
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100806 /* Forbid repeated extensions */
807 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100809
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200810 crt->ext_types |= ext_type;
811
812 switch( ext_type )
813 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100814 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815 /* Parse basic constraints */
816 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
817 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200818 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200819 break;
820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200822 /* Parse key usage */
823 if( ( ret = x509_get_key_usage( p, end_ext_octet,
824 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200825 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826 break;
827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200829 /* Parse extended key usage */
830 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
831 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200832 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200833 break;
834
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100835 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836 /* Parse subject alt name */
837 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
838 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200839 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200840 break;
841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200843 /* Parse netscape certificate type */
844 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
845 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200846 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200847 break;
848
849 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851 }
852 }
853
854 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
856 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200857
858 return( 0 );
859}
860
861/*
862 * Parse and fill a single X.509 certificate in DER format
863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200865 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866{
867 int ret;
868 size_t len;
869 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
873 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
874 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200875
876 /*
877 * Check for valid input
878 */
879 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881
Janos Follathcc0e49d2016-02-17 14:34:12 +0000882 // Use the original buffer until we figure out actual length
883 p = (unsigned char*) buf;
884 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885 end = p + len;
886
887 /*
888 * Certificate ::= SEQUENCE {
889 * tbsCertificate TBSCertificate,
890 * signatureAlgorithm AlgorithmIdentifier,
891 * signatureValue BIT STRING }
892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
894 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_x509_crt_free( crt );
897 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 }
899
900 if( len > (size_t) ( end - p ) )
901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 mbedtls_x509_crt_free( crt );
903 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
904 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 }
906 crt_end = p + len;
907
Janos Follathcc0e49d2016-02-17 14:34:12 +0000908 // Create and populate a new buffer for the raw field
909 crt->raw.len = crt_end - buf;
910 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
911 if( p == NULL )
912 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
913
914 memcpy( p, buf, crt->raw.len );
915
Darryl Green11999bb2018-03-13 15:22:58 +0000916 // Direct pointers to the new buffer
Janos Follathcc0e49d2016-02-17 14:34:12 +0000917 p += crt->raw.len - len;
918 end = crt_end = p + len;
919
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920 /*
921 * TBSCertificate ::= SEQUENCE {
922 */
923 crt->tbs.p = p;
924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
926 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_x509_crt_free( crt );
929 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930 }
931
932 end = p + len;
933 crt->tbs.len = end - crt->tbs.p;
934
935 /*
936 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
937 *
938 * CertificateSerialNumber ::= INTEGER
939 *
940 * signature AlgorithmIdentifier
941 */
942 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
944 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200945 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948 return( ret );
949 }
950
Andres AG7ca4a032017-03-09 16:16:11 +0000951 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_x509_crt_free( crt );
954 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 }
956
Andres AG7ca4a032017-03-09 16:16:11 +0000957 crt->version++;
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200960 &crt->sig_md, &crt->sig_pk,
961 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964 return( ret );
965 }
966
967 /*
968 * issuer Name
969 */
970 crt->issuer_raw.p = p;
971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
973 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_x509_crt_free( crt );
976 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977 }
978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 return( ret );
983 }
984
985 crt->issuer_raw.len = p - crt->issuer_raw.p;
986
987 /*
988 * Validity ::= SEQUENCE {
989 * notBefore Time,
990 * notAfter Time }
991 *
992 */
993 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
994 &crt->valid_to ) ) != 0 )
995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 return( ret );
998 }
999
1000 /*
1001 * subject Name
1002 */
1003 crt->subject_raw.p = p;
1004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1006 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 mbedtls_x509_crt_free( crt );
1009 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 }
1011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015 return( ret );
1016 }
1017
1018 crt->subject_raw.len = p - crt->subject_raw.p;
1019
1020 /*
1021 * SubjectPublicKeyInfo
1022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 return( ret );
1027 }
1028
1029 /*
1030 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1031 * -- If present, version shall be v2 or v3
1032 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1033 * -- If present, version shall be v2 or v3
1034 * extensions [3] EXPLICIT Extensions OPTIONAL
1035 * -- If present, version shall be v3
1036 */
1037 if( crt->version == 2 || crt->version == 3 )
1038 {
1039 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1040 if( ret != 0 )
1041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 return( ret );
1044 }
1045 }
1046
1047 if( crt->version == 2 || crt->version == 3 )
1048 {
1049 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1050 if( ret != 0 )
1051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053 return( ret );
1054 }
1055 }
1056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001058 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001059#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001060 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001061 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062 if( ret != 0 )
1063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065 return( ret );
1066 }
1067 }
1068
1069 if( p != end )
1070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 mbedtls_x509_crt_free( crt );
1072 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1073 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074 }
1075
1076 end = crt_end;
1077
1078 /*
1079 * }
1080 * -- end of TBSCertificate
1081 *
1082 * signatureAlgorithm AlgorithmIdentifier,
1083 * signatureValue BIT STRING
1084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088 return( ret );
1089 }
1090
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001091 if( crt->sig_oid.len != sig_oid2.len ||
1092 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001093 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001094 ( sig_params1.len != 0 &&
1095 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 mbedtls_x509_crt_free( crt );
1098 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099 }
1100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104 return( ret );
1105 }
1106
1107 if( p != end )
1108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 mbedtls_x509_crt_free( crt );
1110 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1111 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112 }
1113
1114 return( 0 );
1115}
1116
1117/*
1118 * Parse one X.509 certificate in DER format from a buffer and add them to a
1119 * chained list
1120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001122 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123{
1124 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001126
1127 /*
1128 * Check for valid input
1129 */
1130 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132
1133 while( crt->version != 0 && crt->next != NULL )
1134 {
1135 prev = crt;
1136 crt = crt->next;
1137 }
1138
1139 /*
1140 * Add new certificate on the end of the chain if needed.
1141 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001142 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001143 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001144 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001145
1146 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001147 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148
1149 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001151 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152 }
1153
Paul Bakkerddf26b42013-09-18 13:46:23 +02001154 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155 {
1156 if( prev )
1157 prev->next = NULL;
1158
1159 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001161
1162 return( ret );
1163 }
1164
1165 return( 0 );
1166}
1167
1168/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001169 * Parse one or more PEM certificates from a buffer and add them to the chained
1170 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173{
Janos Follath98e28a72016-05-31 14:03:54 +01001174#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001175 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001177#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001178
1179 /*
1180 * Check for valid input
1181 */
1182 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001184
1185 /*
1186 * Determine buffer content. Buffer contains either one DER certificate or
1187 * one or more PEM certificates.
1188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001190 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001191 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001194 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1197 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001198#else
1199 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1200#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#if defined(MBEDTLS_PEM_PARSE_C)
1203 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001204 {
1205 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001207
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001208 /* 1 rather than 0 since the terminating NULL byte is counted in */
1209 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001210 {
1211 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001213
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001214 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001216 "-----BEGIN CERTIFICATE-----",
1217 "-----END CERTIFICATE-----",
1218 buf, NULL, 0, &use_len );
1219
1220 if( ret == 0 )
1221 {
1222 /*
1223 * Was PEM encoded
1224 */
1225 buflen -= use_len;
1226 buf += use_len;
1227 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229 {
1230 return( ret );
1231 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235
1236 /*
1237 * PEM header and footer were found
1238 */
1239 buflen -= use_len;
1240 buf += use_len;
1241
1242 if( first_error == 0 )
1243 first_error = ret;
1244
Paul Bakker5a5fa922014-09-26 14:53:04 +02001245 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001246 continue;
1247 }
1248 else
1249 break;
1250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001254
1255 if( ret != 0 )
1256 {
1257 /*
1258 * Quit parsing on a memory error
1259 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001260 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001261 return( ret );
1262
1263 if( first_error == 0 )
1264 first_error = ret;
1265
1266 total_failed++;
1267 continue;
1268 }
1269
1270 success = 1;
1271 }
1272 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273
1274 if( success )
1275 return( total_failed );
1276 else if( first_error )
1277 return( first_error );
1278 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001280#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281}
1282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001284/*
1285 * Load one or more certificates and add them to the chained list
1286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001288{
1289 int ret;
1290 size_t n;
1291 unsigned char *buf;
1292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001294 return( ret );
1295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001297
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001298 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001300
1301 return( ret );
1302}
1303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001305{
1306 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001307#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001308 int w_ret;
1309 WCHAR szDir[MAX_PATH];
1310 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001311 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001312 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001313
Paul Bakker9af723c2014-05-01 13:03:14 +02001314 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001315 HANDLE hFind;
1316
1317 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319
Paul Bakker9af723c2014-05-01 13:03:14 +02001320 memset( szDir, 0, sizeof(szDir) );
1321 memset( filename, 0, MAX_PATH );
1322 memcpy( filename, path, len );
1323 filename[len++] = '\\';
1324 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001325 filename[len++] = '*';
1326
Simon B3c6b18d2016-11-03 01:11:37 +00001327 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001328 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001329 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001331
1332 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001333 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001335
1336 len = MAX_PATH - len;
1337 do
1338 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001339 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001340
1341 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1342 continue;
1343
Paul Bakker9af723c2014-05-01 13:03:14 +02001344 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001345 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001346 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001347 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001348 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001349 {
1350 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1351 goto cleanup;
1352 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001355 if( w_ret < 0 )
1356 ret++;
1357 else
1358 ret += w_ret;
1359 }
1360 while( FindNextFileW( hFind, &file_data ) != 0 );
1361
Paul Bakker66d5d072014-06-17 16:39:18 +02001362 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364
Ron Eldor36d90422017-01-09 15:09:16 +02001365cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001367#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001368 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001369 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001371 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001372 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373 DIR *dir = opendir( path );
1374
Paul Bakker66d5d072014-06-17 16:39:18 +02001375 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001377
Ron Eldor63140682017-01-09 19:27:59 +02001378#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001379 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001380 {
1381 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001382 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001383 }
Ron Eldor63140682017-01-09 19:27:59 +02001384#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001385
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001386 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001387 {
Andres AGf9113192016-09-02 14:06:04 +01001388 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1389 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001390
Andres AGf9113192016-09-02 14:06:04 +01001391 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001392 {
Andres AGf9113192016-09-02 14:06:04 +01001393 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1394 goto cleanup;
1395 }
1396 else if( stat( entry_name, &sb ) == -1 )
1397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001399 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001400 }
1401
Janos Follathcabf0ee2020-02-04 14:42:15 +00001402 if( !S_ISREG( sb.st_mode ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001403 continue;
1404
1405 // Ignore parse errors
1406 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001408 if( t_ret < 0 )
1409 ret++;
1410 else
1411 ret += t_ret;
1412 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001413
1414cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001415 closedir( dir );
1416
Ron Eldor63140682017-01-09 19:27:59 +02001417#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001418 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001420#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001421
Paul Bakkerbe089b02013-10-14 15:51:50 +02001422#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423
1424 return( ret );
1425}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001428static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001430{
1431 size_t i;
1432 size_t n = *size;
1433 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001435 const char *sep = "";
1436 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001437
1438 while( cur != NULL )
1439 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001440 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001441 {
1442 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001443 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001444 }
1445
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001446 n -= cur->buf.len + sep_len;
1447 for( i = 0; i < sep_len; i++ )
1448 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001449 for( i = 0; i < cur->buf.len; i++ )
1450 *p++ = cur->buf.p[i];
1451
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001452 sep = ", ";
1453 sep_len = 2;
1454
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001455 cur = cur->next;
1456 }
1457
1458 *p = '\0';
1459
1460 *size = n;
1461 *buf = p;
1462
1463 return( 0 );
1464}
1465
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001466#define PRINT_ITEM(i) \
1467 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001469 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001470 sep = ", "; \
1471 }
1472
1473#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01001474 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001475 PRINT_ITEM( name );
1476
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001477static int x509_info_cert_type( char **buf, size_t *size,
1478 unsigned char ns_cert_type )
1479{
1480 int ret;
1481 size_t n = *size;
1482 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001483 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001486 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1488 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1489 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001490 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1491 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1492 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001493
1494 *size = n;
1495 *buf = p;
1496
1497 return( 0 );
1498}
1499
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001500#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01001501 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001502 PRINT_ITEM( name );
1503
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001504static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001505 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001506{
1507 int ret;
1508 size_t n = *size;
1509 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001510 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1513 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001514 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1515 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1516 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1518 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001519 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1520 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001521
1522 *size = n;
1523 *buf = p;
1524
1525 return( 0 );
1526}
1527
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001528static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001530{
1531 int ret;
1532 const char *desc;
1533 size_t n = *size;
1534 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001536 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001537
1538 while( cur != NULL )
1539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001541 desc = "???";
1542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001544 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001545
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001546 sep = ", ";
1547
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001548 cur = cur->next;
1549 }
1550
1551 *size = n;
1552 *buf = p;
1553
1554 return( 0 );
1555}
1556
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557/*
1558 * Return an informational string about the certificate.
1559 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001560#define BEFORE_COLON 18
1561#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1563 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001564{
1565 int ret;
1566 size_t n;
1567 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001568 char key_size_str[BEFORE_COLON];
1569
1570 p = buf;
1571 n = size;
1572
Janos Follath98e28a72016-05-31 14:03:54 +01001573 if( NULL == crt )
1574 {
1575 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
1576 MBEDTLS_X509_SAFE_SNPRINTF;
1577
1578 return( (int) ( size - n ) );
1579 }
1580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001583 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001586 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001589 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001592 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001594 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001597 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001599 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1603 crt->valid_from.year, crt->valid_from.mon,
1604 crt->valid_from.day, crt->valid_from.hour,
1605 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001606 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001609 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1610 crt->valid_to.year, crt->valid_to.mon,
1611 crt->valid_to.day, crt->valid_to.hour,
1612 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001613 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001616 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001619 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001620 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001622 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1624 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625 {
1626 return( ret );
1627 }
1628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001630 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001631 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001632
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001633 /*
1634 * Optional extensions
1635 */
1636
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001637 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001640 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001641 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001642
1643 if( crt->max_pathlen > 0 )
1644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001646 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001647 }
1648 }
1649
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001650 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001653 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001654
1655 if( ( ret = x509_info_subject_alt_name( &p, &n,
1656 &crt->subject_alt_names ) ) != 0 )
1657 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001658 }
1659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001663 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001664
1665 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1666 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001667 }
1668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001672 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001673
1674 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1675 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001676 }
1677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001681 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001682
1683 if( ( ret = x509_info_ext_key_usage( &p, &n,
1684 &crt->ext_key_usage ) ) != 0 )
1685 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001686 }
1687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001689 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001690
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001691 return( (int) ( size - n ) );
1692}
1693
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001694struct x509_crt_verify_string {
1695 int code;
1696 const char *string;
1697};
1698
1699static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001700 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001701 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1702 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1703 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1704 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1705 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001706 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1707 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1708 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001709 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001710 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1711 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1712 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1713 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001714 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1715 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1716 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1717 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1718 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1719 { 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 +01001720 { 0, NULL }
1721};
1722
1723int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001724 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001725{
1726 int ret;
1727 const struct x509_crt_verify_string *cur;
1728 char *p = buf;
1729 size_t n = size;
1730
1731 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1732 {
1733 if( ( flags & cur->code ) == 0 )
1734 continue;
1735
1736 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001737 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001738 flags ^= cur->code;
1739 }
1740
1741 if( flags != 0 )
1742 {
1743 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1744 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001745 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001746 }
1747
1748 return( (int) ( size - n ) );
1749}
1750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001752int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1753 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001754{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001755 unsigned int usage_must, usage_may;
1756 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1757 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1758
1759 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1760 return( 0 );
1761
1762 usage_must = usage & ~may_mask;
1763
1764 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1765 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1766
1767 usage_may = usage & may_mask;
1768
1769 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001771
1772 return( 0 );
1773}
1774#endif
1775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1777int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001778 const char *usage_oid,
1779 size_t usage_len )
1780{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001782
1783 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001785 return( 0 );
1786
1787 /*
1788 * Look for the requested usage (or wildcard ANY) in our list
1789 */
1790 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001793
1794 if( cur_oid->len == usage_len &&
1795 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1796 {
1797 return( 0 );
1798 }
1799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001801 return( 0 );
1802 }
1803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001805}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001809/*
1810 * Return 1 if the certificate is revoked, or 0 otherwise.
1811 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001812int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001813{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001815
1816 while( cur != NULL && cur->serial.len != 0 )
1817 {
1818 if( crt->serial.len == cur->serial.len &&
1819 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1820 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001821 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001822 return( 1 );
1823 }
1824
1825 cur = cur->next;
1826 }
1827
1828 return( 0 );
1829}
1830
1831/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01001832 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02001833 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001836 mbedtls_x509_crl *crl_list,
1837 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001838{
1839 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1841 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001842
1843 if( ca == NULL )
1844 return( flags );
1845
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001846 while( crl_list != NULL )
1847 {
1848 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00001849 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001850 {
1851 crl_list = crl_list->next;
1852 continue;
1853 }
1854
1855 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001856 * Check if the CA is configured to sign CRLs
1857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00001859 if( mbedtls_x509_crt_check_key_usage( ca,
1860 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001863 break;
1864 }
1865#endif
1866
1867 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001868 * Check if CRL is correctly signed by the trusted CA
1869 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001870 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1871 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1872
1873 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1874 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02001877 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001878 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02001879 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001881 break;
1882 }
1883
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02001884 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001885 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1888 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001889 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001892 break;
1893 }
1894
1895 /*
1896 * Check for validity of CRL (Do not drop out)
1897 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001898 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001900
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001901 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001902 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001903
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001904 /*
1905 * Check if certificate is revoked
1906 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001907 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001910 break;
1911 }
1912
1913 crl_list = crl_list->next;
1914 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001915
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001916 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001917}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001919
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001920/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001921 * Check the signature of a certificate by its parent
1922 */
1923static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02001924 mbedtls_x509_crt *parent,
1925 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001926{
1927 const mbedtls_md_info_t *md_info;
1928 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1929
1930 md_info = mbedtls_md_info_from_type( child->sig_md );
1931 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
1932 {
1933 /* Note: this can't happen except after an internal error */
1934 return( -1 );
1935 }
1936
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02001937 /* Skip expensive computation on obvious mismatch */
1938 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001939 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001940
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02001941#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02001942 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
1943 {
1944 return( mbedtls_pk_verify_restartable( &parent->pk,
1945 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001946 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02001947 }
1948#else
1949 (void) rs_ctx;
1950#endif
1951
1952 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1953 child->sig_md, hash, mbedtls_md_get_size( md_info ),
1954 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001955}
1956
1957/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001958 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1959 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001960 *
1961 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1964 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02001965 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001966{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001967 int need_ca_bit;
1968
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001969 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001970 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001971 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001972
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001973 /* Parent must have the basicConstraints CA bit set as a general rule */
1974 need_ca_bit = 1;
1975
1976 /* Exception: v1/v2 certificates that are locally trusted. */
1977 if( top && parent->version < 3 )
1978 need_ca_bit = 0;
1979
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001980 if( need_ca_bit && ! parent->ca_istrue )
1981 return( -1 );
1982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001984 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001986 {
1987 return( -1 );
1988 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001989#endif
1990
1991 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001992}
1993
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02001994/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02001995 * Find a suitable parent for child in candidates, or return NULL.
1996 *
1997 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02001998 * 1. subject name matches child's issuer
1999 * 2. if necessary, the CA bit is set and key usage allows signing certs
2000 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002001 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002002 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002003 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002004 * If there's a suitable candidate which is also time-valid, return the first
2005 * such. Otherwise, return the first suitable candidate (or NULL if there is
2006 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002007 *
2008 * The rationale for this rule is that someone could have a list of trusted
2009 * roots with two versions on the same root with different validity periods.
2010 * (At least one user reported having such a list and wanted it to just work.)
2011 * The reason we don't just require time-validity is that generally there is
2012 * only one version, and if it's expired we want the flags to state that
2013 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002014 *
2015 * The rationale for rule 3 (signature for trusted roots) is that users might
2016 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002017 * way we select the correct one is by checking the signature (as we don't
2018 * rely on key identifier extensions). (This is one way users might choose to
2019 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002020 *
2021 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002022 * - [in] child: certificate for which we're looking for a parent
2023 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002024 * - [out] r_parent: parent found (or NULL)
2025 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002026 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2027 * of the chain, 0 otherwise
2028 * - [in] path_cnt: number of intermediates seen so far
2029 * - [in] self_cnt: number of self-signed intermediates seen so far
2030 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002031 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002032 *
2033 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002034 * - 0 on success
2035 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002036 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002037static int x509_crt_find_parent_in(
2038 mbedtls_x509_crt *child,
2039 mbedtls_x509_crt *candidates,
2040 mbedtls_x509_crt **r_parent,
2041 int *r_signature_is_good,
2042 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002043 unsigned path_cnt,
2044 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002045 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002046{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002047 int ret;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002048 mbedtls_x509_crt *parent, *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002049 int signature_is_good, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002050
2051#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002052 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002053 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2054 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002055 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002056 parent = rs_ctx->parent;
2057 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002058 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002059
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002060 /* clear saved state */
2061 rs_ctx->parent = NULL;
2062 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002063 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002064
2065 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002066 goto check_signature;
2067 }
2068#endif
2069
2070 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002071 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002072
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002073 for( parent = candidates; parent != NULL; parent = parent->next )
2074 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002075 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002076 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002077 continue;
2078
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002079 /* +1 because stored max_pathlen is 1 higher that the actual value */
2080 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002081 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002082 {
2083 continue;
2084 }
2085
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002086 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002087#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2088check_signature:
2089#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002090 ret = x509_crt_check_signature( child, parent, rs_ctx );
2091
2092#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002093 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2094 {
2095 /* save state */
2096 rs_ctx->parent = parent;
2097 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002098 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002099
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002100 return( ret );
2101 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002102#else
2103 (void) ret;
2104#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002105
2106 signature_is_good = ret == 0;
2107 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002108 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002109
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002110 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002111 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2112 mbedtls_x509_time_is_future( &parent->valid_from ) )
2113 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002114 if( fallback_parent == NULL )
2115 {
2116 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002117 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002118 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002119
2120 continue;
2121 }
2122
Andy Gross3fc6f9d2019-01-30 10:25:53 -06002123 *r_parent = parent;
2124 *r_signature_is_good = signature_is_good;
2125
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002126 break;
2127 }
2128
Andy Gross3fc6f9d2019-01-30 10:25:53 -06002129 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002130 {
2131 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002132 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002133 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002134
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002135 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002136}
2137
2138/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002139 * Find a parent in trusted CAs or the provided chain, or return NULL.
2140 *
2141 * Searches in trusted CAs first, and return the first suitable parent found
2142 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002143 *
2144 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002145 * - [in] child: certificate for which we're looking for a parent, followed
2146 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002147 * - [in] trust_ca: list of locally trusted certificates
2148 * - [out] parent: parent found (or NULL)
2149 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2150 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2151 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2152 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002153 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002154 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002155 *
2156 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002157 * - 0 on success
2158 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002159 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002160static int x509_crt_find_parent(
2161 mbedtls_x509_crt *child,
2162 mbedtls_x509_crt *trust_ca,
2163 mbedtls_x509_crt **parent,
2164 int *parent_is_trusted,
2165 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002166 unsigned path_cnt,
2167 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002168 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002169{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002170 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002171 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002172
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002173 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002174
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002175#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002176 /* restore then clear saved state if we have some stored */
2177 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2178 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002179 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002180 rs_ctx->parent_is_trusted = -1;
2181 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002182#endif
2183
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002184 while( 1 ) {
2185 search_list = *parent_is_trusted ? trust_ca : child->next;
2186
2187 ret = x509_crt_find_parent_in( child, search_list,
2188 parent, signature_is_good,
2189 *parent_is_trusted,
2190 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002191
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002192#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002193 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2194 {
2195 /* save state */
2196 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002197 return( ret );
2198 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002199#else
2200 (void) ret;
2201#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002202
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002203 /* stop here if found or already in second iteration */
2204 if( *parent != NULL || *parent_is_trusted == 0 )
2205 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002206
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002207 /* prepare second iteration */
2208 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002209 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002210
2211 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01002212 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002213 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002214 *parent_is_trusted = 0;
2215 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002216 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002217
2218 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002219}
2220
2221/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002222 * Check if an end-entity certificate is locally trusted
2223 *
2224 * Currently we require such certificates to be self-signed (actually only
2225 * check for self-issued as self-signatures are not checked)
2226 */
2227static int x509_crt_check_ee_locally_trusted(
2228 mbedtls_x509_crt *crt,
2229 mbedtls_x509_crt *trust_ca )
2230{
2231 mbedtls_x509_crt *cur;
2232
2233 /* must be self-issued */
2234 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2235 return( -1 );
2236
2237 /* look for an exact match with trusted cert */
2238 for( cur = trust_ca; cur != NULL; cur = cur->next )
2239 {
2240 if( crt->raw.len == cur->raw.len &&
2241 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2242 {
2243 return( 0 );
2244 }
2245 }
2246
2247 /* too bad */
2248 return( -1 );
2249}
2250
2251/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002252 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002253 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002254 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2255 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002256 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002257 * such that every cert in the chain is a child of the next one,
2258 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002259 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002260 * Verify that chain and return it with flags for all issues found.
2261 *
2262 * Special cases:
2263 * - EE == Rj -> return a one-element list containing it
2264 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2265 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002266 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002267 * Tests for (aspects of) this function should include at least:
2268 * - trusted EE
2269 * - EE -> trusted root
Antonin Décimod5f47592019-01-23 15:24:37 +01002270 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002271 * - if relevant: EE untrusted
2272 * - if relevant: EE -> intermediate, untrusted
2273 * with the aspect under test checked at each relevant level (EE, int, root).
2274 * For some aspects longer chains are required, but usually length 2 is
2275 * enough (but length 1 is not in general).
2276 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002277 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002278 * - [in] crt: the cert list EE, C1, ..., Cn
2279 * - [in] trust_ca: the trusted list R1, ..., Rp
2280 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002281 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002282 * Only valid when return value is 0, may contain garbage otherwise!
2283 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002284 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002285 *
2286 * Return value:
2287 * - non-zero if the chain could not be fully built and examined
2288 * - 0 is the chain was successfully built and examined,
2289 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002290 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002291static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002292 mbedtls_x509_crt *crt,
2293 mbedtls_x509_crt *trust_ca,
2294 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002295 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002296 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002297 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002298{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002299 /* Don't initialize any of those variables here, so that the compiler can
2300 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002301 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002302 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002303 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002304 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002305 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002306 int parent_is_trusted;
2307 int child_is_trusted;
2308 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002309 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002310
2311#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2312 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002313 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002314 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002315 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002316 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002317 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002318
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002319 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002320 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002321 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002322 flags = &cur->flags;
2323
2324 goto find_parent;
2325 }
2326#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002327
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002328 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002329 self_cnt = 0;
2330 parent_is_trusted = 0;
2331 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002332
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002333 while( 1 ) {
2334 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002335 cur = &ver_chain->items[ver_chain->len];
2336 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002337 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002338 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002339 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002340
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002341 /* Check time-validity (all certificates) */
2342 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2343 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002344
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002345 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2346 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002347
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002348 /* Stop here for trusted roots (but not for trusted EE certs) */
2349 if( child_is_trusted )
2350 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002351
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002352 /* Check signature algorithm: MD & PK algs */
2353 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2354 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002355
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002356 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2357 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002358
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002359 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002360 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002361 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2362 {
2363 return( 0 );
2364 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002365
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002366#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2367find_parent:
2368#endif
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002369 /* Look for a parent in trusted CAs or up the chain */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002370 ret = x509_crt_find_parent( child, trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002371 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002372 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002373
2374#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002375 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2376 {
2377 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002378 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002379 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002380 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002381
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002382 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002383 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002384#else
2385 (void) ret;
2386#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002387
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002388 /* No parent? We're done here */
2389 if( parent == NULL )
2390 {
2391 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2392 return( 0 );
2393 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002394
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002395 /* Count intermediate self-issued (not necessarily self-signed) certs.
2396 * These can occur with some strategies for key rollover, see [SIRO],
2397 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002398 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002399 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2400 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002401 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002402 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002403
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002404 /* path_cnt is 0 for the first intermediate CA,
2405 * and if parent is trusted it's not an intermediate CA */
2406 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002407 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002408 {
2409 /* return immediately to avoid overflow the chain array */
2410 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2411 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002412
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002413 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002414 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002415 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2416
2417 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002418 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002419 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002422 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002423 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002424#else
2425 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002426#endif
2427
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002428 /* prepare for next iteration */
2429 child = parent;
2430 parent = NULL;
2431 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002432 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002433 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002434}
2435
2436/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002437 * Check for CN match
2438 */
2439static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2440 const char *cn, size_t cn_len )
2441{
2442 /* try exact match */
2443 if( name->len == cn_len &&
2444 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2445 {
2446 return( 0 );
2447 }
2448
2449 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002450 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002451 {
2452 return( 0 );
2453 }
2454
2455 return( -1 );
2456}
2457
2458/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002459 * Verify the requested CN - only call this if cn is not NULL!
2460 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002461static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002462 const char *cn,
2463 uint32_t *flags )
2464{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002465 const mbedtls_x509_name *name;
2466 const mbedtls_x509_sequence *cur;
2467 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002468
2469 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
2470 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002471 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002472 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002473 if( x509_crt_check_cn( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002474 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002475 }
2476
2477 if( cur == NULL )
2478 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2479 }
2480 else
2481 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002482 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002483 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002484 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
2485 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002486 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002487 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002488 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002489 }
2490
2491 if( name == NULL )
2492 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2493 }
2494}
2495
2496/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002497 * Merge the flags for all certs in the chain, after calling callback
2498 */
2499static int x509_crt_merge_flags_with_cb(
2500 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002501 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002502 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2503 void *p_vrfy )
2504{
2505 int ret;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002506 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002507 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002508 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002509
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002510 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002511 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002512 cur = &ver_chain->items[i-1];
2513 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002514
2515 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002516 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002517 return( ret );
2518
2519 *flags |= cur_flags;
2520 }
2521
2522 return( 0 );
2523}
2524
2525/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002526 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2529 mbedtls_x509_crt *trust_ca,
2530 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002531 const char *cn, uint32_t *flags,
2532 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002533 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002534{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002535 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
2536 &mbedtls_x509_crt_profile_default, cn, flags,
2537 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002538}
2539
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002540/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002541 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002542 */
2543int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2544 mbedtls_x509_crt *trust_ca,
2545 mbedtls_x509_crl *ca_crl,
2546 const mbedtls_x509_crt_profile *profile,
2547 const char *cn, uint32_t *flags,
2548 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2549 void *p_vrfy )
2550{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002551 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
2552 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
2553}
2554
2555/*
2556 * Verify the certificate validity, with profile, restartable version
2557 *
2558 * This function:
2559 * - checks the requested CN (if any)
2560 * - checks the type and size of the EE cert's key,
2561 * as that isn't done as part of chain building/verification currently
2562 * - builds and verifies the chain
2563 * - then calls the callback and merges the flags
2564 */
2565int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
2566 mbedtls_x509_crt *trust_ca,
2567 mbedtls_x509_crl *ca_crl,
2568 const mbedtls_x509_crt_profile *profile,
2569 const char *cn, uint32_t *flags,
2570 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2571 void *p_vrfy,
2572 mbedtls_x509_crt_restart_ctx *rs_ctx )
2573{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002574 int ret;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002575 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002576 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002577 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002578
2579 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002580 ee_flags = 0;
2581 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002582
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002583 if( profile == NULL )
2584 {
2585 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2586 goto exit;
2587 }
2588
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002589 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002590 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002591 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002592
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002593 /* Check the type and size of the key */
2594 pk_type = mbedtls_pk_get_type( &crt->pk );
2595
2596 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002597 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002598
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002599 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002600 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002601
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002602 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02002603 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002604 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002605
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002606 if( ret != 0 )
2607 goto exit;
2608
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002609 /* Merge end-entity flags */
2610 ver_chain.items[0].flags |= ee_flags;
2611
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002612 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002613 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002614
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002615exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002616#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2617 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2618 mbedtls_x509_crt_restart_free( rs_ctx );
2619#endif
2620
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02002621 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2622 * the SSL module for authmode optional, but non-zero return from the
2623 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002624 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2625 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2626
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002627 if( ret != 0 )
2628 {
2629 *flags = (uint32_t) -1;
2630 return( ret );
2631 }
2632
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002633 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002635
2636 return( 0 );
2637}
2638
2639/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002640 * Initialize a certificate chain
2641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002643{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002645}
2646
2647/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002648 * Unallocate all certificate data
2649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002651{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 mbedtls_x509_crt *cert_cur = crt;
2653 mbedtls_x509_crt *cert_prv;
2654 mbedtls_x509_name *name_cur;
2655 mbedtls_x509_name *name_prv;
2656 mbedtls_x509_sequence *seq_cur;
2657 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002658
2659 if( crt == NULL )
2660 return;
2661
2662 do
2663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2667 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002668#endif
2669
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002670 name_cur = cert_cur->issuer.next;
2671 while( name_cur != NULL )
2672 {
2673 name_prv = name_cur;
2674 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002675 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002677 }
2678
2679 name_cur = cert_cur->subject.next;
2680 while( name_cur != NULL )
2681 {
2682 name_prv = name_cur;
2683 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002684 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002686 }
2687
2688 seq_cur = cert_cur->ext_key_usage.next;
2689 while( seq_cur != NULL )
2690 {
2691 seq_prv = seq_cur;
2692 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002693 mbedtls_platform_zeroize( seq_prv,
2694 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002696 }
2697
2698 seq_cur = cert_cur->subject_alt_names.next;
2699 while( seq_cur != NULL )
2700 {
2701 seq_prv = seq_cur;
2702 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002703 mbedtls_platform_zeroize( seq_prv,
2704 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002706 }
2707
2708 if( cert_cur->raw.p != NULL )
2709 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002710 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002712 }
2713
2714 cert_cur = cert_cur->next;
2715 }
2716 while( cert_cur != NULL );
2717
2718 cert_cur = crt;
2719 do
2720 {
2721 cert_prv = cert_cur;
2722 cert_cur = cert_cur->next;
2723
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002724 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002725 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002727 }
2728 while( cert_cur != NULL );
2729}
2730
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002731#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2732/*
2733 * Initialize a restart context
2734 */
2735void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
2736{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002737 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002738
2739 ctx->parent = NULL;
2740 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002741 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002742
2743 ctx->parent_is_trusted = -1;
2744
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002745 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002746 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002747 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002748}
2749
2750/*
2751 * Free the components of a restart context
2752 */
2753void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
2754{
2755 if( ctx == NULL )
2756 return;
2757
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002758 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002759 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002760}
2761#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
2762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763#endif /* MBEDTLS_X509_CRT_PARSE_C */