blob: 51330e9d73e6d888784bb392c13ecd21299e4b97 [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 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
49#include "mbedtls/psa_util.h"
50#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Simon Butcherd2642582018-10-03 15:11:19 +010055#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010064#endif
65
Paul Bakkerfa6a6202013-10-28 18:48:30 +010066#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#include <windows.h>
68#else
69#include <time.h>
70#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000073#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020074#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <sys/types.h>
76#include <sys/stat.h>
77#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000078#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#endif
80
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020081/*
82 * Item in a verification chain: cert and flags for it
83 */
84typedef struct {
85 mbedtls_x509_crt *crt;
86 uint32_t flags;
87} x509_crt_verify_chain_item;
88
89/*
90 * Max size of verification chain: end-entity + intermediates + trusted root
91 */
92#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020093
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020095 * Default profile
96 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020097const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
98{
Gilles Peskine5e79cb32017-05-04 16:17:21 +020099 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200100 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
101 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
102 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
103 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
104 0xFFFFFFF, /* Any PK alg */
105 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200106 2048,
107};
108
109/*
110 * Next-default profile
111 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200112const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
113{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200114 /* Hashes from SHA-256 and above */
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
118 0xFFFFFFF, /* Any PK alg */
119#if defined(MBEDTLS_ECP_C)
120 /* Curves at or above 128-bit security level */
121 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
122 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
123 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
124 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
125 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
126 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
127 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
128#else
129 0,
130#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200131 2048,
132};
133
134/*
135 * NSA Suite B Profile
136 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200137const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
138{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200139 /* Only SHA-256 and 384 */
140 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
142 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200143 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
144 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200145#if defined(MBEDTLS_ECP_C)
146 /* Only NIST P-256 and P-384 */
147 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
148 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
149#else
150 0,
151#endif
152 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200153};
154
155/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200156 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200157 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200158 */
159static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
160 mbedtls_md_type_t md_alg )
161{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200162 if( md_alg == MBEDTLS_MD_NONE )
163 return( -1 );
164
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200165 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
166 return( 0 );
167
168 return( -1 );
169}
170
171/*
172 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200173 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200174 */
175static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
176 mbedtls_pk_type_t pk_alg )
177{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200178 if( pk_alg == MBEDTLS_PK_NONE )
179 return( -1 );
180
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200181 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
182 return( 0 );
183
184 return( -1 );
185}
186
187/*
188 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200189 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200190 */
191static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200192 const mbedtls_pk_context *pk )
193{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200194 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200195
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200196#if defined(MBEDTLS_RSA_C)
197 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
198 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200199 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200200 return( 0 );
201
202 return( -1 );
203 }
204#endif
205
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200206#if defined(MBEDTLS_ECP_C)
207 if( pk_alg == MBEDTLS_PK_ECDSA ||
208 pk_alg == MBEDTLS_PK_ECKEY ||
209 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200210 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200211 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200212
Philippe Antoineb5b25432018-05-11 11:06:29 +0200213 if( gid == MBEDTLS_ECP_DP_NONE )
214 return( -1 );
215
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200216 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
217 return( 0 );
218
219 return( -1 );
220 }
221#endif
222
223 return( -1 );
224}
225
226/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000227 * Like memcmp, but case-insensitive and always returns -1 if different
228 */
229static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
230{
231 size_t i;
232 unsigned char diff;
233 const unsigned char *n1 = s1, *n2 = s2;
234
235 for( i = 0; i < len; i++ )
236 {
237 diff = n1[i] ^ n2[i];
238
239 if( diff == 0 )
240 continue;
241
242 if( diff == 32 &&
243 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
244 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
245 {
246 continue;
247 }
248
249 return( -1 );
250 }
251
252 return( 0 );
253}
254
255/*
256 * Return 0 if name matches wildcard, -1 otherwise
257 */
258static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
259{
260 size_t i;
261 size_t cn_idx = 0, cn_len = strlen( cn );
262
263 /* We can't have a match if there is no wildcard to match */
264 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
265 return( -1 );
266
267 for( i = 0; i < cn_len; ++i )
268 {
269 if( cn[i] == '.' )
270 {
271 cn_idx = i;
272 break;
273 }
274 }
275
276 if( cn_idx == 0 )
277 return( -1 );
278
279 if( cn_len - cn_idx == name->len - 1 &&
280 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
281 {
282 return( 0 );
283 }
284
285 return( -1 );
286}
287
288/*
289 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
290 * variations (but not all).
291 *
292 * Return 0 if equal, -1 otherwise.
293 */
294static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
295{
296 if( a->tag == b->tag &&
297 a->len == b->len &&
298 memcmp( a->p, b->p, b->len ) == 0 )
299 {
300 return( 0 );
301 }
302
303 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
304 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
305 a->len == b->len &&
306 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
307 {
308 return( 0 );
309 }
310
311 return( -1 );
312}
313
314/*
315 * Compare two X.509 Names (aka rdnSequence).
316 *
317 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
318 * we sometimes return unequal when the full algorithm would return equal,
319 * but never the other way. (In particular, we don't do Unicode normalisation
320 * or space folding.)
321 *
322 * Return 0 if equal, -1 otherwise.
323 */
324static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
325{
326 /* Avoid recursion, it might not be optimised by the compiler */
327 while( a != NULL || b != NULL )
328 {
329 if( a == NULL || b == NULL )
330 return( -1 );
331
332 /* type */
333 if( a->oid.tag != b->oid.tag ||
334 a->oid.len != b->oid.len ||
335 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
336 {
337 return( -1 );
338 }
339
340 /* value */
341 if( x509_string_cmp( &a->val, &b->val ) != 0 )
342 return( -1 );
343
344 /* structure of the list of sets */
345 if( a->next_merged != b->next_merged )
346 return( -1 );
347
348 a = a->next;
349 b = b->next;
350 }
351
352 /* a == NULL == b */
353 return( 0 );
354}
355
356/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200357 * Reset (init or clear) a verify_chain
358 */
359static void x509_crt_verify_chain_reset(
360 mbedtls_x509_crt_verify_chain *ver_chain )
361{
362 size_t i;
363
364 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
365 {
366 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000367 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200368 }
369
370 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000371
372#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
373 ver_chain->trust_ca_cb_result = NULL;
374#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200375}
376
377/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
379 */
380static int x509_get_version( unsigned char **p,
381 const unsigned char *end,
382 int *ver )
383{
Janos Follath865b3eb2019-12-16 11:46:15 +0000384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200385 size_t len;
386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
388 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 {
392 *ver = 0;
393 return( 0 );
394 }
395
Hanno Becker6ccfb182019-02-12 11:52:10 +0000396 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397 }
398
399 end = *p + len;
400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
402 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403
404 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 return( MBEDTLS_ERR_X509_INVALID_VERSION +
406 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407
408 return( 0 );
409}
410
411/*
412 * Validity ::= SEQUENCE {
413 * notBefore Time,
414 * notAfter Time }
415 */
416static int x509_get_dates( unsigned char **p,
417 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_x509_time *from,
419 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420{
Janos Follath865b3eb2019-12-16 11:46:15 +0000421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422 size_t len;
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
425 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
426 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427
428 end = *p + len;
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431 return( ret );
432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434 return( ret );
435
436 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 return( MBEDTLS_ERR_X509_INVALID_DATE +
438 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439
440 return( 0 );
441}
442
443/*
444 * X.509 v2/v3 unique identifier (not parsed)
445 */
446static int x509_get_uid( unsigned char **p,
447 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449{
Janos Follath865b3eb2019-12-16 11:46:15 +0000450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
452 if( *p == end )
453 return( 0 );
454
455 uid->tag = **p;
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
458 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461 return( 0 );
462
Hanno Becker6ccfb182019-02-12 11:52:10 +0000463 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464 }
465
466 uid->p = *p;
467 *p += uid->len;
468
469 return( 0 );
470}
471
472static int x509_get_basic_constraints( unsigned char **p,
473 const unsigned char *end,
474 int *ca_istrue,
475 int *max_pathlen )
476{
Janos Follath865b3eb2019-12-16 11:46:15 +0000477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478 size_t len;
479
480 /*
481 * BasicConstraints ::= SEQUENCE {
482 * cA BOOLEAN DEFAULT FALSE,
483 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
484 */
485 *ca_istrue = 0; /* DEFAULT FALSE */
486 *max_pathlen = 0; /* endless */
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
489 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
490 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491
492 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200493 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
498 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499
500 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502
503 if( *ca_istrue != 0 )
504 *ca_istrue = 1;
505 }
506
507 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200508 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
511 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512
513 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
515 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516
Andrzej Kurek16050742020-04-14 09:49:52 -0400517 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
518 * overflow, which is an undefined behavior. */
519 if( *max_pathlen == INT_MAX )
520 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
521 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
522
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523 (*max_pathlen)++;
524
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200525 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526}
527
528static int x509_get_ns_cert_type( unsigned char **p,
529 const unsigned char *end,
530 unsigned char *ns_cert_type)
531{
Janos Follath865b3eb2019-12-16 11:46:15 +0000532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
536 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
538 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
540 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541
542 /* Get actual bitstring */
543 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200544 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545}
546
547static int x509_get_key_usage( unsigned char **p,
548 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100549 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550{
Janos Follath865b3eb2019-12-16 11:46:15 +0000551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200552 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
556 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557
558 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
560 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561
562 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200563 *key_usage = 0;
564 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
565 {
566 *key_usage |= (unsigned int) bs.p[i] << (8*i);
567 }
568
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200569 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200570}
571
572/*
573 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
574 *
575 * KeyPurposeId ::= OBJECT IDENTIFIER
576 */
577static int x509_get_ext_key_usage( unsigned char **p,
578 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580{
Janos Follath865b3eb2019-12-16 11:46:15 +0000581 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
584 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585
586 /* Sequence length must be >= 1 */
587 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
589 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200591 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592}
593
594/*
595 * SubjectAltName ::= GeneralNames
596 *
597 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
598 *
599 * GeneralName ::= CHOICE {
600 * otherName [0] OtherName,
601 * rfc822Name [1] IA5String,
602 * dNSName [2] IA5String,
603 * x400Address [3] ORAddress,
604 * directoryName [4] Name,
605 * ediPartyName [5] EDIPartyName,
606 * uniformResourceIdentifier [6] IA5String,
607 * iPAddress [7] OCTET STRING,
608 * registeredID [8] OBJECT IDENTIFIER }
609 *
610 * OtherName ::= SEQUENCE {
611 * type-id OBJECT IDENTIFIER,
612 * value [0] EXPLICIT ANY DEFINED BY type-id }
613 *
614 * EDIPartyName ::= SEQUENCE {
615 * nameAssigner [0] DirectoryString OPTIONAL,
616 * partyName [1] DirectoryString }
617 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300618 * NOTE: we list all types, but only use dNSName and otherName
619 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620 */
621static int x509_get_subject_alt_name( unsigned char **p,
622 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624{
Janos Follath865b3eb2019-12-16 11:46:15 +0000625 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630
631 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
633 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
634 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635
636 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
638 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639
640 while( *p < end )
641 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300642 mbedtls_x509_subject_alternative_name dummy_san_buf;
643 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
644
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645 tag = **p;
646 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
648 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100650 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
651 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
654 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100655 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656
Ron Eldordbbd9662019-05-15 15:46:03 +0300657 /*
irwir74aee1c2019-09-21 18:21:48 +0300658 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300659 */
660 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
661 /*
662 * In case the extension is malformed, return an error,
663 * and clear the allocated sequences.
664 */
665 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
666 {
667 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
668 mbedtls_x509_sequence *seq_prv;
669 while( seq_cur != NULL )
670 {
671 seq_prv = seq_cur;
672 seq_cur = seq_cur->next;
673 mbedtls_platform_zeroize( seq_prv,
674 sizeof( mbedtls_x509_sequence ) );
675 mbedtls_free( seq_prv );
676 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300677 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300678 return( ret );
679 }
680
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200682 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100684 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100686
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200687 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688
689 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200691 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693 cur = cur->next;
694 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200695
696 buf = &(cur->buf);
697 buf->tag = tag;
698 buf->p = *p;
699 buf->len = tag_len;
700 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701 }
702
703 /* Set final sequence entry's next pointer to NULL */
704 cur->next = NULL;
705
706 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
708 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709
710 return( 0 );
711}
712
713/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200714 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
715 *
716 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
717 *
718 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
719 *
720 * PolicyInformation ::= SEQUENCE {
721 * policyIdentifier CertPolicyId,
722 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
723 * PolicyQualifierInfo OPTIONAL }
724 *
725 * CertPolicyId ::= OBJECT IDENTIFIER
726 *
727 * PolicyQualifierInfo ::= SEQUENCE {
728 * policyQualifierId PolicyQualifierId,
729 * qualifier ANY DEFINED BY policyQualifierId }
730 *
731 * -- policyQualifierIds for Internet policy qualifiers
732 *
733 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
734 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
735 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
736 *
737 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
738 *
739 * Qualifier ::= CHOICE {
740 * cPSuri CPSuri,
741 * userNotice UserNotice }
742 *
743 * CPSuri ::= IA5String
744 *
745 * UserNotice ::= SEQUENCE {
746 * noticeRef NoticeReference OPTIONAL,
747 * explicitText DisplayText OPTIONAL }
748 *
749 * NoticeReference ::= SEQUENCE {
750 * organization DisplayText,
751 * noticeNumbers SEQUENCE OF INTEGER }
752 *
753 * DisplayText ::= CHOICE {
754 * ia5String IA5String (SIZE (1..200)),
755 * visibleString VisibleString (SIZE (1..200)),
756 * bmpString BMPString (SIZE (1..200)),
757 * utf8String UTF8String (SIZE (1..200)) }
758 *
759 * NOTE: we only parse and use anyPolicy without qualifiers at this point
760 * as defined in RFC 5280.
761 */
762static int x509_get_certificate_policies( unsigned char **p,
763 const unsigned char *end,
764 mbedtls_x509_sequence *certificate_policies )
765{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300766 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200767 size_t len;
768 mbedtls_asn1_buf *buf;
769 mbedtls_asn1_sequence *cur = certificate_policies;
770
771 /* Get main sequence tag */
772 ret = mbedtls_asn1_get_tag( p, end, &len,
773 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
774 if( ret != 0 )
775 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
776
777 if( *p + len != end )
778 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
779 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
780
781 /*
782 * Cannot be an empty sequence.
783 */
784 if( len == 0 )
785 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
786 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
787
788 while( *p < end )
789 {
790 mbedtls_x509_buf policy_oid;
791 const unsigned char *policy_end;
792
793 /*
794 * Get the policy sequence
795 */
796 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
797 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
798 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
799
800 policy_end = *p + len;
801
Ron Eldor08063792019-05-13 16:38:39 +0300802 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200803 MBEDTLS_ASN1_OID ) ) != 0 )
804 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
805
806 policy_oid.tag = MBEDTLS_ASN1_OID;
807 policy_oid.len = len;
808 policy_oid.p = *p;
809
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300810 /*
811 * Only AnyPolicy is currently supported when enforcing policy.
812 */
813 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
814 {
815 /*
816 * Set the parsing return code but continue parsing, in case this
817 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
818 * is configured.
819 */
820 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
821 }
822
Ron Eldor74d9acc2019-03-21 14:00:03 +0200823 /* Allocate and assign next pointer */
824 if( cur->buf.p != NULL )
825 {
826 if( cur->next != NULL )
827 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
828
829 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
830
831 if( cur->next == NULL )
832 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
833 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
834
835 cur = cur->next;
836 }
837
838 buf = &( cur->buf );
839 buf->tag = policy_oid.tag;
840 buf->p = policy_oid.p;
841 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300842
843 *p += len;
844
845 /*
846 * If there is an optional qualifier, then *p < policy_end
847 * Check the Qualifier len to verify it doesn't exceed policy_end.
848 */
849 if( *p < policy_end )
850 {
851 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
852 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
853 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
854 /*
855 * Skip the optional policy qualifiers.
856 */
857 *p += len;
858 }
859
860 if( *p != policy_end )
861 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
862 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200863 }
864
865 /* Set final sequence entry's next pointer to NULL */
866 cur->next = NULL;
867
868 if( *p != end )
869 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
870 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
871
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300872 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200873}
874
875/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876 * X.509 v3 extensions
877 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878 */
879static int x509_get_crt_ext( unsigned char **p,
880 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200881 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200882 mbedtls_x509_crt_ext_cb_t cb,
883 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200884{
Janos Follath865b3eb2019-12-16 11:46:15 +0000885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200887 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888
Hanno Becker12f62fb2019-02-12 17:22:36 +0000889 if( *p == end )
890 return( 0 );
891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200893 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894
Hanno Becker12f62fb2019-02-12 17:22:36 +0000895 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896 while( *p < end )
897 {
898 /*
899 * Extension ::= SEQUENCE {
900 * extnID OBJECT IDENTIFIER,
901 * critical BOOLEAN DEFAULT FALSE,
902 * extnValue OCTET STRING }
903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 int is_critical = 0; /* DEFAULT FALSE */
906 int ext_type = 0;
907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
909 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
910 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911
912 end_ext_data = *p + len;
913
914 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200915 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200916 MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918
k-stachowiak463928a2018-07-24 12:50:59 +0200919 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920 extn_oid.p = *p;
921 *p += extn_oid.len;
922
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
925 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
926 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927
928 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
930 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
931 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200933 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934 end_ext_octet = *p + len;
935
936 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
938 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939
940 /*
941 * Detect supported extensions
942 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944
945 if( ret != 0 )
946 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200947 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200948 if( cb != NULL )
949 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200950 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200951 if( ret != 0 && is_critical )
952 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200953 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200954 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200955 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200956
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957 /* No parser found, skip extension */
958 *p = end_ext_octet;
959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961 if( is_critical )
962 {
963 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
965 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966 }
967#endif
968 continue;
969 }
970
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100971 /* Forbid repeated extensions */
972 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100974
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975 crt->ext_types |= ext_type;
976
977 switch( ext_type )
978 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100979 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980 /* Parse basic constraints */
981 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
982 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200983 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984 break;
985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987 /* Parse key usage */
988 if( ( ret = x509_get_key_usage( p, end_ext_octet,
989 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200990 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991 break;
992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994 /* Parse extended key usage */
995 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
996 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200997 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998 break;
999
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001000 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001 /* Parse subject alt name */
1002 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1003 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001004 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005 break;
1006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008 /* Parse netscape certificate type */
1009 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1010 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001011 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012 break;
1013
Ron Eldor74d9acc2019-03-21 14:00:03 +02001014 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1015 /* Parse certificate policies type */
1016 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1017 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001018 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001019 /* Give the callback (if any) a chance to handle the extension
1020 * if it contains unsupported policies */
1021 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1022 cb( p_ctx, crt, &extn_oid, is_critical,
1023 start_ext_octet, end_ext_octet ) == 0 )
1024 break;
1025
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001026#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1027 if( is_critical )
1028 return( ret );
1029 else
1030#endif
1031 /*
Ron Eldora2913912019-05-16 16:17:38 +03001032 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1033 * cannot interpret or enforce the policy. However, it is up to
1034 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001035 * unless the extension is critical.
1036 */
1037 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1038 return( ret );
1039 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001040 break;
1041
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001043 /*
1044 * If this is a non-critical extension, which the oid layer
1045 * supports, but there isn't an x509 parser for it,
1046 * skip the extension.
1047 */
1048#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1049 if( is_critical )
1050 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1051 else
1052#endif
1053 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001054 }
1055 }
1056
1057 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1059 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060
1061 return( 0 );
1062}
1063
1064/*
1065 * Parse and fill a single X.509 certificate in DER format
1066 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001067static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1068 const unsigned char *buf,
1069 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001070 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001071 mbedtls_x509_crt_ext_cb_t cb,
1072 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001073{
Janos Follath865b3eb2019-12-16 11:46:15 +00001074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075 size_t len;
1076 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1080 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1081 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001082
1083 /*
1084 * Check for valid input
1085 */
1086 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001089 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001090 p = (unsigned char*) buf;
1091 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092 end = p + len;
1093
1094 /*
1095 * Certificate ::= SEQUENCE {
1096 * tbsCertificate TBSCertificate,
1097 * signatureAlgorithm AlgorithmIdentifier,
1098 * signatureValue BIT STRING }
1099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1101 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 mbedtls_x509_crt_free( crt );
1104 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105 }
1106
Janos Follathcc0e49d2016-02-17 14:34:12 +00001107 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001108 crt->raw.len = crt_end - buf;
1109 if( make_copy != 0 )
1110 {
1111 /* Create and populate a new buffer for the raw field. */
1112 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1113 if( crt->raw.p == NULL )
1114 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1115
1116 memcpy( crt->raw.p, buf, crt->raw.len );
1117 crt->own_buffer = 1;
1118
1119 p += crt->raw.len - len;
1120 end = crt_end = p + len;
1121 }
1122 else
1123 {
1124 crt->raw.p = (unsigned char*) buf;
1125 crt->own_buffer = 0;
1126 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001127
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128 /*
1129 * TBSCertificate ::= SEQUENCE {
1130 */
1131 crt->tbs.p = p;
1132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1134 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 mbedtls_x509_crt_free( crt );
1137 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001138 }
1139
1140 end = p + len;
1141 crt->tbs.len = end - crt->tbs.p;
1142
1143 /*
1144 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1145 *
1146 * CertificateSerialNumber ::= INTEGER
1147 *
1148 * signature AlgorithmIdentifier
1149 */
1150 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1152 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001153 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 return( ret );
1157 }
1158
Andres AG7ca4a032017-03-09 16:16:11 +00001159 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 mbedtls_x509_crt_free( crt );
1162 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001163 }
1164
Andres AG7ca4a032017-03-09 16:16:11 +00001165 crt->version++;
1166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001168 &crt->sig_md, &crt->sig_pk,
1169 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172 return( ret );
1173 }
1174
1175 /*
1176 * issuer Name
1177 */
1178 crt->issuer_raw.p = p;
1179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1181 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 mbedtls_x509_crt_free( crt );
1184 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001185 }
1186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001190 return( ret );
1191 }
1192
1193 crt->issuer_raw.len = p - crt->issuer_raw.p;
1194
1195 /*
1196 * Validity ::= SEQUENCE {
1197 * notBefore Time,
1198 * notAfter Time }
1199 *
1200 */
1201 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1202 &crt->valid_to ) ) != 0 )
1203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001205 return( ret );
1206 }
1207
1208 /*
1209 * subject Name
1210 */
1211 crt->subject_raw.p = p;
1212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1214 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 mbedtls_x509_crt_free( crt );
1217 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001218 }
1219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001223 return( ret );
1224 }
1225
1226 crt->subject_raw.len = p - crt->subject_raw.p;
1227
1228 /*
1229 * SubjectPublicKeyInfo
1230 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001231 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235 return( ret );
1236 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001237 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001238
1239 /*
1240 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1241 * -- If present, version shall be v2 or v3
1242 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1243 * -- If present, version shall be v2 or v3
1244 * extensions [3] EXPLICIT Extensions OPTIONAL
1245 * -- If present, version shall be v3
1246 */
1247 if( crt->version == 2 || crt->version == 3 )
1248 {
1249 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1250 if( ret != 0 )
1251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001253 return( ret );
1254 }
1255 }
1256
1257 if( crt->version == 2 || crt->version == 3 )
1258 {
1259 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1260 if( ret != 0 )
1261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001263 return( ret );
1264 }
1265 }
1266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001268 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001269#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001270 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001271 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001272 if( ret != 0 )
1273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001275 return( ret );
1276 }
1277 }
1278
1279 if( p != end )
1280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 mbedtls_x509_crt_free( crt );
1282 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1283 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001284 }
1285
1286 end = crt_end;
1287
1288 /*
1289 * }
1290 * -- end of TBSCertificate
1291 *
1292 * signatureAlgorithm AlgorithmIdentifier,
1293 * signatureValue BIT STRING
1294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001298 return( ret );
1299 }
1300
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001301 if( crt->sig_oid.len != sig_oid2.len ||
1302 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001303 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001304 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001305 ( sig_params1.len != 0 &&
1306 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 mbedtls_x509_crt_free( crt );
1309 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001310 }
1311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001315 return( ret );
1316 }
1317
1318 if( p != end )
1319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 mbedtls_x509_crt_free( crt );
1321 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1322 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001323 }
1324
1325 return( 0 );
1326}
1327
1328/*
1329 * Parse one X.509 certificate in DER format from a buffer and add them to a
1330 * chained list
1331 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001332static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1333 const unsigned char *buf,
1334 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001335 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001336 mbedtls_x509_crt_ext_cb_t cb,
1337 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001338{
Janos Follath865b3eb2019-12-16 11:46:15 +00001339 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001341
1342 /*
1343 * Check for valid input
1344 */
1345 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001347
1348 while( crt->version != 0 && crt->next != NULL )
1349 {
1350 prev = crt;
1351 crt = crt->next;
1352 }
1353
1354 /*
1355 * Add new certificate on the end of the chain if needed.
1356 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001357 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001358 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001359 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360
1361 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001362 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363
1364 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001367 }
1368
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001369 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001370 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001371 {
1372 if( prev )
1373 prev->next = NULL;
1374
1375 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001377
1378 return( ret );
1379 }
1380
1381 return( 0 );
1382}
1383
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001384int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1385 const unsigned char *buf,
1386 size_t buflen )
1387{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001388 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001389}
1390
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001391int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1392 const unsigned char *buf,
1393 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001394 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001395 mbedtls_x509_crt_ext_cb_t cb,
1396 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001397{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001398 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001399}
1400
1401int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1402 const unsigned char *buf,
1403 size_t buflen )
1404{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001405 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001406}
1407
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001408/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001409 * Parse one or more PEM certificates from a buffer and add them to the chained
1410 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001411 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001412int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1413 const unsigned char *buf,
1414 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415{
Janos Follath98e28a72016-05-31 14:03:54 +01001416#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001417 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001419#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420
1421 /*
1422 * Check for valid input
1423 */
1424 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001426
1427 /*
1428 * Determine buffer content. Buffer contains either one DER certificate or
1429 * one or more PEM certificates.
1430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001432 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001433 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001436 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1439 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001440#else
1441 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1442#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#if defined(MBEDTLS_PEM_PARSE_C)
1445 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001450 /* 1 rather than 0 since the terminating NULL byte is counted in */
1451 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452 {
1453 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001455
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001456 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001458 "-----BEGIN CERTIFICATE-----",
1459 "-----END CERTIFICATE-----",
1460 buf, NULL, 0, &use_len );
1461
1462 if( ret == 0 )
1463 {
1464 /*
1465 * Was PEM encoded
1466 */
1467 buflen -= use_len;
1468 buf += use_len;
1469 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471 {
1472 return( ret );
1473 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001477
1478 /*
1479 * PEM header and footer were found
1480 */
1481 buflen -= use_len;
1482 buf += use_len;
1483
1484 if( first_error == 0 )
1485 first_error = ret;
1486
Paul Bakker5a5fa922014-09-26 14:53:04 +02001487 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488 continue;
1489 }
1490 else
1491 break;
1492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001496
1497 if( ret != 0 )
1498 {
1499 /*
1500 * Quit parsing on a memory error
1501 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001502 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001503 return( ret );
1504
1505 if( first_error == 0 )
1506 first_error = ret;
1507
1508 total_failed++;
1509 continue;
1510 }
1511
1512 success = 1;
1513 }
1514 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515
1516 if( success )
1517 return( total_failed );
1518 else if( first_error )
1519 return( first_error );
1520 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001522#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001523}
1524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526/*
1527 * Load one or more certificates and add them to the chained list
1528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001530{
Janos Follath865b3eb2019-12-16 11:46:15 +00001531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532 size_t n;
1533 unsigned char *buf;
1534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536 return( ret );
1537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001540 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542
1543 return( ret );
1544}
1545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547{
1548 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001549#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001550 int w_ret;
1551 WCHAR szDir[MAX_PATH];
1552 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001553 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001554 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555
Paul Bakker9af723c2014-05-01 13:03:14 +02001556 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557 HANDLE hFind;
1558
1559 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561
Paul Bakker9af723c2014-05-01 13:03:14 +02001562 memset( szDir, 0, sizeof(szDir) );
1563 memset( filename, 0, MAX_PATH );
1564 memcpy( filename, path, len );
1565 filename[len++] = '\\';
1566 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567 filename[len++] = '*';
1568
Simon B3c6b18d2016-11-03 01:11:37 +00001569 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001570 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001571 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573
1574 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001575 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577
1578 len = MAX_PATH - len;
1579 do
1580 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001581 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582
1583 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1584 continue;
1585
Paul Bakker9af723c2014-05-01 13:03:14 +02001586 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001587 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001588 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001589 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001590 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001591 {
1592 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1593 goto cleanup;
1594 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597 if( w_ret < 0 )
1598 ret++;
1599 else
1600 ret += w_ret;
1601 }
1602 while( FindNextFileW( hFind, &file_data ) != 0 );
1603
Paul Bakker66d5d072014-06-17 16:39:18 +02001604 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001606
Ron Eldor36d90422017-01-09 15:09:16 +02001607cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001608 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001609#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001610 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001611 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001613 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001614 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615 DIR *dir = opendir( path );
1616
Paul Bakker66d5d072014-06-17 16:39:18 +02001617 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619
Ron Eldor63140682017-01-09 19:27:59 +02001620#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001621 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001622 {
1623 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001624 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001625 }
Ron Eldor63140682017-01-09 19:27:59 +02001626#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001627
Paul Elliottfb91a482021-03-05 14:17:51 +00001628 memset( &sb, 0, sizeof( sb ) );
1629
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001630 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631 {
Andres AGf9113192016-09-02 14:06:04 +01001632 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1633 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634
Andres AGf9113192016-09-02 14:06:04 +01001635 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636 {
Andres AGf9113192016-09-02 14:06:04 +01001637 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1638 goto cleanup;
1639 }
1640 else if( stat( entry_name, &sb ) == -1 )
1641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001643 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644 }
1645
1646 if( !S_ISREG( sb.st_mode ) )
1647 continue;
1648
1649 // Ignore parse errors
1650 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001652 if( t_ret < 0 )
1653 ret++;
1654 else
1655 ret += t_ret;
1656 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001657
1658cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001659 closedir( dir );
1660
Ron Eldor63140682017-01-09 19:27:59 +02001661#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001662 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001664#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001665
Paul Bakkerbe089b02013-10-14 15:51:50 +02001666#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001667
1668 return( ret );
1669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001672/*
1673 * OtherName ::= SEQUENCE {
1674 * type-id OBJECT IDENTIFIER,
1675 * value [0] EXPLICIT ANY DEFINED BY type-id }
1676 *
1677 * HardwareModuleName ::= SEQUENCE {
1678 * hwType OBJECT IDENTIFIER,
1679 * hwSerialNum OCTET STRING }
1680 *
1681 * NOTE: we currently only parse and use otherName of type HwModuleName,
1682 * as defined in RFC 4108.
1683 */
1684static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1685 mbedtls_x509_san_other_name *other_name )
1686{
Janos Follathab23cd12019-05-09 13:53:57 +01001687 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001688 size_t len;
1689 unsigned char *p = subject_alt_name->p;
1690 const unsigned char *end = p + subject_alt_name->len;
1691 mbedtls_x509_buf cur_oid;
1692
1693 if( ( subject_alt_name->tag &
1694 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1695 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1696 {
1697 /*
1698 * The given subject alternative name is not of type "othername".
1699 */
1700 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1701 }
1702
1703 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1704 MBEDTLS_ASN1_OID ) ) != 0 )
1705 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1706
1707 cur_oid.tag = MBEDTLS_ASN1_OID;
1708 cur_oid.p = p;
1709 cur_oid.len = len;
1710
1711 /*
1712 * Only HwModuleName is currently supported.
1713 */
1714 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1715 {
1716 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1717 }
1718
Ron Eldor890819a2019-05-13 19:03:04 +03001719 if( p + len >= end )
1720 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001721 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001722 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1723 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1724 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001725 p += len;
1726 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1727 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
1728 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1729
1730 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1731 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1732 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1733
1734 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
1735 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1736
1737 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1738 other_name->value.hardware_module_name.oid.p = p;
1739 other_name->value.hardware_module_name.oid.len = len;
1740
Ron Eldor890819a2019-05-13 19:03:04 +03001741 if( p + len >= end )
1742 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001743 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001744 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1745 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1746 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001747 p += len;
1748 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1749 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Ron Eldor890819a2019-05-13 19:03:04 +03001750 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001751
1752 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1753 other_name->value.hardware_module_name.val.p = p;
1754 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001755 p += len;
1756 if( p != end )
1757 {
Ron Eldor890819a2019-05-13 19:03:04 +03001758 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001759 sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001760 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1761 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001762 }
1763 return( 0 );
1764}
1765
Peter Kolbus9a969b62018-12-11 13:55:56 -06001766int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1767 mbedtls_x509_subject_alternative_name *san )
1768{
1769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1770 switch( san_buf->tag &
1771 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1772 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1773 {
1774 /*
1775 * otherName
1776 */
1777 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1778 {
1779 mbedtls_x509_san_other_name other_name;
1780
1781 ret = x509_get_other_name( san_buf, &other_name );
1782 if( ret != 0 )
1783 return( ret );
1784
1785 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1786 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1787 memcpy( &san->san.other_name,
1788 &other_name, sizeof( other_name ) );
1789
1790 }
1791 break;
1792
1793 /*
1794 * dNSName
1795 */
1796 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1797 {
1798 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1799 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1800
1801 memcpy( &san->san.unstructured_name,
1802 san_buf, sizeof( *san_buf ) );
1803
1804 }
1805 break;
1806
1807 /*
1808 * Type not supported
1809 */
1810 default:
1811 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1812 }
1813 return( 0 );
1814}
1815
1816#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001817static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001818 const mbedtls_x509_sequence
1819 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001820 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001821{
Janos Follath865b3eb2019-12-16 11:46:15 +00001822 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001823 size_t n = *size;
1824 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001826 mbedtls_x509_subject_alternative_name san;
1827 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001828
1829 while( cur != NULL )
1830 {
Ron Eldor890819a2019-05-13 19:03:04 +03001831 memset( &san, 0, sizeof( san ) );
1832 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1833 if( parse_ret != 0 )
1834 {
1835 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1836 {
1837 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1838 MBEDTLS_X509_SAFE_SNPRINTF;
1839 }
1840 else
1841 {
1842 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1843 MBEDTLS_X509_SAFE_SNPRINTF;
1844 }
1845 cur = cur->next;
1846 continue;
1847 }
1848
1849 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001850 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001851 /*
1852 * otherName
1853 */
Ron Eldora2913912019-05-16 16:17:38 +03001854 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001855 {
1856 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1857
1858 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1859 MBEDTLS_X509_SAFE_SNPRINTF;
1860
1861 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1862 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001863 {
Ron Eldor890819a2019-05-13 19:03:04 +03001864 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1865 MBEDTLS_X509_SAFE_SNPRINTF;
1866 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001867 MBEDTLS_X509_SAFE_SNPRINTF;
1868
Ron Eldor890819a2019-05-13 19:03:04 +03001869 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1870 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001871
Ron Eldor890819a2019-05-13 19:03:04 +03001872 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1873 MBEDTLS_X509_SAFE_SNPRINTF;
1874
1875 if( other_name->value.hardware_module_name.val.len >= n )
1876 {
1877 *p = '\0';
1878 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001879 }
1880
Ron Eldora2913912019-05-16 16:17:38 +03001881 memcpy( p, other_name->value.hardware_module_name.val.p,
1882 other_name->value.hardware_module_name.val.len );
1883 p += other_name->value.hardware_module_name.val.len;
1884
Ron Eldor890819a2019-05-13 19:03:04 +03001885 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001886
Ron Eldor890819a2019-05-13 19:03:04 +03001887 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1888 }
1889 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001890
1891 /*
1892 * dNSName
1893 */
Ron Eldora2913912019-05-16 16:17:38 +03001894 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001895 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001896 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1897 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001898 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001899 {
1900 *p = '\0';
1901 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1902 }
Ron Eldora2913912019-05-16 16:17:38 +03001903
1904 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1905 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001906 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001907 }
1908 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001909
1910 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001911 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001912 */
1913 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001914 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1915 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001916 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001917 }
1918
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001919 cur = cur->next;
1920 }
1921
1922 *p = '\0';
1923
1924 *size = n;
1925 *buf = p;
1926
1927 return( 0 );
1928}
1929
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001930#define PRINT_ITEM(i) \
1931 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001933 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001934 sep = ", "; \
1935 }
1936
1937#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001938 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001939 PRINT_ITEM( name );
1940
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001941static int x509_info_cert_type( char **buf, size_t *size,
1942 unsigned char ns_cert_type )
1943{
Janos Follath865b3eb2019-12-16 11:46:15 +00001944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001945 size_t n = *size;
1946 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001947 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001950 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1952 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1953 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001954 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1955 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1956 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001957
1958 *size = n;
1959 *buf = p;
1960
1961 return( 0 );
1962}
1963
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001964#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001965 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001966 PRINT_ITEM( name );
1967
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001968static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001969 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001970{
Janos Follath865b3eb2019-12-16 11:46:15 +00001971 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001972 size_t n = *size;
1973 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001974 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1977 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001978 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1979 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1980 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1982 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001983 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1984 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001985
1986 *size = n;
1987 *buf = p;
1988
1989 return( 0 );
1990}
1991
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001992static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001994{
Janos Follath865b3eb2019-12-16 11:46:15 +00001995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001996 const char *desc;
1997 size_t n = *size;
1998 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002000 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002001
2002 while( cur != NULL )
2003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002005 desc = "???";
2006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002008 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002009
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002010 sep = ", ";
2011
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002012 cur = cur->next;
2013 }
2014
2015 *size = n;
2016 *buf = p;
2017
2018 return( 0 );
2019}
2020
Ron Eldor74d9acc2019-03-21 14:00:03 +02002021static int x509_info_cert_policies( char **buf, size_t *size,
2022 const mbedtls_x509_sequence *certificate_policies )
2023{
Janos Follath865b3eb2019-12-16 11:46:15 +00002024 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002025 const char *desc;
2026 size_t n = *size;
2027 char *p = *buf;
2028 const mbedtls_x509_sequence *cur = certificate_policies;
2029 const char *sep = "";
2030
2031 while( cur != NULL )
2032 {
2033 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2034 desc = "???";
2035
2036 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2037 MBEDTLS_X509_SAFE_SNPRINTF;
2038
2039 sep = ", ";
2040
2041 cur = cur->next;
2042 }
2043
2044 *size = n;
2045 *buf = p;
2046
2047 return( 0 );
2048}
2049
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002050/*
2051 * Return an informational string about the certificate.
2052 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002053#define BEFORE_COLON 18
2054#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2056 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002057{
Janos Follath865b3eb2019-12-16 11:46:15 +00002058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002059 size_t n;
2060 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002061 char key_size_str[BEFORE_COLON];
2062
2063 p = buf;
2064 n = size;
2065
Janos Follath98e28a72016-05-31 14:03:54 +01002066 if( NULL == crt )
2067 {
2068 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2069 MBEDTLS_X509_SAFE_SNPRINTF;
2070
2071 return( (int) ( size - n ) );
2072 }
2073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002075 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002076 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002078 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002079 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002082 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002085 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002087 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002090 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002092 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002095 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2096 crt->valid_from.year, crt->valid_from.mon,
2097 crt->valid_from.day, crt->valid_from.hour,
2098 crt->valid_from.min, crt->valid_from.sec );
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002102 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2103 crt->valid_to.year, crt->valid_to.mon,
2104 crt->valid_to.day, crt->valid_to.hour,
2105 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002106 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002109 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002112 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002113 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002114
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002115 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2117 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118 {
2119 return( ret );
2120 }
2121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002123 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002124 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002125
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002126 /*
2127 * Optional extensions
2128 */
2129
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002130 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002133 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002134 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002135
2136 if( crt->max_pathlen > 0 )
2137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002139 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002140 }
2141 }
2142
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002143 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002144 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002145 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002146 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002147
2148 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002149 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002150 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002151 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002152 }
2153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002157 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002158
2159 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2160 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002161 }
2162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002166 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002167
2168 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2169 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002170 }
2171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002175 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002176
2177 if( ( ret = x509_info_ext_key_usage( &p, &n,
2178 &crt->ext_key_usage ) ) != 0 )
2179 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002180 }
2181
Ron Eldor74d9acc2019-03-21 14:00:03 +02002182 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2183 {
2184 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2185 MBEDTLS_X509_SAFE_SNPRINTF;
2186
2187 if( ( ret = x509_info_cert_policies( &p, &n,
2188 &crt->certificate_policies ) ) != 0 )
2189 return( ret );
2190 }
2191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002193 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002194
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002195 return( (int) ( size - n ) );
2196}
2197
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002198struct x509_crt_verify_string {
2199 int code;
2200 const char *string;
2201};
2202
2203static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002204 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002205 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2206 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2207 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2208 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2209 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002210 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2211 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2212 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002213 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002214 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2215 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2216 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2217 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002218 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2219 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2220 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2221 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2222 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2223 { 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 +01002224 { 0, NULL }
2225};
2226
2227int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002228 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002229{
Janos Follath865b3eb2019-12-16 11:46:15 +00002230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002231 const struct x509_crt_verify_string *cur;
2232 char *p = buf;
2233 size_t n = size;
2234
2235 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2236 {
2237 if( ( flags & cur->code ) == 0 )
2238 continue;
2239
2240 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002241 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002242 flags ^= cur->code;
2243 }
2244
2245 if( flags != 0 )
2246 {
2247 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2248 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002249 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002250 }
2251
2252 return( (int) ( size - n ) );
2253}
Peter Kolbus9a969b62018-12-11 13:55:56 -06002254#endif /* MBEDTLS_X509_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002257int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2258 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002259{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002260 unsigned int usage_must, usage_may;
2261 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2262 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2263
2264 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2265 return( 0 );
2266
2267 usage_must = usage & ~may_mask;
2268
2269 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2270 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2271
2272 usage_may = usage & may_mask;
2273
2274 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002276
2277 return( 0 );
2278}
2279#endif
2280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2282int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002283 const char *usage_oid,
2284 size_t usage_len )
2285{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002287
2288 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002290 return( 0 );
2291
2292 /*
2293 * Look for the requested usage (or wildcard ANY) in our list
2294 */
2295 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002298
2299 if( cur_oid->len == usage_len &&
2300 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2301 {
2302 return( 0 );
2303 }
2304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002306 return( 0 );
2307 }
2308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002310}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002314/*
2315 * Return 1 if the certificate is revoked, or 0 otherwise.
2316 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002317int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002320
2321 while( cur != NULL && cur->serial.len != 0 )
2322 {
2323 if( crt->serial.len == cur->serial.len &&
2324 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2325 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002326 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002327 }
2328
2329 cur = cur->next;
2330 }
2331
2332 return( 0 );
2333}
2334
2335/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002336 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002337 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002340 mbedtls_x509_crl *crl_list,
2341 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002342{
2343 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2345 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002346
2347 if( ca == NULL )
2348 return( flags );
2349
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002350 while( crl_list != NULL )
2351 {
2352 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002353 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002354 {
2355 crl_list = crl_list->next;
2356 continue;
2357 }
2358
2359 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002360 * Check if the CA is configured to sign CRLs
2361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002363 if( mbedtls_x509_crt_check_key_usage( ca,
2364 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002367 break;
2368 }
2369#endif
2370
2371 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002372 * Check if CRL is correctly signed by the trusted CA
2373 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002374 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2375 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2376
2377 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2378 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002381 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002382 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002383 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002385 break;
2386 }
2387
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002388 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002389 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2392 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002393 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002396 break;
2397 }
2398
2399 /*
2400 * Check for validity of CRL (Do not drop out)
2401 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002402 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002404
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002405 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002406 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002407
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002408 /*
2409 * Check if certificate is revoked
2410 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002411 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002414 break;
2415 }
2416
2417 crl_list = crl_list->next;
2418 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002419
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002420 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002421}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002423
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002424/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002425 * Check the signature of a certificate by its parent
2426 */
2427static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002428 mbedtls_x509_crt *parent,
2429 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002430{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002431 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002432 size_t hash_len;
2433#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2434 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002435 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002436 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002437
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002438 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002439 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002440 return( -1 );
2441#else
Jaeden Amero34973232019-02-20 10:32:28 +00002442 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002443 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2444
2445 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2446 return( -1 );
2447
2448 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2449 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002450 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002451 return( -1 );
2452 }
2453
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002454 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2455 != PSA_SUCCESS )
2456 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002457 return( -1 );
2458 }
2459#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002460 /* Skip expensive computation on obvious mismatch */
2461 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002462 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002463
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002464#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002465 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2466 {
2467 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002468 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002469 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002470 }
2471#else
2472 (void) rs_ctx;
2473#endif
2474
2475 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002476 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002477 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002478}
2479
2480/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002481 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2482 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002483 *
2484 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2487 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002488 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002489{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002490 int need_ca_bit;
2491
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002492 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002493 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002494 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002495
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002496 /* Parent must have the basicConstraints CA bit set as a general rule */
2497 need_ca_bit = 1;
2498
2499 /* Exception: v1/v2 certificates that are locally trusted. */
2500 if( top && parent->version < 3 )
2501 need_ca_bit = 0;
2502
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002503 if( need_ca_bit && ! parent->ca_istrue )
2504 return( -1 );
2505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002507 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002509 {
2510 return( -1 );
2511 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002512#endif
2513
2514 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002515}
2516
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002517/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002518 * Find a suitable parent for child in candidates, or return NULL.
2519 *
2520 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002521 * 1. subject name matches child's issuer
2522 * 2. if necessary, the CA bit is set and key usage allows signing certs
2523 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002524 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002525 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002526 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002527 * If there's a suitable candidate which is also time-valid, return the first
2528 * such. Otherwise, return the first suitable candidate (or NULL if there is
2529 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002530 *
2531 * The rationale for this rule is that someone could have a list of trusted
2532 * roots with two versions on the same root with different validity periods.
2533 * (At least one user reported having such a list and wanted it to just work.)
2534 * The reason we don't just require time-validity is that generally there is
2535 * only one version, and if it's expired we want the flags to state that
2536 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002537 *
2538 * The rationale for rule 3 (signature for trusted roots) is that users might
2539 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002540 * way we select the correct one is by checking the signature (as we don't
2541 * rely on key identifier extensions). (This is one way users might choose to
2542 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002543 *
2544 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002545 * - [in] child: certificate for which we're looking for a parent
2546 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002547 * - [out] r_parent: parent found (or NULL)
2548 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002549 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2550 * of the chain, 0 otherwise
2551 * - [in] path_cnt: number of intermediates seen so far
2552 * - [in] self_cnt: number of self-signed intermediates seen so far
2553 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002554 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002555 *
2556 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002557 * - 0 on success
2558 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002559 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002560static int x509_crt_find_parent_in(
2561 mbedtls_x509_crt *child,
2562 mbedtls_x509_crt *candidates,
2563 mbedtls_x509_crt **r_parent,
2564 int *r_signature_is_good,
2565 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002566 unsigned path_cnt,
2567 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002568 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002569{
Janos Follath865b3eb2019-12-16 11:46:15 +00002570 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002571 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002572 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002573
2574#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002575 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002576 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2577 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002578 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002579 parent = rs_ctx->parent;
2580 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002581 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002582
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002583 /* clear saved state */
2584 rs_ctx->parent = NULL;
2585 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002586 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002587
2588 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002589 goto check_signature;
2590 }
2591#endif
2592
2593 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002594 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002595
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002596 for( parent = candidates; parent != NULL; parent = parent->next )
2597 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002598 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002599 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002600 continue;
2601
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002602 /* +1 because stored max_pathlen is 1 higher that the actual value */
2603 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002604 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002605 {
2606 continue;
2607 }
2608
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002609 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002610#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2611check_signature:
2612#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002613 ret = x509_crt_check_signature( child, parent, rs_ctx );
2614
2615#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002616 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2617 {
2618 /* save state */
2619 rs_ctx->parent = parent;
2620 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002621 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002622
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002623 return( ret );
2624 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002625#else
2626 (void) ret;
2627#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002628
2629 signature_is_good = ret == 0;
2630 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002631 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002632
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002633 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002634 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2635 mbedtls_x509_time_is_future( &parent->valid_from ) )
2636 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002637 if( fallback_parent == NULL )
2638 {
2639 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002640 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002641 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002642
2643 continue;
2644 }
2645
Andy Gross1f627142019-01-30 10:25:53 -06002646 *r_parent = parent;
2647 *r_signature_is_good = signature_is_good;
2648
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002649 break;
2650 }
2651
Andy Gross1f627142019-01-30 10:25:53 -06002652 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002653 {
2654 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002655 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002656 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002657
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002658 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002659}
2660
2661/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002662 * Find a parent in trusted CAs or the provided chain, or return NULL.
2663 *
2664 * Searches in trusted CAs first, and return the first suitable parent found
2665 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002666 *
2667 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002668 * - [in] child: certificate for which we're looking for a parent, followed
2669 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002670 * - [in] trust_ca: list of locally trusted certificates
2671 * - [out] parent: parent found (or NULL)
2672 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2673 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2674 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2675 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002676 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002677 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002678 *
2679 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002680 * - 0 on success
2681 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002682 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002683static int x509_crt_find_parent(
2684 mbedtls_x509_crt *child,
2685 mbedtls_x509_crt *trust_ca,
2686 mbedtls_x509_crt **parent,
2687 int *parent_is_trusted,
2688 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002689 unsigned path_cnt,
2690 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002691 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002692{
Janos Follath865b3eb2019-12-16 11:46:15 +00002693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002694 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002695
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002696 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002697
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002698#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002699 /* restore then clear saved state if we have some stored */
2700 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2701 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002702 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002703 rs_ctx->parent_is_trusted = -1;
2704 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002705#endif
2706
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002707 while( 1 ) {
2708 search_list = *parent_is_trusted ? trust_ca : child->next;
2709
2710 ret = x509_crt_find_parent_in( child, search_list,
2711 parent, signature_is_good,
2712 *parent_is_trusted,
2713 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002714
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002715#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002716 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2717 {
2718 /* save state */
2719 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002720 return( ret );
2721 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002722#else
2723 (void) ret;
2724#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002725
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002726 /* stop here if found or already in second iteration */
2727 if( *parent != NULL || *parent_is_trusted == 0 )
2728 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002729
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002730 /* prepare second iteration */
2731 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002732 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002733
2734 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002735 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002736 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002737 *parent_is_trusted = 0;
2738 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002739 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002740
2741 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002742}
2743
2744/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002745 * Check if an end-entity certificate is locally trusted
2746 *
2747 * Currently we require such certificates to be self-signed (actually only
2748 * check for self-issued as self-signatures are not checked)
2749 */
2750static int x509_crt_check_ee_locally_trusted(
2751 mbedtls_x509_crt *crt,
2752 mbedtls_x509_crt *trust_ca )
2753{
2754 mbedtls_x509_crt *cur;
2755
2756 /* must be self-issued */
2757 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2758 return( -1 );
2759
2760 /* look for an exact match with trusted cert */
2761 for( cur = trust_ca; cur != NULL; cur = cur->next )
2762 {
2763 if( crt->raw.len == cur->raw.len &&
2764 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2765 {
2766 return( 0 );
2767 }
2768 }
2769
2770 /* too bad */
2771 return( -1 );
2772}
2773
2774/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002775 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002776 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002777 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2778 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002779 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002780 * such that every cert in the chain is a child of the next one,
2781 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002782 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002783 * Verify that chain and return it with flags for all issues found.
2784 *
2785 * Special cases:
2786 * - EE == Rj -> return a one-element list containing it
2787 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2788 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002789 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002790 * Tests for (aspects of) this function should include at least:
2791 * - trusted EE
2792 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002793 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002794 * - if relevant: EE untrusted
2795 * - if relevant: EE -> intermediate, untrusted
2796 * with the aspect under test checked at each relevant level (EE, int, root).
2797 * For some aspects longer chains are required, but usually length 2 is
2798 * enough (but length 1 is not in general).
2799 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002800 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002801 * - [in] crt: the cert list EE, C1, ..., Cn
2802 * - [in] trust_ca: the trusted list R1, ..., Rp
2803 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002804 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002805 * Only valid when return value is 0, may contain garbage otherwise!
2806 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002807 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002808 *
2809 * Return value:
2810 * - non-zero if the chain could not be fully built and examined
2811 * - 0 is the chain was successfully built and examined,
2812 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002813 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002814static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002815 mbedtls_x509_crt *crt,
2816 mbedtls_x509_crt *trust_ca,
2817 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002818 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2819 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002820 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002821 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002822 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002823{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002824 /* Don't initialize any of those variables here, so that the compiler can
2825 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002827 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002828 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002829 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002830 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002831 int parent_is_trusted;
2832 int child_is_trusted;
2833 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002834 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002835 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002836
2837#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2838 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002839 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002840 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002841 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002842 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002843 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002844
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002845 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002846 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002847 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002848 flags = &cur->flags;
2849
2850 goto find_parent;
2851 }
2852#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002853
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002854 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002855 self_cnt = 0;
2856 parent_is_trusted = 0;
2857 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002858
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002859 while( 1 ) {
2860 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002861 cur = &ver_chain->items[ver_chain->len];
2862 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002863 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002864 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002865 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002866
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002867 /* Check time-validity (all certificates) */
2868 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2869 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002870
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002871 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2872 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002873
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002874 /* Stop here for trusted roots (but not for trusted EE certs) */
2875 if( child_is_trusted )
2876 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002877
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002878 /* Check signature algorithm: MD & PK algs */
2879 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2880 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002881
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002882 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2883 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002884
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002885 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002886 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002887 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2888 {
2889 return( 0 );
2890 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002891
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002892#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2893find_parent:
2894#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002895
2896 /* Obtain list of potential trusted signers from CA callback,
2897 * or use statically provided list. */
2898#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2899 if( f_ca_cb != NULL )
2900 {
2901 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2902 mbedtls_free( ver_chain->trust_ca_cb_result );
2903 ver_chain->trust_ca_cb_result = NULL;
2904
2905 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2906 if( ret != 0 )
2907 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2908
2909 cur_trust_ca = ver_chain->trust_ca_cb_result;
2910 }
2911 else
2912#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2913 {
2914 ((void) f_ca_cb);
2915 ((void) p_ca_cb);
2916 cur_trust_ca = trust_ca;
2917 }
2918
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002919 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002920 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002921 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002922 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002923
2924#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002925 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2926 {
2927 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002928 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002929 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002930 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002931
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002932 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002933 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002934#else
2935 (void) ret;
2936#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002937
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002938 /* No parent? We're done here */
2939 if( parent == NULL )
2940 {
2941 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2942 return( 0 );
2943 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002944
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002945 /* Count intermediate self-issued (not necessarily self-signed) certs.
2946 * These can occur with some strategies for key rollover, see [SIRO],
2947 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002948 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002949 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2950 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002951 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002952 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002953
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002954 /* path_cnt is 0 for the first intermediate CA,
2955 * and if parent is trusted it's not an intermediate CA */
2956 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002957 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002958 {
2959 /* return immediately to avoid overflow the chain array */
2960 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2961 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002962
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002963 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002964 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002965 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2966
2967 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002968 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002969 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002972 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002973 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002974#else
2975 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002976#endif
2977
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002978 /* prepare for next iteration */
2979 child = parent;
2980 parent = NULL;
2981 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002982 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002983 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002984}
2985
2986/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002987 * Check for CN match
2988 */
2989static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2990 const char *cn, size_t cn_len )
2991{
2992 /* try exact match */
2993 if( name->len == cn_len &&
2994 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2995 {
2996 return( 0 );
2997 }
2998
2999 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003000 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003001 {
3002 return( 0 );
3003 }
3004
3005 return( -1 );
3006}
3007
3008/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003009 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3010 */
3011static int x509_crt_check_san( const mbedtls_x509_buf *name,
3012 const char *cn, size_t cn_len )
3013{
3014 const unsigned char san_type = (unsigned char) name->tag &
3015 MBEDTLS_ASN1_TAG_VALUE_MASK;
3016
3017 /* dNSName */
3018 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3019 return( x509_crt_check_cn( name, cn, cn_len ) );
3020
3021 /* (We may handle other types here later.) */
3022
3023 /* Unrecognized type */
3024 return( -1 );
3025}
3026
3027/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003028 * Verify the requested CN - only call this if cn is not NULL!
3029 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003030static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003031 const char *cn,
3032 uint32_t *flags )
3033{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003034 const mbedtls_x509_name *name;
3035 const mbedtls_x509_sequence *cur;
3036 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003037
3038 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3039 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003040 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003041 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003042 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003043 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003044 }
3045
3046 if( cur == NULL )
3047 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3048 }
3049 else
3050 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003051 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003052 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003053 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3054 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003055 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003056 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003057 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003058 }
3059
3060 if( name == NULL )
3061 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3062 }
3063}
3064
3065/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003066 * Merge the flags for all certs in the chain, after calling callback
3067 */
3068static int x509_crt_merge_flags_with_cb(
3069 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003070 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003071 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3072 void *p_vrfy )
3073{
Janos Follath865b3eb2019-12-16 11:46:15 +00003074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003075 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003076 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003077 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003078
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003079 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003080 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003081 cur = &ver_chain->items[i-1];
3082 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003083
3084 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003085 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003086 return( ret );
3087
3088 *flags |= cur_flags;
3089 }
3090
3091 return( 0 );
3092}
3093
3094/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003095 * Verify the certificate validity, with profile, restartable version
3096 *
3097 * This function:
3098 * - checks the requested CN (if any)
3099 * - checks the type and size of the EE cert's key,
3100 * as that isn't done as part of chain building/verification currently
3101 * - builds and verifies the chain
3102 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003103 *
3104 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3105 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3106 * verification routine to search for trusted signers, and CRLs will
3107 * be disabled. Otherwise, `trust_ca` will be used as the static list
3108 * of trusted signers, and `ca_crl` will be use as the static list
3109 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003110 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003111static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003112 mbedtls_x509_crt *trust_ca,
3113 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003114 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3115 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003116 const mbedtls_x509_crt_profile *profile,
3117 const char *cn, uint32_t *flags,
3118 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3119 void *p_vrfy,
3120 mbedtls_x509_crt_restart_ctx *rs_ctx )
3121{
Janos Follath865b3eb2019-12-16 11:46:15 +00003122 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003123 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003124 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003125 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003126
3127 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003128 ee_flags = 0;
3129 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003130
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003131 if( profile == NULL )
3132 {
3133 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3134 goto exit;
3135 }
3136
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003137 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003138 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003139 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003140
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003141 /* Check the type and size of the key */
3142 pk_type = mbedtls_pk_get_type( &crt->pk );
3143
3144 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003145 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003146
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003147 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003148 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003149
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003150 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003151 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3152 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003153 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003154
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003155 if( ret != 0 )
3156 goto exit;
3157
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003158 /* Merge end-entity flags */
3159 ver_chain.items[0].flags |= ee_flags;
3160
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003161 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003162 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003163
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003164exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003165
3166#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3167 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3168 mbedtls_free( ver_chain.trust_ca_cb_result );
3169 ver_chain.trust_ca_cb_result = NULL;
3170#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3171
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003172#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3173 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3174 mbedtls_x509_crt_restart_free( rs_ctx );
3175#endif
3176
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003177 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3178 * the SSL module for authmode optional, but non-zero return from the
3179 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003180 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3181 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3182
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003183 if( ret != 0 )
3184 {
3185 *flags = (uint32_t) -1;
3186 return( ret );
3187 }
3188
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003189 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003191
3192 return( 0 );
3193}
3194
Hanno Becker3116fb32019-03-28 13:34:42 +00003195
3196/*
3197 * Verify the certificate validity (default profile, not restartable)
3198 */
3199int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3200 mbedtls_x509_crt *trust_ca,
3201 mbedtls_x509_crl *ca_crl,
3202 const char *cn, uint32_t *flags,
3203 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3204 void *p_vrfy )
3205{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003206 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003207 NULL, NULL,
3208 &mbedtls_x509_crt_profile_default,
3209 cn, flags,
3210 f_vrfy, p_vrfy, NULL ) );
3211}
3212
3213/*
3214 * Verify the certificate validity (user-chosen profile, not restartable)
3215 */
3216int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3217 mbedtls_x509_crt *trust_ca,
3218 mbedtls_x509_crl *ca_crl,
3219 const mbedtls_x509_crt_profile *profile,
3220 const char *cn, uint32_t *flags,
3221 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3222 void *p_vrfy )
3223{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003224 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003225 NULL, NULL,
3226 profile, cn, flags,
3227 f_vrfy, p_vrfy, NULL ) );
3228}
3229
3230#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3231/*
3232 * Verify the certificate validity (user-chosen profile, CA callback,
3233 * not restartable).
3234 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003235int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003236 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3237 void *p_ca_cb,
3238 const mbedtls_x509_crt_profile *profile,
3239 const char *cn, uint32_t *flags,
3240 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3241 void *p_vrfy )
3242{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003243 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003244 f_ca_cb, p_ca_cb,
3245 profile, cn, flags,
3246 f_vrfy, p_vrfy, NULL ) );
3247}
3248#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3249
3250int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3251 mbedtls_x509_crt *trust_ca,
3252 mbedtls_x509_crl *ca_crl,
3253 const mbedtls_x509_crt_profile *profile,
3254 const char *cn, uint32_t *flags,
3255 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3256 void *p_vrfy,
3257 mbedtls_x509_crt_restart_ctx *rs_ctx )
3258{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003259 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003260 NULL, NULL,
3261 profile, cn, flags,
3262 f_vrfy, p_vrfy, rs_ctx ) );
3263}
3264
3265
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003266/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003267 * Initialize a certificate chain
3268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003270{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003272}
3273
3274/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003275 * Unallocate all certificate data
3276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003278{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279 mbedtls_x509_crt *cert_cur = crt;
3280 mbedtls_x509_crt *cert_prv;
3281 mbedtls_x509_name *name_cur;
3282 mbedtls_x509_name *name_prv;
3283 mbedtls_x509_sequence *seq_cur;
3284 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003285
3286 if( crt == NULL )
3287 return;
3288
3289 do
3290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3294 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003295#endif
3296
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003297 name_cur = cert_cur->issuer.next;
3298 while( name_cur != NULL )
3299 {
3300 name_prv = name_cur;
3301 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003302 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003304 }
3305
3306 name_cur = cert_cur->subject.next;
3307 while( name_cur != NULL )
3308 {
3309 name_prv = name_cur;
3310 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003311 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003313 }
3314
3315 seq_cur = cert_cur->ext_key_usage.next;
3316 while( seq_cur != NULL )
3317 {
3318 seq_prv = seq_cur;
3319 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003320 mbedtls_platform_zeroize( seq_prv,
3321 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003323 }
3324
3325 seq_cur = cert_cur->subject_alt_names.next;
3326 while( seq_cur != NULL )
3327 {
3328 seq_prv = seq_cur;
3329 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003330 mbedtls_platform_zeroize( seq_prv,
3331 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003333 }
3334
Ron Eldor74d9acc2019-03-21 14:00:03 +02003335 seq_cur = cert_cur->certificate_policies.next;
3336 while( seq_cur != NULL )
3337 {
3338 seq_prv = seq_cur;
3339 seq_cur = seq_cur->next;
3340 mbedtls_platform_zeroize( seq_prv,
3341 sizeof( mbedtls_x509_sequence ) );
3342 mbedtls_free( seq_prv );
3343 }
3344
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003345 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003346 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003347 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003349 }
3350
3351 cert_cur = cert_cur->next;
3352 }
3353 while( cert_cur != NULL );
3354
3355 cert_cur = crt;
3356 do
3357 {
3358 cert_prv = cert_cur;
3359 cert_cur = cert_cur->next;
3360
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003361 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003362 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003364 }
3365 while( cert_cur != NULL );
3366}
3367
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003368#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3369/*
3370 * Initialize a restart context
3371 */
3372void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3373{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003374 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003375
3376 ctx->parent = NULL;
3377 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003378 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003379
3380 ctx->parent_is_trusted = -1;
3381
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003382 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003383 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003384 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003385}
3386
3387/*
3388 * Free the components of a restart context
3389 */
3390void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3391{
3392 if( ctx == NULL )
3393 return;
3394
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003395 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003396 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003397}
3398#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400#endif /* MBEDTLS_X509_CRT_PARSE_C */