blob: 2a2a5ce4de6d9117eb6cbf3363608567d93a98bb [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"
pespacek7599a772022-02-07 14:40:55 +010050#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemek Stekiel40afdd22022-09-06 13:08:28 +020051#include "hash_info.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040052
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010057#endif
58
Daniel Axtensf0710242020-05-28 11:43:41 +100059#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010060#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#include <windows.h>
62#else
63#include <time.h>
64#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100065#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000068#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020069#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070#include <sys/types.h>
71#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020072#if defined(__MBED__)
73#include <platform/mbed_retarget.h>
74#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020076#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060077#include <errno.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
Gilles Peskineffb92da2021-06-02 00:03:26 +020094/* Default profile. Do not remove items unless there are serious security
95 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020096const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
97{
Gilles Peskineae270bf2021-06-02 00:05:29 +020098 /* Hashes from SHA-256 and above. Note that this selection
99 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200100 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
101 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
102 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
103 0xFFFFFFF, /* Any PK alg */
104#if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200105 /* Curves at or above 128-bit security level. Note that this selection
106 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200107 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
108 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
110 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
111 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
112 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
Gilles Peskine39957502021-06-17 23:17:52 +0200113 0,
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200114#else
115 0,
116#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200117 2048,
118};
119
Gilles Peskineffb92da2021-06-02 00:03:26 +0200120/* Next-generation profile. Currently identical to the default, but may
121 * be tightened at any time. */
122const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200123{
124 /* Hashes from SHA-256 and above. */
125 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
126 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
127 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
128 0xFFFFFFF, /* Any PK alg */
129#if defined(MBEDTLS_ECP_C)
130 /* Curves at or above 128-bit security level. */
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
136 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
138#else
139 0,
140#endif
141 2048,
142};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200143
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200144/*
145 * NSA Suite B Profile
146 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200147const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
148{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200149 /* Only SHA-256 and 384 */
150 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
151 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
152 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200153 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
154 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200155#if defined(MBEDTLS_ECP_C)
156 /* Only NIST P-256 and P-384 */
157 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
158 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
159#else
160 0,
161#endif
162 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200163};
164
165/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200166 * Empty / all-forbidden profile
167 */
168const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
169{
170 0,
171 0,
172 0,
173 (uint32_t) -1,
174};
175
176/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200177 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200178 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200179 */
180static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
181 mbedtls_md_type_t md_alg )
182{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200183 if( md_alg == MBEDTLS_MD_NONE )
184 return( -1 );
185
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200186 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
187 return( 0 );
188
189 return( -1 );
190}
191
192/*
193 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200194 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200195 */
196static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
197 mbedtls_pk_type_t pk_alg )
198{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200199 if( pk_alg == MBEDTLS_PK_NONE )
200 return( -1 );
201
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200202 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
203 return( 0 );
204
205 return( -1 );
206}
207
208/*
209 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200210 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200211 */
212static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200213 const mbedtls_pk_context *pk )
214{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200215 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200216
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200217#if defined(MBEDTLS_RSA_C)
218 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
219 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200220 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200221 return( 0 );
222
223 return( -1 );
224 }
225#endif
226
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200227#if defined(MBEDTLS_ECP_C)
228 if( pk_alg == MBEDTLS_PK_ECDSA ||
229 pk_alg == MBEDTLS_PK_ECKEY ||
230 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200231 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200232 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200233
Philippe Antoineb5b25432018-05-11 11:06:29 +0200234 if( gid == MBEDTLS_ECP_DP_NONE )
235 return( -1 );
236
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200237 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
238 return( 0 );
239
240 return( -1 );
241 }
242#endif
243
244 return( -1 );
245}
246
247/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000248 * Like memcmp, but case-insensitive and always returns -1 if different
249 */
250static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
251{
252 size_t i;
253 unsigned char diff;
254 const unsigned char *n1 = s1, *n2 = s2;
255
256 for( i = 0; i < len; i++ )
257 {
258 diff = n1[i] ^ n2[i];
259
260 if( diff == 0 )
261 continue;
262
263 if( diff == 32 &&
264 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
265 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
266 {
267 continue;
268 }
269
270 return( -1 );
271 }
272
273 return( 0 );
274}
275
276/*
277 * Return 0 if name matches wildcard, -1 otherwise
278 */
279static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
280{
281 size_t i;
282 size_t cn_idx = 0, cn_len = strlen( cn );
283
284 /* We can't have a match if there is no wildcard to match */
285 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
286 return( -1 );
287
288 for( i = 0; i < cn_len; ++i )
289 {
290 if( cn[i] == '.' )
291 {
292 cn_idx = i;
293 break;
294 }
295 }
296
297 if( cn_idx == 0 )
298 return( -1 );
299
300 if( cn_len - cn_idx == name->len - 1 &&
301 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
302 {
303 return( 0 );
304 }
305
306 return( -1 );
307}
308
309/*
310 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
311 * variations (but not all).
312 *
313 * Return 0 if equal, -1 otherwise.
314 */
315static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
316{
317 if( a->tag == b->tag &&
318 a->len == b->len &&
319 memcmp( a->p, b->p, b->len ) == 0 )
320 {
321 return( 0 );
322 }
323
324 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
325 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
326 a->len == b->len &&
327 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
328 {
329 return( 0 );
330 }
331
332 return( -1 );
333}
334
335/*
336 * Compare two X.509 Names (aka rdnSequence).
337 *
338 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
339 * we sometimes return unequal when the full algorithm would return equal,
340 * but never the other way. (In particular, we don't do Unicode normalisation
341 * or space folding.)
342 *
343 * Return 0 if equal, -1 otherwise.
344 */
345static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
346{
347 /* Avoid recursion, it might not be optimised by the compiler */
348 while( a != NULL || b != NULL )
349 {
350 if( a == NULL || b == NULL )
351 return( -1 );
352
353 /* type */
354 if( a->oid.tag != b->oid.tag ||
355 a->oid.len != b->oid.len ||
356 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
357 {
358 return( -1 );
359 }
360
361 /* value */
362 if( x509_string_cmp( &a->val, &b->val ) != 0 )
363 return( -1 );
364
365 /* structure of the list of sets */
366 if( a->next_merged != b->next_merged )
367 return( -1 );
368
369 a = a->next;
370 b = b->next;
371 }
372
373 /* a == NULL == b */
374 return( 0 );
375}
376
377/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200378 * Reset (init or clear) a verify_chain
379 */
380static void x509_crt_verify_chain_reset(
381 mbedtls_x509_crt_verify_chain *ver_chain )
382{
383 size_t i;
384
385 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
386 {
387 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000388 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200389 }
390
391 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000392
393#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
394 ver_chain->trust_ca_cb_result = NULL;
395#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200396}
397
398/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
400 */
401static int x509_get_version( unsigned char **p,
402 const unsigned char *end,
403 int *ver )
404{
Janos Follath865b3eb2019-12-16 11:46:15 +0000405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 size_t len;
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
409 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 {
413 *ver = 0;
414 return( 0 );
415 }
416
Chris Jones9f7a6932021-04-14 12:12:09 +0100417 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 }
419
420 end = *p + len;
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100423 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424
425 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100426 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
427 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
429 return( 0 );
430}
431
432/*
433 * Validity ::= SEQUENCE {
434 * notBefore Time,
435 * notAfter Time }
436 */
437static int x509_get_dates( unsigned char **p,
438 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_x509_time *from,
440 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441{
Janos Follath865b3eb2019-12-16 11:46:15 +0000442 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443 size_t len;
444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
446 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100447 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448
449 end = *p + len;
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452 return( ret );
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 return( ret );
456
457 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100458 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
459 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
461 return( 0 );
462}
463
464/*
465 * X.509 v2/v3 unique identifier (not parsed)
466 */
467static int x509_get_uid( unsigned char **p,
468 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470{
Janos Follath865b3eb2019-12-16 11:46:15 +0000471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
473 if( *p == end )
474 return( 0 );
475
476 uid->tag = **p;
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
479 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482 return( 0 );
483
Chris Jones9f7a6932021-04-14 12:12:09 +0100484 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 }
486
487 uid->p = *p;
488 *p += uid->len;
489
490 return( 0 );
491}
492
493static int x509_get_basic_constraints( unsigned char **p,
494 const unsigned char *end,
495 int *ca_istrue,
496 int *max_pathlen )
497{
Janos Follath865b3eb2019-12-16 11:46:15 +0000498 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499 size_t len;
500
501 /*
502 * BasicConstraints ::= SEQUENCE {
503 * cA BOOLEAN DEFAULT FALSE,
504 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
505 */
506 *ca_istrue = 0; /* DEFAULT FALSE */
507 *max_pathlen = 0; /* endless */
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
510 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100511 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512
513 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200514 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
519 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520
521 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100522 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523
524 if( *ca_istrue != 0 )
525 *ca_istrue = 1;
526 }
527
528 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200529 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100532 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533
534 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100535 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
536 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
Andrzej Kurek16050742020-04-14 09:49:52 -0400538 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
539 * overflow, which is an undefined behavior. */
540 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100541 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
542 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400543
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544 (*max_pathlen)++;
545
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200546 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547}
548
549static int x509_get_ns_cert_type( unsigned char **p,
550 const unsigned char *end,
551 unsigned char *ns_cert_type)
552{
Janos Follath865b3eb2019-12-16 11:46:15 +0000553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100557 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
559 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100560 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
561 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562
563 /* Get actual bitstring */
564 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200565 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566}
567
568static int x509_get_key_usage( unsigned char **p,
569 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100570 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571{
Janos Follath865b3eb2019-12-16 11:46:15 +0000572 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200573 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100577 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578
579 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100580 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
581 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582
583 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200584 *key_usage = 0;
585 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
586 {
587 *key_usage |= (unsigned int) bs.p[i] << (8*i);
588 }
589
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200590 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591}
592
593/*
594 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
595 *
596 * KeyPurposeId ::= OBJECT IDENTIFIER
597 */
598static int x509_get_ext_key_usage( unsigned char **p,
599 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200601{
Janos Follath865b3eb2019-12-16 11:46:15 +0000602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100605 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200606
607 /* Sequence length must be >= 1 */
608 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100609 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
610 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200612 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200613}
614
615/*
toth92g1bbc2fe2021-02-12 16:11:17 +0100616* SubjectKeyIdentifier ::= KeyIdentifier
617*
618* KeyIdentifier ::= OCTET STRING
619*/
toth92gdd123902021-04-08 10:12:52 +0200620static int x509_get_subject_key_id( unsigned char** p,
toth92g1bbc2fe2021-02-12 16:11:17 +0100621 const unsigned char* end,
toth92gdd123902021-04-08 10:12:52 +0200622 mbedtls_x509_buf* subject_key_id )
toth92g1bbc2fe2021-02-12 16:11:17 +0100623{
624 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
625 size_t len = 0u;
626
toth92g98fa4c92021-04-27 15:41:25 +0200627 if ( ( ret = mbedtls_asn1_get_tag( p, end, &len,
628 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100629 {
toth92gdd123902021-04-08 10:12:52 +0200630 return( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +0100631 }
632 else
633 {
634 subject_key_id->len = len;
635 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
636 subject_key_id->p = *p;
637 *p += len;
638 }
toth92gbf2dae12021-05-04 11:31:03 +0200639
640 if( *p != end )
641 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
642 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
toth92g1bbc2fe2021-02-12 16:11:17 +0100643
toth92gdd123902021-04-08 10:12:52 +0200644 return( 0 );
toth92g1bbc2fe2021-02-12 16:11:17 +0100645}
646
647/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
649 *
650 * GeneralName ::= CHOICE {
651 * otherName [0] OtherName,
652 * rfc822Name [1] IA5String,
653 * dNSName [2] IA5String,
654 * x400Address [3] ORAddress,
655 * directoryName [4] Name,
656 * ediPartyName [5] EDIPartyName,
657 * uniformResourceIdentifier [6] IA5String,
658 * iPAddress [7] OCTET STRING,
659 * registeredID [8] OBJECT IDENTIFIER }
660 *
661 * OtherName ::= SEQUENCE {
662 * type-id OBJECT IDENTIFIER,
663 * value [0] EXPLICIT ANY DEFINED BY type-id }
664 *
665 * EDIPartyName ::= SEQUENCE {
666 * nameAssigner [0] DirectoryString OPTIONAL,
667 * partyName [1] DirectoryString }
668 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300669 * NOTE: we list all types, but only use dNSName and otherName
670 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671 */
toth92gd2df7ec2021-05-10 15:16:33 +0200672static int x509_get_general_names( unsigned char **p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675{
Janos Follath865b3eb2019-12-16 11:46:15 +0000676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200679 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
682 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
684 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100685 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686
687 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100688 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
689 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690
691 while( *p < end )
692 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300693 mbedtls_x509_subject_alternative_name dummy_san_buf;
694 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
695
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696 tag = **p;
697 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100699 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700
toth92gd2df7ec2021-05-10 15:16:33 +0200701 /* Tag shall be CONTEXT_SPECIFIC or SET */
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100702 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
703 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
704 {
toth92gd2df7ec2021-05-10 15:16:33 +0200705 if( ( tag & (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
706 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
707 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100708 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709
Ron Eldordbbd9662019-05-15 15:46:03 +0300710 /*
irwir74aee1c2019-09-21 18:21:48 +0300711 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300712 */
Przemek Stekiel23be8f72023-01-03 10:23:13 +0100713 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
Ron Eldordbbd9662019-05-15 15:46:03 +0300714 /*
715 * In case the extension is malformed, return an error,
716 * and clear the allocated sequences.
717 */
718 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
719 {
Glenn Straussa4b40412022-06-26 19:32:09 -0400720 mbedtls_asn1_sequence_free( subject_alt_name->next );
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300721 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300722 return( ret );
723 }
724
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200726 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100728 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100730
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200731 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200732
733 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100734 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
735 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737 cur = cur->next;
738 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200739
740 buf = &(cur->buf);
741 buf->tag = tag;
742 buf->p = *p;
743 buf->len = tag_len;
744 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745 }
746
747 /* Set final sequence entry's next pointer to NULL */
748 cur->next = NULL;
749
750 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100751 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
752 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753
754 return( 0 );
755}
756
757/*
toth92gd2df7ec2021-05-10 15:16:33 +0200758 * AuthorityKeyIdentifier ::= SEQUENCE {
759 * keyIdentifier [0] KeyIdentifier OPTIONAL,
760 * authorityCertIssuer [1] GeneralNames OPTIONAL,
761 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
762 *
763 * KeyIdentifier ::= OCTET STRING
764 */
765static int x509_get_authority_key_id( unsigned char** p,
766 unsigned char* end,
767 mbedtls_x509_authority* authority_key_id )
768{
769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
770 size_t len = 0u;
771
772 if ( ( ret = mbedtls_asn1_get_tag( p, end, &len,
773 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE | 0 ) ) != 0 )
774 {
775 return( ret );
776 }
777
778 if ( (ret = mbedtls_asn1_get_tag( p, end, &len,
779 MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
780 {
781 /* KeyIdentifier is an OPTIONAL field */
782 }
783 else
784 {
785 authority_key_id->keyIdentifier.len = len;
786 authority_key_id->keyIdentifier.p = *p;
toth92g93e4f762021-05-11 12:55:58 +0200787 /* Setting tag of the keyIdentfier intentionally to 0x04.
788 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
789 * its tag with the content is the payload of on OCTET STRING primitive */
toth92gd2df7ec2021-05-10 15:16:33 +0200790 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
791
792 *p += len;
793 }
794
795 if ( *p < end )
796 {
toth92g93e4f762021-05-11 12:55:58 +0200797 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92gd2df7ec2021-05-10 15:16:33 +0200798 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
799 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) != 0)
800 {
801 /* authorityCertIssuer is an OPTIONAL field */
802 }
803 else
804 {
toth92g0e095672021-05-11 13:04:25 +0200805 /* Getting directoryName using the required specific class tag [4] */
toth92gd2df7ec2021-05-10 15:16:33 +0200806 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
807 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 4 ) ) != 0)
808 {
809 return(ret);
810 }
811 else
812 {
813 /* "end" also includes the CertSerialNumber field so "len" shall be used */
814 ret = x509_get_general_names( p,
815 (*p+len),
816 &authority_key_id->authorityCertIssuer );
817 }
818 }
819 }
820
821 if ( *p < end )
822 {
823 if ( ( ret = mbedtls_asn1_get_tag( p, end, &len,
824 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_INTEGER ) ) != 0 )
825 {
826 /* authorityCertSerialNumber is an OPTIONAL field, but if there are still data it must be the serial number */
827 return( ret );
828 }
829 else
830 {
831 authority_key_id->authorityCertSerialNumber.len = len;
832 authority_key_id->authorityCertSerialNumber.p = *p;
833 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_OCTET_STRING;
834 *p += len;
835 }
836 }
837
838 if ( *p != end )
839 {
840 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
841 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
842 }
843
844 return( 0 );
845}
846
847/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200848 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
849 *
850 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
851 *
852 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
853 *
854 * PolicyInformation ::= SEQUENCE {
855 * policyIdentifier CertPolicyId,
856 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
857 * PolicyQualifierInfo OPTIONAL }
858 *
859 * CertPolicyId ::= OBJECT IDENTIFIER
860 *
861 * PolicyQualifierInfo ::= SEQUENCE {
862 * policyQualifierId PolicyQualifierId,
863 * qualifier ANY DEFINED BY policyQualifierId }
864 *
865 * -- policyQualifierIds for Internet policy qualifiers
866 *
867 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
868 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
869 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
870 *
871 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
872 *
873 * Qualifier ::= CHOICE {
874 * cPSuri CPSuri,
875 * userNotice UserNotice }
876 *
877 * CPSuri ::= IA5String
878 *
879 * UserNotice ::= SEQUENCE {
880 * noticeRef NoticeReference OPTIONAL,
881 * explicitText DisplayText OPTIONAL }
882 *
883 * NoticeReference ::= SEQUENCE {
884 * organization DisplayText,
885 * noticeNumbers SEQUENCE OF INTEGER }
886 *
887 * DisplayText ::= CHOICE {
888 * ia5String IA5String (SIZE (1..200)),
889 * visibleString VisibleString (SIZE (1..200)),
890 * bmpString BMPString (SIZE (1..200)),
891 * utf8String UTF8String (SIZE (1..200)) }
892 *
893 * NOTE: we only parse and use anyPolicy without qualifiers at this point
894 * as defined in RFC 5280.
895 */
896static int x509_get_certificate_policies( unsigned char **p,
897 const unsigned char *end,
898 mbedtls_x509_sequence *certificate_policies )
899{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300900 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200901 size_t len;
902 mbedtls_asn1_buf *buf;
903 mbedtls_asn1_sequence *cur = certificate_policies;
904
905 /* Get main sequence tag */
906 ret = mbedtls_asn1_get_tag( p, end, &len,
907 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
908 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100909 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200910
911 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100912 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
913 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200914
915 /*
916 * Cannot be an empty sequence.
917 */
918 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100919 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
920 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200921
922 while( *p < end )
923 {
924 mbedtls_x509_buf policy_oid;
925 const unsigned char *policy_end;
926
927 /*
928 * Get the policy sequence
929 */
930 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
931 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100932 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200933
934 policy_end = *p + len;
935
Ron Eldor08063792019-05-13 16:38:39 +0300936 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200937 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100938 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200939
940 policy_oid.tag = MBEDTLS_ASN1_OID;
941 policy_oid.len = len;
942 policy_oid.p = *p;
943
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300944 /*
945 * Only AnyPolicy is currently supported when enforcing policy.
946 */
947 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
948 {
949 /*
950 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200951 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300952 */
953 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
954 }
955
Ron Eldor74d9acc2019-03-21 14:00:03 +0200956 /* Allocate and assign next pointer */
957 if( cur->buf.p != NULL )
958 {
959 if( cur->next != NULL )
960 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
961
962 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
963
964 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100965 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
966 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200967
968 cur = cur->next;
969 }
970
971 buf = &( cur->buf );
972 buf->tag = policy_oid.tag;
973 buf->p = policy_oid.p;
974 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300975
976 *p += len;
977
978 /*
979 * If there is an optional qualifier, then *p < policy_end
980 * Check the Qualifier len to verify it doesn't exceed policy_end.
981 */
982 if( *p < policy_end )
983 {
984 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
985 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100986 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300987 /*
988 * Skip the optional policy qualifiers.
989 */
990 *p += len;
991 }
992
993 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100994 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
995 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200996 }
997
998 /* Set final sequence entry's next pointer to NULL */
999 cur->next = NULL;
1000
1001 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001002 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1003 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +02001004
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001005 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +02001006}
1007
1008/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 * X.509 v3 extensions
1010 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011 */
1012static int x509_get_crt_ext( unsigned char **p,
1013 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001014 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001015 mbedtls_x509_crt_ext_cb_t cb,
1016 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017{
Janos Follath865b3eb2019-12-16 11:46:15 +00001018 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001020 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021
Hanno Becker12f62fb2019-02-12 17:22:36 +00001022 if( *p == end )
1023 return( 0 );
1024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027
Hanno Becker12f62fb2019-02-12 17:22:36 +00001028 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029 while( *p < end )
1030 {
1031 /*
1032 * Extension ::= SEQUENCE {
1033 * extnID OBJECT IDENTIFIER,
1034 * critical BOOLEAN DEFAULT FALSE,
1035 * extnValue OCTET STRING }
1036 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038 int is_critical = 0; /* DEFAULT FALSE */
1039 int ext_type = 0;
1040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
1042 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001043 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044
1045 end_ext_data = *p + len;
1046
1047 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +02001048 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +02001049 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001050 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051
k-stachowiak463928a2018-07-24 12:50:59 +02001052 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053 extn_oid.p = *p;
1054 *p += extn_oid.len;
1055
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
1058 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +01001059 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060
1061 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
1063 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001064 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001066 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067 end_ext_octet = *p + len;
1068
1069 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +01001070 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1071 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072
1073 /*
1074 * Detect supported extensions
1075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077
1078 if( ret != 0 )
1079 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001080 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +02001081 if( cb != NULL )
1082 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001083 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +02001084 if( ret != 0 && is_critical )
1085 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001086 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001087 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001088 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001089
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090 /* No parser found, skip extension */
1091 *p = end_ext_octet;
1092
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093 if( is_critical )
1094 {
1095 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +01001096 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1097 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099 continue;
1100 }
1101
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001102 /* Forbid repeated extensions */
1103 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001105
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106 crt->ext_types |= ext_type;
1107
1108 switch( ext_type )
1109 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001110 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111 /* Parse basic constraints */
1112 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1113 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001114 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001115 break;
1116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118 /* Parse key usage */
1119 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1120 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001121 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122 break;
1123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001125 /* Parse extended key usage */
1126 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1127 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001128 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129 break;
toth92gd2df7ec2021-05-10 15:16:33 +02001130
toth92g1bbc2fe2021-02-12 16:11:17 +01001131 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
1132 /* Parse subject key identifier */
1133 if ( ( ret = x509_get_subject_key_id( p, end_ext_data,
1134 &crt->subject_key_id ) ) != 0 )
1135 {
1136 return ( ret );
1137 }
1138 break;
toth92gd2df7ec2021-05-10 15:16:33 +02001139
toth92g1bbc2fe2021-02-12 16:11:17 +01001140 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1141 /* Parse authority key identifier */
toth92gd2df7ec2021-05-10 15:16:33 +02001142 if ( ( ret = x509_get_authority_key_id( p, end_ext_octet,
1143 &crt->authority_key_id ) ) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +01001144 {
toth92gdd123902021-04-08 10:12:52 +02001145 return ( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +01001146 }
1147 break;
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001148 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g7e7f6062021-05-11 11:24:54 +02001149 /* Parse subject alt name
toth92gd2df7ec2021-05-10 15:16:33 +02001150 * SubjectAltName ::= GeneralNames
1151 */
1152 if( ( ret = x509_get_general_names( p, end_ext_octet,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001153 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001154 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155 break;
1156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158 /* Parse netscape certificate type */
1159 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1160 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001161 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162 break;
1163
Ron Eldor74d9acc2019-03-21 14:00:03 +02001164 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1165 /* Parse certificate policies type */
1166 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1167 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001168 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001169 /* Give the callback (if any) a chance to handle the extension
1170 * if it contains unsupported policies */
1171 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1172 cb( p_ctx, crt, &extn_oid, is_critical,
1173 start_ext_octet, end_ext_octet ) == 0 )
1174 break;
1175
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001176 if( is_critical )
1177 return( ret );
1178 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001179 /*
Ron Eldora2913912019-05-16 16:17:38 +03001180 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1181 * cannot interpret or enforce the policy. However, it is up to
1182 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001183 * unless the extension is critical.
1184 */
1185 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1186 return( ret );
1187 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001188 break;
1189
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001190 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001191 /*
1192 * If this is a non-critical extension, which the oid layer
1193 * supports, but there isn't an x509 parser for it,
1194 * skip the extension.
1195 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001196 if( is_critical )
1197 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1198 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001199 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001200 }
1201 }
1202
1203 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001204 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1205 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206
1207 return( 0 );
1208}
1209
1210/*
1211 * Parse and fill a single X.509 certificate in DER format
1212 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001213static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1214 const unsigned char *buf,
1215 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001216 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001217 mbedtls_x509_crt_ext_cb_t cb,
1218 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219{
Janos Follath865b3eb2019-12-16 11:46:15 +00001220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001221 size_t len;
1222 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1226 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1227 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001228
1229 /*
1230 * Check for valid input
1231 */
1232 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001234
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001235 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001236 p = (unsigned char*) buf;
1237 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001238 end = p + len;
1239
1240 /*
1241 * Certificate ::= SEQUENCE {
1242 * tbsCertificate TBSCertificate,
1243 * signatureAlgorithm AlgorithmIdentifier,
1244 * signatureValue BIT STRING }
1245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1247 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 mbedtls_x509_crt_free( crt );
1250 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001251 }
1252
Janos Follathcc0e49d2016-02-17 14:34:12 +00001253 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001254 crt->raw.len = crt_end - buf;
1255 if( make_copy != 0 )
1256 {
1257 /* Create and populate a new buffer for the raw field. */
1258 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1259 if( crt->raw.p == NULL )
1260 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1261
1262 memcpy( crt->raw.p, buf, crt->raw.len );
1263 crt->own_buffer = 1;
1264
1265 p += crt->raw.len - len;
1266 end = crt_end = p + len;
1267 }
1268 else
1269 {
1270 crt->raw.p = (unsigned char*) buf;
1271 crt->own_buffer = 0;
1272 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001273
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001274 /*
1275 * TBSCertificate ::= SEQUENCE {
1276 */
1277 crt->tbs.p = p;
1278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1280 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001283 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001284 }
1285
1286 end = p + len;
1287 crt->tbs.len = end - crt->tbs.p;
1288
1289 /*
1290 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1291 *
1292 * CertificateSerialNumber ::= INTEGER
1293 *
1294 * signature AlgorithmIdentifier
1295 */
1296 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1298 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001299 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001302 return( ret );
1303 }
1304
Andres AG7ca4a032017-03-09 16:16:11 +00001305 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 mbedtls_x509_crt_free( crt );
1308 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001309 }
1310
Andres AG7ca4a032017-03-09 16:16:11 +00001311 crt->version++;
1312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001314 &crt->sig_md, &crt->sig_pk,
1315 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001318 return( ret );
1319 }
1320
1321 /*
1322 * issuer Name
1323 */
1324 crt->issuer_raw.p = p;
1325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1327 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001330 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001331 }
1332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001336 return( ret );
1337 }
1338
1339 crt->issuer_raw.len = p - crt->issuer_raw.p;
1340
1341 /*
1342 * Validity ::= SEQUENCE {
1343 * notBefore Time,
1344 * notAfter Time }
1345 *
1346 */
1347 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1348 &crt->valid_to ) ) != 0 )
1349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001351 return( ret );
1352 }
1353
1354 /*
1355 * subject Name
1356 */
1357 crt->subject_raw.p = p;
1358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1360 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001363 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364 }
1365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369 return( ret );
1370 }
1371
1372 crt->subject_raw.len = p - crt->subject_raw.p;
1373
1374 /*
1375 * SubjectPublicKeyInfo
1376 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001377 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001381 return( ret );
1382 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001383 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001384
1385 /*
1386 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1387 * -- If present, version shall be v2 or v3
1388 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1389 * -- If present, version shall be v2 or v3
1390 * extensions [3] EXPLICIT Extensions OPTIONAL
1391 * -- If present, version shall be v3
1392 */
1393 if( crt->version == 2 || crt->version == 3 )
1394 {
1395 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1396 if( ret != 0 )
1397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001399 return( ret );
1400 }
1401 }
1402
1403 if( crt->version == 2 || crt->version == 3 )
1404 {
1405 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1406 if( ret != 0 )
1407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001409 return( ret );
1410 }
1411 }
1412
1413 if( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001414 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001415 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001416 if( ret != 0 )
1417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001419 return( ret );
1420 }
1421 }
1422
1423 if( p != end )
1424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001426 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1427 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001428 }
1429
1430 end = crt_end;
1431
1432 /*
1433 * }
1434 * -- end of TBSCertificate
1435 *
1436 * signatureAlgorithm AlgorithmIdentifier,
1437 * signatureValue BIT STRING
1438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442 return( ret );
1443 }
1444
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001445 if( crt->sig_oid.len != sig_oid2.len ||
1446 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001447 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001448 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001449 ( sig_params1.len != 0 &&
1450 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 mbedtls_x509_crt_free( crt );
1453 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454 }
1455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001459 return( ret );
1460 }
1461
1462 if( p != end )
1463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001465 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1466 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467 }
1468
1469 return( 0 );
1470}
1471
1472/*
1473 * Parse one X.509 certificate in DER format from a buffer and add them to a
1474 * chained list
1475 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001476static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1477 const unsigned char *buf,
1478 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001479 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001480 mbedtls_x509_crt_ext_cb_t cb,
1481 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482{
Janos Follath865b3eb2019-12-16 11:46:15 +00001483 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485
1486 /*
1487 * Check for valid input
1488 */
1489 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491
1492 while( crt->version != 0 && crt->next != NULL )
1493 {
1494 prev = crt;
1495 crt = crt->next;
1496 }
1497
1498 /*
1499 * Add new certificate on the end of the chain if needed.
1500 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001501 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001502 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001503 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504
1505 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001506 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507
1508 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001511 }
1512
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001513 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001514 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515 {
1516 if( prev )
1517 prev->next = NULL;
1518
1519 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001521
1522 return( ret );
1523 }
1524
1525 return( 0 );
1526}
1527
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001528int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1529 const unsigned char *buf,
1530 size_t buflen )
1531{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001532 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001533}
1534
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001535int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1536 const unsigned char *buf,
1537 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001538 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001539 mbedtls_x509_crt_ext_cb_t cb,
1540 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001541{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001542 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001543}
1544
1545int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1546 const unsigned char *buf,
1547 size_t buflen )
1548{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001549 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001550}
1551
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001553 * Parse one or more PEM certificates from a buffer and add them to the chained
1554 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001556int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1557 const unsigned char *buf,
1558 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559{
Janos Follath98e28a72016-05-31 14:03:54 +01001560#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001561 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001563#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001564
1565 /*
1566 * Check for valid input
1567 */
1568 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570
1571 /*
1572 * Determine buffer content. Buffer contains either one DER certificate or
1573 * one or more PEM certificates.
1574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001576 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001577 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001580 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1583 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001584#else
1585 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1586#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588#if defined(MBEDTLS_PEM_PARSE_C)
1589 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001594 /* 1 rather than 0 since the terminating NULL byte is counted in */
1595 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596 {
1597 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001600 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602 "-----BEGIN CERTIFICATE-----",
1603 "-----END CERTIFICATE-----",
1604 buf, NULL, 0, &use_len );
1605
1606 if( ret == 0 )
1607 {
1608 /*
1609 * Was PEM encoded
1610 */
1611 buflen -= use_len;
1612 buf += use_len;
1613 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615 {
1616 return( ret );
1617 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621
1622 /*
1623 * PEM header and footer were found
1624 */
1625 buflen -= use_len;
1626 buf += use_len;
1627
1628 if( first_error == 0 )
1629 first_error = ret;
1630
Paul Bakker5a5fa922014-09-26 14:53:04 +02001631 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001632 continue;
1633 }
1634 else
1635 break;
1636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640
1641 if( ret != 0 )
1642 {
1643 /*
1644 * Quit parsing on a memory error
1645 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001646 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001647 return( ret );
1648
1649 if( first_error == 0 )
1650 first_error = ret;
1651
1652 total_failed++;
1653 continue;
1654 }
1655
1656 success = 1;
1657 }
1658 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659
1660 if( success )
1661 return( total_failed );
1662 else if( first_error )
1663 return( first_error );
1664 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001666#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001667}
1668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670/*
1671 * Load one or more certificates and add them to the chained list
1672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001674{
Janos Follath865b3eb2019-12-16 11:46:15 +00001675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001676 size_t n;
1677 unsigned char *buf;
1678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680 return( ret );
1681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001683
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001684 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001686
1687 return( ret );
1688}
1689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001691{
1692 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001693#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001694 int w_ret;
1695 WCHAR szDir[MAX_PATH];
1696 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001697 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001698 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001699
Paul Bakker9af723c2014-05-01 13:03:14 +02001700 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001701 HANDLE hFind;
1702
1703 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705
Paul Bakker9af723c2014-05-01 13:03:14 +02001706 memset( szDir, 0, sizeof(szDir) );
1707 memset( filename, 0, MAX_PATH );
1708 memcpy( filename, path, len );
1709 filename[len++] = '\\';
1710 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001711 filename[len++] = '*';
1712
Simon B3c6b18d2016-11-03 01:11:37 +00001713 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001714 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001715 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001717
1718 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001719 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721
1722 len = MAX_PATH - len;
1723 do
1724 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001725 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001726
1727 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1728 continue;
1729
Paul Bakker9af723c2014-05-01 13:03:14 +02001730 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001731 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001732 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001733 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001734 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001735 {
1736 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1737 goto cleanup;
1738 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001741 if( w_ret < 0 )
1742 ret++;
1743 else
1744 ret += w_ret;
1745 }
1746 while( FindNextFileW( hFind, &file_data ) != 0 );
1747
Paul Bakker66d5d072014-06-17 16:39:18 +02001748 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001750
Ron Eldor36d90422017-01-09 15:09:16 +02001751cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001752 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001753#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001754 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001755 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001756 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001757 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001758 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001759 DIR *dir = opendir( path );
1760
Paul Bakker66d5d072014-06-17 16:39:18 +02001761 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001763
Ron Eldor63140682017-01-09 19:27:59 +02001764#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001765 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001766 {
1767 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001768 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001769 }
Ron Eldor63140682017-01-09 19:27:59 +02001770#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001771
Paul Elliottfb91a482021-03-05 14:17:51 +00001772 memset( &sb, 0, sizeof( sb ) );
1773
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001774 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001775 {
Andres AGf9113192016-09-02 14:06:04 +01001776 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1777 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001778
Andres AGf9113192016-09-02 14:06:04 +01001779 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001780 {
Andres AGf9113192016-09-02 14:06:04 +01001781 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1782 goto cleanup;
1783 }
1784 else if( stat( entry_name, &sb ) == -1 )
1785 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001786 if( errno == ENOENT )
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001787 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001788 /* Broken symbolic link - ignore this entry.
1789 stat(2) will return this error for either (a) a dangling
1790 symlink or (b) a missing file.
1791 Given that we have just obtained the filename from readdir,
1792 assume that it does exist and therefore treat this as a
1793 dangling symlink. */
1794 continue;
1795 }
1796 else
1797 {
1798 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001799 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1800 goto cleanup;
1801 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001802 }
1803
1804 if( !S_ISREG( sb.st_mode ) )
1805 continue;
1806
1807 // Ignore parse errors
1808 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001810 if( t_ret < 0 )
1811 ret++;
1812 else
1813 ret += t_ret;
1814 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001815
1816cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001817 closedir( dir );
1818
Ron Eldor63140682017-01-09 19:27:59 +02001819#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001820 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001822#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001823
Paul Bakkerbe089b02013-10-14 15:51:50 +02001824#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001825
1826 return( ret );
1827}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001829
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001830/*
1831 * OtherName ::= SEQUENCE {
1832 * type-id OBJECT IDENTIFIER,
1833 * value [0] EXPLICIT ANY DEFINED BY type-id }
1834 *
1835 * HardwareModuleName ::= SEQUENCE {
1836 * hwType OBJECT IDENTIFIER,
1837 * hwSerialNum OCTET STRING }
1838 *
1839 * NOTE: we currently only parse and use otherName of type HwModuleName,
1840 * as defined in RFC 4108.
1841 */
1842static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1843 mbedtls_x509_san_other_name *other_name )
1844{
Janos Follathab23cd12019-05-09 13:53:57 +01001845 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001846 size_t len;
1847 unsigned char *p = subject_alt_name->p;
1848 const unsigned char *end = p + subject_alt_name->len;
1849 mbedtls_x509_buf cur_oid;
1850
1851 if( ( subject_alt_name->tag &
1852 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1853 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1854 {
1855 /*
1856 * The given subject alternative name is not of type "othername".
1857 */
1858 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1859 }
1860
1861 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1862 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001863 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001864
1865 cur_oid.tag = MBEDTLS_ASN1_OID;
1866 cur_oid.p = p;
1867 cur_oid.len = len;
1868
1869 /*
1870 * Only HwModuleName is currently supported.
1871 */
1872 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1873 {
1874 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1875 }
1876
Ron Eldor890819a2019-05-13 19:03:04 +03001877 if( p + len >= end )
1878 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001879 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001880 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1881 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001882 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001883 p += len;
1884 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1885 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001886 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001887
1888 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1889 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001890 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001891
1892 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001893 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001894
1895 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1896 other_name->value.hardware_module_name.oid.p = p;
1897 other_name->value.hardware_module_name.oid.len = len;
1898
Ron Eldor890819a2019-05-13 19:03:04 +03001899 if( p + len >= end )
1900 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001901 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001902 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1903 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001904 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001905 p += len;
1906 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1907 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001908 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001909
1910 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1911 other_name->value.hardware_module_name.val.p = p;
1912 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001913 p += len;
1914 if( p != end )
1915 {
Ron Eldor890819a2019-05-13 19:03:04 +03001916 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001917 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001918 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1919 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001920 }
1921 return( 0 );
1922}
1923
Peter Kolbus9a969b62018-12-11 13:55:56 -06001924int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1925 mbedtls_x509_subject_alternative_name *san )
1926{
1927 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1928 switch( san_buf->tag &
1929 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1930 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1931 {
1932 /*
1933 * otherName
1934 */
1935 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1936 {
1937 mbedtls_x509_san_other_name other_name;
1938
1939 ret = x509_get_other_name( san_buf, &other_name );
1940 if( ret != 0 )
1941 return( ret );
1942
1943 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1944 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1945 memcpy( &san->san.other_name,
1946 &other_name, sizeof( other_name ) );
1947
1948 }
1949 break;
1950
1951 /*
1952 * dNSName
1953 */
1954 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1955 {
1956 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1957 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1958
1959 memcpy( &san->san.unstructured_name,
1960 san_buf, sizeof( *san_buf ) );
1961
1962 }
1963 break;
1964
1965 /*
1966 * Type not supported
1967 */
1968 default:
1969 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1970 }
1971 return( 0 );
1972}
1973
1974#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001975static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001976 const mbedtls_x509_sequence
1977 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001978 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001979{
Janos Follath865b3eb2019-12-16 11:46:15 +00001980 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03001981 size_t i;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001982 size_t n = *size;
1983 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001985 mbedtls_x509_subject_alternative_name san;
1986 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001987
1988 while( cur != NULL )
1989 {
Ron Eldor890819a2019-05-13 19:03:04 +03001990 memset( &san, 0, sizeof( san ) );
Przemek Stekiel23be8f72023-01-03 10:23:13 +01001991 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
Ron Eldor890819a2019-05-13 19:03:04 +03001992 if( parse_ret != 0 )
1993 {
1994 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1995 {
1996 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1997 MBEDTLS_X509_SAFE_SNPRINTF;
1998 }
1999 else
2000 {
2001 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
2002 MBEDTLS_X509_SAFE_SNPRINTF;
2003 }
2004 cur = cur->next;
2005 continue;
2006 }
2007
2008 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002009 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002010 /*
2011 * otherName
2012 */
Ron Eldora2913912019-05-16 16:17:38 +03002013 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03002014 {
2015 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
2016
2017 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
2018 MBEDTLS_X509_SAFE_SNPRINTF;
2019
2020 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
2021 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01002022 {
Ron Eldor890819a2019-05-13 19:03:04 +03002023 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
2024 MBEDTLS_X509_SAFE_SNPRINTF;
2025 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01002026 MBEDTLS_X509_SAFE_SNPRINTF;
2027
Ron Eldor890819a2019-05-13 19:03:04 +03002028 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
2029 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01002030
Ron Eldor890819a2019-05-13 19:03:04 +03002031 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
2032 MBEDTLS_X509_SAFE_SNPRINTF;
2033
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03002034 for( i = 0; i < other_name->value.hardware_module_name.val.len; i++ )
Ron Eldor890819a2019-05-13 19:03:04 +03002035 {
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03002036 ret = mbedtls_snprintf( p, n, "%02X", other_name->value.hardware_module_name.val.p[i] );
2037 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath22f605f2019-05-10 10:37:17 +01002038 }
Ron Eldor890819a2019-05-13 19:03:04 +03002039 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
2040 }
2041 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002042
2043 /*
2044 * dNSName
2045 */
Ron Eldora2913912019-05-16 16:17:38 +03002046 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03002047 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002048 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
2049 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03002050 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002051 {
2052 *p = '\0';
2053 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
2054 }
Ron Eldora2913912019-05-16 16:17:38 +03002055
2056 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
2057 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03002058 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03002059 }
2060 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002061
2062 /*
Ron Eldor890819a2019-05-13 19:03:04 +03002063 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002064 */
2065 default:
Janos Follath22f605f2019-05-10 10:37:17 +01002066 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
2067 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002068 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002069 }
2070
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002071 cur = cur->next;
2072 }
2073
2074 *p = '\0';
2075
2076 *size = n;
2077 *buf = p;
2078
2079 return( 0 );
2080}
2081
Przemek Stekiel23be8f72023-01-03 10:23:13 +01002082int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
toth92gd2df7ec2021-05-10 15:16:33 +02002083 mbedtls_x509_subject_alternative_name *san )
2084{
2085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2086 switch( san_buf->tag &
2087 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
2088 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
2089 {
2090 /*
2091 * otherName
2092 */
2093 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
2094 {
2095 mbedtls_x509_san_other_name other_name;
2096
2097 ret = x509_get_other_name( san_buf, &other_name );
2098 if( ret != 0 )
2099 return( ret );
2100
2101 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
2102 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
2103 memcpy( &san->san.other_name,
2104 &other_name, sizeof( other_name ) );
2105
2106 }
2107 break;
2108
2109 /*
2110 * dNSName
2111 */
2112 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
2113 {
2114 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
2115 san->type = MBEDTLS_X509_SAN_DNS_NAME;
2116
2117 memcpy( &san->san.unstructured_name,
2118 san_buf, sizeof( *san_buf ) );
2119
2120 }
2121 break;
2122
2123 /*
2124 * RFC822 Name
2125 */
2126 case( MBEDTLS_ASN1_SEQUENCE | MBEDTLS_X509_SAN_RFC822_NAME ):
2127 {
2128 mbedtls_x509_name rfc822Name;
2129 unsigned char* bufferPointer = san_buf->p;
2130 unsigned char** p = &bufferPointer;
2131 const unsigned char* end = san_buf->p + san_buf->len;
toth92g7e7f6062021-05-11 11:24:54 +02002132
toth92gd2df7ec2021-05-10 15:16:33 +02002133 /* The leading ASN1 tag and length has been processed. Stepping back with 2 bytes, because mbedtls_x509_get_name expects the beginning of the SET tag */
2134 *p = *p - 2;
2135
2136 ret = mbedtls_x509_get_name( p, end, &rfc822Name );
2137 if ( ret != 0 )
2138 return( ret );
2139
2140 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
2141 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
2142 memcpy( &san->san.unstructured_name,
2143 &rfc822Name, sizeof( rfc822Name ) );
2144 }
2145 break;
2146
2147 /*
2148 * Type not supported
2149 */
2150 default:
2151 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
2152 }
2153 return( 0 );
2154}
2155
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002156#define PRINT_ITEM(i) \
2157 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002159 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002160 sep = ", "; \
2161 }
2162
2163#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01002164 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002165 PRINT_ITEM( name );
2166
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002167static int x509_info_cert_type( char **buf, size_t *size,
2168 unsigned char ns_cert_type )
2169{
Janos Follath865b3eb2019-12-16 11:46:15 +00002170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002171 size_t n = *size;
2172 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002173 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002176 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
2178 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
2179 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002180 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
2181 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
2182 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002183
2184 *size = n;
2185 *buf = p;
2186
2187 return( 0 );
2188}
2189
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002190#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01002191 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002192 PRINT_ITEM( name );
2193
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002194static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002195 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002196{
Janos Follath865b3eb2019-12-16 11:46:15 +00002197 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002198 size_t n = *size;
2199 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002200 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2203 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002204 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2205 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2206 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2208 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002209 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2210 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002211
2212 *size = n;
2213 *buf = p;
2214
2215 return( 0 );
2216}
2217
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002218static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002220{
Janos Follath865b3eb2019-12-16 11:46:15 +00002221 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002222 const char *desc;
2223 size_t n = *size;
2224 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002226 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002227
2228 while( cur != NULL )
2229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002231 desc = "???";
2232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002234 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002235
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002236 sep = ", ";
2237
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002238 cur = cur->next;
2239 }
2240
2241 *size = n;
2242 *buf = p;
2243
2244 return( 0 );
2245}
2246
Ron Eldor74d9acc2019-03-21 14:00:03 +02002247static int x509_info_cert_policies( char **buf, size_t *size,
2248 const mbedtls_x509_sequence *certificate_policies )
2249{
Janos Follath865b3eb2019-12-16 11:46:15 +00002250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002251 const char *desc;
2252 size_t n = *size;
2253 char *p = *buf;
2254 const mbedtls_x509_sequence *cur = certificate_policies;
2255 const char *sep = "";
2256
2257 while( cur != NULL )
2258 {
2259 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2260 desc = "???";
2261
2262 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2263 MBEDTLS_X509_SAFE_SNPRINTF;
2264
2265 sep = ", ";
2266
2267 cur = cur->next;
2268 }
2269
2270 *size = n;
2271 *buf = p;
2272
2273 return( 0 );
2274}
2275
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002276/*
2277 * Return an informational string about the certificate.
2278 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002279#define BEFORE_COLON 18
2280#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2282 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002283{
Janos Follath865b3eb2019-12-16 11:46:15 +00002284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002285 size_t n;
2286 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002287 char key_size_str[BEFORE_COLON];
2288
2289 p = buf;
2290 n = size;
2291
Janos Follath98e28a72016-05-31 14:03:54 +01002292 if( NULL == crt )
2293 {
2294 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2295 MBEDTLS_X509_SAFE_SNPRINTF;
2296
2297 return( (int) ( size - n ) );
2298 }
2299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002301 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002302 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002304 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002305 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002308 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002311 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002313 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002316 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002318 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002321 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2322 crt->valid_from.year, crt->valid_from.mon,
2323 crt->valid_from.day, crt->valid_from.hour,
2324 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002325 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002328 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2329 crt->valid_to.year, crt->valid_to.mon,
2330 crt->valid_to.day, crt->valid_to.hour,
2331 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002332 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002335 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002338 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002339 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002340
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002341 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2343 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002344 {
2345 return( ret );
2346 }
2347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002349 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002350 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002351
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002352 /*
2353 * Optional extensions
2354 */
2355
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002356 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002359 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002360 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002361
2362 if( crt->max_pathlen > 0 )
2363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002365 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002366 }
2367 }
2368
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002369 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002370 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002371 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002372 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002373
2374 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002375 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002376 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002377 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002378 }
2379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002383 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002384
2385 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2386 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002387 }
2388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002392 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002393
2394 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2395 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002396 }
2397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002401 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002402
2403 if( ( ret = x509_info_ext_key_usage( &p, &n,
2404 &crt->ext_key_usage ) ) != 0 )
2405 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002406 }
2407
Ron Eldor74d9acc2019-03-21 14:00:03 +02002408 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2409 {
2410 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2411 MBEDTLS_X509_SAFE_SNPRINTF;
2412
2413 if( ( ret = x509_info_cert_policies( &p, &n,
2414 &crt->certificate_policies ) ) != 0 )
2415 return( ret );
2416 }
2417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002419 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002420
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002421 return( (int) ( size - n ) );
2422}
2423
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002424struct x509_crt_verify_string {
2425 int code;
2426 const char *string;
2427};
2428
Hanno Becker7ac83f92020-10-09 10:48:22 +01002429#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002430static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002431 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002432 { 0, NULL }
2433};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002434#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002435
2436int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002437 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002438{
Janos Follath865b3eb2019-12-16 11:46:15 +00002439 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002440 const struct x509_crt_verify_string *cur;
2441 char *p = buf;
2442 size_t n = size;
2443
2444 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2445 {
2446 if( ( flags & cur->code ) == 0 )
2447 continue;
2448
2449 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002450 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002451 flags ^= cur->code;
2452 }
2453
2454 if( flags != 0 )
2455 {
2456 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2457 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002458 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002459 }
2460
2461 return( (int) ( size - n ) );
2462}
Hanno Becker612a2f12020-10-09 09:19:39 +01002463#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002464
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002465int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2466 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002467{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002468 unsigned int usage_must, usage_may;
2469 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2470 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2471
2472 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2473 return( 0 );
2474
2475 usage_must = usage & ~may_mask;
2476
2477 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2478 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2479
2480 usage_may = usage & may_mask;
2481
2482 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002484
2485 return( 0 );
2486}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002489 const char *usage_oid,
2490 size_t usage_len )
2491{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002493
2494 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002496 return( 0 );
2497
2498 /*
2499 * Look for the requested usage (or wildcard ANY) in our list
2500 */
2501 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002504
2505 if( cur_oid->len == usage_len &&
2506 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2507 {
2508 return( 0 );
2509 }
2510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002512 return( 0 );
2513 }
2514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002516}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002519/*
2520 * Return 1 if the certificate is revoked, or 0 otherwise.
2521 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002522int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002523{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002525
2526 while( cur != NULL && cur->serial.len != 0 )
2527 {
2528 if( crt->serial.len == cur->serial.len &&
2529 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2530 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002531 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002532 }
2533
2534 cur = cur->next;
2535 }
2536
2537 return( 0 );
2538}
2539
2540/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002541 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002542 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002545 mbedtls_x509_crl *crl_list,
2546 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002547{
2548 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002549 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002550#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002551 psa_algorithm_t psa_algorithm;
2552#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002554#endif /* MBEDTLS_USE_PSA_CRYPTO */
2555 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002556
2557 if( ca == NULL )
2558 return( flags );
2559
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002560 while( crl_list != NULL )
2561 {
2562 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002563 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002564 {
2565 crl_list = crl_list->next;
2566 continue;
2567 }
2568
2569 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002570 * Check if the CA is configured to sign CRLs
2571 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002572 if( mbedtls_x509_crt_check_key_usage( ca,
2573 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002576 break;
2577 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002578
2579 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002580 * Check if CRL is correctly signed by the trusted CA
2581 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002582 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2583 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2584
2585 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2586 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2587
pespacek7599a772022-02-07 14:40:55 +01002588#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002589 psa_algorithm = mbedtls_hash_info_psa_from_md( crl_list->sig_md );
pespacekd924e552022-02-28 11:49:54 +01002590 if( psa_hash_compute( psa_algorithm,
2591 crl_list->tbs.p,
2592 crl_list->tbs.len,
2593 hash,
2594 sizeof( hash ),
2595 &hash_length ) != PSA_SUCCESS )
pespaceka7a64692022-02-14 15:18:43 +01002596 {
2597 /* Note: this can't happen except after an internal error */
2598 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2599 break;
2600 }
pespacek7599a772022-02-07 14:40:55 +01002601#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002603 hash_length = mbedtls_md_get_size( md_info );
2604 if( mbedtls_md( md_info,
2605 crl_list->tbs.p,
2606 crl_list->tbs.len,
2607 hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002608 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002609 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002611 break;
2612 }
pespaceka7a64692022-02-14 15:18:43 +01002613#endif /* MBEDTLS_USE_PSA_CRYPTO */
2614
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002615 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002616 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
pespacek7599a772022-02-07 14:40:55 +01002619 crl_list->sig_md, hash, hash_length,
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002620 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002623 break;
2624 }
2625
2626 /*
2627 * Check for validity of CRL (Do not drop out)
2628 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002629 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002631
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002632 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002633 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002634
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002635 /*
2636 * Check if certificate is revoked
2637 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002638 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002641 break;
2642 }
2643
2644 crl_list = crl_list->next;
2645 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002646
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002647 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002648}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002650
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002651/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002652 * Check the signature of a certificate by its parent
2653 */
2654static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002655 mbedtls_x509_crt *parent,
2656 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002657{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002658 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02002659 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002660#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2661 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002662 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002663 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002664
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002665 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002666 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002667 return( -1 );
2668#else
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002669 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( child->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002670 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002671
pespacek7599a772022-02-07 14:40:55 +01002672 status = psa_hash_compute( hash_alg,
2673 child->tbs.p,
2674 child->tbs.len,
2675 hash,
pespacekb9f07a72022-02-14 15:13:26 +01002676 sizeof( hash ),
pespacek7599a772022-02-07 14:40:55 +01002677 &hash_len );
2678 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002679 {
pespacek3110c7b2022-02-14 15:07:41 +01002680 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002681 }
2682
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002683#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002684 /* Skip expensive computation on obvious mismatch */
2685 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002686 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002687
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002688#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002689 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2690 {
2691 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002692 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002693 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002694 }
2695#else
2696 (void) rs_ctx;
2697#endif
2698
2699 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002700 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002701 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002702}
2703
2704/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002705 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2706 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002707 *
2708 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2711 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002712 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002713{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002714 int need_ca_bit;
2715
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002716 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002717 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002718 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002719
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002720 /* Parent must have the basicConstraints CA bit set as a general rule */
2721 need_ca_bit = 1;
2722
2723 /* Exception: v1/v2 certificates that are locally trusted. */
2724 if( top && parent->version < 3 )
2725 need_ca_bit = 0;
2726
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002727 if( need_ca_bit && ! parent->ca_istrue )
2728 return( -1 );
2729
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002730 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002732 {
2733 return( -1 );
2734 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002735
2736 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002737}
2738
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002739/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002740 * Find a suitable parent for child in candidates, or return NULL.
2741 *
2742 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002743 * 1. subject name matches child's issuer
2744 * 2. if necessary, the CA bit is set and key usage allows signing certs
2745 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002746 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002747 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002748 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002749 * If there's a suitable candidate which is also time-valid, return the first
2750 * such. Otherwise, return the first suitable candidate (or NULL if there is
2751 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002752 *
2753 * The rationale for this rule is that someone could have a list of trusted
2754 * roots with two versions on the same root with different validity periods.
2755 * (At least one user reported having such a list and wanted it to just work.)
2756 * The reason we don't just require time-validity is that generally there is
2757 * only one version, and if it's expired we want the flags to state that
2758 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002759 *
2760 * The rationale for rule 3 (signature for trusted roots) is that users might
2761 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002762 * way we select the correct one is by checking the signature (as we don't
2763 * rely on key identifier extensions). (This is one way users might choose to
2764 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002765 *
2766 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002767 * - [in] child: certificate for which we're looking for a parent
2768 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002769 * - [out] r_parent: parent found (or NULL)
2770 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002771 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2772 * of the chain, 0 otherwise
2773 * - [in] path_cnt: number of intermediates seen so far
2774 * - [in] self_cnt: number of self-signed intermediates seen so far
2775 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002776 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002777 *
2778 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002779 * - 0 on success
2780 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002781 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002782static int x509_crt_find_parent_in(
2783 mbedtls_x509_crt *child,
2784 mbedtls_x509_crt *candidates,
2785 mbedtls_x509_crt **r_parent,
2786 int *r_signature_is_good,
2787 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002788 unsigned path_cnt,
2789 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002790 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002791{
Janos Follath865b3eb2019-12-16 11:46:15 +00002792 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002793 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002794 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002795
2796#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002797 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002798 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2799 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002800 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002801 parent = rs_ctx->parent;
2802 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002803 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002804
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002805 /* clear saved state */
2806 rs_ctx->parent = NULL;
2807 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002808 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002809
2810 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002811 goto check_signature;
2812 }
2813#endif
2814
2815 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002816 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002817
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002818 for( parent = candidates; parent != NULL; parent = parent->next )
2819 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002820 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002821 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002822 continue;
2823
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002824 /* +1 because stored max_pathlen is 1 higher that the actual value */
2825 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002826 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002827 {
2828 continue;
2829 }
2830
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002831 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002832#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2833check_signature:
2834#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002835 ret = x509_crt_check_signature( child, parent, rs_ctx );
2836
2837#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002838 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2839 {
2840 /* save state */
2841 rs_ctx->parent = parent;
2842 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002843 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002844
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002845 return( ret );
2846 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002847#else
2848 (void) ret;
2849#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002850
2851 signature_is_good = ret == 0;
2852 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002853 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002854
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002855 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002856 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2857 mbedtls_x509_time_is_future( &parent->valid_from ) )
2858 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002859 if( fallback_parent == NULL )
2860 {
2861 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002862 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002863 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002864
2865 continue;
2866 }
2867
Andy Gross1f627142019-01-30 10:25:53 -06002868 *r_parent = parent;
2869 *r_signature_is_good = signature_is_good;
2870
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002871 break;
2872 }
2873
Andy Gross1f627142019-01-30 10:25:53 -06002874 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002875 {
2876 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002877 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002878 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002879
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002880 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002881}
2882
2883/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002884 * Find a parent in trusted CAs or the provided chain, or return NULL.
2885 *
2886 * Searches in trusted CAs first, and return the first suitable parent found
2887 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002888 *
2889 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002890 * - [in] child: certificate for which we're looking for a parent, followed
2891 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002892 * - [in] trust_ca: list of locally trusted certificates
2893 * - [out] parent: parent found (or NULL)
2894 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2895 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2896 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2897 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002898 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002899 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002900 *
2901 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002902 * - 0 on success
2903 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002904 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002905static int x509_crt_find_parent(
2906 mbedtls_x509_crt *child,
2907 mbedtls_x509_crt *trust_ca,
2908 mbedtls_x509_crt **parent,
2909 int *parent_is_trusted,
2910 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002911 unsigned path_cnt,
2912 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002913 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002914{
Janos Follath865b3eb2019-12-16 11:46:15 +00002915 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002916 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002917
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002918 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002919
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002920#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002921 /* restore then clear saved state if we have some stored */
2922 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2923 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002924 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002925 rs_ctx->parent_is_trusted = -1;
2926 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002927#endif
2928
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002929 while( 1 ) {
2930 search_list = *parent_is_trusted ? trust_ca : child->next;
2931
2932 ret = x509_crt_find_parent_in( child, search_list,
2933 parent, signature_is_good,
2934 *parent_is_trusted,
2935 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002936
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002937#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002938 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2939 {
2940 /* save state */
2941 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002942 return( ret );
2943 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002944#else
2945 (void) ret;
2946#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002947
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002948 /* stop here if found or already in second iteration */
2949 if( *parent != NULL || *parent_is_trusted == 0 )
2950 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002951
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002952 /* prepare second iteration */
2953 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002954 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002955
2956 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002957 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002958 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002959 *parent_is_trusted = 0;
2960 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002961 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002962
2963 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002964}
2965
2966/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002967 * Check if an end-entity certificate is locally trusted
2968 *
2969 * Currently we require such certificates to be self-signed (actually only
2970 * check for self-issued as self-signatures are not checked)
2971 */
2972static int x509_crt_check_ee_locally_trusted(
2973 mbedtls_x509_crt *crt,
2974 mbedtls_x509_crt *trust_ca )
2975{
2976 mbedtls_x509_crt *cur;
2977
2978 /* must be self-issued */
2979 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2980 return( -1 );
2981
2982 /* look for an exact match with trusted cert */
2983 for( cur = trust_ca; cur != NULL; cur = cur->next )
2984 {
2985 if( crt->raw.len == cur->raw.len &&
2986 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2987 {
2988 return( 0 );
2989 }
2990 }
2991
2992 /* too bad */
2993 return( -1 );
2994}
2995
2996/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002997 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002998 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002999 * Given a peer-provided list of certificates EE, C1, ..., Cn and
3000 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003001 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003002 * such that every cert in the chain is a child of the next one,
3003 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003004 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003005 * Verify that chain and return it with flags for all issues found.
3006 *
3007 * Special cases:
3008 * - EE == Rj -> return a one-element list containing it
3009 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
3010 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003011 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003012 * Tests for (aspects of) this function should include at least:
3013 * - trusted EE
3014 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01003015 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003016 * - if relevant: EE untrusted
3017 * - if relevant: EE -> intermediate, untrusted
3018 * with the aspect under test checked at each relevant level (EE, int, root).
3019 * For some aspects longer chains are required, but usually length 2 is
3020 * enough (but length 1 is not in general).
3021 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003022 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003023 * - [in] crt: the cert list EE, C1, ..., Cn
3024 * - [in] trust_ca: the trusted list R1, ..., Rp
3025 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003026 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003027 * Only valid when return value is 0, may contain garbage otherwise!
3028 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003029 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003030 *
3031 * Return value:
3032 * - non-zero if the chain could not be fully built and examined
3033 * - 0 is the chain was successfully built and examined,
3034 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003035 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003036static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003037 mbedtls_x509_crt *crt,
3038 mbedtls_x509_crt *trust_ca,
3039 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003040 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3041 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003042 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003043 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003044 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003045{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003046 /* Don't initialize any of those variables here, so that the compiler can
3047 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00003048 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003049 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003050 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003051 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02003052 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003053 int parent_is_trusted;
3054 int child_is_trusted;
3055 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003056 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00003057 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003058
3059#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3060 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003061 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003062 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003063 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003064 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003065 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003066
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003067 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003068 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003069 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003070 flags = &cur->flags;
3071
3072 goto find_parent;
3073 }
3074#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003075
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003076 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003077 self_cnt = 0;
3078 parent_is_trusted = 0;
3079 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003080
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003081 while( 1 ) {
3082 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003083 cur = &ver_chain->items[ver_chain->len];
3084 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003085 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003086 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003087 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003088
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003089 /* Check time-validity (all certificates) */
3090 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3091 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003092
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003093 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3094 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02003095
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003096 /* Stop here for trusted roots (but not for trusted EE certs) */
3097 if( child_is_trusted )
3098 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003099
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003100 /* Check signature algorithm: MD & PK algs */
3101 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3102 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003103
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003104 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3105 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003106
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003107 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003108 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003109 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3110 {
3111 return( 0 );
3112 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003113
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003114#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3115find_parent:
3116#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00003117
3118 /* Obtain list of potential trusted signers from CA callback,
3119 * or use statically provided list. */
3120#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3121 if( f_ca_cb != NULL )
3122 {
3123 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
3124 mbedtls_free( ver_chain->trust_ca_cb_result );
3125 ver_chain->trust_ca_cb_result = NULL;
3126
3127 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
3128 if( ret != 0 )
3129 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3130
3131 cur_trust_ca = ver_chain->trust_ca_cb_result;
3132 }
3133 else
3134#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3135 {
3136 ((void) f_ca_cb);
3137 ((void) p_ca_cb);
3138 cur_trust_ca = trust_ca;
3139 }
3140
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003141 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00003142 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003143 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003144 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003145
3146#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003147 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3148 {
3149 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003150 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003151 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003152 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003153
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003154 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003155 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003156#else
3157 (void) ret;
3158#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003159
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003160 /* No parent? We're done here */
3161 if( parent == NULL )
3162 {
3163 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3164 return( 0 );
3165 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003166
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003167 /* Count intermediate self-issued (not necessarily self-signed) certs.
3168 * These can occur with some strategies for key rollover, see [SIRO],
3169 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003170 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02003171 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
3172 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003173 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02003174 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003175
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003176 /* path_cnt is 0 for the first intermediate CA,
3177 * and if parent is trusted it's not an intermediate CA */
3178 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003179 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003180 {
3181 /* return immediately to avoid overflow the chain array */
3182 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3183 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003184
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003185 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003186 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003187 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3188
3189 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003190 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003191 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003194 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003195 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003196#else
3197 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003198#endif
3199
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003200 /* prepare for next iteration */
3201 child = parent;
3202 parent = NULL;
3203 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003204 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003205 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003206}
3207
3208/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003209 * Check for CN match
3210 */
3211static int x509_crt_check_cn( const mbedtls_x509_buf *name,
3212 const char *cn, size_t cn_len )
3213{
3214 /* try exact match */
3215 if( name->len == cn_len &&
3216 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3217 {
3218 return( 0 );
3219 }
3220
3221 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003222 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003223 {
3224 return( 0 );
3225 }
3226
3227 return( -1 );
3228}
3229
3230/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003231 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3232 */
3233static int x509_crt_check_san( const mbedtls_x509_buf *name,
3234 const char *cn, size_t cn_len )
3235{
3236 const unsigned char san_type = (unsigned char) name->tag &
3237 MBEDTLS_ASN1_TAG_VALUE_MASK;
3238
3239 /* dNSName */
3240 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3241 return( x509_crt_check_cn( name, cn, cn_len ) );
3242
3243 /* (We may handle other types here later.) */
3244
3245 /* Unrecognized type */
3246 return( -1 );
3247}
3248
3249/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003250 * Verify the requested CN - only call this if cn is not NULL!
3251 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003252static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003253 const char *cn,
3254 uint32_t *flags )
3255{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003256 const mbedtls_x509_name *name;
3257 const mbedtls_x509_sequence *cur;
3258 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003259
3260 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3261 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003262 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003263 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003264 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003265 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003266 }
3267
3268 if( cur == NULL )
3269 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3270 }
3271 else
3272 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003273 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003274 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003275 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3276 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003277 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003278 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003279 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003280 }
3281
3282 if( name == NULL )
3283 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3284 }
3285}
3286
3287/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003288 * Merge the flags for all certs in the chain, after calling callback
3289 */
3290static int x509_crt_merge_flags_with_cb(
3291 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003292 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003293 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3294 void *p_vrfy )
3295{
Janos Follath865b3eb2019-12-16 11:46:15 +00003296 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003297 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003298 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003299 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003300
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003301 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003302 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003303 cur = &ver_chain->items[i-1];
3304 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003305
3306 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003307 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003308 return( ret );
3309
3310 *flags |= cur_flags;
3311 }
3312
3313 return( 0 );
3314}
3315
3316/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003317 * Verify the certificate validity, with profile, restartable version
3318 *
3319 * This function:
3320 * - checks the requested CN (if any)
3321 * - checks the type and size of the EE cert's key,
3322 * as that isn't done as part of chain building/verification currently
3323 * - builds and verifies the chain
3324 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003325 *
3326 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3327 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3328 * verification routine to search for trusted signers, and CRLs will
3329 * be disabled. Otherwise, `trust_ca` will be used as the static list
3330 * of trusted signers, and `ca_crl` will be use as the static list
3331 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003332 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003333static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003334 mbedtls_x509_crt *trust_ca,
3335 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003336 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3337 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003338 const mbedtls_x509_crt_profile *profile,
3339 const char *cn, uint32_t *flags,
3340 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3341 void *p_vrfy,
3342 mbedtls_x509_crt_restart_ctx *rs_ctx )
3343{
Janos Follath865b3eb2019-12-16 11:46:15 +00003344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003345 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003346 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003347 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003348
3349 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003350 ee_flags = 0;
3351 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003352
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003353 if( profile == NULL )
3354 {
3355 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3356 goto exit;
3357 }
3358
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003359 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003360 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003361 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003362
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003363 /* Check the type and size of the key */
3364 pk_type = mbedtls_pk_get_type( &crt->pk );
3365
3366 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003367 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003368
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003369 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003370 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003371
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003372 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003373 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3374 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003375 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003376
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003377 if( ret != 0 )
3378 goto exit;
3379
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003380 /* Merge end-entity flags */
3381 ver_chain.items[0].flags |= ee_flags;
3382
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003383 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003384 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003385
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003386exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003387
3388#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3389 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3390 mbedtls_free( ver_chain.trust_ca_cb_result );
3391 ver_chain.trust_ca_cb_result = NULL;
3392#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3393
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003394#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3395 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3396 mbedtls_x509_crt_restart_free( rs_ctx );
3397#endif
3398
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003399 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3400 * the SSL module for authmode optional, but non-zero return from the
3401 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003402 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3403 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3404
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003405 if( ret != 0 )
3406 {
3407 *flags = (uint32_t) -1;
3408 return( ret );
3409 }
3410
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003411 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003413
3414 return( 0 );
3415}
3416
Hanno Becker3116fb32019-03-28 13:34:42 +00003417
3418/*
3419 * Verify the certificate validity (default profile, not restartable)
3420 */
3421int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3422 mbedtls_x509_crt *trust_ca,
3423 mbedtls_x509_crl *ca_crl,
3424 const char *cn, uint32_t *flags,
3425 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3426 void *p_vrfy )
3427{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003428 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003429 NULL, NULL,
3430 &mbedtls_x509_crt_profile_default,
3431 cn, flags,
3432 f_vrfy, p_vrfy, NULL ) );
3433}
3434
3435/*
3436 * Verify the certificate validity (user-chosen profile, not restartable)
3437 */
3438int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3439 mbedtls_x509_crt *trust_ca,
3440 mbedtls_x509_crl *ca_crl,
3441 const mbedtls_x509_crt_profile *profile,
3442 const char *cn, uint32_t *flags,
3443 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3444 void *p_vrfy )
3445{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003446 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003447 NULL, NULL,
3448 profile, cn, flags,
3449 f_vrfy, p_vrfy, NULL ) );
3450}
3451
3452#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3453/*
3454 * Verify the certificate validity (user-chosen profile, CA callback,
3455 * not restartable).
3456 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003457int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003458 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3459 void *p_ca_cb,
3460 const mbedtls_x509_crt_profile *profile,
3461 const char *cn, uint32_t *flags,
3462 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3463 void *p_vrfy )
3464{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003465 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003466 f_ca_cb, p_ca_cb,
3467 profile, cn, flags,
3468 f_vrfy, p_vrfy, NULL ) );
3469}
3470#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3471
3472int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3473 mbedtls_x509_crt *trust_ca,
3474 mbedtls_x509_crl *ca_crl,
3475 const mbedtls_x509_crt_profile *profile,
3476 const char *cn, uint32_t *flags,
3477 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3478 void *p_vrfy,
3479 mbedtls_x509_crt_restart_ctx *rs_ctx )
3480{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003481 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003482 NULL, NULL,
3483 profile, cn, flags,
3484 f_vrfy, p_vrfy, rs_ctx ) );
3485}
3486
3487
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003488/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003489 * Initialize a certificate chain
3490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003492{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003494}
3495
3496/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003497 * Unallocate all certificate data
3498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003500{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 mbedtls_x509_crt *cert_cur = crt;
3502 mbedtls_x509_crt *cert_prv;
Przemek Stekiel7cc28052023-01-03 09:37:47 +01003503 mbedtls_x509_sequence *seq_cur;
3504 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003505
Glenn Straussa4b40412022-06-26 19:32:09 -04003506 while( cert_cur != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3511 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003512#endif
3513
Glenn Straussa4b40412022-06-26 19:32:09 -04003514 mbedtls_asn1_free_named_data_list_shallow( cert_cur->issuer.next );
3515 mbedtls_asn1_free_named_data_list_shallow( cert_cur->subject.next );
3516 mbedtls_asn1_sequence_free( cert_cur->ext_key_usage.next );
3517 mbedtls_asn1_sequence_free( cert_cur->subject_alt_names.next );
3518 mbedtls_asn1_sequence_free( cert_cur->certificate_policies.next );
Ron Eldor74d9acc2019-03-21 14:00:03 +02003519
toth92gd2df7ec2021-05-10 15:16:33 +02003520 seq_cur = cert_cur->authority_key_id.authorityCertIssuer.next;
3521 while ( seq_cur != NULL )
toth92g1bbc2fe2021-02-12 16:11:17 +01003522 {
toth92gd2df7ec2021-05-10 15:16:33 +02003523 seq_prv = seq_cur;
3524 seq_cur = seq_cur->next;
3525 mbedtls_platform_zeroize( seq_prv,
3526 sizeof( mbedtls_x509_sequence ) );
3527 mbedtls_free( seq_prv );
toth92g1bbc2fe2021-02-12 16:11:17 +01003528 }
3529
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003530 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003531 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003532 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003534 }
3535
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003536 cert_prv = cert_cur;
3537 cert_cur = cert_cur->next;
3538
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003539 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003540 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003542 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003543}
3544
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003545#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3546/*
3547 * Initialize a restart context
3548 */
3549void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3550{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003551 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003552
3553 ctx->parent = NULL;
3554 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003555 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003556
3557 ctx->parent_is_trusted = -1;
3558
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003559 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003560 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003561 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003562}
3563
3564/*
3565 * Free the components of a restart context
3566 */
3567void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3568{
3569 if( ctx == NULL )
3570 return;
3571
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003572 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003573 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003574}
3575#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577#endif /* MBEDTLS_X509_CRT_PARSE_C */