blob: c3e5f37784dd01ca6f6421b69366f31d53c2c7f8 [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/*
648 * AuthorityKeyIdentifier ::= SEQUENCE {
649 * keyIdentifier [0] KeyIdentifier OPTIONAL,
650 * authorityCertIssuer [1] GeneralNames OPTIONAL,
651 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
652 *
653 * KeyIdentifier ::= OCTET STRING
654 */
toth92gdd123902021-04-08 10:12:52 +0200655static int x509_get_authority_key_id( unsigned char** p,
toth92g1bbc2fe2021-02-12 16:11:17 +0100656 unsigned char* end,
toth92gdd123902021-04-08 10:12:52 +0200657 mbedtls_x509_authority* authority_key_id )
toth92g1bbc2fe2021-02-12 16:11:17 +0100658{
659 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
660 size_t len = 0u;
661
toth92g98fa4c92021-04-27 15:41:25 +0200662 if ( ( ret = mbedtls_asn1_get_tag( p, end, &len,
663 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100664 {
toth92gdd123902021-04-08 10:12:52 +0200665 return( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +0100666 }
667
toth92g98fa4c92021-04-27 15:41:25 +0200668 if ( (ret = mbedtls_asn1_get_tag( p, end, &len,
669 MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100670 {
671 /* KeyIdentifier is an OPTIONAL field */
672 }
673 else
674 {
675 authority_key_id->keyIdentifier.len = len;
676 authority_key_id->keyIdentifier.p = *p;
677 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
678
679 *p += len;
680 }
681
682 if ( *p < end )
683 {
toth92g98fa4c92021-04-27 15:41:25 +0200684 if ( ( ret = mbedtls_asn1_get_tag( p, end, &len,
685 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_BOOLEAN ) ) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100686 {
687 /* authorityCertIssuer is an OPTIONAL field */
688 }
689 else
690 {
toth92g98fa4c92021-04-27 15:41:25 +0200691 if ( ( ret = mbedtls_asn1_get_tag( p, end, &len,
692 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100693 {
toth92gdd123902021-04-08 10:12:52 +0200694 return( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +0100695 }
696 else
697 {
698 authority_key_id->raw.p = *p;
699
toth92g98fa4c92021-04-27 15:41:25 +0200700 if ( ( ret = mbedtls_asn1_get_tag( p, end, &len,
701 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100702 {
toth92gdd123902021-04-08 10:12:52 +0200703 return( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +0100704 }
705
toth92g98fa4c92021-04-27 15:41:25 +0200706 if ( ( ret = mbedtls_x509_get_name( p, *p + len, &authority_key_id->authorityCertIssuer ) ) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100707 {
toth92gdd123902021-04-08 10:12:52 +0200708 return( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +0100709 }
710
711 authority_key_id->raw.len = *p - authority_key_id->raw.p;
712 }
713 }
714 }
715
716 if ( *p < end )
717 {
toth92g98fa4c92021-04-27 15:41:25 +0200718 if ( ( ret = mbedtls_asn1_get_tag( p, end, &len,
719 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_INTEGER ) ) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100720 {
721 /* authorityCertSerialNumber is an OPTIONAL field, but if there are still data it must be the serial number */
toth92gdd123902021-04-08 10:12:52 +0200722 return( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +0100723 }
724 else
725 {
726 authority_key_id->authorityCertSerialNumber.len = len;
727 authority_key_id->authorityCertSerialNumber.p = *p;
728 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_OCTET_STRING;
729 *p += len;
730 }
731 }
732
toth92gdd123902021-04-08 10:12:52 +0200733 if ( *p != end )
toth92g1bbc2fe2021-02-12 16:11:17 +0100734 {
toth92gdd123902021-04-08 10:12:52 +0200735 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
736 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
toth92g1bbc2fe2021-02-12 16:11:17 +0100737 }
738
toth92gdd123902021-04-08 10:12:52 +0200739 return( 0 );
toth92g1bbc2fe2021-02-12 16:11:17 +0100740}
741
742/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200743 * SubjectAltName ::= GeneralNames
744 *
745 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
746 *
747 * GeneralName ::= CHOICE {
748 * otherName [0] OtherName,
749 * rfc822Name [1] IA5String,
750 * dNSName [2] IA5String,
751 * x400Address [3] ORAddress,
752 * directoryName [4] Name,
753 * ediPartyName [5] EDIPartyName,
754 * uniformResourceIdentifier [6] IA5String,
755 * iPAddress [7] OCTET STRING,
756 * registeredID [8] OBJECT IDENTIFIER }
757 *
758 * OtherName ::= SEQUENCE {
759 * type-id OBJECT IDENTIFIER,
760 * value [0] EXPLICIT ANY DEFINED BY type-id }
761 *
762 * EDIPartyName ::= SEQUENCE {
763 * nameAssigner [0] DirectoryString OPTIONAL,
764 * partyName [1] DirectoryString }
765 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300766 * NOTE: we list all types, but only use dNSName and otherName
767 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 */
769static int x509_get_subject_alt_name( unsigned char **p,
770 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772{
Janos Follath865b3eb2019-12-16 11:46:15 +0000773 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200776 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200778
779 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
781 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100782 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200783
784 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100785 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
786 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200787
788 while( *p < end )
789 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300790 mbedtls_x509_subject_alternative_name dummy_san_buf;
791 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
792
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793 tag = **p;
794 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100796 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100798 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
799 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
800 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100801 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
802 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100803 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804
Ron Eldordbbd9662019-05-15 15:46:03 +0300805 /*
irwir74aee1c2019-09-21 18:21:48 +0300806 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300807 */
808 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
809 /*
810 * In case the extension is malformed, return an error,
811 * and clear the allocated sequences.
812 */
813 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
814 {
Glenn Straussa4b40412022-06-26 19:32:09 -0400815 mbedtls_asn1_sequence_free( subject_alt_name->next );
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300816 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300817 return( ret );
818 }
819
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200821 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200822 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100823 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100825
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200826 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827
828 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100829 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
830 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200831
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832 cur = cur->next;
833 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200834
835 buf = &(cur->buf);
836 buf->tag = tag;
837 buf->p = *p;
838 buf->len = tag_len;
839 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200840 }
841
842 /* Set final sequence entry's next pointer to NULL */
843 cur->next = NULL;
844
845 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100846 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
847 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200848
849 return( 0 );
850}
851
852/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200853 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
854 *
855 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
856 *
857 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
858 *
859 * PolicyInformation ::= SEQUENCE {
860 * policyIdentifier CertPolicyId,
861 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
862 * PolicyQualifierInfo OPTIONAL }
863 *
864 * CertPolicyId ::= OBJECT IDENTIFIER
865 *
866 * PolicyQualifierInfo ::= SEQUENCE {
867 * policyQualifierId PolicyQualifierId,
868 * qualifier ANY DEFINED BY policyQualifierId }
869 *
870 * -- policyQualifierIds for Internet policy qualifiers
871 *
872 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
873 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
874 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
875 *
876 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
877 *
878 * Qualifier ::= CHOICE {
879 * cPSuri CPSuri,
880 * userNotice UserNotice }
881 *
882 * CPSuri ::= IA5String
883 *
884 * UserNotice ::= SEQUENCE {
885 * noticeRef NoticeReference OPTIONAL,
886 * explicitText DisplayText OPTIONAL }
887 *
888 * NoticeReference ::= SEQUENCE {
889 * organization DisplayText,
890 * noticeNumbers SEQUENCE OF INTEGER }
891 *
892 * DisplayText ::= CHOICE {
893 * ia5String IA5String (SIZE (1..200)),
894 * visibleString VisibleString (SIZE (1..200)),
895 * bmpString BMPString (SIZE (1..200)),
896 * utf8String UTF8String (SIZE (1..200)) }
897 *
898 * NOTE: we only parse and use anyPolicy without qualifiers at this point
899 * as defined in RFC 5280.
900 */
901static int x509_get_certificate_policies( unsigned char **p,
902 const unsigned char *end,
903 mbedtls_x509_sequence *certificate_policies )
904{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300905 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200906 size_t len;
907 mbedtls_asn1_buf *buf;
908 mbedtls_asn1_sequence *cur = certificate_policies;
909
910 /* Get main sequence tag */
911 ret = mbedtls_asn1_get_tag( p, end, &len,
912 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
913 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100914 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200915
916 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100917 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
918 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200919
920 /*
921 * Cannot be an empty sequence.
922 */
923 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100924 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
925 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200926
927 while( *p < end )
928 {
929 mbedtls_x509_buf policy_oid;
930 const unsigned char *policy_end;
931
932 /*
933 * Get the policy sequence
934 */
935 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
936 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100937 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200938
939 policy_end = *p + len;
940
Ron Eldor08063792019-05-13 16:38:39 +0300941 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200942 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100943 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200944
945 policy_oid.tag = MBEDTLS_ASN1_OID;
946 policy_oid.len = len;
947 policy_oid.p = *p;
948
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300949 /*
950 * Only AnyPolicy is currently supported when enforcing policy.
951 */
952 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
953 {
954 /*
955 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200956 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300957 */
958 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
959 }
960
Ron Eldor74d9acc2019-03-21 14:00:03 +0200961 /* Allocate and assign next pointer */
962 if( cur->buf.p != NULL )
963 {
964 if( cur->next != NULL )
965 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
966
967 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
968
969 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100970 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
971 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200972
973 cur = cur->next;
974 }
975
976 buf = &( cur->buf );
977 buf->tag = policy_oid.tag;
978 buf->p = policy_oid.p;
979 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300980
981 *p += len;
982
983 /*
984 * If there is an optional qualifier, then *p < policy_end
985 * Check the Qualifier len to verify it doesn't exceed policy_end.
986 */
987 if( *p < policy_end )
988 {
989 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
990 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100991 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300992 /*
993 * Skip the optional policy qualifiers.
994 */
995 *p += len;
996 }
997
998 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100999 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1000 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +02001001 }
1002
1003 /* Set final sequence entry's next pointer to NULL */
1004 cur->next = NULL;
1005
1006 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001007 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1008 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +02001009
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001010 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +02001011}
1012
1013/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014 * X.509 v3 extensions
1015 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 */
1017static int x509_get_crt_ext( unsigned char **p,
1018 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001019 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001020 mbedtls_x509_crt_ext_cb_t cb,
1021 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001022{
Janos Follath865b3eb2019-12-16 11:46:15 +00001023 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001025 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026
Hanno Becker12f62fb2019-02-12 17:22:36 +00001027 if( *p == end )
1028 return( 0 );
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032
Hanno Becker12f62fb2019-02-12 17:22:36 +00001033 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 while( *p < end )
1035 {
1036 /*
1037 * Extension ::= SEQUENCE {
1038 * extnID OBJECT IDENTIFIER,
1039 * critical BOOLEAN DEFAULT FALSE,
1040 * extnValue OCTET STRING }
1041 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 int is_critical = 0; /* DEFAULT FALSE */
1044 int ext_type = 0;
1045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
1047 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001048 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001049
1050 end_ext_data = *p + len;
1051
1052 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +02001053 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +02001054 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001055 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056
k-stachowiak463928a2018-07-24 12:50:59 +02001057 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001058 extn_oid.p = *p;
1059 *p += extn_oid.len;
1060
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001061 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
1063 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
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
1066 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
1068 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001069 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001071 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072 end_ext_octet = *p + len;
1073
1074 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +01001075 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1076 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077
1078 /*
1079 * Detect supported extensions
1080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001082
1083 if( ret != 0 )
1084 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001085 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +02001086 if( cb != NULL )
1087 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001088 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +02001089 if( ret != 0 && is_critical )
1090 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001091 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001092 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001093 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001094
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095 /* No parser found, skip extension */
1096 *p = end_ext_octet;
1097
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 if( is_critical )
1099 {
1100 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +01001101 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1102 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104 continue;
1105 }
1106
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001107 /* Forbid repeated extensions */
1108 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001110
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111 crt->ext_types |= ext_type;
1112
1113 switch( ext_type )
1114 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001115 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001116 /* Parse basic constraints */
1117 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1118 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001119 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120 break;
1121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123 /* Parse key usage */
1124 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1125 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001126 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127 break;
1128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001130 /* Parse extended key usage */
1131 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1132 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001133 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001134 break;
toth92g1bbc2fe2021-02-12 16:11:17 +01001135 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
1136 /* Parse subject key identifier */
1137 if ( ( ret = x509_get_subject_key_id( p, end_ext_data,
1138 &crt->subject_key_id ) ) != 0 )
1139 {
1140 return ( ret );
1141 }
1142 break;
1143 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1144 /* Parse authority key identifier */
1145 if ( (ret = x509_get_authority_key_id(p, end_ext_octet,
1146 &crt->authority_key_id)) != 0 )
1147 {
toth92gdd123902021-04-08 10:12:52 +02001148 return ( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +01001149 }
1150 break;
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001151 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152 /* Parse subject alt name */
1153 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1154 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001155 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 break;
1157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001159 /* Parse netscape certificate type */
1160 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1161 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001162 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001163 break;
1164
Ron Eldor74d9acc2019-03-21 14:00:03 +02001165 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1166 /* Parse certificate policies type */
1167 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1168 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001169 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001170 /* Give the callback (if any) a chance to handle the extension
1171 * if it contains unsupported policies */
1172 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1173 cb( p_ctx, crt, &extn_oid, is_critical,
1174 start_ext_octet, end_ext_octet ) == 0 )
1175 break;
1176
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001177 if( is_critical )
1178 return( ret );
1179 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001180 /*
Ron Eldora2913912019-05-16 16:17:38 +03001181 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1182 * cannot interpret or enforce the policy. However, it is up to
1183 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001184 * unless the extension is critical.
1185 */
1186 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1187 return( ret );
1188 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001189 break;
1190
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001192 /*
1193 * If this is a non-critical extension, which the oid layer
1194 * supports, but there isn't an x509 parser for it,
1195 * skip the extension.
1196 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001197 if( is_critical )
1198 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1199 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001200 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001201 }
1202 }
1203
1204 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001205 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1206 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001207
1208 return( 0 );
1209}
1210
1211/*
1212 * Parse and fill a single X.509 certificate in DER format
1213 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001214static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1215 const unsigned char *buf,
1216 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001217 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001218 mbedtls_x509_crt_ext_cb_t cb,
1219 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001220{
Janos Follath865b3eb2019-12-16 11:46:15 +00001221 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001222 size_t len;
1223 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1227 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1228 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229
1230 /*
1231 * Check for valid input
1232 */
1233 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001236 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001237 p = (unsigned char*) buf;
1238 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001239 end = p + len;
1240
1241 /*
1242 * Certificate ::= SEQUENCE {
1243 * tbsCertificate TBSCertificate,
1244 * signatureAlgorithm AlgorithmIdentifier,
1245 * signatureValue BIT STRING }
1246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1248 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 mbedtls_x509_crt_free( crt );
1251 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001252 }
1253
Janos Follathcc0e49d2016-02-17 14:34:12 +00001254 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001255 crt->raw.len = crt_end - buf;
1256 if( make_copy != 0 )
1257 {
1258 /* Create and populate a new buffer for the raw field. */
1259 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1260 if( crt->raw.p == NULL )
1261 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1262
1263 memcpy( crt->raw.p, buf, crt->raw.len );
1264 crt->own_buffer = 1;
1265
1266 p += crt->raw.len - len;
1267 end = crt_end = p + len;
1268 }
1269 else
1270 {
1271 crt->raw.p = (unsigned char*) buf;
1272 crt->own_buffer = 0;
1273 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001274
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001275 /*
1276 * TBSCertificate ::= SEQUENCE {
1277 */
1278 crt->tbs.p = p;
1279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1281 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001284 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001285 }
1286
1287 end = p + len;
1288 crt->tbs.len = end - crt->tbs.p;
1289
1290 /*
1291 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1292 *
1293 * CertificateSerialNumber ::= INTEGER
1294 *
1295 * signature AlgorithmIdentifier
1296 */
1297 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1299 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001300 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001303 return( ret );
1304 }
1305
Andres AG7ca4a032017-03-09 16:16:11 +00001306 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 mbedtls_x509_crt_free( crt );
1309 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001310 }
1311
Andres AG7ca4a032017-03-09 16:16:11 +00001312 crt->version++;
1313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001315 &crt->sig_md, &crt->sig_pk,
1316 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319 return( ret );
1320 }
1321
1322 /*
1323 * issuer Name
1324 */
1325 crt->issuer_raw.p = p;
1326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1328 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001331 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001332 }
1333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001337 return( ret );
1338 }
1339
1340 crt->issuer_raw.len = p - crt->issuer_raw.p;
1341
1342 /*
1343 * Validity ::= SEQUENCE {
1344 * notBefore Time,
1345 * notAfter Time }
1346 *
1347 */
1348 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1349 &crt->valid_to ) ) != 0 )
1350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352 return( ret );
1353 }
1354
1355 /*
1356 * subject Name
1357 */
1358 crt->subject_raw.p = p;
1359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1361 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001364 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365 }
1366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370 return( ret );
1371 }
1372
1373 crt->subject_raw.len = p - crt->subject_raw.p;
1374
1375 /*
1376 * SubjectPublicKeyInfo
1377 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001378 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382 return( ret );
1383 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001384 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001385
1386 /*
1387 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1388 * -- If present, version shall be v2 or v3
1389 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1390 * -- If present, version shall be v2 or v3
1391 * extensions [3] EXPLICIT Extensions OPTIONAL
1392 * -- If present, version shall be v3
1393 */
1394 if( crt->version == 2 || crt->version == 3 )
1395 {
1396 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1397 if( ret != 0 )
1398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001400 return( ret );
1401 }
1402 }
1403
1404 if( crt->version == 2 || crt->version == 3 )
1405 {
1406 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1407 if( ret != 0 )
1408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001410 return( ret );
1411 }
1412 }
1413
1414 if( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001415 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001416 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001417 if( ret != 0 )
1418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420 return( ret );
1421 }
1422 }
1423
1424 if( p != end )
1425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001427 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1428 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429 }
1430
1431 end = crt_end;
1432
1433 /*
1434 * }
1435 * -- end of TBSCertificate
1436 *
1437 * signatureAlgorithm AlgorithmIdentifier,
1438 * signatureValue BIT STRING
1439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443 return( ret );
1444 }
1445
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001446 if( crt->sig_oid.len != sig_oid2.len ||
1447 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001448 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001449 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001450 ( sig_params1.len != 0 &&
1451 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 mbedtls_x509_crt_free( crt );
1454 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001455 }
1456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460 return( ret );
1461 }
1462
1463 if( p != end )
1464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001466 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1467 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468 }
1469
1470 return( 0 );
1471}
1472
1473/*
1474 * Parse one X.509 certificate in DER format from a buffer and add them to a
1475 * chained list
1476 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001477static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1478 const unsigned char *buf,
1479 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001480 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001481 mbedtls_x509_crt_ext_cb_t cb,
1482 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001483{
Janos Follath865b3eb2019-12-16 11:46:15 +00001484 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486
1487 /*
1488 * Check for valid input
1489 */
1490 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001492
1493 while( crt->version != 0 && crt->next != NULL )
1494 {
1495 prev = crt;
1496 crt = crt->next;
1497 }
1498
1499 /*
1500 * Add new certificate on the end of the chain if needed.
1501 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001502 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001503 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001504 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001505
1506 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001507 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508
1509 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001511 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512 }
1513
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001514 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001515 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516 {
1517 if( prev )
1518 prev->next = NULL;
1519
1520 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522
1523 return( ret );
1524 }
1525
1526 return( 0 );
1527}
1528
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001529int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1530 const unsigned char *buf,
1531 size_t buflen )
1532{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001533 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001534}
1535
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001536int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1537 const unsigned char *buf,
1538 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001539 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001540 mbedtls_x509_crt_ext_cb_t cb,
1541 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001542{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001543 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001544}
1545
1546int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1547 const unsigned char *buf,
1548 size_t buflen )
1549{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001550 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001551}
1552
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001554 * Parse one or more PEM certificates from a buffer and add them to the chained
1555 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001557int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1558 const unsigned char *buf,
1559 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560{
Janos Follath98e28a72016-05-31 14:03:54 +01001561#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001562 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001564#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001565
1566 /*
1567 * Check for valid input
1568 */
1569 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571
1572 /*
1573 * Determine buffer content. Buffer contains either one DER certificate or
1574 * one or more PEM certificates.
1575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001577 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001578 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001581 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1584 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001585#else
1586 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1587#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_PEM_PARSE_C)
1590 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001595 /* 1 rather than 0 since the terminating NULL byte is counted in */
1596 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597 {
1598 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001601 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001603 "-----BEGIN CERTIFICATE-----",
1604 "-----END CERTIFICATE-----",
1605 buf, NULL, 0, &use_len );
1606
1607 if( ret == 0 )
1608 {
1609 /*
1610 * Was PEM encoded
1611 */
1612 buflen -= use_len;
1613 buf += use_len;
1614 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616 {
1617 return( ret );
1618 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001622
1623 /*
1624 * PEM header and footer were found
1625 */
1626 buflen -= use_len;
1627 buf += use_len;
1628
1629 if( first_error == 0 )
1630 first_error = ret;
1631
Paul Bakker5a5fa922014-09-26 14:53:04 +02001632 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633 continue;
1634 }
1635 else
1636 break;
1637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641
1642 if( ret != 0 )
1643 {
1644 /*
1645 * Quit parsing on a memory error
1646 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001647 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001648 return( ret );
1649
1650 if( first_error == 0 )
1651 first_error = ret;
1652
1653 total_failed++;
1654 continue;
1655 }
1656
1657 success = 1;
1658 }
1659 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660
1661 if( success )
1662 return( total_failed );
1663 else if( first_error )
1664 return( first_error );
1665 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001667#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001668}
1669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671/*
1672 * Load one or more certificates and add them to the chained list
1673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675{
Janos Follath865b3eb2019-12-16 11:46:15 +00001676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001677 size_t n;
1678 unsigned char *buf;
1679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001681 return( ret );
1682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001684
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001685 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001687
1688 return( ret );
1689}
1690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001692{
1693 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001694#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001695 int w_ret;
1696 WCHAR szDir[MAX_PATH];
1697 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001698 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001699 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001700
Paul Bakker9af723c2014-05-01 13:03:14 +02001701 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702 HANDLE hFind;
1703
1704 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001706
Paul Bakker9af723c2014-05-01 13:03:14 +02001707 memset( szDir, 0, sizeof(szDir) );
1708 memset( filename, 0, MAX_PATH );
1709 memcpy( filename, path, len );
1710 filename[len++] = '\\';
1711 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001712 filename[len++] = '*';
1713
Simon B3c6b18d2016-11-03 01:11:37 +00001714 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001715 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001716 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001718
1719 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001720 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001722
1723 len = MAX_PATH - len;
1724 do
1725 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001726 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001727
1728 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1729 continue;
1730
Paul Bakker9af723c2014-05-01 13:03:14 +02001731 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001732 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001733 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001734 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001735 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001736 {
1737 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1738 goto cleanup;
1739 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001742 if( w_ret < 0 )
1743 ret++;
1744 else
1745 ret += w_ret;
1746 }
1747 while( FindNextFileW( hFind, &file_data ) != 0 );
1748
Paul Bakker66d5d072014-06-17 16:39:18 +02001749 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001751
Ron Eldor36d90422017-01-09 15:09:16 +02001752cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001753 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001754#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001755 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001756 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001757 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001758 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001759 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001760 DIR *dir = opendir( path );
1761
Paul Bakker66d5d072014-06-17 16:39:18 +02001762 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001764
Ron Eldor63140682017-01-09 19:27:59 +02001765#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001766 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001767 {
1768 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001769 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001770 }
Ron Eldor63140682017-01-09 19:27:59 +02001771#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001772
Paul Elliottfb91a482021-03-05 14:17:51 +00001773 memset( &sb, 0, sizeof( sb ) );
1774
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001775 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001776 {
Andres AGf9113192016-09-02 14:06:04 +01001777 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1778 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001779
Andres AGf9113192016-09-02 14:06:04 +01001780 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001781 {
Andres AGf9113192016-09-02 14:06:04 +01001782 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1783 goto cleanup;
1784 }
1785 else if( stat( entry_name, &sb ) == -1 )
1786 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001787 if( errno == ENOENT )
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001788 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001789 /* Broken symbolic link - ignore this entry.
1790 stat(2) will return this error for either (a) a dangling
1791 symlink or (b) a missing file.
1792 Given that we have just obtained the filename from readdir,
1793 assume that it does exist and therefore treat this as a
1794 dangling symlink. */
1795 continue;
1796 }
1797 else
1798 {
1799 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001800 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1801 goto cleanup;
1802 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001803 }
1804
1805 if( !S_ISREG( sb.st_mode ) )
1806 continue;
1807
1808 // Ignore parse errors
1809 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001811 if( t_ret < 0 )
1812 ret++;
1813 else
1814 ret += t_ret;
1815 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001816
1817cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001818 closedir( dir );
1819
Ron Eldor63140682017-01-09 19:27:59 +02001820#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001821 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001823#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001824
Paul Bakkerbe089b02013-10-14 15:51:50 +02001825#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001826
1827 return( ret );
1828}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001830
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001831/*
1832 * OtherName ::= SEQUENCE {
1833 * type-id OBJECT IDENTIFIER,
1834 * value [0] EXPLICIT ANY DEFINED BY type-id }
1835 *
1836 * HardwareModuleName ::= SEQUENCE {
1837 * hwType OBJECT IDENTIFIER,
1838 * hwSerialNum OCTET STRING }
1839 *
1840 * NOTE: we currently only parse and use otherName of type HwModuleName,
1841 * as defined in RFC 4108.
1842 */
1843static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1844 mbedtls_x509_san_other_name *other_name )
1845{
Janos Follathab23cd12019-05-09 13:53:57 +01001846 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001847 size_t len;
1848 unsigned char *p = subject_alt_name->p;
1849 const unsigned char *end = p + subject_alt_name->len;
1850 mbedtls_x509_buf cur_oid;
1851
1852 if( ( subject_alt_name->tag &
1853 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1854 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1855 {
1856 /*
1857 * The given subject alternative name is not of type "othername".
1858 */
1859 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1860 }
1861
1862 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1863 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001864 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001865
1866 cur_oid.tag = MBEDTLS_ASN1_OID;
1867 cur_oid.p = p;
1868 cur_oid.len = len;
1869
1870 /*
1871 * Only HwModuleName is currently supported.
1872 */
1873 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1874 {
1875 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1876 }
1877
Ron Eldor890819a2019-05-13 19:03:04 +03001878 if( p + len >= end )
1879 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001880 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001881 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1882 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001883 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001884 p += len;
1885 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1886 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001887 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001888
1889 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1890 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001891 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001892
1893 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001894 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001895
1896 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1897 other_name->value.hardware_module_name.oid.p = p;
1898 other_name->value.hardware_module_name.oid.len = len;
1899
Ron Eldor890819a2019-05-13 19:03:04 +03001900 if( p + len >= end )
1901 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001902 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001903 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1904 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001905 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001906 p += len;
1907 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1908 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001909 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001910
1911 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1912 other_name->value.hardware_module_name.val.p = p;
1913 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001914 p += len;
1915 if( p != end )
1916 {
Ron Eldor890819a2019-05-13 19:03:04 +03001917 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001918 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001919 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1920 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001921 }
1922 return( 0 );
1923}
1924
Peter Kolbus9a969b62018-12-11 13:55:56 -06001925int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1926 mbedtls_x509_subject_alternative_name *san )
1927{
1928 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1929 switch( san_buf->tag &
1930 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1931 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1932 {
1933 /*
1934 * otherName
1935 */
1936 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1937 {
1938 mbedtls_x509_san_other_name other_name;
1939
1940 ret = x509_get_other_name( san_buf, &other_name );
1941 if( ret != 0 )
1942 return( ret );
1943
1944 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1945 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1946 memcpy( &san->san.other_name,
1947 &other_name, sizeof( other_name ) );
1948
1949 }
1950 break;
1951
1952 /*
1953 * dNSName
1954 */
1955 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1956 {
1957 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1958 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1959
1960 memcpy( &san->san.unstructured_name,
1961 san_buf, sizeof( *san_buf ) );
1962
1963 }
1964 break;
1965
1966 /*
1967 * Type not supported
1968 */
1969 default:
1970 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1971 }
1972 return( 0 );
1973}
1974
1975#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001976static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001977 const mbedtls_x509_sequence
1978 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001979 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001980{
Janos Follath865b3eb2019-12-16 11:46:15 +00001981 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03001982 size_t i;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001983 size_t n = *size;
1984 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001986 mbedtls_x509_subject_alternative_name san;
1987 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001988
1989 while( cur != NULL )
1990 {
Ron Eldor890819a2019-05-13 19:03:04 +03001991 memset( &san, 0, sizeof( san ) );
1992 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1993 if( parse_ret != 0 )
1994 {
1995 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1996 {
1997 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1998 MBEDTLS_X509_SAFE_SNPRINTF;
1999 }
2000 else
2001 {
2002 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
2003 MBEDTLS_X509_SAFE_SNPRINTF;
2004 }
2005 cur = cur->next;
2006 continue;
2007 }
2008
2009 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002010 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002011 /*
2012 * otherName
2013 */
Ron Eldora2913912019-05-16 16:17:38 +03002014 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03002015 {
2016 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
2017
2018 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
2019 MBEDTLS_X509_SAFE_SNPRINTF;
2020
2021 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
2022 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01002023 {
Ron Eldor890819a2019-05-13 19:03:04 +03002024 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
2025 MBEDTLS_X509_SAFE_SNPRINTF;
2026 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01002027 MBEDTLS_X509_SAFE_SNPRINTF;
2028
Ron Eldor890819a2019-05-13 19:03:04 +03002029 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
2030 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01002031
Ron Eldor890819a2019-05-13 19:03:04 +03002032 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
2033 MBEDTLS_X509_SAFE_SNPRINTF;
2034
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03002035 for( i = 0; i < other_name->value.hardware_module_name.val.len; i++ )
Ron Eldor890819a2019-05-13 19:03:04 +03002036 {
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03002037 ret = mbedtls_snprintf( p, n, "%02X", other_name->value.hardware_module_name.val.p[i] );
2038 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath22f605f2019-05-10 10:37:17 +01002039 }
Ron Eldor890819a2019-05-13 19:03:04 +03002040 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
2041 }
2042 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002043
2044 /*
2045 * dNSName
2046 */
Ron Eldora2913912019-05-16 16:17:38 +03002047 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03002048 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002049 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
2050 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03002051 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002052 {
2053 *p = '\0';
2054 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
2055 }
Ron Eldora2913912019-05-16 16:17:38 +03002056
2057 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
2058 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03002059 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03002060 }
2061 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002062
2063 /*
Ron Eldor890819a2019-05-13 19:03:04 +03002064 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002065 */
2066 default:
Janos Follath22f605f2019-05-10 10:37:17 +01002067 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
2068 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002069 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002070 }
2071
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002072 cur = cur->next;
2073 }
2074
2075 *p = '\0';
2076
2077 *size = n;
2078 *buf = p;
2079
2080 return( 0 );
2081}
2082
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002083#define PRINT_ITEM(i) \
2084 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002086 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002087 sep = ", "; \
2088 }
2089
2090#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01002091 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002092 PRINT_ITEM( name );
2093
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002094static int x509_info_cert_type( char **buf, size_t *size,
2095 unsigned char ns_cert_type )
2096{
Janos Follath865b3eb2019-12-16 11:46:15 +00002097 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002098 size_t n = *size;
2099 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002100 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002103 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
2105 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
2106 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002107 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
2108 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
2109 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002110
2111 *size = n;
2112 *buf = p;
2113
2114 return( 0 );
2115}
2116
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002117#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01002118 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002119 PRINT_ITEM( name );
2120
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002121static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002122 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002123{
Janos Follath865b3eb2019-12-16 11:46:15 +00002124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002125 size_t n = *size;
2126 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002127 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2130 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002131 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2132 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2133 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2135 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002136 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2137 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002138
2139 *size = n;
2140 *buf = p;
2141
2142 return( 0 );
2143}
2144
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002145static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002147{
Janos Follath865b3eb2019-12-16 11:46:15 +00002148 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002149 const char *desc;
2150 size_t n = *size;
2151 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002153 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002154
2155 while( cur != NULL )
2156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002158 desc = "???";
2159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002161 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002162
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002163 sep = ", ";
2164
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002165 cur = cur->next;
2166 }
2167
2168 *size = n;
2169 *buf = p;
2170
2171 return( 0 );
2172}
2173
Ron Eldor74d9acc2019-03-21 14:00:03 +02002174static int x509_info_cert_policies( char **buf, size_t *size,
2175 const mbedtls_x509_sequence *certificate_policies )
2176{
Janos Follath865b3eb2019-12-16 11:46:15 +00002177 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002178 const char *desc;
2179 size_t n = *size;
2180 char *p = *buf;
2181 const mbedtls_x509_sequence *cur = certificate_policies;
2182 const char *sep = "";
2183
2184 while( cur != NULL )
2185 {
2186 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2187 desc = "???";
2188
2189 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2190 MBEDTLS_X509_SAFE_SNPRINTF;
2191
2192 sep = ", ";
2193
2194 cur = cur->next;
2195 }
2196
2197 *size = n;
2198 *buf = p;
2199
2200 return( 0 );
2201}
2202
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002203/*
2204 * Return an informational string about the certificate.
2205 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002206#define BEFORE_COLON 18
2207#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2209 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002210{
Janos Follath865b3eb2019-12-16 11:46:15 +00002211 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002212 size_t n;
2213 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002214 char key_size_str[BEFORE_COLON];
2215
2216 p = buf;
2217 n = size;
2218
Janos Follath98e28a72016-05-31 14:03:54 +01002219 if( NULL == crt )
2220 {
2221 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2222 MBEDTLS_X509_SAFE_SNPRINTF;
2223
2224 return( (int) ( size - n ) );
2225 }
2226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002228 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002229 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002231 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002232 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002235 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002238 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002240 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002243 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002245 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002248 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2249 crt->valid_from.year, crt->valid_from.mon,
2250 crt->valid_from.day, crt->valid_from.hour,
2251 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002252 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002255 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2256 crt->valid_to.year, crt->valid_to.mon,
2257 crt->valid_to.day, crt->valid_to.hour,
2258 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002259 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002262 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002265 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002266 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002267
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002268 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2270 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002271 {
2272 return( ret );
2273 }
2274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002276 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002277 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002278
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002279 /*
2280 * Optional extensions
2281 */
2282
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002283 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002286 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002287 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002288
2289 if( crt->max_pathlen > 0 )
2290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002292 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002293 }
2294 }
2295
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002296 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002297 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002298 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002299 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002300
2301 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002302 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002303 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002304 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002305 }
2306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002310 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002311
2312 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2313 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002314 }
2315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002319 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002320
2321 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2322 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002323 }
2324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002328 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002329
2330 if( ( ret = x509_info_ext_key_usage( &p, &n,
2331 &crt->ext_key_usage ) ) != 0 )
2332 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002333 }
2334
Ron Eldor74d9acc2019-03-21 14:00:03 +02002335 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2336 {
2337 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2338 MBEDTLS_X509_SAFE_SNPRINTF;
2339
2340 if( ( ret = x509_info_cert_policies( &p, &n,
2341 &crt->certificate_policies ) ) != 0 )
2342 return( ret );
2343 }
2344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002346 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002347
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002348 return( (int) ( size - n ) );
2349}
2350
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002351struct x509_crt_verify_string {
2352 int code;
2353 const char *string;
2354};
2355
Hanno Becker7ac83f92020-10-09 10:48:22 +01002356#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002357static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002358 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002359 { 0, NULL }
2360};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002361#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002362
2363int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002364 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002365{
Janos Follath865b3eb2019-12-16 11:46:15 +00002366 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002367 const struct x509_crt_verify_string *cur;
2368 char *p = buf;
2369 size_t n = size;
2370
2371 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2372 {
2373 if( ( flags & cur->code ) == 0 )
2374 continue;
2375
2376 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002377 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002378 flags ^= cur->code;
2379 }
2380
2381 if( flags != 0 )
2382 {
2383 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2384 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002385 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002386 }
2387
2388 return( (int) ( size - n ) );
2389}
Hanno Becker612a2f12020-10-09 09:19:39 +01002390#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002391
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002392int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2393 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002394{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002395 unsigned int usage_must, usage_may;
2396 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2397 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2398
2399 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2400 return( 0 );
2401
2402 usage_must = usage & ~may_mask;
2403
2404 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2405 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2406
2407 usage_may = usage & may_mask;
2408
2409 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002411
2412 return( 0 );
2413}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002416 const char *usage_oid,
2417 size_t usage_len )
2418{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002420
2421 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002423 return( 0 );
2424
2425 /*
2426 * Look for the requested usage (or wildcard ANY) in our list
2427 */
2428 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002431
2432 if( cur_oid->len == usage_len &&
2433 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2434 {
2435 return( 0 );
2436 }
2437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002439 return( 0 );
2440 }
2441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002443}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002446/*
2447 * Return 1 if the certificate is revoked, or 0 otherwise.
2448 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002449int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002450{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002452
2453 while( cur != NULL && cur->serial.len != 0 )
2454 {
2455 if( crt->serial.len == cur->serial.len &&
2456 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2457 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002458 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002459 }
2460
2461 cur = cur->next;
2462 }
2463
2464 return( 0 );
2465}
2466
2467/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002468 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002469 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002472 mbedtls_x509_crl *crl_list,
2473 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002474{
2475 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002476 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002477#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002478 psa_algorithm_t psa_algorithm;
2479#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002481#endif /* MBEDTLS_USE_PSA_CRYPTO */
2482 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002483
2484 if( ca == NULL )
2485 return( flags );
2486
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002487 while( crl_list != NULL )
2488 {
2489 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002490 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002491 {
2492 crl_list = crl_list->next;
2493 continue;
2494 }
2495
2496 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002497 * Check if the CA is configured to sign CRLs
2498 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002499 if( mbedtls_x509_crt_check_key_usage( ca,
2500 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002503 break;
2504 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002505
2506 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002507 * Check if CRL is correctly signed by the trusted CA
2508 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002509 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2510 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2511
2512 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2513 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2514
pespacek7599a772022-02-07 14:40:55 +01002515#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002516 psa_algorithm = mbedtls_hash_info_psa_from_md( crl_list->sig_md );
pespacekd924e552022-02-28 11:49:54 +01002517 if( psa_hash_compute( psa_algorithm,
2518 crl_list->tbs.p,
2519 crl_list->tbs.len,
2520 hash,
2521 sizeof( hash ),
2522 &hash_length ) != PSA_SUCCESS )
pespaceka7a64692022-02-14 15:18:43 +01002523 {
2524 /* Note: this can't happen except after an internal error */
2525 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2526 break;
2527 }
pespacek7599a772022-02-07 14:40:55 +01002528#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002530 hash_length = mbedtls_md_get_size( md_info );
2531 if( mbedtls_md( md_info,
2532 crl_list->tbs.p,
2533 crl_list->tbs.len,
2534 hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002535 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002536 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002538 break;
2539 }
pespaceka7a64692022-02-14 15:18:43 +01002540#endif /* MBEDTLS_USE_PSA_CRYPTO */
2541
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002542 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002543 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
pespacek7599a772022-02-07 14:40:55 +01002546 crl_list->sig_md, hash, hash_length,
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002547 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002550 break;
2551 }
2552
2553 /*
2554 * Check for validity of CRL (Do not drop out)
2555 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002556 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002558
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002559 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002560 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002561
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002562 /*
2563 * Check if certificate is revoked
2564 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002565 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002568 break;
2569 }
2570
2571 crl_list = crl_list->next;
2572 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002573
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002574 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002577
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002578/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002579 * Check the signature of a certificate by its parent
2580 */
2581static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002582 mbedtls_x509_crt *parent,
2583 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002584{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002585 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02002586 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002587#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2588 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002589 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002590 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002591
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002592 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002593 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002594 return( -1 );
2595#else
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002596 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( child->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002597 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002598
pespacek7599a772022-02-07 14:40:55 +01002599 status = psa_hash_compute( hash_alg,
2600 child->tbs.p,
2601 child->tbs.len,
2602 hash,
pespacekb9f07a72022-02-14 15:13:26 +01002603 sizeof( hash ),
pespacek7599a772022-02-07 14:40:55 +01002604 &hash_len );
2605 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002606 {
pespacek3110c7b2022-02-14 15:07:41 +01002607 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002608 }
2609
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002610#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002611 /* Skip expensive computation on obvious mismatch */
2612 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002613 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002614
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002615#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002616 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2617 {
2618 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002619 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002620 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002621 }
2622#else
2623 (void) rs_ctx;
2624#endif
2625
2626 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002627 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002628 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002629}
2630
2631/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002632 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2633 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002634 *
2635 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2638 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002639 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002640{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002641 int need_ca_bit;
2642
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002643 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002644 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002645 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002646
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002647 /* Parent must have the basicConstraints CA bit set as a general rule */
2648 need_ca_bit = 1;
2649
2650 /* Exception: v1/v2 certificates that are locally trusted. */
2651 if( top && parent->version < 3 )
2652 need_ca_bit = 0;
2653
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002654 if( need_ca_bit && ! parent->ca_istrue )
2655 return( -1 );
2656
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002657 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002659 {
2660 return( -1 );
2661 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002662
2663 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002664}
2665
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002666/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002667 * Find a suitable parent for child in candidates, or return NULL.
2668 *
2669 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002670 * 1. subject name matches child's issuer
2671 * 2. if necessary, the CA bit is set and key usage allows signing certs
2672 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002673 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002674 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002675 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002676 * If there's a suitable candidate which is also time-valid, return the first
2677 * such. Otherwise, return the first suitable candidate (or NULL if there is
2678 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002679 *
2680 * The rationale for this rule is that someone could have a list of trusted
2681 * roots with two versions on the same root with different validity periods.
2682 * (At least one user reported having such a list and wanted it to just work.)
2683 * The reason we don't just require time-validity is that generally there is
2684 * only one version, and if it's expired we want the flags to state that
2685 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002686 *
2687 * The rationale for rule 3 (signature for trusted roots) is that users might
2688 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002689 * way we select the correct one is by checking the signature (as we don't
2690 * rely on key identifier extensions). (This is one way users might choose to
2691 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002692 *
2693 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002694 * - [in] child: certificate for which we're looking for a parent
2695 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002696 * - [out] r_parent: parent found (or NULL)
2697 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002698 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2699 * of the chain, 0 otherwise
2700 * - [in] path_cnt: number of intermediates seen so far
2701 * - [in] self_cnt: number of self-signed intermediates seen so far
2702 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002703 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002704 *
2705 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002706 * - 0 on success
2707 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002708 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002709static int x509_crt_find_parent_in(
2710 mbedtls_x509_crt *child,
2711 mbedtls_x509_crt *candidates,
2712 mbedtls_x509_crt **r_parent,
2713 int *r_signature_is_good,
2714 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002715 unsigned path_cnt,
2716 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002717 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002718{
Janos Follath865b3eb2019-12-16 11:46:15 +00002719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002720 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002721 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002722
2723#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002724 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002725 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2726 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002727 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002728 parent = rs_ctx->parent;
2729 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002730 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002731
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002732 /* clear saved state */
2733 rs_ctx->parent = NULL;
2734 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002735 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002736
2737 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002738 goto check_signature;
2739 }
2740#endif
2741
2742 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002743 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002744
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002745 for( parent = candidates; parent != NULL; parent = parent->next )
2746 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002747 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002748 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002749 continue;
2750
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002751 /* +1 because stored max_pathlen is 1 higher that the actual value */
2752 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002753 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002754 {
2755 continue;
2756 }
2757
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002758 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002759#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2760check_signature:
2761#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002762 ret = x509_crt_check_signature( child, parent, rs_ctx );
2763
2764#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002765 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2766 {
2767 /* save state */
2768 rs_ctx->parent = parent;
2769 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002770 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002771
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002772 return( ret );
2773 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002774#else
2775 (void) ret;
2776#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002777
2778 signature_is_good = ret == 0;
2779 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002780 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002781
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002782 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002783 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2784 mbedtls_x509_time_is_future( &parent->valid_from ) )
2785 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002786 if( fallback_parent == NULL )
2787 {
2788 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002789 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002790 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002791
2792 continue;
2793 }
2794
Andy Gross1f627142019-01-30 10:25:53 -06002795 *r_parent = parent;
2796 *r_signature_is_good = signature_is_good;
2797
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002798 break;
2799 }
2800
Andy Gross1f627142019-01-30 10:25:53 -06002801 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002802 {
2803 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002804 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002805 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002806
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002807 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002808}
2809
2810/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002811 * Find a parent in trusted CAs or the provided chain, or return NULL.
2812 *
2813 * Searches in trusted CAs first, and return the first suitable parent found
2814 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002815 *
2816 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002817 * - [in] child: certificate for which we're looking for a parent, followed
2818 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002819 * - [in] trust_ca: list of locally trusted certificates
2820 * - [out] parent: parent found (or NULL)
2821 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2822 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2823 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2824 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002825 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002826 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002827 *
2828 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002829 * - 0 on success
2830 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002831 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002832static int x509_crt_find_parent(
2833 mbedtls_x509_crt *child,
2834 mbedtls_x509_crt *trust_ca,
2835 mbedtls_x509_crt **parent,
2836 int *parent_is_trusted,
2837 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002838 unsigned path_cnt,
2839 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002840 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002841{
Janos Follath865b3eb2019-12-16 11:46:15 +00002842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002843 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002844
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002845 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002846
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002847#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002848 /* restore then clear saved state if we have some stored */
2849 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2850 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002851 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002852 rs_ctx->parent_is_trusted = -1;
2853 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002854#endif
2855
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002856 while( 1 ) {
2857 search_list = *parent_is_trusted ? trust_ca : child->next;
2858
2859 ret = x509_crt_find_parent_in( child, search_list,
2860 parent, signature_is_good,
2861 *parent_is_trusted,
2862 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002863
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002864#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002865 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2866 {
2867 /* save state */
2868 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002869 return( ret );
2870 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002871#else
2872 (void) ret;
2873#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002874
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002875 /* stop here if found or already in second iteration */
2876 if( *parent != NULL || *parent_is_trusted == 0 )
2877 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002878
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002879 /* prepare second iteration */
2880 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002881 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002882
2883 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002884 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002885 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002886 *parent_is_trusted = 0;
2887 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002888 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002889
2890 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002891}
2892
2893/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002894 * Check if an end-entity certificate is locally trusted
2895 *
2896 * Currently we require such certificates to be self-signed (actually only
2897 * check for self-issued as self-signatures are not checked)
2898 */
2899static int x509_crt_check_ee_locally_trusted(
2900 mbedtls_x509_crt *crt,
2901 mbedtls_x509_crt *trust_ca )
2902{
2903 mbedtls_x509_crt *cur;
2904
2905 /* must be self-issued */
2906 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2907 return( -1 );
2908
2909 /* look for an exact match with trusted cert */
2910 for( cur = trust_ca; cur != NULL; cur = cur->next )
2911 {
2912 if( crt->raw.len == cur->raw.len &&
2913 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2914 {
2915 return( 0 );
2916 }
2917 }
2918
2919 /* too bad */
2920 return( -1 );
2921}
2922
2923/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002924 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002925 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002926 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2927 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002928 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002929 * such that every cert in the chain is a child of the next one,
2930 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002931 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002932 * Verify that chain and return it with flags for all issues found.
2933 *
2934 * Special cases:
2935 * - EE == Rj -> return a one-element list containing it
2936 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2937 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002938 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002939 * Tests for (aspects of) this function should include at least:
2940 * - trusted EE
2941 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002942 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002943 * - if relevant: EE untrusted
2944 * - if relevant: EE -> intermediate, untrusted
2945 * with the aspect under test checked at each relevant level (EE, int, root).
2946 * For some aspects longer chains are required, but usually length 2 is
2947 * enough (but length 1 is not in general).
2948 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002949 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002950 * - [in] crt: the cert list EE, C1, ..., Cn
2951 * - [in] trust_ca: the trusted list R1, ..., Rp
2952 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002953 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002954 * Only valid when return value is 0, may contain garbage otherwise!
2955 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002956 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002957 *
2958 * Return value:
2959 * - non-zero if the chain could not be fully built and examined
2960 * - 0 is the chain was successfully built and examined,
2961 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002962 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002963static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002964 mbedtls_x509_crt *crt,
2965 mbedtls_x509_crt *trust_ca,
2966 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002967 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2968 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002969 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002970 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002971 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002972{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002973 /* Don't initialize any of those variables here, so that the compiler can
2974 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002975 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002976 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002977 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002978 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002979 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002980 int parent_is_trusted;
2981 int child_is_trusted;
2982 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002983 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002984 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002985
2986#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2987 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002988 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002989 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002990 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002991 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002992 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002993
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002994 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002995 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002996 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002997 flags = &cur->flags;
2998
2999 goto find_parent;
3000 }
3001#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003002
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003003 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003004 self_cnt = 0;
3005 parent_is_trusted = 0;
3006 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003007
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003008 while( 1 ) {
3009 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003010 cur = &ver_chain->items[ver_chain->len];
3011 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003012 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003013 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003014 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003015
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003016 /* Check time-validity (all certificates) */
3017 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3018 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003019
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003020 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3021 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02003022
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003023 /* Stop here for trusted roots (but not for trusted EE certs) */
3024 if( child_is_trusted )
3025 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003026
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003027 /* Check signature algorithm: MD & PK algs */
3028 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3029 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003030
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003031 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3032 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003033
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003034 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003035 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003036 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3037 {
3038 return( 0 );
3039 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003040
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003041#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3042find_parent:
3043#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00003044
3045 /* Obtain list of potential trusted signers from CA callback,
3046 * or use statically provided list. */
3047#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3048 if( f_ca_cb != NULL )
3049 {
3050 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
3051 mbedtls_free( ver_chain->trust_ca_cb_result );
3052 ver_chain->trust_ca_cb_result = NULL;
3053
3054 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
3055 if( ret != 0 )
3056 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3057
3058 cur_trust_ca = ver_chain->trust_ca_cb_result;
3059 }
3060 else
3061#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3062 {
3063 ((void) f_ca_cb);
3064 ((void) p_ca_cb);
3065 cur_trust_ca = trust_ca;
3066 }
3067
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003068 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00003069 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003070 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003071 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003072
3073#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003074 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3075 {
3076 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003077 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003078 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003079 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003080
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003081 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003082 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003083#else
3084 (void) ret;
3085#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003086
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003087 /* No parent? We're done here */
3088 if( parent == NULL )
3089 {
3090 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3091 return( 0 );
3092 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003093
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003094 /* Count intermediate self-issued (not necessarily self-signed) certs.
3095 * These can occur with some strategies for key rollover, see [SIRO],
3096 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003097 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02003098 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
3099 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003100 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02003101 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003102
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003103 /* path_cnt is 0 for the first intermediate CA,
3104 * and if parent is trusted it's not an intermediate CA */
3105 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003106 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003107 {
3108 /* return immediately to avoid overflow the chain array */
3109 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3110 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003111
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003112 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003113 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003114 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3115
3116 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003117 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003118 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003121 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003122 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003123#else
3124 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003125#endif
3126
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003127 /* prepare for next iteration */
3128 child = parent;
3129 parent = NULL;
3130 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003131 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003132 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003133}
3134
3135/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003136 * Check for CN match
3137 */
3138static int x509_crt_check_cn( const mbedtls_x509_buf *name,
3139 const char *cn, size_t cn_len )
3140{
3141 /* try exact match */
3142 if( name->len == cn_len &&
3143 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3144 {
3145 return( 0 );
3146 }
3147
3148 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003149 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003150 {
3151 return( 0 );
3152 }
3153
3154 return( -1 );
3155}
3156
3157/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003158 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3159 */
3160static int x509_crt_check_san( const mbedtls_x509_buf *name,
3161 const char *cn, size_t cn_len )
3162{
3163 const unsigned char san_type = (unsigned char) name->tag &
3164 MBEDTLS_ASN1_TAG_VALUE_MASK;
3165
3166 /* dNSName */
3167 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3168 return( x509_crt_check_cn( name, cn, cn_len ) );
3169
3170 /* (We may handle other types here later.) */
3171
3172 /* Unrecognized type */
3173 return( -1 );
3174}
3175
3176/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003177 * Verify the requested CN - only call this if cn is not NULL!
3178 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003179static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003180 const char *cn,
3181 uint32_t *flags )
3182{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003183 const mbedtls_x509_name *name;
3184 const mbedtls_x509_sequence *cur;
3185 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003186
3187 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3188 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003189 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003190 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003191 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003192 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003193 }
3194
3195 if( cur == NULL )
3196 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3197 }
3198 else
3199 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003200 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003201 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003202 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3203 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003204 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003205 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003206 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003207 }
3208
3209 if( name == NULL )
3210 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3211 }
3212}
3213
3214/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003215 * Merge the flags for all certs in the chain, after calling callback
3216 */
3217static int x509_crt_merge_flags_with_cb(
3218 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003219 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003220 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3221 void *p_vrfy )
3222{
Janos Follath865b3eb2019-12-16 11:46:15 +00003223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003224 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003225 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003226 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003227
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003228 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003229 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003230 cur = &ver_chain->items[i-1];
3231 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003232
3233 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003234 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003235 return( ret );
3236
3237 *flags |= cur_flags;
3238 }
3239
3240 return( 0 );
3241}
3242
3243/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003244 * Verify the certificate validity, with profile, restartable version
3245 *
3246 * This function:
3247 * - checks the requested CN (if any)
3248 * - checks the type and size of the EE cert's key,
3249 * as that isn't done as part of chain building/verification currently
3250 * - builds and verifies the chain
3251 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003252 *
3253 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3254 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3255 * verification routine to search for trusted signers, and CRLs will
3256 * be disabled. Otherwise, `trust_ca` will be used as the static list
3257 * of trusted signers, and `ca_crl` will be use as the static list
3258 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003259 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003260static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003261 mbedtls_x509_crt *trust_ca,
3262 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003263 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3264 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003265 const mbedtls_x509_crt_profile *profile,
3266 const char *cn, uint32_t *flags,
3267 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3268 void *p_vrfy,
3269 mbedtls_x509_crt_restart_ctx *rs_ctx )
3270{
Janos Follath865b3eb2019-12-16 11:46:15 +00003271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003272 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003273 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003274 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003275
3276 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003277 ee_flags = 0;
3278 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003279
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003280 if( profile == NULL )
3281 {
3282 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3283 goto exit;
3284 }
3285
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003286 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003287 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003288 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003289
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003290 /* Check the type and size of the key */
3291 pk_type = mbedtls_pk_get_type( &crt->pk );
3292
3293 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003294 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003295
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003296 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003297 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003298
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003299 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003300 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3301 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003302 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003303
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003304 if( ret != 0 )
3305 goto exit;
3306
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003307 /* Merge end-entity flags */
3308 ver_chain.items[0].flags |= ee_flags;
3309
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003310 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003311 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003312
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003313exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003314
3315#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3316 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3317 mbedtls_free( ver_chain.trust_ca_cb_result );
3318 ver_chain.trust_ca_cb_result = NULL;
3319#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3320
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003321#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3322 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3323 mbedtls_x509_crt_restart_free( rs_ctx );
3324#endif
3325
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003326 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3327 * the SSL module for authmode optional, but non-zero return from the
3328 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003329 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3330 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3331
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003332 if( ret != 0 )
3333 {
3334 *flags = (uint32_t) -1;
3335 return( ret );
3336 }
3337
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003338 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003340
3341 return( 0 );
3342}
3343
Hanno Becker3116fb32019-03-28 13:34:42 +00003344
3345/*
3346 * Verify the certificate validity (default profile, not restartable)
3347 */
3348int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3349 mbedtls_x509_crt *trust_ca,
3350 mbedtls_x509_crl *ca_crl,
3351 const char *cn, uint32_t *flags,
3352 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3353 void *p_vrfy )
3354{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003355 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003356 NULL, NULL,
3357 &mbedtls_x509_crt_profile_default,
3358 cn, flags,
3359 f_vrfy, p_vrfy, NULL ) );
3360}
3361
3362/*
3363 * Verify the certificate validity (user-chosen profile, not restartable)
3364 */
3365int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3366 mbedtls_x509_crt *trust_ca,
3367 mbedtls_x509_crl *ca_crl,
3368 const mbedtls_x509_crt_profile *profile,
3369 const char *cn, uint32_t *flags,
3370 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3371 void *p_vrfy )
3372{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003373 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003374 NULL, NULL,
3375 profile, cn, flags,
3376 f_vrfy, p_vrfy, NULL ) );
3377}
3378
3379#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3380/*
3381 * Verify the certificate validity (user-chosen profile, CA callback,
3382 * not restartable).
3383 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003384int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003385 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3386 void *p_ca_cb,
3387 const mbedtls_x509_crt_profile *profile,
3388 const char *cn, uint32_t *flags,
3389 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3390 void *p_vrfy )
3391{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003392 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003393 f_ca_cb, p_ca_cb,
3394 profile, cn, flags,
3395 f_vrfy, p_vrfy, NULL ) );
3396}
3397#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3398
3399int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3400 mbedtls_x509_crt *trust_ca,
3401 mbedtls_x509_crl *ca_crl,
3402 const mbedtls_x509_crt_profile *profile,
3403 const char *cn, uint32_t *flags,
3404 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3405 void *p_vrfy,
3406 mbedtls_x509_crt_restart_ctx *rs_ctx )
3407{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003408 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003409 NULL, NULL,
3410 profile, cn, flags,
3411 f_vrfy, p_vrfy, rs_ctx ) );
3412}
3413
3414
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003415/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003416 * Initialize a certificate chain
3417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003419{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003421}
3422
3423/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003424 * Unallocate all certificate data
3425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003427{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 mbedtls_x509_crt *cert_cur = crt;
3429 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003430
Glenn Straussa4b40412022-06-26 19:32:09 -04003431 while( cert_cur != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3436 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003437#endif
3438
Glenn Straussa4b40412022-06-26 19:32:09 -04003439 mbedtls_asn1_free_named_data_list_shallow( cert_cur->issuer.next );
3440 mbedtls_asn1_free_named_data_list_shallow( cert_cur->subject.next );
3441 mbedtls_asn1_sequence_free( cert_cur->ext_key_usage.next );
3442 mbedtls_asn1_sequence_free( cert_cur->subject_alt_names.next );
3443 mbedtls_asn1_sequence_free( cert_cur->certificate_policies.next );
Ron Eldor74d9acc2019-03-21 14:00:03 +02003444
toth92g1bbc2fe2021-02-12 16:11:17 +01003445 name_cur = cert_cur->authority_key_id.authorityCertIssuer.next;
toth92gdd123902021-04-08 10:12:52 +02003446 while ( name_cur != NULL )
toth92g1bbc2fe2021-02-12 16:11:17 +01003447 {
3448 name_prv = name_cur;
3449 name_cur = name_cur->next;
toth92gdd123902021-04-08 10:12:52 +02003450 mbedtls_platform_zeroize( name_prv, sizeof(mbedtls_x509_name) );
3451 mbedtls_free( name_prv );
toth92g1bbc2fe2021-02-12 16:11:17 +01003452 }
3453
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003454 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003455 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003456 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003458 }
3459
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003460 cert_prv = cert_cur;
3461 cert_cur = cert_cur->next;
3462
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003463 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003464 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003466 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003467}
3468
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003469#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3470/*
3471 * Initialize a restart context
3472 */
3473void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3474{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003475 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003476
3477 ctx->parent = NULL;
3478 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003479 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003480
3481 ctx->parent_is_trusted = -1;
3482
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003483 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003484 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003485 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003486}
3487
3488/*
3489 * Free the components of a restart context
3490 */
3491void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3492{
3493 if( ctx == NULL )
3494 return;
3495
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003496 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003497 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003498}
3499#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501#endif /* MBEDTLS_X509_CRT_PARSE_C */