blob: bf06872d4b312c3a8b8101476c2622fe11dd4f03 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 */
21/*
22 * The ITU-T X.509 standard defines a certificate format for PKI.
23 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020024 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020030 *
31 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032 */
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000043#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050045#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000046
Rich Evans00ab4702015-02-06 13:43:58 +000047#include <string.h>
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051#endif
52
Andrzej Kurekd4a65532018-10-31 06:18:39 -040053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "psa/crypto.h"
55#include "mbedtls/psa_util.h"
56#endif
57
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#else
Simon Butcherd2642582018-10-03 15:11:19 +010061#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000062#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020064#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066#endif
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010070#endif
71
Paul Bakkerfa6a6202013-10-28 18:48:30 +010072#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073#include <windows.h>
74#else
75#include <time.h>
76#endif
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000079#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020080#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081#include <sys/types.h>
82#include <sys/stat.h>
83#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000084#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#endif
86
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020087/*
88 * Item in a verification chain: cert and flags for it
89 */
90typedef struct {
91 mbedtls_x509_crt *crt;
92 uint32_t flags;
93} x509_crt_verify_chain_item;
94
95/*
96 * Max size of verification chain: end-entity + intermediates + trusted root
97 */
98#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020099
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200101 * Default profile
102 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200103const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
104{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200105#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200106 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200107 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200108#endif
109 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200110 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
111 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
112 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
113 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
114 0xFFFFFFF, /* Any PK alg */
115 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200116 2048,
117};
118
119/*
120 * Next-default profile
121 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200122const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
123{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200124 /* Hashes from SHA-256 and above */
125 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
126 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
127 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
128 0xFFFFFFF, /* Any PK alg */
129#if defined(MBEDTLS_ECP_C)
130 /* Curves at or above 128-bit security level */
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
136 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
138#else
139 0,
140#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200141 2048,
142};
143
144/*
145 * NSA Suite B Profile
146 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200147const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
148{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200149 /* Only SHA-256 and 384 */
150 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
151 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
152 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200153 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
154 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200155#if defined(MBEDTLS_ECP_C)
156 /* Only NIST P-256 and P-384 */
157 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
158 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
159#else
160 0,
161#endif
162 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200163};
164
165/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200166 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200167 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200168 */
169static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
170 mbedtls_md_type_t md_alg )
171{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200172 if( md_alg == MBEDTLS_MD_NONE )
173 return( -1 );
174
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200175 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
176 return( 0 );
177
178 return( -1 );
179}
180
181/*
182 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200183 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200184 */
185static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
186 mbedtls_pk_type_t pk_alg )
187{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200188 if( pk_alg == MBEDTLS_PK_NONE )
189 return( -1 );
190
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200191 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
192 return( 0 );
193
194 return( -1 );
195}
196
197/*
198 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200199 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200200 */
201static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200202 const mbedtls_pk_context *pk )
203{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200204 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200205
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200206#if defined(MBEDTLS_RSA_C)
207 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
208 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200209 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200210 return( 0 );
211
212 return( -1 );
213 }
214#endif
215
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200216#if defined(MBEDTLS_ECP_C)
217 if( pk_alg == MBEDTLS_PK_ECDSA ||
218 pk_alg == MBEDTLS_PK_ECKEY ||
219 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200220 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200221 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200222
Philippe Antoineb5b25432018-05-11 11:06:29 +0200223 if( gid == MBEDTLS_ECP_DP_NONE )
224 return( -1 );
225
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200226 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
227 return( 0 );
228
229 return( -1 );
230 }
231#endif
232
233 return( -1 );
234}
235
236/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000237 * Like memcmp, but case-insensitive and always returns -1 if different
238 */
239static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
240{
241 size_t i;
242 unsigned char diff;
243 const unsigned char *n1 = s1, *n2 = s2;
244
245 for( i = 0; i < len; i++ )
246 {
247 diff = n1[i] ^ n2[i];
248
249 if( diff == 0 )
250 continue;
251
252 if( diff == 32 &&
253 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
254 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
255 {
256 continue;
257 }
258
259 return( -1 );
260 }
261
262 return( 0 );
263}
264
265/*
266 * Return 0 if name matches wildcard, -1 otherwise
267 */
268static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
269{
270 size_t i;
271 size_t cn_idx = 0, cn_len = strlen( cn );
272
273 /* We can't have a match if there is no wildcard to match */
274 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
275 return( -1 );
276
277 for( i = 0; i < cn_len; ++i )
278 {
279 if( cn[i] == '.' )
280 {
281 cn_idx = i;
282 break;
283 }
284 }
285
286 if( cn_idx == 0 )
287 return( -1 );
288
289 if( cn_len - cn_idx == name->len - 1 &&
290 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
291 {
292 return( 0 );
293 }
294
295 return( -1 );
296}
297
298/*
299 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
300 * variations (but not all).
301 *
302 * Return 0 if equal, -1 otherwise.
303 */
304static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
305{
306 if( a->tag == b->tag &&
307 a->len == b->len &&
308 memcmp( a->p, b->p, b->len ) == 0 )
309 {
310 return( 0 );
311 }
312
313 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
314 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
315 a->len == b->len &&
316 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
317 {
318 return( 0 );
319 }
320
321 return( -1 );
322}
323
324/*
325 * Compare two X.509 Names (aka rdnSequence).
326 *
327 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
328 * we sometimes return unequal when the full algorithm would return equal,
329 * but never the other way. (In particular, we don't do Unicode normalisation
330 * or space folding.)
331 *
332 * Return 0 if equal, -1 otherwise.
333 */
334static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
335{
336 /* Avoid recursion, it might not be optimised by the compiler */
337 while( a != NULL || b != NULL )
338 {
339 if( a == NULL || b == NULL )
340 return( -1 );
341
342 /* type */
343 if( a->oid.tag != b->oid.tag ||
344 a->oid.len != b->oid.len ||
345 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
346 {
347 return( -1 );
348 }
349
350 /* value */
351 if( x509_string_cmp( &a->val, &b->val ) != 0 )
352 return( -1 );
353
354 /* structure of the list of sets */
355 if( a->next_merged != b->next_merged )
356 return( -1 );
357
358 a = a->next;
359 b = b->next;
360 }
361
362 /* a == NULL == b */
363 return( 0 );
364}
365
366/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200367 * Reset (init or clear) a verify_chain
368 */
369static void x509_crt_verify_chain_reset(
370 mbedtls_x509_crt_verify_chain *ver_chain )
371{
372 size_t i;
373
374 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
375 {
376 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000377 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200378 }
379
380 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000381
382#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
383 ver_chain->trust_ca_cb_result = NULL;
384#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200385}
386
387/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
389 */
390static int x509_get_version( unsigned char **p,
391 const unsigned char *end,
392 int *ver )
393{
Janos Follath865b3eb2019-12-16 11:46:15 +0000394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200395 size_t len;
396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
398 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401 {
402 *ver = 0;
403 return( 0 );
404 }
405
Hanno Becker6ccfb182019-02-12 11:52:10 +0000406 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407 }
408
409 end = *p + len;
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
412 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413
414 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 return( MBEDTLS_ERR_X509_INVALID_VERSION +
416 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417
418 return( 0 );
419}
420
421/*
422 * Validity ::= SEQUENCE {
423 * notBefore Time,
424 * notAfter Time }
425 */
426static int x509_get_dates( unsigned char **p,
427 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_x509_time *from,
429 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430{
Janos Follath865b3eb2019-12-16 11:46:15 +0000431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432 size_t len;
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
435 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
436 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437
438 end = *p + len;
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441 return( ret );
442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444 return( ret );
445
446 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 return( MBEDTLS_ERR_X509_INVALID_DATE +
448 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449
450 return( 0 );
451}
452
453/*
454 * X.509 v2/v3 unique identifier (not parsed)
455 */
456static int x509_get_uid( unsigned char **p,
457 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459{
Janos Follath865b3eb2019-12-16 11:46:15 +0000460 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461
462 if( *p == end )
463 return( 0 );
464
465 uid->tag = **p;
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
468 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471 return( 0 );
472
Hanno Becker6ccfb182019-02-12 11:52:10 +0000473 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474 }
475
476 uid->p = *p;
477 *p += uid->len;
478
479 return( 0 );
480}
481
482static int x509_get_basic_constraints( unsigned char **p,
483 const unsigned char *end,
484 int *ca_istrue,
485 int *max_pathlen )
486{
Janos Follath865b3eb2019-12-16 11:46:15 +0000487 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488 size_t len;
489
490 /*
491 * BasicConstraints ::= SEQUENCE {
492 * cA BOOLEAN DEFAULT FALSE,
493 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
494 */
495 *ca_istrue = 0; /* DEFAULT FALSE */
496 *max_pathlen = 0; /* endless */
497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
499 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
500 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501
502 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200503 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
508 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509
510 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512
513 if( *ca_istrue != 0 )
514 *ca_istrue = 1;
515 }
516
517 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200518 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
521 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522
523 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
525 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526
Andrzej Kurek16050742020-04-14 09:49:52 -0400527 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
528 * overflow, which is an undefined behavior. */
529 if( *max_pathlen == INT_MAX )
530 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
531 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
532
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533 (*max_pathlen)++;
534
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200535 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536}
537
538static int x509_get_ns_cert_type( unsigned char **p,
539 const unsigned char *end,
540 unsigned char *ns_cert_type)
541{
Janos Follath865b3eb2019-12-16 11:46:15 +0000542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
546 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547
548 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
550 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551
552 /* Get actual bitstring */
553 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200554 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555}
556
557static int x509_get_key_usage( unsigned char **p,
558 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100559 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560{
Janos Follath865b3eb2019-12-16 11:46:15 +0000561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200562 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
566 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
568 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
570 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571
572 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200573 *key_usage = 0;
574 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
575 {
576 *key_usage |= (unsigned int) bs.p[i] << (8*i);
577 }
578
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200579 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580}
581
582/*
583 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
584 *
585 * KeyPurposeId ::= OBJECT IDENTIFIER
586 */
587static int x509_get_ext_key_usage( unsigned char **p,
588 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590{
Janos Follath865b3eb2019-12-16 11:46:15 +0000591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
594 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595
596 /* Sequence length must be >= 1 */
597 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
599 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200600
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200601 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602}
603
604/*
605 * SubjectAltName ::= GeneralNames
606 *
607 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
608 *
609 * GeneralName ::= CHOICE {
610 * otherName [0] OtherName,
611 * rfc822Name [1] IA5String,
612 * dNSName [2] IA5String,
613 * x400Address [3] ORAddress,
614 * directoryName [4] Name,
615 * ediPartyName [5] EDIPartyName,
616 * uniformResourceIdentifier [6] IA5String,
617 * iPAddress [7] OCTET STRING,
618 * registeredID [8] OBJECT IDENTIFIER }
619 *
620 * OtherName ::= SEQUENCE {
621 * type-id OBJECT IDENTIFIER,
622 * value [0] EXPLICIT ANY DEFINED BY type-id }
623 *
624 * EDIPartyName ::= SEQUENCE {
625 * nameAssigner [0] DirectoryString OPTIONAL,
626 * partyName [1] DirectoryString }
627 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300628 * NOTE: we list all types, but only use dNSName and otherName
629 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 */
631static int x509_get_subject_alt_name( unsigned char **p,
632 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634{
Janos Follath865b3eb2019-12-16 11:46:15 +0000635 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640
641 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
643 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
644 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645
646 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
648 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649
650 while( *p < end )
651 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300652 mbedtls_x509_subject_alternative_name dummy_san_buf;
653 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
654
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
657 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658
659 tag = **p;
660 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
662 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100664 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
665 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
668 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100669 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670
Ron Eldordbbd9662019-05-15 15:46:03 +0300671 /*
672 * Check that the SAN are structured correct.
673 */
674 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
675 /*
676 * In case the extension is malformed, return an error,
677 * and clear the allocated sequences.
678 */
679 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
680 {
681 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
682 mbedtls_x509_sequence *seq_prv;
683 while( seq_cur != NULL )
684 {
685 seq_prv = seq_cur;
686 seq_cur = seq_cur->next;
687 mbedtls_platform_zeroize( seq_prv,
688 sizeof( mbedtls_x509_sequence ) );
689 mbedtls_free( seq_prv );
690 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300691 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300692 return( ret );
693 }
694
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200696 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100698 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100700
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200701 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
703 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200705 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707 cur = cur->next;
708 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200709
710 buf = &(cur->buf);
711 buf->tag = tag;
712 buf->p = *p;
713 buf->len = tag_len;
714 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715 }
716
717 /* Set final sequence entry's next pointer to NULL */
718 cur->next = NULL;
719
720 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
722 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200723
724 return( 0 );
725}
726
727/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200728 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
729 *
730 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
731 *
732 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
733 *
734 * PolicyInformation ::= SEQUENCE {
735 * policyIdentifier CertPolicyId,
736 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
737 * PolicyQualifierInfo OPTIONAL }
738 *
739 * CertPolicyId ::= OBJECT IDENTIFIER
740 *
741 * PolicyQualifierInfo ::= SEQUENCE {
742 * policyQualifierId PolicyQualifierId,
743 * qualifier ANY DEFINED BY policyQualifierId }
744 *
745 * -- policyQualifierIds for Internet policy qualifiers
746 *
747 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
748 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
749 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
750 *
751 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
752 *
753 * Qualifier ::= CHOICE {
754 * cPSuri CPSuri,
755 * userNotice UserNotice }
756 *
757 * CPSuri ::= IA5String
758 *
759 * UserNotice ::= SEQUENCE {
760 * noticeRef NoticeReference OPTIONAL,
761 * explicitText DisplayText OPTIONAL }
762 *
763 * NoticeReference ::= SEQUENCE {
764 * organization DisplayText,
765 * noticeNumbers SEQUENCE OF INTEGER }
766 *
767 * DisplayText ::= CHOICE {
768 * ia5String IA5String (SIZE (1..200)),
769 * visibleString VisibleString (SIZE (1..200)),
770 * bmpString BMPString (SIZE (1..200)),
771 * utf8String UTF8String (SIZE (1..200)) }
772 *
773 * NOTE: we only parse and use anyPolicy without qualifiers at this point
774 * as defined in RFC 5280.
775 */
776static int x509_get_certificate_policies( unsigned char **p,
777 const unsigned char *end,
778 mbedtls_x509_sequence *certificate_policies )
779{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300780 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200781 size_t len;
782 mbedtls_asn1_buf *buf;
783 mbedtls_asn1_sequence *cur = certificate_policies;
784
785 /* Get main sequence tag */
786 ret = mbedtls_asn1_get_tag( p, end, &len,
787 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
788 if( ret != 0 )
789 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
790
791 if( *p + len != end )
792 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
793 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
794
795 /*
796 * Cannot be an empty sequence.
797 */
798 if( len == 0 )
799 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
800 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
801
802 while( *p < end )
803 {
804 mbedtls_x509_buf policy_oid;
805 const unsigned char *policy_end;
806
807 /*
808 * Get the policy sequence
809 */
810 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
811 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
812 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
813
814 policy_end = *p + len;
815
Ron Eldor08063792019-05-13 16:38:39 +0300816 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200817 MBEDTLS_ASN1_OID ) ) != 0 )
818 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
819
820 policy_oid.tag = MBEDTLS_ASN1_OID;
821 policy_oid.len = len;
822 policy_oid.p = *p;
823
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300824 /*
825 * Only AnyPolicy is currently supported when enforcing policy.
826 */
827 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
828 {
829 /*
830 * Set the parsing return code but continue parsing, in case this
831 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
832 * is configured.
833 */
834 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
835 }
836
Ron Eldor74d9acc2019-03-21 14:00:03 +0200837 /* Allocate and assign next pointer */
838 if( cur->buf.p != NULL )
839 {
840 if( cur->next != NULL )
841 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
842
843 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
844
845 if( cur->next == NULL )
846 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
847 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
848
849 cur = cur->next;
850 }
851
852 buf = &( cur->buf );
853 buf->tag = policy_oid.tag;
854 buf->p = policy_oid.p;
855 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300856
857 *p += len;
858
859 /*
860 * If there is an optional qualifier, then *p < policy_end
861 * Check the Qualifier len to verify it doesn't exceed policy_end.
862 */
863 if( *p < policy_end )
864 {
865 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
866 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
867 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
868 /*
869 * Skip the optional policy qualifiers.
870 */
871 *p += len;
872 }
873
874 if( *p != policy_end )
875 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
876 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200877 }
878
879 /* Set final sequence entry's next pointer to NULL */
880 cur->next = NULL;
881
882 if( *p != end )
883 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
884 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
885
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300886 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200887}
888
889/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 * X.509 v3 extensions
891 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 */
893static int x509_get_crt_ext( unsigned char **p,
894 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200895 mbedtls_x509_crt *crt,
896 mbedtls_x509_crt_ext_cb_t cb )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897{
Janos Follath865b3eb2019-12-16 11:46:15 +0000898 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 size_t len;
900 unsigned char *end_ext_data, *end_ext_octet;
901
Hanno Becker12f62fb2019-02-12 17:22:36 +0000902 if( *p == end )
903 return( 0 );
904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907
Hanno Becker12f62fb2019-02-12 17:22:36 +0000908 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909 while( *p < end )
910 {
911 /*
912 * Extension ::= SEQUENCE {
913 * extnID OBJECT IDENTIFIER,
914 * critical BOOLEAN DEFAULT FALSE,
915 * extnValue OCTET STRING }
916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918 int is_critical = 0; /* DEFAULT FALSE */
919 int ext_type = 0;
920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
922 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
923 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924
925 end_ext_data = *p + len;
926
927 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200928 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200929 MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931
k-stachowiak463928a2018-07-24 12:50:59 +0200932 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 extn_oid.p = *p;
934 *p += extn_oid.len;
935
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
938 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
939 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940
941 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
943 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
944 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945
946 end_ext_octet = *p + len;
947
948 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
950 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951
952 /*
953 * Detect supported extensions
954 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956
957 if( ret != 0 )
958 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200959 /* Give the callback (if any) a chance to handle the extension */
ndilieto6e249802020-05-28 06:17:38 +0000960 if( cb != NULL && cb( crt, &extn_oid, is_critical, p, end_ext_octet ) == 0 )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200961 continue;
962
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963 /* No parser found, skip extension */
964 *p = end_ext_octet;
965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 if( is_critical )
968 {
969 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
971 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972 }
973#endif
974 continue;
975 }
976
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100977 /* Forbid repeated extensions */
978 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100980
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 crt->ext_types |= ext_type;
982
983 switch( ext_type )
984 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100985 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 /* Parse basic constraints */
987 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
988 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200989 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 break;
991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 /* Parse key usage */
994 if( ( ret = x509_get_key_usage( p, end_ext_octet,
995 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200996 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 break;
998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000 /* Parse extended key usage */
1001 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1002 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001003 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004 break;
1005
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001006 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007 /* Parse subject alt name */
1008 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1009 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001010 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011 break;
1012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014 /* Parse netscape certificate type */
1015 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1016 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001017 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018 break;
1019
Ron Eldor74d9acc2019-03-21 14:00:03 +02001020 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1021 /* Parse certificate policies type */
1022 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1023 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001024 {
1025#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1026 if( is_critical )
1027 return( ret );
1028 else
1029#endif
1030 /*
Ron Eldora2913912019-05-16 16:17:38 +03001031 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1032 * cannot interpret or enforce the policy. However, it is up to
1033 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001034 * unless the extension is critical.
1035 */
1036 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1037 return( ret );
1038 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001039 break;
1040
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001042 /*
1043 * If this is a non-critical extension, which the oid layer
1044 * supports, but there isn't an x509 parser for it,
1045 * skip the extension.
1046 */
1047#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1048 if( is_critical )
1049 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1050 else
1051#endif
1052 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053 }
1054 }
1055
1056 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1058 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059
1060 return( 0 );
1061}
1062
1063/*
1064 * Parse and fill a single X.509 certificate in DER format
1065 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001066static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1067 const unsigned char *buf,
1068 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001069 int make_copy,
1070 mbedtls_x509_crt_ext_cb_t cb )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071{
Janos Follath865b3eb2019-12-16 11:46:15 +00001072 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001073 size_t len;
1074 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1078 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1079 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080
1081 /*
1082 * Check for valid input
1083 */
1084 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001087 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001088 p = (unsigned char*) buf;
1089 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090 end = p + len;
1091
1092 /*
1093 * Certificate ::= SEQUENCE {
1094 * tbsCertificate TBSCertificate,
1095 * signatureAlgorithm AlgorithmIdentifier,
1096 * signatureValue BIT STRING }
1097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1099 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 mbedtls_x509_crt_free( crt );
1102 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103 }
1104
Janos Follathcc0e49d2016-02-17 14:34:12 +00001105 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001106 crt->raw.len = crt_end - buf;
1107 if( make_copy != 0 )
1108 {
1109 /* Create and populate a new buffer for the raw field. */
1110 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1111 if( crt->raw.p == NULL )
1112 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1113
1114 memcpy( crt->raw.p, buf, crt->raw.len );
1115 crt->own_buffer = 1;
1116
1117 p += crt->raw.len - len;
1118 end = crt_end = p + len;
1119 }
1120 else
1121 {
1122 crt->raw.p = (unsigned char*) buf;
1123 crt->own_buffer = 0;
1124 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001125
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001126 /*
1127 * TBSCertificate ::= SEQUENCE {
1128 */
1129 crt->tbs.p = p;
1130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1132 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 mbedtls_x509_crt_free( crt );
1135 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001136 }
1137
1138 end = p + len;
1139 crt->tbs.len = end - crt->tbs.p;
1140
1141 /*
1142 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1143 *
1144 * CertificateSerialNumber ::= INTEGER
1145 *
1146 * signature AlgorithmIdentifier
1147 */
1148 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1150 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001151 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154 return( ret );
1155 }
1156
Andres AG7ca4a032017-03-09 16:16:11 +00001157 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 mbedtls_x509_crt_free( crt );
1160 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001161 }
1162
Andres AG7ca4a032017-03-09 16:16:11 +00001163 crt->version++;
1164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001166 &crt->sig_md, &crt->sig_pk,
1167 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170 return( ret );
1171 }
1172
1173 /*
1174 * issuer Name
1175 */
1176 crt->issuer_raw.p = p;
1177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1179 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 mbedtls_x509_crt_free( crt );
1182 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001183 }
1184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001188 return( ret );
1189 }
1190
1191 crt->issuer_raw.len = p - crt->issuer_raw.p;
1192
1193 /*
1194 * Validity ::= SEQUENCE {
1195 * notBefore Time,
1196 * notAfter Time }
1197 *
1198 */
1199 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1200 &crt->valid_to ) ) != 0 )
1201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203 return( ret );
1204 }
1205
1206 /*
1207 * subject Name
1208 */
1209 crt->subject_raw.p = p;
1210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1212 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_x509_crt_free( crt );
1215 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001216 }
1217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001221 return( ret );
1222 }
1223
1224 crt->subject_raw.len = p - crt->subject_raw.p;
1225
1226 /*
1227 * SubjectPublicKeyInfo
1228 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001229 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001233 return( ret );
1234 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001235 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001236
1237 /*
1238 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1239 * -- If present, version shall be v2 or v3
1240 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1241 * -- If present, version shall be v2 or v3
1242 * extensions [3] EXPLICIT Extensions OPTIONAL
1243 * -- If present, version shall be v3
1244 */
1245 if( crt->version == 2 || crt->version == 3 )
1246 {
1247 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1248 if( ret != 0 )
1249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001251 return( ret );
1252 }
1253 }
1254
1255 if( crt->version == 2 || crt->version == 3 )
1256 {
1257 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1258 if( ret != 0 )
1259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001261 return( ret );
1262 }
1263 }
1264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001266 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001267#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001268 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001269 ret = x509_get_crt_ext( &p, end, crt, cb );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001270 if( ret != 0 )
1271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273 return( ret );
1274 }
1275 }
1276
1277 if( p != end )
1278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 mbedtls_x509_crt_free( crt );
1280 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1281 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282 }
1283
1284 end = crt_end;
1285
1286 /*
1287 * }
1288 * -- end of TBSCertificate
1289 *
1290 * signatureAlgorithm AlgorithmIdentifier,
1291 * signatureValue BIT STRING
1292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001296 return( ret );
1297 }
1298
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001299 if( crt->sig_oid.len != sig_oid2.len ||
1300 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001301 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001302 ( sig_params1.len != 0 &&
1303 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 mbedtls_x509_crt_free( crt );
1306 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 }
1308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001312 return( ret );
1313 }
1314
1315 if( p != end )
1316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 mbedtls_x509_crt_free( crt );
1318 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1319 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001320 }
1321
1322 return( 0 );
1323}
1324
1325/*
1326 * Parse one X.509 certificate in DER format from a buffer and add them to a
1327 * chained list
1328 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001329static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1330 const unsigned char *buf,
1331 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001332 int make_copy,
1333 mbedtls_x509_crt_ext_cb_t cb )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001334{
Janos Follath865b3eb2019-12-16 11:46:15 +00001335 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001337
1338 /*
1339 * Check for valid input
1340 */
1341 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343
1344 while( crt->version != 0 && crt->next != NULL )
1345 {
1346 prev = crt;
1347 crt = crt->next;
1348 }
1349
1350 /*
1351 * Add new certificate on the end of the chain if needed.
1352 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001353 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001354 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001355 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001356
1357 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001358 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001359
1360 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001362 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363 }
1364
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001365 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb );
1366 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001367 {
1368 if( prev )
1369 prev->next = NULL;
1370
1371 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373
1374 return( ret );
1375 }
1376
1377 return( 0 );
1378}
1379
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001380int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1381 const unsigned char *buf,
1382 size_t buflen )
1383{
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001384 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL ) );
1385}
1386
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001387int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1388 const unsigned char *buf,
1389 size_t buflen,
1390 mbedtls_x509_crt_ext_cb_t cb )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001391{
1392 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, cb ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001393}
1394
1395int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1396 const unsigned char *buf,
1397 size_t buflen )
1398{
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001399 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001400}
1401
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001402/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001403 * Parse one or more PEM certificates from a buffer and add them to the chained
1404 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001405 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001406int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1407 const unsigned char *buf,
1408 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001409{
Janos Follath98e28a72016-05-31 14:03:54 +01001410#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001411 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001413#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001414
1415 /*
1416 * Check for valid input
1417 */
1418 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420
1421 /*
1422 * Determine buffer content. Buffer contains either one DER certificate or
1423 * one or more PEM certificates.
1424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001426 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001427 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001430 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1433 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001434#else
1435 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1436#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#if defined(MBEDTLS_PEM_PARSE_C)
1439 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001444 /* 1 rather than 0 since the terminating NULL byte is counted in */
1445 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446 {
1447 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001450 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452 "-----BEGIN CERTIFICATE-----",
1453 "-----END CERTIFICATE-----",
1454 buf, NULL, 0, &use_len );
1455
1456 if( ret == 0 )
1457 {
1458 /*
1459 * Was PEM encoded
1460 */
1461 buflen -= use_len;
1462 buf += use_len;
1463 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001465 {
1466 return( ret );
1467 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471
1472 /*
1473 * PEM header and footer were found
1474 */
1475 buflen -= use_len;
1476 buf += use_len;
1477
1478 if( first_error == 0 )
1479 first_error = ret;
1480
Paul Bakker5a5fa922014-09-26 14:53:04 +02001481 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482 continue;
1483 }
1484 else
1485 break;
1486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490
1491 if( ret != 0 )
1492 {
1493 /*
1494 * Quit parsing on a memory error
1495 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001496 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001497 return( ret );
1498
1499 if( first_error == 0 )
1500 first_error = ret;
1501
1502 total_failed++;
1503 continue;
1504 }
1505
1506 success = 1;
1507 }
1508 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001509
1510 if( success )
1511 return( total_failed );
1512 else if( first_error )
1513 return( first_error );
1514 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001516#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517}
1518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520/*
1521 * Load one or more certificates and add them to the chained list
1522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524{
Janos Follath865b3eb2019-12-16 11:46:15 +00001525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526 size_t n;
1527 unsigned char *buf;
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001530 return( ret );
1531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001534 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536
1537 return( ret );
1538}
1539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541{
1542 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001543#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544 int w_ret;
1545 WCHAR szDir[MAX_PATH];
1546 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001547 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001548 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549
Paul Bakker9af723c2014-05-01 13:03:14 +02001550 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551 HANDLE hFind;
1552
1553 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555
Paul Bakker9af723c2014-05-01 13:03:14 +02001556 memset( szDir, 0, sizeof(szDir) );
1557 memset( filename, 0, MAX_PATH );
1558 memcpy( filename, path, len );
1559 filename[len++] = '\\';
1560 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561 filename[len++] = '*';
1562
Simon B3c6b18d2016-11-03 01:11:37 +00001563 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001564 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001565 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567
1568 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001569 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571
1572 len = MAX_PATH - len;
1573 do
1574 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001575 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001576
1577 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1578 continue;
1579
Paul Bakker9af723c2014-05-01 13:03:14 +02001580 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001581 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001582 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001583 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001584 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001585 {
1586 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1587 goto cleanup;
1588 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591 if( w_ret < 0 )
1592 ret++;
1593 else
1594 ret += w_ret;
1595 }
1596 while( FindNextFileW( hFind, &file_data ) != 0 );
1597
Paul Bakker66d5d072014-06-17 16:39:18 +02001598 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600
Ron Eldor36d90422017-01-09 15:09:16 +02001601cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001603#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001604 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001605 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001606 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001607 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001608 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001609 DIR *dir = opendir( path );
1610
Paul Bakker66d5d072014-06-17 16:39:18 +02001611 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001613
Ron Eldor63140682017-01-09 19:27:59 +02001614#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001615 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001616 {
1617 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001618 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001619 }
Ron Eldor63140682017-01-09 19:27:59 +02001620#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001621
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001622 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001623 {
Andres AGf9113192016-09-02 14:06:04 +01001624 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1625 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001626
Andres AGf9113192016-09-02 14:06:04 +01001627 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628 {
Andres AGf9113192016-09-02 14:06:04 +01001629 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1630 goto cleanup;
1631 }
1632 else if( stat( entry_name, &sb ) == -1 )
1633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001635 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636 }
1637
Janos Follath85de7a62020-02-04 14:12:03 +00001638 if( !S_ISREG( sb.st_mode ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639 continue;
1640
1641 // Ignore parse errors
1642 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644 if( t_ret < 0 )
1645 ret++;
1646 else
1647 ret += t_ret;
1648 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001649
1650cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001651 closedir( dir );
1652
Ron Eldor63140682017-01-09 19:27:59 +02001653#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001654 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001656#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001657
Paul Bakkerbe089b02013-10-14 15:51:50 +02001658#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659
1660 return( ret );
1661}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001663
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001664/*
1665 * OtherName ::= SEQUENCE {
1666 * type-id OBJECT IDENTIFIER,
1667 * value [0] EXPLICIT ANY DEFINED BY type-id }
1668 *
1669 * HardwareModuleName ::= SEQUENCE {
1670 * hwType OBJECT IDENTIFIER,
1671 * hwSerialNum OCTET STRING }
1672 *
1673 * NOTE: we currently only parse and use otherName of type HwModuleName,
1674 * as defined in RFC 4108.
1675 */
1676static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1677 mbedtls_x509_san_other_name *other_name )
1678{
Janos Follathab23cd12019-05-09 13:53:57 +01001679 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001680 size_t len;
1681 unsigned char *p = subject_alt_name->p;
1682 const unsigned char *end = p + subject_alt_name->len;
1683 mbedtls_x509_buf cur_oid;
1684
1685 if( ( subject_alt_name->tag &
1686 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1687 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1688 {
1689 /*
1690 * The given subject alternative name is not of type "othername".
1691 */
1692 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1693 }
1694
1695 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1696 MBEDTLS_ASN1_OID ) ) != 0 )
1697 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1698
1699 cur_oid.tag = MBEDTLS_ASN1_OID;
1700 cur_oid.p = p;
1701 cur_oid.len = len;
1702
1703 /*
1704 * Only HwModuleName is currently supported.
1705 */
1706 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1707 {
1708 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1709 }
1710
Ron Eldor890819a2019-05-13 19:03:04 +03001711 if( p + len >= end )
1712 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001713 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001714 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1715 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1716 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001717 p += len;
1718 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1719 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
1720 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1721
1722 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1723 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1724 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1725
1726 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
1727 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1728
1729 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1730 other_name->value.hardware_module_name.oid.p = p;
1731 other_name->value.hardware_module_name.oid.len = len;
1732
Ron Eldor890819a2019-05-13 19:03:04 +03001733 if( p + len >= end )
1734 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001735 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001736 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1737 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1738 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001739 p += len;
1740 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1741 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Ron Eldor890819a2019-05-13 19:03:04 +03001742 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001743
1744 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1745 other_name->value.hardware_module_name.val.p = p;
1746 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001747 p += len;
1748 if( p != end )
1749 {
Ron Eldor890819a2019-05-13 19:03:04 +03001750 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001751 sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001752 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1753 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001754 }
1755 return( 0 );
1756}
1757
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001758static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001759 const mbedtls_x509_sequence
1760 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001761 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001762{
Janos Follath865b3eb2019-12-16 11:46:15 +00001763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001764 size_t n = *size;
1765 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001767 mbedtls_x509_subject_alternative_name san;
1768 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001769
1770 while( cur != NULL )
1771 {
Ron Eldor890819a2019-05-13 19:03:04 +03001772 memset( &san, 0, sizeof( san ) );
1773 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1774 if( parse_ret != 0 )
1775 {
1776 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1777 {
1778 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1779 MBEDTLS_X509_SAFE_SNPRINTF;
1780 }
1781 else
1782 {
1783 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1784 MBEDTLS_X509_SAFE_SNPRINTF;
1785 }
1786 cur = cur->next;
1787 continue;
1788 }
1789
1790 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001791 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001792 /*
1793 * otherName
1794 */
Ron Eldora2913912019-05-16 16:17:38 +03001795 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001796 {
1797 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1798
1799 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1800 MBEDTLS_X509_SAFE_SNPRINTF;
1801
1802 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1803 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001804 {
Ron Eldor890819a2019-05-13 19:03:04 +03001805 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1806 MBEDTLS_X509_SAFE_SNPRINTF;
1807 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001808 MBEDTLS_X509_SAFE_SNPRINTF;
1809
Ron Eldor890819a2019-05-13 19:03:04 +03001810 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1811 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001812
Ron Eldor890819a2019-05-13 19:03:04 +03001813 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1814 MBEDTLS_X509_SAFE_SNPRINTF;
1815
1816 if( other_name->value.hardware_module_name.val.len >= n )
1817 {
1818 *p = '\0';
1819 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001820 }
1821
Ron Eldora2913912019-05-16 16:17:38 +03001822 memcpy( p, other_name->value.hardware_module_name.val.p,
1823 other_name->value.hardware_module_name.val.len );
1824 p += other_name->value.hardware_module_name.val.len;
1825
Ron Eldor890819a2019-05-13 19:03:04 +03001826 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001827
Ron Eldor890819a2019-05-13 19:03:04 +03001828 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1829 }
1830 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001831
1832 /*
1833 * dNSName
1834 */
Ron Eldora2913912019-05-16 16:17:38 +03001835 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001836 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001837 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1838 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001839 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001840 {
1841 *p = '\0';
1842 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1843 }
Ron Eldora2913912019-05-16 16:17:38 +03001844
1845 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1846 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001847 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001848 }
1849 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001850
1851 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001852 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001853 */
1854 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001855 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1856 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001857 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001858 }
1859
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001860 cur = cur->next;
1861 }
1862
1863 *p = '\0';
1864
1865 *size = n;
1866 *buf = p;
1867
1868 return( 0 );
1869}
1870
Ron Eldor890819a2019-05-13 19:03:04 +03001871int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1872 mbedtls_x509_subject_alternative_name *san )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001873{
Janos Follath865b3eb2019-12-16 11:46:15 +00001874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor890819a2019-05-13 19:03:04 +03001875 switch( san_buf->tag &
1876 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1877 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001878 {
Ron Eldor890819a2019-05-13 19:03:04 +03001879 /*
1880 * otherName
1881 */
1882 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001883 {
Ron Eldor890819a2019-05-13 19:03:04 +03001884 mbedtls_x509_san_other_name other_name;
1885
1886 ret = x509_get_other_name( san_buf, &other_name );
1887 if( ret != 0 )
1888 return( ret );
1889
1890 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1891 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1892 memcpy( &san->san.other_name,
1893 &other_name, sizeof( other_name ) );
1894
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001895 }
Ron Eldor890819a2019-05-13 19:03:04 +03001896 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001897
Ron Eldor890819a2019-05-13 19:03:04 +03001898 /*
1899 * dNSName
1900 */
1901 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001902 {
Ron Eldor890819a2019-05-13 19:03:04 +03001903 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1904 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001905
Ron Eldor890819a2019-05-13 19:03:04 +03001906 memcpy( &san->san.unstructured_name,
1907 san_buf, sizeof( *san_buf ) );
Janos Follath6c379b42019-05-10 14:17:16 +01001908
Ron Eldor890819a2019-05-13 19:03:04 +03001909 }
1910 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001911
Ron Eldor890819a2019-05-13 19:03:04 +03001912 /*
1913 * Type not supported
1914 */
1915 default:
1916 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1917 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001918 return( 0 );
1919}
1920
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001921#define PRINT_ITEM(i) \
1922 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001924 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001925 sep = ", "; \
1926 }
1927
1928#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001929 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001930 PRINT_ITEM( name );
1931
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001932static int x509_info_cert_type( char **buf, size_t *size,
1933 unsigned char ns_cert_type )
1934{
Janos Follath865b3eb2019-12-16 11:46:15 +00001935 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001936 size_t n = *size;
1937 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001938 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001941 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1943 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1944 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001945 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1946 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1947 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001948
1949 *size = n;
1950 *buf = p;
1951
1952 return( 0 );
1953}
1954
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001955#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001956 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001957 PRINT_ITEM( name );
1958
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001959static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001960 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001961{
Janos Follath865b3eb2019-12-16 11:46:15 +00001962 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001963 size_t n = *size;
1964 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001965 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1968 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001969 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1970 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1971 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1973 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001974 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1975 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001976
1977 *size = n;
1978 *buf = p;
1979
1980 return( 0 );
1981}
1982
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001983static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001985{
Janos Follath865b3eb2019-12-16 11:46:15 +00001986 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001987 const char *desc;
1988 size_t n = *size;
1989 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001991 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001992
1993 while( cur != NULL )
1994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001996 desc = "???";
1997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001999 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002000
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002001 sep = ", ";
2002
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002003 cur = cur->next;
2004 }
2005
2006 *size = n;
2007 *buf = p;
2008
2009 return( 0 );
2010}
2011
Ron Eldor74d9acc2019-03-21 14:00:03 +02002012static int x509_info_cert_policies( char **buf, size_t *size,
2013 const mbedtls_x509_sequence *certificate_policies )
2014{
Janos Follath865b3eb2019-12-16 11:46:15 +00002015 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002016 const char *desc;
2017 size_t n = *size;
2018 char *p = *buf;
2019 const mbedtls_x509_sequence *cur = certificate_policies;
2020 const char *sep = "";
2021
2022 while( cur != NULL )
2023 {
2024 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2025 desc = "???";
2026
2027 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2028 MBEDTLS_X509_SAFE_SNPRINTF;
2029
2030 sep = ", ";
2031
2032 cur = cur->next;
2033 }
2034
2035 *size = n;
2036 *buf = p;
2037
2038 return( 0 );
2039}
2040
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002041/*
2042 * Return an informational string about the certificate.
2043 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002044#define BEFORE_COLON 18
2045#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2047 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002048{
Janos Follath865b3eb2019-12-16 11:46:15 +00002049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002050 size_t n;
2051 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002052 char key_size_str[BEFORE_COLON];
2053
2054 p = buf;
2055 n = size;
2056
Janos Follath98e28a72016-05-31 14:03:54 +01002057 if( NULL == crt )
2058 {
2059 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2060 MBEDTLS_X509_SAFE_SNPRINTF;
2061
2062 return( (int) ( size - n ) );
2063 }
2064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002066 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002067 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002069 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002070 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002073 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002076 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002078 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002081 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002083 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002086 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2087 crt->valid_from.year, crt->valid_from.mon,
2088 crt->valid_from.day, crt->valid_from.hour,
2089 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002090 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002093 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2094 crt->valid_to.year, crt->valid_to.mon,
2095 crt->valid_to.day, crt->valid_to.hour,
2096 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002097 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002100 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002103 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002104 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002105
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002106 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2108 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002109 {
2110 return( ret );
2111 }
2112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002114 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002115 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002116
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002117 /*
2118 * Optional extensions
2119 */
2120
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002121 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002124 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002125 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002126
2127 if( crt->max_pathlen > 0 )
2128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002130 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002131 }
2132 }
2133
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002134 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002135 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002136 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002137 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002138
2139 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002140 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002141 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002142 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002143 }
2144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002148 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002149
2150 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2151 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002152 }
2153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002157 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002158
2159 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2160 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002161 }
2162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002166 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002167
2168 if( ( ret = x509_info_ext_key_usage( &p, &n,
2169 &crt->ext_key_usage ) ) != 0 )
2170 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002171 }
2172
Ron Eldor74d9acc2019-03-21 14:00:03 +02002173 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2174 {
2175 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2176 MBEDTLS_X509_SAFE_SNPRINTF;
2177
2178 if( ( ret = x509_info_cert_policies( &p, &n,
2179 &crt->certificate_policies ) ) != 0 )
2180 return( ret );
2181 }
2182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002184 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002185
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002186 return( (int) ( size - n ) );
2187}
2188
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002189struct x509_crt_verify_string {
2190 int code;
2191 const char *string;
2192};
2193
2194static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002195 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002196 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2197 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2198 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2199 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2200 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002201 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2202 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2203 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002204 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002205 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2206 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2207 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2208 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002209 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2210 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2211 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2212 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2213 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2214 { 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 +01002215 { 0, NULL }
2216};
2217
2218int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002219 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002220{
Janos Follath865b3eb2019-12-16 11:46:15 +00002221 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002222 const struct x509_crt_verify_string *cur;
2223 char *p = buf;
2224 size_t n = size;
2225
2226 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2227 {
2228 if( ( flags & cur->code ) == 0 )
2229 continue;
2230
2231 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002232 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002233 flags ^= cur->code;
2234 }
2235
2236 if( flags != 0 )
2237 {
2238 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2239 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002240 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002241 }
2242
2243 return( (int) ( size - n ) );
2244}
2245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002247int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2248 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002249{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002250 unsigned int usage_must, usage_may;
2251 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2252 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2253
2254 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2255 return( 0 );
2256
2257 usage_must = usage & ~may_mask;
2258
2259 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2260 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2261
2262 usage_may = usage & may_mask;
2263
2264 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002266
2267 return( 0 );
2268}
2269#endif
2270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2272int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002273 const char *usage_oid,
2274 size_t usage_len )
2275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002277
2278 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002280 return( 0 );
2281
2282 /*
2283 * Look for the requested usage (or wildcard ANY) in our list
2284 */
2285 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002288
2289 if( cur_oid->len == usage_len &&
2290 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2291 {
2292 return( 0 );
2293 }
2294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002296 return( 0 );
2297 }
2298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002304/*
2305 * Return 1 if the certificate is revoked, or 0 otherwise.
2306 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002307int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002308{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002310
2311 while( cur != NULL && cur->serial.len != 0 )
2312 {
2313 if( crt->serial.len == cur->serial.len &&
2314 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2315 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002316 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002317 return( 1 );
2318 }
2319
2320 cur = cur->next;
2321 }
2322
2323 return( 0 );
2324}
2325
2326/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002327 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002328 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002331 mbedtls_x509_crl *crl_list,
2332 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002333{
2334 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2336 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002337
2338 if( ca == NULL )
2339 return( flags );
2340
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002341 while( crl_list != NULL )
2342 {
2343 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002344 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002345 {
2346 crl_list = crl_list->next;
2347 continue;
2348 }
2349
2350 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002351 * Check if the CA is configured to sign CRLs
2352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002354 if( mbedtls_x509_crt_check_key_usage( ca,
2355 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002358 break;
2359 }
2360#endif
2361
2362 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002363 * Check if CRL is correctly signed by the trusted CA
2364 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002365 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2366 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2367
2368 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2369 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002372 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002373 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002374 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002376 break;
2377 }
2378
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002379 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002380 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2383 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002384 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002387 break;
2388 }
2389
2390 /*
2391 * Check for validity of CRL (Do not drop out)
2392 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002393 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002395
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002396 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002397 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002398
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002399 /*
2400 * Check if certificate is revoked
2401 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002402 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002405 break;
2406 }
2407
2408 crl_list = crl_list->next;
2409 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002410
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002411 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002414
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002415/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002416 * Check the signature of a certificate by its parent
2417 */
2418static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002419 mbedtls_x509_crt *parent,
2420 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002421{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002422 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002423 size_t hash_len;
2424#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2425 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002426 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002427 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002428
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002429 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002430 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002431 return( -1 );
2432#else
Jaeden Amero34973232019-02-20 10:32:28 +00002433 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002434 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2435
2436 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2437 return( -1 );
2438
2439 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2440 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002441 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002442 return( -1 );
2443 }
2444
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002445 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2446 != PSA_SUCCESS )
2447 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002448 return( -1 );
2449 }
2450#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002451 /* Skip expensive computation on obvious mismatch */
2452 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002453 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002454
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002455#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002456 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2457 {
2458 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002459 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002460 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002461 }
2462#else
2463 (void) rs_ctx;
2464#endif
2465
2466 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002467 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002468 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002469}
2470
2471/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002472 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2473 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002474 *
2475 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2478 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002479 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002480{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002481 int need_ca_bit;
2482
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002483 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002484 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002485 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002486
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002487 /* Parent must have the basicConstraints CA bit set as a general rule */
2488 need_ca_bit = 1;
2489
2490 /* Exception: v1/v2 certificates that are locally trusted. */
2491 if( top && parent->version < 3 )
2492 need_ca_bit = 0;
2493
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002494 if( need_ca_bit && ! parent->ca_istrue )
2495 return( -1 );
2496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002498 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002500 {
2501 return( -1 );
2502 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002503#endif
2504
2505 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002506}
2507
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002508/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002509 * Find a suitable parent for child in candidates, or return NULL.
2510 *
2511 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002512 * 1. subject name matches child's issuer
2513 * 2. if necessary, the CA bit is set and key usage allows signing certs
2514 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002515 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002516 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002517 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002518 * If there's a suitable candidate which is also time-valid, return the first
2519 * such. Otherwise, return the first suitable candidate (or NULL if there is
2520 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002521 *
2522 * The rationale for this rule is that someone could have a list of trusted
2523 * roots with two versions on the same root with different validity periods.
2524 * (At least one user reported having such a list and wanted it to just work.)
2525 * The reason we don't just require time-validity is that generally there is
2526 * only one version, and if it's expired we want the flags to state that
2527 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002528 *
2529 * The rationale for rule 3 (signature for trusted roots) is that users might
2530 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002531 * way we select the correct one is by checking the signature (as we don't
2532 * rely on key identifier extensions). (This is one way users might choose to
2533 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002534 *
2535 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002536 * - [in] child: certificate for which we're looking for a parent
2537 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002538 * - [out] r_parent: parent found (or NULL)
2539 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002540 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2541 * of the chain, 0 otherwise
2542 * - [in] path_cnt: number of intermediates seen so far
2543 * - [in] self_cnt: number of self-signed intermediates seen so far
2544 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002545 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002546 *
2547 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002548 * - 0 on success
2549 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002550 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002551static int x509_crt_find_parent_in(
2552 mbedtls_x509_crt *child,
2553 mbedtls_x509_crt *candidates,
2554 mbedtls_x509_crt **r_parent,
2555 int *r_signature_is_good,
2556 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002557 unsigned path_cnt,
2558 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002559 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002560{
Janos Follath865b3eb2019-12-16 11:46:15 +00002561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002562 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002563 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002564
2565#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002566 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002567 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2568 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002569 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002570 parent = rs_ctx->parent;
2571 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002572 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002573
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002574 /* clear saved state */
2575 rs_ctx->parent = NULL;
2576 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002577 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002578
2579 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002580 goto check_signature;
2581 }
2582#endif
2583
2584 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002585 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002586
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002587 for( parent = candidates; parent != NULL; parent = parent->next )
2588 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002589 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002590 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002591 continue;
2592
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002593 /* +1 because stored max_pathlen is 1 higher that the actual value */
2594 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002595 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002596 {
2597 continue;
2598 }
2599
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002600 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002601#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2602check_signature:
2603#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002604 ret = x509_crt_check_signature( child, parent, rs_ctx );
2605
2606#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002607 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2608 {
2609 /* save state */
2610 rs_ctx->parent = parent;
2611 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002612 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002613
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002614 return( ret );
2615 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002616#else
2617 (void) ret;
2618#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002619
2620 signature_is_good = ret == 0;
2621 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002622 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002623
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002624 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002625 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2626 mbedtls_x509_time_is_future( &parent->valid_from ) )
2627 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002628 if( fallback_parent == NULL )
2629 {
2630 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002631 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002632 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002633
2634 continue;
2635 }
2636
Andy Gross1f627142019-01-30 10:25:53 -06002637 *r_parent = parent;
2638 *r_signature_is_good = signature_is_good;
2639
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002640 break;
2641 }
2642
Andy Gross1f627142019-01-30 10:25:53 -06002643 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002644 {
2645 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002646 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002647 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002648
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002649 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002650}
2651
2652/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002653 * Find a parent in trusted CAs or the provided chain, or return NULL.
2654 *
2655 * Searches in trusted CAs first, and return the first suitable parent found
2656 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002657 *
2658 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002659 * - [in] child: certificate for which we're looking for a parent, followed
2660 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002661 * - [in] trust_ca: list of locally trusted certificates
2662 * - [out] parent: parent found (or NULL)
2663 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2664 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2665 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2666 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002667 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002668 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002669 *
2670 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002671 * - 0 on success
2672 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002673 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002674static int x509_crt_find_parent(
2675 mbedtls_x509_crt *child,
2676 mbedtls_x509_crt *trust_ca,
2677 mbedtls_x509_crt **parent,
2678 int *parent_is_trusted,
2679 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002680 unsigned path_cnt,
2681 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002682 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002683{
Janos Follath865b3eb2019-12-16 11:46:15 +00002684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002685 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002686
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002687 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002688
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002689#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002690 /* restore then clear saved state if we have some stored */
2691 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2692 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002693 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002694 rs_ctx->parent_is_trusted = -1;
2695 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002696#endif
2697
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002698 while( 1 ) {
2699 search_list = *parent_is_trusted ? trust_ca : child->next;
2700
2701 ret = x509_crt_find_parent_in( child, search_list,
2702 parent, signature_is_good,
2703 *parent_is_trusted,
2704 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002705
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002706#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002707 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2708 {
2709 /* save state */
2710 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002711 return( ret );
2712 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002713#else
2714 (void) ret;
2715#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002716
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002717 /* stop here if found or already in second iteration */
2718 if( *parent != NULL || *parent_is_trusted == 0 )
2719 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002720
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002721 /* prepare second iteration */
2722 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002723 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002724
2725 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002726 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002727 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002728 *parent_is_trusted = 0;
2729 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002730 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002731
2732 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002733}
2734
2735/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002736 * Check if an end-entity certificate is locally trusted
2737 *
2738 * Currently we require such certificates to be self-signed (actually only
2739 * check for self-issued as self-signatures are not checked)
2740 */
2741static int x509_crt_check_ee_locally_trusted(
2742 mbedtls_x509_crt *crt,
2743 mbedtls_x509_crt *trust_ca )
2744{
2745 mbedtls_x509_crt *cur;
2746
2747 /* must be self-issued */
2748 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2749 return( -1 );
2750
2751 /* look for an exact match with trusted cert */
2752 for( cur = trust_ca; cur != NULL; cur = cur->next )
2753 {
2754 if( crt->raw.len == cur->raw.len &&
2755 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2756 {
2757 return( 0 );
2758 }
2759 }
2760
2761 /* too bad */
2762 return( -1 );
2763}
2764
2765/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002766 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002767 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002768 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2769 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002770 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002771 * such that every cert in the chain is a child of the next one,
2772 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002773 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002774 * Verify that chain and return it with flags for all issues found.
2775 *
2776 * Special cases:
2777 * - EE == Rj -> return a one-element list containing it
2778 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2779 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002780 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002781 * Tests for (aspects of) this function should include at least:
2782 * - trusted EE
2783 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002784 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002785 * - if relevant: EE untrusted
2786 * - if relevant: EE -> intermediate, untrusted
2787 * with the aspect under test checked at each relevant level (EE, int, root).
2788 * For some aspects longer chains are required, but usually length 2 is
2789 * enough (but length 1 is not in general).
2790 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002791 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002792 * - [in] crt: the cert list EE, C1, ..., Cn
2793 * - [in] trust_ca: the trusted list R1, ..., Rp
2794 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002795 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002796 * Only valid when return value is 0, may contain garbage otherwise!
2797 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002798 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002799 *
2800 * Return value:
2801 * - non-zero if the chain could not be fully built and examined
2802 * - 0 is the chain was successfully built and examined,
2803 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002804 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002805static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002806 mbedtls_x509_crt *crt,
2807 mbedtls_x509_crt *trust_ca,
2808 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002809 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2810 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002811 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002812 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002813 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002814{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002815 /* Don't initialize any of those variables here, so that the compiler can
2816 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002818 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002819 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002820 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002821 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002822 int parent_is_trusted;
2823 int child_is_trusted;
2824 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002825 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002826 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002827
2828#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2829 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002830 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002831 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002832 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002833 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002834 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002835
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002836 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002837 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002838 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002839 flags = &cur->flags;
2840
2841 goto find_parent;
2842 }
2843#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002844
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002845 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002846 self_cnt = 0;
2847 parent_is_trusted = 0;
2848 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002849
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002850 while( 1 ) {
2851 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002852 cur = &ver_chain->items[ver_chain->len];
2853 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002854 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002855 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002856 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002857
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002858 /* Check time-validity (all certificates) */
2859 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2860 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002861
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002862 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2863 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002864
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002865 /* Stop here for trusted roots (but not for trusted EE certs) */
2866 if( child_is_trusted )
2867 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002868
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002869 /* Check signature algorithm: MD & PK algs */
2870 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2871 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002872
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002873 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2874 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002875
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002876 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002877 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002878 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2879 {
2880 return( 0 );
2881 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002882
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002883#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2884find_parent:
2885#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002886
2887 /* Obtain list of potential trusted signers from CA callback,
2888 * or use statically provided list. */
2889#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2890 if( f_ca_cb != NULL )
2891 {
2892 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2893 mbedtls_free( ver_chain->trust_ca_cb_result );
2894 ver_chain->trust_ca_cb_result = NULL;
2895
2896 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2897 if( ret != 0 )
2898 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2899
2900 cur_trust_ca = ver_chain->trust_ca_cb_result;
2901 }
2902 else
2903#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2904 {
2905 ((void) f_ca_cb);
2906 ((void) p_ca_cb);
2907 cur_trust_ca = trust_ca;
2908 }
2909
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002910 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002911 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002912 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002913 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002914
2915#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002916 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2917 {
2918 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002919 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002920 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002921 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002922
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002923 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002924 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002925#else
2926 (void) ret;
2927#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002928
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002929 /* No parent? We're done here */
2930 if( parent == NULL )
2931 {
2932 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2933 return( 0 );
2934 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002935
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002936 /* Count intermediate self-issued (not necessarily self-signed) certs.
2937 * These can occur with some strategies for key rollover, see [SIRO],
2938 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002939 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002940 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2941 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002942 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002943 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002944
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002945 /* path_cnt is 0 for the first intermediate CA,
2946 * and if parent is trusted it's not an intermediate CA */
2947 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002948 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002949 {
2950 /* return immediately to avoid overflow the chain array */
2951 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2952 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002953
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002954 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002955 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002956 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2957
2958 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002959 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002960 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002963 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002964 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002965#else
2966 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002967#endif
2968
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002969 /* prepare for next iteration */
2970 child = parent;
2971 parent = NULL;
2972 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002973 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002974 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002975}
2976
2977/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002978 * Check for CN match
2979 */
2980static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2981 const char *cn, size_t cn_len )
2982{
2983 /* try exact match */
2984 if( name->len == cn_len &&
2985 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2986 {
2987 return( 0 );
2988 }
2989
2990 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002991 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002992 {
2993 return( 0 );
2994 }
2995
2996 return( -1 );
2997}
2998
2999/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003000 * Verify the requested CN - only call this if cn is not NULL!
3001 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003002static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003003 const char *cn,
3004 uint32_t *flags )
3005{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003006 const mbedtls_x509_name *name;
3007 const mbedtls_x509_sequence *cur;
3008 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003009
3010 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3011 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003012 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003013 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003014 if( x509_crt_check_cn( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003015 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003016 }
3017
3018 if( cur == NULL )
3019 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3020 }
3021 else
3022 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003023 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003024 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003025 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3026 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003027 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003028 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003029 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003030 }
3031
3032 if( name == NULL )
3033 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3034 }
3035}
3036
3037/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003038 * Merge the flags for all certs in the chain, after calling callback
3039 */
3040static int x509_crt_merge_flags_with_cb(
3041 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003042 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003043 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3044 void *p_vrfy )
3045{
Janos Follath865b3eb2019-12-16 11:46:15 +00003046 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003047 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003048 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003049 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003050
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003051 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003052 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003053 cur = &ver_chain->items[i-1];
3054 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003055
3056 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003057 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003058 return( ret );
3059
3060 *flags |= cur_flags;
3061 }
3062
3063 return( 0 );
3064}
3065
3066/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003067 * Verify the certificate validity, with profile, restartable version
3068 *
3069 * This function:
3070 * - checks the requested CN (if any)
3071 * - checks the type and size of the EE cert's key,
3072 * as that isn't done as part of chain building/verification currently
3073 * - builds and verifies the chain
3074 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003075 *
3076 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3077 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3078 * verification routine to search for trusted signers, and CRLs will
3079 * be disabled. Otherwise, `trust_ca` will be used as the static list
3080 * of trusted signers, and `ca_crl` will be use as the static list
3081 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003082 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003083static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003084 mbedtls_x509_crt *trust_ca,
3085 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003086 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3087 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003088 const mbedtls_x509_crt_profile *profile,
3089 const char *cn, uint32_t *flags,
3090 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3091 void *p_vrfy,
3092 mbedtls_x509_crt_restart_ctx *rs_ctx )
3093{
Janos Follath865b3eb2019-12-16 11:46:15 +00003094 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003095 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003096 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003097 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003098
3099 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003100 ee_flags = 0;
3101 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003102
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003103 if( profile == NULL )
3104 {
3105 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3106 goto exit;
3107 }
3108
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003109 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003110 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003111 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003112
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003113 /* Check the type and size of the key */
3114 pk_type = mbedtls_pk_get_type( &crt->pk );
3115
3116 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003117 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003118
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003119 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003120 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003121
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003122 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003123 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3124 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003125 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003126
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003127 if( ret != 0 )
3128 goto exit;
3129
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003130 /* Merge end-entity flags */
3131 ver_chain.items[0].flags |= ee_flags;
3132
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003133 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003134 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003135
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003136exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003137
3138#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3139 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3140 mbedtls_free( ver_chain.trust_ca_cb_result );
3141 ver_chain.trust_ca_cb_result = NULL;
3142#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3143
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003144#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3145 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3146 mbedtls_x509_crt_restart_free( rs_ctx );
3147#endif
3148
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003149 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3150 * the SSL module for authmode optional, but non-zero return from the
3151 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003152 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3153 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3154
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003155 if( ret != 0 )
3156 {
3157 *flags = (uint32_t) -1;
3158 return( ret );
3159 }
3160
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003161 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003163
3164 return( 0 );
3165}
3166
Hanno Becker3116fb32019-03-28 13:34:42 +00003167
3168/*
3169 * Verify the certificate validity (default profile, not restartable)
3170 */
3171int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3172 mbedtls_x509_crt *trust_ca,
3173 mbedtls_x509_crl *ca_crl,
3174 const char *cn, uint32_t *flags,
3175 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3176 void *p_vrfy )
3177{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003178 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003179 NULL, NULL,
3180 &mbedtls_x509_crt_profile_default,
3181 cn, flags,
3182 f_vrfy, p_vrfy, NULL ) );
3183}
3184
3185/*
3186 * Verify the certificate validity (user-chosen profile, not restartable)
3187 */
3188int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3189 mbedtls_x509_crt *trust_ca,
3190 mbedtls_x509_crl *ca_crl,
3191 const mbedtls_x509_crt_profile *profile,
3192 const char *cn, uint32_t *flags,
3193 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3194 void *p_vrfy )
3195{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003196 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003197 NULL, NULL,
3198 profile, cn, flags,
3199 f_vrfy, p_vrfy, NULL ) );
3200}
3201
3202#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3203/*
3204 * Verify the certificate validity (user-chosen profile, CA callback,
3205 * not restartable).
3206 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003207int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003208 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3209 void *p_ca_cb,
3210 const mbedtls_x509_crt_profile *profile,
3211 const char *cn, uint32_t *flags,
3212 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3213 void *p_vrfy )
3214{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003215 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003216 f_ca_cb, p_ca_cb,
3217 profile, cn, flags,
3218 f_vrfy, p_vrfy, NULL ) );
3219}
3220#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3221
3222int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3223 mbedtls_x509_crt *trust_ca,
3224 mbedtls_x509_crl *ca_crl,
3225 const mbedtls_x509_crt_profile *profile,
3226 const char *cn, uint32_t *flags,
3227 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3228 void *p_vrfy,
3229 mbedtls_x509_crt_restart_ctx *rs_ctx )
3230{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003231 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003232 NULL, NULL,
3233 profile, cn, flags,
3234 f_vrfy, p_vrfy, rs_ctx ) );
3235}
3236
3237
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003238/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003239 * Initialize a certificate chain
3240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003244}
3245
3246/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003247 * Unallocate all certificate data
3248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003250{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 mbedtls_x509_crt *cert_cur = crt;
3252 mbedtls_x509_crt *cert_prv;
3253 mbedtls_x509_name *name_cur;
3254 mbedtls_x509_name *name_prv;
3255 mbedtls_x509_sequence *seq_cur;
3256 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003257
3258 if( crt == NULL )
3259 return;
3260
3261 do
3262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3266 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003267#endif
3268
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003269 name_cur = cert_cur->issuer.next;
3270 while( name_cur != NULL )
3271 {
3272 name_prv = name_cur;
3273 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003274 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003276 }
3277
3278 name_cur = cert_cur->subject.next;
3279 while( name_cur != NULL )
3280 {
3281 name_prv = name_cur;
3282 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003283 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003285 }
3286
3287 seq_cur = cert_cur->ext_key_usage.next;
3288 while( seq_cur != NULL )
3289 {
3290 seq_prv = seq_cur;
3291 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003292 mbedtls_platform_zeroize( seq_prv,
3293 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003295 }
3296
3297 seq_cur = cert_cur->subject_alt_names.next;
3298 while( seq_cur != NULL )
3299 {
3300 seq_prv = seq_cur;
3301 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003302 mbedtls_platform_zeroize( seq_prv,
3303 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003305 }
3306
Ron Eldor74d9acc2019-03-21 14:00:03 +02003307 seq_cur = cert_cur->certificate_policies.next;
3308 while( seq_cur != NULL )
3309 {
3310 seq_prv = seq_cur;
3311 seq_cur = seq_cur->next;
3312 mbedtls_platform_zeroize( seq_prv,
3313 sizeof( mbedtls_x509_sequence ) );
3314 mbedtls_free( seq_prv );
3315 }
3316
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003317 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003318 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003319 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003321 }
3322
3323 cert_cur = cert_cur->next;
3324 }
3325 while( cert_cur != NULL );
3326
3327 cert_cur = crt;
3328 do
3329 {
3330 cert_prv = cert_cur;
3331 cert_cur = cert_cur->next;
3332
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003333 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003334 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003336 }
3337 while( cert_cur != NULL );
3338}
3339
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003340#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3341/*
3342 * Initialize a restart context
3343 */
3344void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3345{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003346 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003347
3348 ctx->parent = NULL;
3349 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003350 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003351
3352 ctx->parent_is_trusted = -1;
3353
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003354 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003355 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003356 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003357}
3358
3359/*
3360 * Free the components of a restart context
3361 */
3362void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3363{
3364 if( ctx == NULL )
3365 return;
3366
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003367 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003368 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003369}
3370#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372#endif /* MBEDTLS_X509_CRT_PARSE_C */