blob: 1e62ed5b0688ca224c5c53fb1ae4f65ad9c57e7b [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,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896{
Janos Follath865b3eb2019-12-16 11:46:15 +0000897 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 size_t len;
899 unsigned char *end_ext_data, *end_ext_octet;
900
Hanno Becker12f62fb2019-02-12 17:22:36 +0000901 if( *p == end )
902 return( 0 );
903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906
Hanno Becker12f62fb2019-02-12 17:22:36 +0000907 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908 while( *p < end )
909 {
910 /*
911 * Extension ::= SEQUENCE {
912 * extnID OBJECT IDENTIFIER,
913 * critical BOOLEAN DEFAULT FALSE,
914 * extnValue OCTET STRING }
915 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917 int is_critical = 0; /* DEFAULT FALSE */
918 int ext_type = 0;
919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
921 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
922 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923
924 end_ext_data = *p + len;
925
926 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200927 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200928 MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930
k-stachowiak463928a2018-07-24 12:50:59 +0200931 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932 extn_oid.p = *p;
933 *p += extn_oid.len;
934
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
937 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
938 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939
940 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
942 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
943 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944
945 end_ext_octet = *p + len;
946
947 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
949 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950
951 /*
952 * Detect supported extensions
953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955
956 if( ret != 0 )
957 {
958 /* No parser found, skip extension */
959 *p = end_ext_octet;
960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 if( is_critical )
963 {
964 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
966 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 }
968#endif
969 continue;
970 }
971
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100972 /* Forbid repeated extensions */
973 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100975
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976 crt->ext_types |= ext_type;
977
978 switch( ext_type )
979 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100980 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 /* Parse basic constraints */
982 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
983 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200984 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 break;
986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988 /* Parse key usage */
989 if( ( ret = x509_get_key_usage( p, end_ext_octet,
990 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200991 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 break;
993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995 /* Parse extended key usage */
996 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
997 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200998 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999 break;
1000
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001001 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 /* Parse subject alt name */
1003 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1004 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001005 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006 break;
1007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 /* Parse netscape certificate type */
1010 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1011 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001012 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 break;
1014
Ron Eldor74d9acc2019-03-21 14:00:03 +02001015 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1016 /* Parse certificate policies type */
1017 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1018 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001019 {
1020#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1021 if( is_critical )
1022 return( ret );
1023 else
1024#endif
1025 /*
Ron Eldora2913912019-05-16 16:17:38 +03001026 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1027 * cannot interpret or enforce the policy. However, it is up to
1028 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001029 * unless the extension is critical.
1030 */
1031 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1032 return( ret );
1033 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001034 break;
1035
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001037 /*
1038 * If this is a non-critical extension, which the oid layer
1039 * supports, but there isn't an x509 parser for it,
1040 * skip the extension.
1041 */
1042#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1043 if( is_critical )
1044 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1045 else
1046#endif
1047 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 }
1049 }
1050
1051 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1053 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001054
1055 return( 0 );
1056}
1057
1058/*
1059 * Parse and fill a single X.509 certificate in DER format
1060 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001061static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1062 const unsigned char *buf,
1063 size_t buflen,
1064 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065{
Janos Follath865b3eb2019-12-16 11:46:15 +00001066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067 size_t len;
1068 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1072 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1073 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074
1075 /*
1076 * Check for valid input
1077 */
1078 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001081 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001082 p = (unsigned char*) buf;
1083 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001084 end = p + len;
1085
1086 /*
1087 * Certificate ::= SEQUENCE {
1088 * tbsCertificate TBSCertificate,
1089 * signatureAlgorithm AlgorithmIdentifier,
1090 * signatureValue BIT STRING }
1091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1093 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 mbedtls_x509_crt_free( crt );
1096 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097 }
1098
Janos Follathcc0e49d2016-02-17 14:34:12 +00001099 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001100 crt->raw.len = crt_end - buf;
1101 if( make_copy != 0 )
1102 {
1103 /* Create and populate a new buffer for the raw field. */
1104 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1105 if( crt->raw.p == NULL )
1106 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1107
1108 memcpy( crt->raw.p, buf, crt->raw.len );
1109 crt->own_buffer = 1;
1110
1111 p += crt->raw.len - len;
1112 end = crt_end = p + len;
1113 }
1114 else
1115 {
1116 crt->raw.p = (unsigned char*) buf;
1117 crt->own_buffer = 0;
1118 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001119
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120 /*
1121 * TBSCertificate ::= SEQUENCE {
1122 */
1123 crt->tbs.p = p;
1124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1126 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 mbedtls_x509_crt_free( crt );
1129 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001130 }
1131
1132 end = p + len;
1133 crt->tbs.len = end - crt->tbs.p;
1134
1135 /*
1136 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1137 *
1138 * CertificateSerialNumber ::= INTEGER
1139 *
1140 * signature AlgorithmIdentifier
1141 */
1142 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1144 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001145 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 return( ret );
1149 }
1150
Andres AG7ca4a032017-03-09 16:16:11 +00001151 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 mbedtls_x509_crt_free( crt );
1154 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155 }
1156
Andres AG7ca4a032017-03-09 16:16:11 +00001157 crt->version++;
1158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001160 &crt->sig_md, &crt->sig_pk,
1161 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164 return( ret );
1165 }
1166
1167 /*
1168 * issuer Name
1169 */
1170 crt->issuer_raw.p = p;
1171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1173 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 mbedtls_x509_crt_free( crt );
1176 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001177 }
1178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 return( ret );
1183 }
1184
1185 crt->issuer_raw.len = p - crt->issuer_raw.p;
1186
1187 /*
1188 * Validity ::= SEQUENCE {
1189 * notBefore Time,
1190 * notAfter Time }
1191 *
1192 */
1193 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1194 &crt->valid_to ) ) != 0 )
1195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001197 return( ret );
1198 }
1199
1200 /*
1201 * subject Name
1202 */
1203 crt->subject_raw.p = p;
1204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1206 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 mbedtls_x509_crt_free( crt );
1209 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001210 }
1211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001215 return( ret );
1216 }
1217
1218 crt->subject_raw.len = p - crt->subject_raw.p;
1219
1220 /*
1221 * SubjectPublicKeyInfo
1222 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001223 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227 return( ret );
1228 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001229 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001230
1231 /*
1232 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1233 * -- If present, version shall be v2 or v3
1234 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1235 * -- If present, version shall be v2 or v3
1236 * extensions [3] EXPLICIT Extensions OPTIONAL
1237 * -- If present, version shall be v3
1238 */
1239 if( crt->version == 2 || crt->version == 3 )
1240 {
1241 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1242 if( ret != 0 )
1243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245 return( ret );
1246 }
1247 }
1248
1249 if( crt->version == 2 || crt->version == 3 )
1250 {
1251 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1252 if( ret != 0 )
1253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001255 return( ret );
1256 }
1257 }
1258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001260 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001261#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001262 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001263 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001264 if( ret != 0 )
1265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001267 return( ret );
1268 }
1269 }
1270
1271 if( p != end )
1272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 mbedtls_x509_crt_free( crt );
1274 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1275 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 }
1277
1278 end = crt_end;
1279
1280 /*
1281 * }
1282 * -- end of TBSCertificate
1283 *
1284 * signatureAlgorithm AlgorithmIdentifier,
1285 * signatureValue BIT STRING
1286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001290 return( ret );
1291 }
1292
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001293 if( crt->sig_oid.len != sig_oid2.len ||
1294 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001295 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001296 ( sig_params1.len != 0 &&
1297 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 mbedtls_x509_crt_free( crt );
1300 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001301 }
1302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001306 return( ret );
1307 }
1308
1309 if( p != end )
1310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 mbedtls_x509_crt_free( crt );
1312 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1313 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 }
1315
1316 return( 0 );
1317}
1318
1319/*
1320 * Parse one X.509 certificate in DER format from a buffer and add them to a
1321 * chained list
1322 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001323static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1324 const unsigned char *buf,
1325 size_t buflen,
1326 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001327{
Janos Follath865b3eb2019-12-16 11:46:15 +00001328 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001330
1331 /*
1332 * Check for valid input
1333 */
1334 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001336
1337 while( crt->version != 0 && crt->next != NULL )
1338 {
1339 prev = crt;
1340 crt = crt->next;
1341 }
1342
1343 /*
1344 * Add new certificate on the end of the chain if needed.
1345 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001346 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001347 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001348 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001349
1350 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001351 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352
1353 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001355 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001356 }
1357
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001358 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001359 {
1360 if( prev )
1361 prev->next = NULL;
1362
1363 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365
1366 return( ret );
1367 }
1368
1369 return( 0 );
1370}
1371
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001372int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1373 const unsigned char *buf,
1374 size_t buflen )
1375{
1376 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0 ) );
1377}
1378
1379int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1380 const unsigned char *buf,
1381 size_t buflen )
1382{
1383 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1384}
1385
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001386/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001387 * Parse one or more PEM certificates from a buffer and add them to the chained
1388 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001390int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1391 const unsigned char *buf,
1392 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001393{
Janos Follath98e28a72016-05-31 14:03:54 +01001394#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001395 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001397#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001398
1399 /*
1400 * Check for valid input
1401 */
1402 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001404
1405 /*
1406 * Determine buffer content. Buffer contains either one DER certificate or
1407 * one or more PEM certificates.
1408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001410 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001411 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001414 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1417 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001418#else
1419 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1420#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_PEM_PARSE_C)
1423 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001424 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001428 /* 1 rather than 0 since the terminating NULL byte is counted in */
1429 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001430 {
1431 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001434 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436 "-----BEGIN CERTIFICATE-----",
1437 "-----END CERTIFICATE-----",
1438 buf, NULL, 0, &use_len );
1439
1440 if( ret == 0 )
1441 {
1442 /*
1443 * Was PEM encoded
1444 */
1445 buflen -= use_len;
1446 buf += use_len;
1447 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449 {
1450 return( ret );
1451 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001455
1456 /*
1457 * PEM header and footer were found
1458 */
1459 buflen -= use_len;
1460 buf += use_len;
1461
1462 if( first_error == 0 )
1463 first_error = ret;
1464
Paul Bakker5a5fa922014-09-26 14:53:04 +02001465 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466 continue;
1467 }
1468 else
1469 break;
1470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474
1475 if( ret != 0 )
1476 {
1477 /*
1478 * Quit parsing on a memory error
1479 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001480 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001481 return( ret );
1482
1483 if( first_error == 0 )
1484 first_error = ret;
1485
1486 total_failed++;
1487 continue;
1488 }
1489
1490 success = 1;
1491 }
1492 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493
1494 if( success )
1495 return( total_failed );
1496 else if( first_error )
1497 return( first_error );
1498 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001500#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001501}
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504/*
1505 * Load one or more certificates and add them to the chained list
1506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508{
Janos Follath865b3eb2019-12-16 11:46:15 +00001509 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510 size_t n;
1511 unsigned char *buf;
1512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514 return( ret );
1515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001518 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520
1521 return( ret );
1522}
1523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001525{
1526 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001527#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001528 int w_ret;
1529 WCHAR szDir[MAX_PATH];
1530 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001531 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001532 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533
Paul Bakker9af723c2014-05-01 13:03:14 +02001534 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001535 HANDLE hFind;
1536
1537 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539
Paul Bakker9af723c2014-05-01 13:03:14 +02001540 memset( szDir, 0, sizeof(szDir) );
1541 memset( filename, 0, MAX_PATH );
1542 memcpy( filename, path, len );
1543 filename[len++] = '\\';
1544 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545 filename[len++] = '*';
1546
Simon B3c6b18d2016-11-03 01:11:37 +00001547 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001548 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001549 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551
1552 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001553 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555
1556 len = MAX_PATH - len;
1557 do
1558 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001559 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560
1561 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1562 continue;
1563
Paul Bakker9af723c2014-05-01 13:03:14 +02001564 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001565 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001566 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001567 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001568 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001569 {
1570 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1571 goto cleanup;
1572 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575 if( w_ret < 0 )
1576 ret++;
1577 else
1578 ret += w_ret;
1579 }
1580 while( FindNextFileW( hFind, &file_data ) != 0 );
1581
Paul Bakker66d5d072014-06-17 16:39:18 +02001582 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001584
Ron Eldor36d90422017-01-09 15:09:16 +02001585cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001586 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001587#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001588 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001589 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001591 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001592 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593 DIR *dir = opendir( path );
1594
Paul Bakker66d5d072014-06-17 16:39:18 +02001595 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597
Ron Eldor63140682017-01-09 19:27:59 +02001598#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001599 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001600 {
1601 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001602 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001603 }
Ron Eldor63140682017-01-09 19:27:59 +02001604#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001605
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001606 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001607 {
Andres AGf9113192016-09-02 14:06:04 +01001608 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1609 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001610
Andres AGf9113192016-09-02 14:06:04 +01001611 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612 {
Andres AGf9113192016-09-02 14:06:04 +01001613 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1614 goto cleanup;
1615 }
1616 else if( stat( entry_name, &sb ) == -1 )
1617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001619 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620 }
1621
Janos Follath85de7a62020-02-04 14:12:03 +00001622 if( !S_ISREG( sb.st_mode ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001623 continue;
1624
1625 // Ignore parse errors
1626 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628 if( t_ret < 0 )
1629 ret++;
1630 else
1631 ret += t_ret;
1632 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001633
1634cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001635 closedir( dir );
1636
Ron Eldor63140682017-01-09 19:27:59 +02001637#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001638 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001640#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001641
Paul Bakkerbe089b02013-10-14 15:51:50 +02001642#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001643
1644 return( ret );
1645}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001647
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001648/*
1649 * OtherName ::= SEQUENCE {
1650 * type-id OBJECT IDENTIFIER,
1651 * value [0] EXPLICIT ANY DEFINED BY type-id }
1652 *
1653 * HardwareModuleName ::= SEQUENCE {
1654 * hwType OBJECT IDENTIFIER,
1655 * hwSerialNum OCTET STRING }
1656 *
1657 * NOTE: we currently only parse and use otherName of type HwModuleName,
1658 * as defined in RFC 4108.
1659 */
1660static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1661 mbedtls_x509_san_other_name *other_name )
1662{
Janos Follathab23cd12019-05-09 13:53:57 +01001663 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001664 size_t len;
1665 unsigned char *p = subject_alt_name->p;
1666 const unsigned char *end = p + subject_alt_name->len;
1667 mbedtls_x509_buf cur_oid;
1668
1669 if( ( subject_alt_name->tag &
1670 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1671 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1672 {
1673 /*
1674 * The given subject alternative name is not of type "othername".
1675 */
1676 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1677 }
1678
1679 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1680 MBEDTLS_ASN1_OID ) ) != 0 )
1681 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1682
1683 cur_oid.tag = MBEDTLS_ASN1_OID;
1684 cur_oid.p = p;
1685 cur_oid.len = len;
1686
1687 /*
1688 * Only HwModuleName is currently supported.
1689 */
1690 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1691 {
1692 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1693 }
1694
Ron Eldor890819a2019-05-13 19:03:04 +03001695 if( p + len >= end )
1696 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001697 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001698 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1699 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1700 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001701 p += len;
1702 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1703 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
1704 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1705
1706 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1707 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1708 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1709
1710 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
1711 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1712
1713 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1714 other_name->value.hardware_module_name.oid.p = p;
1715 other_name->value.hardware_module_name.oid.len = len;
1716
Ron Eldor890819a2019-05-13 19:03:04 +03001717 if( p + len >= end )
1718 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001719 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001720 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1721 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1722 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001723 p += len;
1724 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1725 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Ron Eldor890819a2019-05-13 19:03:04 +03001726 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001727
1728 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1729 other_name->value.hardware_module_name.val.p = p;
1730 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001731 p += len;
1732 if( p != end )
1733 {
Ron Eldor890819a2019-05-13 19:03:04 +03001734 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001735 sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001736 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1737 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001738 }
1739 return( 0 );
1740}
1741
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001742static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001743 const mbedtls_x509_sequence
1744 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001745 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001746{
Janos Follath865b3eb2019-12-16 11:46:15 +00001747 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001748 size_t n = *size;
1749 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001751 mbedtls_x509_subject_alternative_name san;
1752 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001753
1754 while( cur != NULL )
1755 {
Ron Eldor890819a2019-05-13 19:03:04 +03001756 memset( &san, 0, sizeof( san ) );
1757 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1758 if( parse_ret != 0 )
1759 {
1760 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1761 {
1762 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1763 MBEDTLS_X509_SAFE_SNPRINTF;
1764 }
1765 else
1766 {
1767 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1768 MBEDTLS_X509_SAFE_SNPRINTF;
1769 }
1770 cur = cur->next;
1771 continue;
1772 }
1773
1774 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001775 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001776 /*
1777 * otherName
1778 */
Ron Eldora2913912019-05-16 16:17:38 +03001779 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001780 {
1781 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1782
1783 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1784 MBEDTLS_X509_SAFE_SNPRINTF;
1785
1786 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1787 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001788 {
Ron Eldor890819a2019-05-13 19:03:04 +03001789 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1790 MBEDTLS_X509_SAFE_SNPRINTF;
1791 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001792 MBEDTLS_X509_SAFE_SNPRINTF;
1793
Ron Eldor890819a2019-05-13 19:03:04 +03001794 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1795 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001796
Ron Eldor890819a2019-05-13 19:03:04 +03001797 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1798 MBEDTLS_X509_SAFE_SNPRINTF;
1799
1800 if( other_name->value.hardware_module_name.val.len >= n )
1801 {
1802 *p = '\0';
1803 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001804 }
1805
Ron Eldora2913912019-05-16 16:17:38 +03001806 memcpy( p, other_name->value.hardware_module_name.val.p,
1807 other_name->value.hardware_module_name.val.len );
1808 p += other_name->value.hardware_module_name.val.len;
1809
Ron Eldor890819a2019-05-13 19:03:04 +03001810 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001811
Ron Eldor890819a2019-05-13 19:03:04 +03001812 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1813 }
1814 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001815
1816 /*
1817 * dNSName
1818 */
Ron Eldora2913912019-05-16 16:17:38 +03001819 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001820 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001821 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1822 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001823 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001824 {
1825 *p = '\0';
1826 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1827 }
Ron Eldora2913912019-05-16 16:17:38 +03001828
1829 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1830 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001831 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001832 }
1833 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001834
1835 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001836 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001837 */
1838 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001839 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1840 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001841 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001842 }
1843
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001844 cur = cur->next;
1845 }
1846
1847 *p = '\0';
1848
1849 *size = n;
1850 *buf = p;
1851
1852 return( 0 );
1853}
1854
Ron Eldor890819a2019-05-13 19:03:04 +03001855int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1856 mbedtls_x509_subject_alternative_name *san )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001857{
Janos Follath865b3eb2019-12-16 11:46:15 +00001858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor890819a2019-05-13 19:03:04 +03001859 switch( san_buf->tag &
1860 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1861 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001862 {
Ron Eldor890819a2019-05-13 19:03:04 +03001863 /*
1864 * otherName
1865 */
1866 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001867 {
Ron Eldor890819a2019-05-13 19:03:04 +03001868 mbedtls_x509_san_other_name other_name;
1869
1870 ret = x509_get_other_name( san_buf, &other_name );
1871 if( ret != 0 )
1872 return( ret );
1873
1874 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1875 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1876 memcpy( &san->san.other_name,
1877 &other_name, sizeof( other_name ) );
1878
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001879 }
Ron Eldor890819a2019-05-13 19:03:04 +03001880 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001881
Ron Eldor890819a2019-05-13 19:03:04 +03001882 /*
1883 * dNSName
1884 */
1885 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001886 {
Ron Eldor890819a2019-05-13 19:03:04 +03001887 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1888 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001889
Ron Eldor890819a2019-05-13 19:03:04 +03001890 memcpy( &san->san.unstructured_name,
1891 san_buf, sizeof( *san_buf ) );
Janos Follath6c379b42019-05-10 14:17:16 +01001892
Ron Eldor890819a2019-05-13 19:03:04 +03001893 }
1894 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001895
Ron Eldor890819a2019-05-13 19:03:04 +03001896 /*
1897 * Type not supported
1898 */
1899 default:
1900 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1901 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001902 return( 0 );
1903}
1904
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001905#define PRINT_ITEM(i) \
1906 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001908 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001909 sep = ", "; \
1910 }
1911
1912#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001913 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001914 PRINT_ITEM( name );
1915
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001916static int x509_info_cert_type( char **buf, size_t *size,
1917 unsigned char ns_cert_type )
1918{
Janos Follath865b3eb2019-12-16 11:46:15 +00001919 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001920 size_t n = *size;
1921 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001922 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001925 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1927 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1928 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001929 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1930 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1931 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001932
1933 *size = n;
1934 *buf = p;
1935
1936 return( 0 );
1937}
1938
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001939#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001940 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001941 PRINT_ITEM( name );
1942
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001943static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001944 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001945{
Janos Follath865b3eb2019-12-16 11:46:15 +00001946 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001947 size_t n = *size;
1948 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001949 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1952 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001953 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1954 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1955 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1957 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001958 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1959 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001960
1961 *size = n;
1962 *buf = p;
1963
1964 return( 0 );
1965}
1966
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001967static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001969{
Janos Follath865b3eb2019-12-16 11:46:15 +00001970 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001971 const char *desc;
1972 size_t n = *size;
1973 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001975 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001976
1977 while( cur != NULL )
1978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001980 desc = "???";
1981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001983 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001984
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001985 sep = ", ";
1986
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001987 cur = cur->next;
1988 }
1989
1990 *size = n;
1991 *buf = p;
1992
1993 return( 0 );
1994}
1995
Ron Eldor74d9acc2019-03-21 14:00:03 +02001996static int x509_info_cert_policies( char **buf, size_t *size,
1997 const mbedtls_x509_sequence *certificate_policies )
1998{
Janos Follath865b3eb2019-12-16 11:46:15 +00001999 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002000 const char *desc;
2001 size_t n = *size;
2002 char *p = *buf;
2003 const mbedtls_x509_sequence *cur = certificate_policies;
2004 const char *sep = "";
2005
2006 while( cur != NULL )
2007 {
2008 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2009 desc = "???";
2010
2011 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2012 MBEDTLS_X509_SAFE_SNPRINTF;
2013
2014 sep = ", ";
2015
2016 cur = cur->next;
2017 }
2018
2019 *size = n;
2020 *buf = p;
2021
2022 return( 0 );
2023}
2024
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002025/*
2026 * Return an informational string about the certificate.
2027 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002028#define BEFORE_COLON 18
2029#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2031 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002032{
Janos Follath865b3eb2019-12-16 11:46:15 +00002033 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002034 size_t n;
2035 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002036 char key_size_str[BEFORE_COLON];
2037
2038 p = buf;
2039 n = size;
2040
Janos Follath98e28a72016-05-31 14:03:54 +01002041 if( NULL == crt )
2042 {
2043 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2044 MBEDTLS_X509_SAFE_SNPRINTF;
2045
2046 return( (int) ( size - n ) );
2047 }
2048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002050 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002051 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002053 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002054 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002057 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002060 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002062 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002065 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002067 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002070 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2071 crt->valid_from.year, crt->valid_from.mon,
2072 crt->valid_from.day, crt->valid_from.hour,
2073 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002074 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002077 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2078 crt->valid_to.year, crt->valid_to.mon,
2079 crt->valid_to.day, crt->valid_to.hour,
2080 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002081 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002084 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002087 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002088 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002089
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002090 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2092 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002093 {
2094 return( ret );
2095 }
2096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002098 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002099 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002100
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002101 /*
2102 * Optional extensions
2103 */
2104
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002105 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002108 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002109 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002110
2111 if( crt->max_pathlen > 0 )
2112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002114 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002115 }
2116 }
2117
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002118 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002119 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002120 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002121 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002122
2123 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002124 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002125 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002126 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002127 }
2128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002132 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002133
2134 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2135 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002136 }
2137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002141 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002142
2143 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2144 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002145 }
2146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002150 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002151
2152 if( ( ret = x509_info_ext_key_usage( &p, &n,
2153 &crt->ext_key_usage ) ) != 0 )
2154 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002155 }
2156
Ron Eldor74d9acc2019-03-21 14:00:03 +02002157 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2158 {
2159 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2160 MBEDTLS_X509_SAFE_SNPRINTF;
2161
2162 if( ( ret = x509_info_cert_policies( &p, &n,
2163 &crt->certificate_policies ) ) != 0 )
2164 return( ret );
2165 }
2166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002168 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002169
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002170 return( (int) ( size - n ) );
2171}
2172
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002173struct x509_crt_verify_string {
2174 int code;
2175 const char *string;
2176};
2177
2178static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002179 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002180 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2181 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2182 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2183 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2184 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002185 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2186 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2187 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002188 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002189 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2190 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2191 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2192 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002193 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2194 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2195 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2196 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2197 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2198 { 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 +01002199 { 0, NULL }
2200};
2201
2202int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002203 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002204{
Janos Follath865b3eb2019-12-16 11:46:15 +00002205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002206 const struct x509_crt_verify_string *cur;
2207 char *p = buf;
2208 size_t n = size;
2209
2210 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2211 {
2212 if( ( flags & cur->code ) == 0 )
2213 continue;
2214
2215 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002216 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002217 flags ^= cur->code;
2218 }
2219
2220 if( flags != 0 )
2221 {
2222 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2223 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002224 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002225 }
2226
2227 return( (int) ( size - n ) );
2228}
2229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002231int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2232 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002233{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002234 unsigned int usage_must, usage_may;
2235 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2236 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2237
2238 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2239 return( 0 );
2240
2241 usage_must = usage & ~may_mask;
2242
2243 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2244 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2245
2246 usage_may = usage & may_mask;
2247
2248 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002250
2251 return( 0 );
2252}
2253#endif
2254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2256int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002257 const char *usage_oid,
2258 size_t usage_len )
2259{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002261
2262 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002264 return( 0 );
2265
2266 /*
2267 * Look for the requested usage (or wildcard ANY) in our list
2268 */
2269 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002272
2273 if( cur_oid->len == usage_len &&
2274 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2275 {
2276 return( 0 );
2277 }
2278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002280 return( 0 );
2281 }
2282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002284}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002288/*
2289 * Return 1 if the certificate is revoked, or 0 otherwise.
2290 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002291int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002292{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002294
2295 while( cur != NULL && cur->serial.len != 0 )
2296 {
2297 if( crt->serial.len == cur->serial.len &&
2298 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2299 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002300 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002301 return( 1 );
2302 }
2303
2304 cur = cur->next;
2305 }
2306
2307 return( 0 );
2308}
2309
2310/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002311 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002312 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002315 mbedtls_x509_crl *crl_list,
2316 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002317{
2318 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2320 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002321
2322 if( ca == NULL )
2323 return( flags );
2324
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002325 while( crl_list != NULL )
2326 {
2327 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002328 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002329 {
2330 crl_list = crl_list->next;
2331 continue;
2332 }
2333
2334 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002335 * Check if the CA is configured to sign CRLs
2336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002338 if( mbedtls_x509_crt_check_key_usage( ca,
2339 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002342 break;
2343 }
2344#endif
2345
2346 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002347 * Check if CRL is correctly signed by the trusted CA
2348 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002349 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2350 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2351
2352 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2353 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002356 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002357 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002358 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002360 break;
2361 }
2362
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002363 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002364 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2367 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002368 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002371 break;
2372 }
2373
2374 /*
2375 * Check for validity of CRL (Do not drop out)
2376 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002377 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002379
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002380 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002381 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002382
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002383 /*
2384 * Check if certificate is revoked
2385 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002386 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002389 break;
2390 }
2391
2392 crl_list = crl_list->next;
2393 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002394
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002395 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002398
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002399/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002400 * Check the signature of a certificate by its parent
2401 */
2402static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002403 mbedtls_x509_crt *parent,
2404 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002405{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002406 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002407 size_t hash_len;
2408#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2409 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002410 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002411 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002412
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002413 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002414 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002415 return( -1 );
2416#else
Jaeden Amero34973232019-02-20 10:32:28 +00002417 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002418 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2419
2420 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2421 return( -1 );
2422
2423 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2424 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002425 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002426 return( -1 );
2427 }
2428
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002429 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2430 != PSA_SUCCESS )
2431 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002432 return( -1 );
2433 }
2434#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002435 /* Skip expensive computation on obvious mismatch */
2436 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002437 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002438
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002439#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002440 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2441 {
2442 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002443 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002444 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002445 }
2446#else
2447 (void) rs_ctx;
2448#endif
2449
2450 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002451 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002452 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002453}
2454
2455/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002456 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2457 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002458 *
2459 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2462 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002463 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002464{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002465 int need_ca_bit;
2466
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002467 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002468 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002469 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002470
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002471 /* Parent must have the basicConstraints CA bit set as a general rule */
2472 need_ca_bit = 1;
2473
2474 /* Exception: v1/v2 certificates that are locally trusted. */
2475 if( top && parent->version < 3 )
2476 need_ca_bit = 0;
2477
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002478 if( need_ca_bit && ! parent->ca_istrue )
2479 return( -1 );
2480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002482 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002484 {
2485 return( -1 );
2486 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002487#endif
2488
2489 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002490}
2491
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002492/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002493 * Find a suitable parent for child in candidates, or return NULL.
2494 *
2495 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002496 * 1. subject name matches child's issuer
2497 * 2. if necessary, the CA bit is set and key usage allows signing certs
2498 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002499 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002500 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002501 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002502 * If there's a suitable candidate which is also time-valid, return the first
2503 * such. Otherwise, return the first suitable candidate (or NULL if there is
2504 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002505 *
2506 * The rationale for this rule is that someone could have a list of trusted
2507 * roots with two versions on the same root with different validity periods.
2508 * (At least one user reported having such a list and wanted it to just work.)
2509 * The reason we don't just require time-validity is that generally there is
2510 * only one version, and if it's expired we want the flags to state that
2511 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002512 *
2513 * The rationale for rule 3 (signature for trusted roots) is that users might
2514 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002515 * way we select the correct one is by checking the signature (as we don't
2516 * rely on key identifier extensions). (This is one way users might choose to
2517 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002518 *
2519 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002520 * - [in] child: certificate for which we're looking for a parent
2521 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002522 * - [out] r_parent: parent found (or NULL)
2523 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002524 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2525 * of the chain, 0 otherwise
2526 * - [in] path_cnt: number of intermediates seen so far
2527 * - [in] self_cnt: number of self-signed intermediates seen so far
2528 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002529 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002530 *
2531 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002532 * - 0 on success
2533 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002534 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002535static int x509_crt_find_parent_in(
2536 mbedtls_x509_crt *child,
2537 mbedtls_x509_crt *candidates,
2538 mbedtls_x509_crt **r_parent,
2539 int *r_signature_is_good,
2540 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002541 unsigned path_cnt,
2542 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002543 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002544{
Janos Follath865b3eb2019-12-16 11:46:15 +00002545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002546 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002547 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002548
2549#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002550 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002551 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2552 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002553 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002554 parent = rs_ctx->parent;
2555 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002556 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002557
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002558 /* clear saved state */
2559 rs_ctx->parent = NULL;
2560 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002561 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002562
2563 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002564 goto check_signature;
2565 }
2566#endif
2567
2568 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002569 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002570
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002571 for( parent = candidates; parent != NULL; parent = parent->next )
2572 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002573 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002574 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002575 continue;
2576
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002577 /* +1 because stored max_pathlen is 1 higher that the actual value */
2578 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002579 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002580 {
2581 continue;
2582 }
2583
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002584 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002585#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2586check_signature:
2587#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002588 ret = x509_crt_check_signature( child, parent, rs_ctx );
2589
2590#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002591 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2592 {
2593 /* save state */
2594 rs_ctx->parent = parent;
2595 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002596 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002597
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002598 return( ret );
2599 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002600#else
2601 (void) ret;
2602#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002603
2604 signature_is_good = ret == 0;
2605 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002606 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002607
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002608 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002609 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2610 mbedtls_x509_time_is_future( &parent->valid_from ) )
2611 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002612 if( fallback_parent == NULL )
2613 {
2614 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002615 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002616 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002617
2618 continue;
2619 }
2620
Andy Gross1f627142019-01-30 10:25:53 -06002621 *r_parent = parent;
2622 *r_signature_is_good = signature_is_good;
2623
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002624 break;
2625 }
2626
Andy Gross1f627142019-01-30 10:25:53 -06002627 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002628 {
2629 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002630 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002631 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002632
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002633 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002634}
2635
2636/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002637 * Find a parent in trusted CAs or the provided chain, or return NULL.
2638 *
2639 * Searches in trusted CAs first, and return the first suitable parent found
2640 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002641 *
2642 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002643 * - [in] child: certificate for which we're looking for a parent, followed
2644 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002645 * - [in] trust_ca: list of locally trusted certificates
2646 * - [out] parent: parent found (or NULL)
2647 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2648 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2649 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2650 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002651 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002652 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002653 *
2654 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002655 * - 0 on success
2656 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002657 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002658static int x509_crt_find_parent(
2659 mbedtls_x509_crt *child,
2660 mbedtls_x509_crt *trust_ca,
2661 mbedtls_x509_crt **parent,
2662 int *parent_is_trusted,
2663 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002664 unsigned path_cnt,
2665 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002666 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002667{
Janos Follath865b3eb2019-12-16 11:46:15 +00002668 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002669 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002670
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002671 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002672
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002673#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002674 /* restore then clear saved state if we have some stored */
2675 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2676 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002677 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002678 rs_ctx->parent_is_trusted = -1;
2679 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002680#endif
2681
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002682 while( 1 ) {
2683 search_list = *parent_is_trusted ? trust_ca : child->next;
2684
2685 ret = x509_crt_find_parent_in( child, search_list,
2686 parent, signature_is_good,
2687 *parent_is_trusted,
2688 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002689
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002690#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002691 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2692 {
2693 /* save state */
2694 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002695 return( ret );
2696 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002697#else
2698 (void) ret;
2699#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002700
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002701 /* stop here if found or already in second iteration */
2702 if( *parent != NULL || *parent_is_trusted == 0 )
2703 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002704
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002705 /* prepare second iteration */
2706 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002707 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002708
2709 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002710 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002711 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002712 *parent_is_trusted = 0;
2713 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002714 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002715
2716 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002717}
2718
2719/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002720 * Check if an end-entity certificate is locally trusted
2721 *
2722 * Currently we require such certificates to be self-signed (actually only
2723 * check for self-issued as self-signatures are not checked)
2724 */
2725static int x509_crt_check_ee_locally_trusted(
2726 mbedtls_x509_crt *crt,
2727 mbedtls_x509_crt *trust_ca )
2728{
2729 mbedtls_x509_crt *cur;
2730
2731 /* must be self-issued */
2732 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2733 return( -1 );
2734
2735 /* look for an exact match with trusted cert */
2736 for( cur = trust_ca; cur != NULL; cur = cur->next )
2737 {
2738 if( crt->raw.len == cur->raw.len &&
2739 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2740 {
2741 return( 0 );
2742 }
2743 }
2744
2745 /* too bad */
2746 return( -1 );
2747}
2748
2749/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002750 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002751 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002752 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2753 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002754 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002755 * such that every cert in the chain is a child of the next one,
2756 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002757 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002758 * Verify that chain and return it with flags for all issues found.
2759 *
2760 * Special cases:
2761 * - EE == Rj -> return a one-element list containing it
2762 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2763 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002764 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002765 * Tests for (aspects of) this function should include at least:
2766 * - trusted EE
2767 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002768 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002769 * - if relevant: EE untrusted
2770 * - if relevant: EE -> intermediate, untrusted
2771 * with the aspect under test checked at each relevant level (EE, int, root).
2772 * For some aspects longer chains are required, but usually length 2 is
2773 * enough (but length 1 is not in general).
2774 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002775 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002776 * - [in] crt: the cert list EE, C1, ..., Cn
2777 * - [in] trust_ca: the trusted list R1, ..., Rp
2778 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002779 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002780 * Only valid when return value is 0, may contain garbage otherwise!
2781 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002782 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002783 *
2784 * Return value:
2785 * - non-zero if the chain could not be fully built and examined
2786 * - 0 is the chain was successfully built and examined,
2787 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002788 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002789static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002790 mbedtls_x509_crt *crt,
2791 mbedtls_x509_crt *trust_ca,
2792 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002793 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2794 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002795 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002796 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002797 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002798{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002799 /* Don't initialize any of those variables here, so that the compiler can
2800 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002802 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002803 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002804 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002805 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002806 int parent_is_trusted;
2807 int child_is_trusted;
2808 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002809 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002810 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002811
2812#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2813 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002814 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002815 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002816 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002817 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002818 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002819
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002820 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002821 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002822 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002823 flags = &cur->flags;
2824
2825 goto find_parent;
2826 }
2827#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002828
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002829 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002830 self_cnt = 0;
2831 parent_is_trusted = 0;
2832 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002833
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002834 while( 1 ) {
2835 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002836 cur = &ver_chain->items[ver_chain->len];
2837 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002838 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002839 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002840 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002841
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002842 /* Check time-validity (all certificates) */
2843 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2844 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002845
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002846 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2847 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002848
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002849 /* Stop here for trusted roots (but not for trusted EE certs) */
2850 if( child_is_trusted )
2851 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002852
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002853 /* Check signature algorithm: MD & PK algs */
2854 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2855 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002856
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002857 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2858 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002859
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002860 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002861 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002862 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2863 {
2864 return( 0 );
2865 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002866
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002867#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2868find_parent:
2869#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002870
2871 /* Obtain list of potential trusted signers from CA callback,
2872 * or use statically provided list. */
2873#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2874 if( f_ca_cb != NULL )
2875 {
2876 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2877 mbedtls_free( ver_chain->trust_ca_cb_result );
2878 ver_chain->trust_ca_cb_result = NULL;
2879
2880 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2881 if( ret != 0 )
2882 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2883
2884 cur_trust_ca = ver_chain->trust_ca_cb_result;
2885 }
2886 else
2887#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2888 {
2889 ((void) f_ca_cb);
2890 ((void) p_ca_cb);
2891 cur_trust_ca = trust_ca;
2892 }
2893
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002894 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002895 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002896 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002897 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002898
2899#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002900 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2901 {
2902 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002903 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002904 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002905 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002906
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002907 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002908 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002909#else
2910 (void) ret;
2911#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002912
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002913 /* No parent? We're done here */
2914 if( parent == NULL )
2915 {
2916 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2917 return( 0 );
2918 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002919
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002920 /* Count intermediate self-issued (not necessarily self-signed) certs.
2921 * These can occur with some strategies for key rollover, see [SIRO],
2922 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002923 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002924 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2925 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002926 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002927 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002928
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002929 /* path_cnt is 0 for the first intermediate CA,
2930 * and if parent is trusted it's not an intermediate CA */
2931 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002932 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002933 {
2934 /* return immediately to avoid overflow the chain array */
2935 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2936 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002937
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002938 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002939 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002940 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2941
2942 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002943 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002944 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002947 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002948 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002949#else
2950 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002951#endif
2952
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002953 /* prepare for next iteration */
2954 child = parent;
2955 parent = NULL;
2956 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002957 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002958 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002959}
2960
2961/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002962 * Check for CN match
2963 */
2964static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2965 const char *cn, size_t cn_len )
2966{
2967 /* try exact match */
2968 if( name->len == cn_len &&
2969 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2970 {
2971 return( 0 );
2972 }
2973
2974 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002975 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002976 {
2977 return( 0 );
2978 }
2979
2980 return( -1 );
2981}
2982
2983/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002984 * Verify the requested CN - only call this if cn is not NULL!
2985 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002986static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002987 const char *cn,
2988 uint32_t *flags )
2989{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002990 const mbedtls_x509_name *name;
2991 const mbedtls_x509_sequence *cur;
2992 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002993
2994 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
2995 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002996 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002997 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002998 if( x509_crt_check_cn( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002999 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003000 }
3001
3002 if( cur == NULL )
3003 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3004 }
3005 else
3006 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003007 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003008 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003009 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3010 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003011 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003012 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003013 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003014 }
3015
3016 if( name == NULL )
3017 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3018 }
3019}
3020
3021/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003022 * Merge the flags for all certs in the chain, after calling callback
3023 */
3024static int x509_crt_merge_flags_with_cb(
3025 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003026 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003027 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3028 void *p_vrfy )
3029{
Janos Follath865b3eb2019-12-16 11:46:15 +00003030 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003031 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003032 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003033 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003034
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003035 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003036 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003037 cur = &ver_chain->items[i-1];
3038 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003039
3040 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003041 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003042 return( ret );
3043
3044 *flags |= cur_flags;
3045 }
3046
3047 return( 0 );
3048}
3049
3050/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003051 * Verify the certificate validity, with profile, restartable version
3052 *
3053 * This function:
3054 * - checks the requested CN (if any)
3055 * - checks the type and size of the EE cert's key,
3056 * as that isn't done as part of chain building/verification currently
3057 * - builds and verifies the chain
3058 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003059 *
3060 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3061 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3062 * verification routine to search for trusted signers, and CRLs will
3063 * be disabled. Otherwise, `trust_ca` will be used as the static list
3064 * of trusted signers, and `ca_crl` will be use as the static list
3065 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003066 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003067static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003068 mbedtls_x509_crt *trust_ca,
3069 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003070 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3071 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003072 const mbedtls_x509_crt_profile *profile,
3073 const char *cn, uint32_t *flags,
3074 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3075 void *p_vrfy,
3076 mbedtls_x509_crt_restart_ctx *rs_ctx )
3077{
Janos Follath865b3eb2019-12-16 11:46:15 +00003078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003079 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003080 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003081 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003082
3083 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003084 ee_flags = 0;
3085 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003086
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003087 if( profile == NULL )
3088 {
3089 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3090 goto exit;
3091 }
3092
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003093 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003094 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003095 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003096
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003097 /* Check the type and size of the key */
3098 pk_type = mbedtls_pk_get_type( &crt->pk );
3099
3100 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003101 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003102
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003103 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003104 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003105
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003106 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003107 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3108 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003109 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003110
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003111 if( ret != 0 )
3112 goto exit;
3113
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003114 /* Merge end-entity flags */
3115 ver_chain.items[0].flags |= ee_flags;
3116
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003117 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003118 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003119
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003120exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003121
3122#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3123 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3124 mbedtls_free( ver_chain.trust_ca_cb_result );
3125 ver_chain.trust_ca_cb_result = NULL;
3126#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3127
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003128#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3129 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3130 mbedtls_x509_crt_restart_free( rs_ctx );
3131#endif
3132
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003133 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3134 * the SSL module for authmode optional, but non-zero return from the
3135 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003136 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3137 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3138
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003139 if( ret != 0 )
3140 {
3141 *flags = (uint32_t) -1;
3142 return( ret );
3143 }
3144
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003145 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003147
3148 return( 0 );
3149}
3150
Hanno Becker3116fb32019-03-28 13:34:42 +00003151
3152/*
3153 * Verify the certificate validity (default profile, not restartable)
3154 */
3155int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3156 mbedtls_x509_crt *trust_ca,
3157 mbedtls_x509_crl *ca_crl,
3158 const char *cn, uint32_t *flags,
3159 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3160 void *p_vrfy )
3161{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003162 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003163 NULL, NULL,
3164 &mbedtls_x509_crt_profile_default,
3165 cn, flags,
3166 f_vrfy, p_vrfy, NULL ) );
3167}
3168
3169/*
3170 * Verify the certificate validity (user-chosen profile, not restartable)
3171 */
3172int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3173 mbedtls_x509_crt *trust_ca,
3174 mbedtls_x509_crl *ca_crl,
3175 const mbedtls_x509_crt_profile *profile,
3176 const char *cn, uint32_t *flags,
3177 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3178 void *p_vrfy )
3179{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003180 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003181 NULL, NULL,
3182 profile, cn, flags,
3183 f_vrfy, p_vrfy, NULL ) );
3184}
3185
3186#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3187/*
3188 * Verify the certificate validity (user-chosen profile, CA callback,
3189 * not restartable).
3190 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003191int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003192 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3193 void *p_ca_cb,
3194 const mbedtls_x509_crt_profile *profile,
3195 const char *cn, uint32_t *flags,
3196 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3197 void *p_vrfy )
3198{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003199 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003200 f_ca_cb, p_ca_cb,
3201 profile, cn, flags,
3202 f_vrfy, p_vrfy, NULL ) );
3203}
3204#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3205
3206int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3207 mbedtls_x509_crt *trust_ca,
3208 mbedtls_x509_crl *ca_crl,
3209 const mbedtls_x509_crt_profile *profile,
3210 const char *cn, uint32_t *flags,
3211 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3212 void *p_vrfy,
3213 mbedtls_x509_crt_restart_ctx *rs_ctx )
3214{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003215 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003216 NULL, NULL,
3217 profile, cn, flags,
3218 f_vrfy, p_vrfy, rs_ctx ) );
3219}
3220
3221
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003222/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003223 * Initialize a certificate chain
3224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003226{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003228}
3229
3230/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003231 * Unallocate all certificate data
3232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 mbedtls_x509_crt *cert_cur = crt;
3236 mbedtls_x509_crt *cert_prv;
3237 mbedtls_x509_name *name_cur;
3238 mbedtls_x509_name *name_prv;
3239 mbedtls_x509_sequence *seq_cur;
3240 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003241
3242 if( crt == NULL )
3243 return;
3244
3245 do
3246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3250 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003251#endif
3252
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003253 name_cur = cert_cur->issuer.next;
3254 while( name_cur != NULL )
3255 {
3256 name_prv = name_cur;
3257 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003258 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003260 }
3261
3262 name_cur = cert_cur->subject.next;
3263 while( name_cur != NULL )
3264 {
3265 name_prv = name_cur;
3266 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003267 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003269 }
3270
3271 seq_cur = cert_cur->ext_key_usage.next;
3272 while( seq_cur != NULL )
3273 {
3274 seq_prv = seq_cur;
3275 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003276 mbedtls_platform_zeroize( seq_prv,
3277 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003279 }
3280
3281 seq_cur = cert_cur->subject_alt_names.next;
3282 while( seq_cur != NULL )
3283 {
3284 seq_prv = seq_cur;
3285 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003286 mbedtls_platform_zeroize( seq_prv,
3287 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003289 }
3290
Ron Eldor74d9acc2019-03-21 14:00:03 +02003291 seq_cur = cert_cur->certificate_policies.next;
3292 while( seq_cur != NULL )
3293 {
3294 seq_prv = seq_cur;
3295 seq_cur = seq_cur->next;
3296 mbedtls_platform_zeroize( seq_prv,
3297 sizeof( mbedtls_x509_sequence ) );
3298 mbedtls_free( seq_prv );
3299 }
3300
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003301 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003302 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003303 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003305 }
3306
3307 cert_cur = cert_cur->next;
3308 }
3309 while( cert_cur != NULL );
3310
3311 cert_cur = crt;
3312 do
3313 {
3314 cert_prv = cert_cur;
3315 cert_cur = cert_cur->next;
3316
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003317 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003318 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003320 }
3321 while( cert_cur != NULL );
3322}
3323
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003324#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3325/*
3326 * Initialize a restart context
3327 */
3328void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3329{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003330 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003331
3332 ctx->parent = NULL;
3333 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003334 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003335
3336 ctx->parent_is_trusted = -1;
3337
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003338 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003339 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003340 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003341}
3342
3343/*
3344 * Free the components of a restart context
3345 */
3346void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3347{
3348 if( ctx == NULL )
3349 return;
3350
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003351 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003352 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003353}
3354#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356#endif /* MBEDTLS_X509_CRT_PARSE_C */