blob: 0d8d134cc23e30b3ec7558d3e0748a3103e29d30 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
49#include "mbedtls/psa_util.h"
pespacek7599a772022-02-07 14:40:55 +010050#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemek Stekiel40afdd22022-09-06 13:08:28 +020051#include "hash_info.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040052
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010057#endif
58
Daniel Axtensf0710242020-05-28 11:43:41 +100059#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010060#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#include <windows.h>
62#else
63#include <time.h>
64#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100065#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000068#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020069#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070#include <sys/types.h>
71#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020072#if defined(__MBED__)
73#include <platform/mbed_retarget.h>
74#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020076#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060077#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000078#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#endif
80
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020081/*
82 * Item in a verification chain: cert and flags for it
83 */
84typedef struct {
85 mbedtls_x509_crt *crt;
86 uint32_t flags;
87} x509_crt_verify_chain_item;
88
89/*
90 * Max size of verification chain: end-entity + intermediates + trusted root
91 */
92#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020093
Gilles Peskineffb92da2021-06-02 00:03:26 +020094/* Default profile. Do not remove items unless there are serious security
95 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020096const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
97{
Gilles Peskineae270bf2021-06-02 00:05:29 +020098 /* Hashes from SHA-256 and above. Note that this selection
99 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200100 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
101 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
102 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
103 0xFFFFFFF, /* Any PK alg */
104#if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200105 /* Curves at or above 128-bit security level. Note that this selection
106 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200107 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
108 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
110 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
111 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
112 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
Gilles Peskine39957502021-06-17 23:17:52 +0200113 0,
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200114#else
115 0,
116#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200117 2048,
118};
119
Gilles Peskineffb92da2021-06-02 00:03:26 +0200120/* Next-generation profile. Currently identical to the default, but may
121 * be tightened at any time. */
122const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200123{
124 /* Hashes from SHA-256 and above. */
125 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
126 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
127 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
128 0xFFFFFFF, /* Any PK alg */
129#if defined(MBEDTLS_ECP_C)
130 /* Curves at or above 128-bit security level. */
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
136 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
138#else
139 0,
140#endif
141 2048,
142};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200143
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200144/*
145 * NSA Suite B Profile
146 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200147const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
148{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200149 /* Only SHA-256 and 384 */
150 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
151 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
152 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200153 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
154 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200155#if defined(MBEDTLS_ECP_C)
156 /* Only NIST P-256 and P-384 */
157 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
158 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
159#else
160 0,
161#endif
162 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200163};
164
165/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200166 * Empty / all-forbidden profile
167 */
168const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
169{
170 0,
171 0,
172 0,
173 (uint32_t) -1,
174};
175
176/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200177 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200178 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200179 */
180static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
181 mbedtls_md_type_t md_alg )
182{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200183 if( md_alg == MBEDTLS_MD_NONE )
184 return( -1 );
185
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200186 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
187 return( 0 );
188
189 return( -1 );
190}
191
192/*
193 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200194 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200195 */
196static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
197 mbedtls_pk_type_t pk_alg )
198{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200199 if( pk_alg == MBEDTLS_PK_NONE )
200 return( -1 );
201
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200202 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
203 return( 0 );
204
205 return( -1 );
206}
207
208/*
209 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200210 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200211 */
212static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200213 const mbedtls_pk_context *pk )
214{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200215 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200216
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200217#if defined(MBEDTLS_RSA_C)
218 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
219 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200220 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200221 return( 0 );
222
223 return( -1 );
224 }
225#endif
226
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200227#if defined(MBEDTLS_ECP_C)
228 if( pk_alg == MBEDTLS_PK_ECDSA ||
229 pk_alg == MBEDTLS_PK_ECKEY ||
230 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200231 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200232 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200233
Philippe Antoineb5b25432018-05-11 11:06:29 +0200234 if( gid == MBEDTLS_ECP_DP_NONE )
235 return( -1 );
236
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200237 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
238 return( 0 );
239
240 return( -1 );
241 }
242#endif
243
244 return( -1 );
245}
246
247/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000248 * Like memcmp, but case-insensitive and always returns -1 if different
249 */
250static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
251{
252 size_t i;
253 unsigned char diff;
254 const unsigned char *n1 = s1, *n2 = s2;
255
256 for( i = 0; i < len; i++ )
257 {
258 diff = n1[i] ^ n2[i];
259
260 if( diff == 0 )
261 continue;
262
263 if( diff == 32 &&
264 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
265 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
266 {
267 continue;
268 }
269
270 return( -1 );
271 }
272
273 return( 0 );
274}
275
276/*
277 * Return 0 if name matches wildcard, -1 otherwise
278 */
279static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
280{
281 size_t i;
282 size_t cn_idx = 0, cn_len = strlen( cn );
283
284 /* We can't have a match if there is no wildcard to match */
285 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
286 return( -1 );
287
288 for( i = 0; i < cn_len; ++i )
289 {
290 if( cn[i] == '.' )
291 {
292 cn_idx = i;
293 break;
294 }
295 }
296
297 if( cn_idx == 0 )
298 return( -1 );
299
300 if( cn_len - cn_idx == name->len - 1 &&
301 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
302 {
303 return( 0 );
304 }
305
306 return( -1 );
307}
308
309/*
310 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
311 * variations (but not all).
312 *
313 * Return 0 if equal, -1 otherwise.
314 */
315static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
316{
317 if( a->tag == b->tag &&
318 a->len == b->len &&
319 memcmp( a->p, b->p, b->len ) == 0 )
320 {
321 return( 0 );
322 }
323
324 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
325 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
326 a->len == b->len &&
327 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
328 {
329 return( 0 );
330 }
331
332 return( -1 );
333}
334
335/*
336 * Compare two X.509 Names (aka rdnSequence).
337 *
338 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
339 * we sometimes return unequal when the full algorithm would return equal,
340 * but never the other way. (In particular, we don't do Unicode normalisation
341 * or space folding.)
342 *
343 * Return 0 if equal, -1 otherwise.
344 */
345static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
346{
347 /* Avoid recursion, it might not be optimised by the compiler */
348 while( a != NULL || b != NULL )
349 {
350 if( a == NULL || b == NULL )
351 return( -1 );
352
353 /* type */
354 if( a->oid.tag != b->oid.tag ||
355 a->oid.len != b->oid.len ||
356 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
357 {
358 return( -1 );
359 }
360
361 /* value */
362 if( x509_string_cmp( &a->val, &b->val ) != 0 )
363 return( -1 );
364
365 /* structure of the list of sets */
366 if( a->next_merged != b->next_merged )
367 return( -1 );
368
369 a = a->next;
370 b = b->next;
371 }
372
373 /* a == NULL == b */
374 return( 0 );
375}
376
377/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200378 * Reset (init or clear) a verify_chain
379 */
380static void x509_crt_verify_chain_reset(
381 mbedtls_x509_crt_verify_chain *ver_chain )
382{
383 size_t i;
384
385 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
386 {
387 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000388 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200389 }
390
391 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000392
393#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
394 ver_chain->trust_ca_cb_result = NULL;
395#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200396}
397
398/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
400 */
401static int x509_get_version( unsigned char **p,
402 const unsigned char *end,
403 int *ver )
404{
Janos Follath865b3eb2019-12-16 11:46:15 +0000405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 size_t len;
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
409 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 {
413 *ver = 0;
414 return( 0 );
415 }
416
Chris Jones9f7a6932021-04-14 12:12:09 +0100417 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 }
419
420 end = *p + len;
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100423 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424
425 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100426 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
427 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
429 return( 0 );
430}
431
432/*
433 * Validity ::= SEQUENCE {
434 * notBefore Time,
435 * notAfter Time }
436 */
437static int x509_get_dates( unsigned char **p,
438 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_x509_time *from,
440 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441{
Janos Follath865b3eb2019-12-16 11:46:15 +0000442 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443 size_t len;
444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
446 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100447 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448
449 end = *p + len;
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452 return( ret );
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 return( ret );
456
457 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100458 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
459 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
461 return( 0 );
462}
463
464/*
465 * X.509 v2/v3 unique identifier (not parsed)
466 */
467static int x509_get_uid( unsigned char **p,
468 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470{
Janos Follath865b3eb2019-12-16 11:46:15 +0000471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
473 if( *p == end )
474 return( 0 );
475
476 uid->tag = **p;
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
479 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482 return( 0 );
483
Chris Jones9f7a6932021-04-14 12:12:09 +0100484 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 }
486
487 uid->p = *p;
488 *p += uid->len;
489
490 return( 0 );
491}
492
493static int x509_get_basic_constraints( unsigned char **p,
494 const unsigned char *end,
495 int *ca_istrue,
496 int *max_pathlen )
497{
Janos Follath865b3eb2019-12-16 11:46:15 +0000498 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499 size_t len;
500
501 /*
502 * BasicConstraints ::= SEQUENCE {
503 * cA BOOLEAN DEFAULT FALSE,
504 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
505 */
506 *ca_istrue = 0; /* DEFAULT FALSE */
507 *max_pathlen = 0; /* endless */
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
510 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100511 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512
513 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200514 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
519 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520
521 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100522 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523
524 if( *ca_istrue != 0 )
525 *ca_istrue = 1;
526 }
527
528 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200529 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100532 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533
534 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100535 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
536 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
Andrzej Kurek16050742020-04-14 09:49:52 -0400538 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
539 * overflow, which is an undefined behavior. */
540 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100541 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
542 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400543
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544 (*max_pathlen)++;
545
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200546 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547}
548
549static int x509_get_ns_cert_type( unsigned char **p,
550 const unsigned char *end,
551 unsigned char *ns_cert_type)
552{
Janos Follath865b3eb2019-12-16 11:46:15 +0000553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100557 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
559 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100560 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
561 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562
563 /* Get actual bitstring */
564 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200565 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566}
567
568static int x509_get_key_usage( unsigned char **p,
569 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100570 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571{
Janos Follath865b3eb2019-12-16 11:46:15 +0000572 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200573 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100577 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578
579 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100580 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
581 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582
583 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200584 *key_usage = 0;
585 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
586 {
587 *key_usage |= (unsigned int) bs.p[i] << (8*i);
588 }
589
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200590 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591}
592
593/*
594 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
595 *
596 * KeyPurposeId ::= OBJECT IDENTIFIER
597 */
598static int x509_get_ext_key_usage( unsigned char **p,
599 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200601{
Janos Follath865b3eb2019-12-16 11:46:15 +0000602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100605 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200606
607 /* Sequence length must be >= 1 */
608 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100609 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
610 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200612 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200613}
614
615/*
toth92g1bbc2fe2021-02-12 16:11:17 +0100616* SubjectKeyIdentifier ::= KeyIdentifier
617*
618* KeyIdentifier ::= OCTET STRING
619*/
toth92gdd123902021-04-08 10:12:52 +0200620static int x509_get_subject_key_id( unsigned char** p,
toth92g1bbc2fe2021-02-12 16:11:17 +0100621 const unsigned char* end,
toth92gdd123902021-04-08 10:12:52 +0200622 mbedtls_x509_buf* subject_key_id )
toth92g1bbc2fe2021-02-12 16:11:17 +0100623{
624 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
625 size_t len = 0u;
626
toth92g98fa4c92021-04-27 15:41:25 +0200627 if ( ( ret = mbedtls_asn1_get_tag( p, end, &len,
628 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100629 {
toth92gdd123902021-04-08 10:12:52 +0200630 return( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +0100631 }
632 else
633 {
634 subject_key_id->len = len;
635 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
636 subject_key_id->p = *p;
637 *p += len;
638 }
toth92gbf2dae12021-05-04 11:31:03 +0200639
640 if( *p != end )
641 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
642 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
toth92g1bbc2fe2021-02-12 16:11:17 +0100643
toth92gdd123902021-04-08 10:12:52 +0200644 return( 0 );
toth92g1bbc2fe2021-02-12 16:11:17 +0100645}
646
647/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
649 *
650 * GeneralName ::= CHOICE {
651 * otherName [0] OtherName,
652 * rfc822Name [1] IA5String,
653 * dNSName [2] IA5String,
654 * x400Address [3] ORAddress,
655 * directoryName [4] Name,
656 * ediPartyName [5] EDIPartyName,
657 * uniformResourceIdentifier [6] IA5String,
658 * iPAddress [7] OCTET STRING,
659 * registeredID [8] OBJECT IDENTIFIER }
660 *
661 * OtherName ::= SEQUENCE {
662 * type-id OBJECT IDENTIFIER,
663 * value [0] EXPLICIT ANY DEFINED BY type-id }
664 *
665 * EDIPartyName ::= SEQUENCE {
666 * nameAssigner [0] DirectoryString OPTIONAL,
667 * partyName [1] DirectoryString }
668 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300669 * NOTE: we list all types, but only use dNSName and otherName
670 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671 */
toth92gd2df7ec2021-05-10 15:16:33 +0200672static int x509_get_general_names( unsigned char **p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675{
Janos Follath865b3eb2019-12-16 11:46:15 +0000676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200679 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
682 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
684 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100685 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686
687 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100688 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
689 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690
691 while( *p < end )
692 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300693 mbedtls_x509_subject_alternative_name dummy_san_buf;
694 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
695
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696 tag = **p;
697 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100699 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700
toth92gd2df7ec2021-05-10 15:16:33 +0200701 /* Tag shall be CONTEXT_SPECIFIC or SET */
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100702 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
703 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
704 {
toth92gd2df7ec2021-05-10 15:16:33 +0200705 if( ( tag & (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
706 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
707 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100708 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709
Ron Eldordbbd9662019-05-15 15:46:03 +0300710 /*
irwir74aee1c2019-09-21 18:21:48 +0300711 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300712 */
toth92gd2df7ec2021-05-10 15:16:33 +0200713 ret = mbedtls_x509_parse_general_name( &(cur->buf), &dummy_san_buf );
Ron Eldordbbd9662019-05-15 15:46:03 +0300714 /*
715 * In case the extension is malformed, return an error,
716 * and clear the allocated sequences.
717 */
718 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
719 {
Glenn Straussa4b40412022-06-26 19:32:09 -0400720 mbedtls_asn1_sequence_free( subject_alt_name->next );
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300721 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300722 return( ret );
723 }
724
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200726 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100728 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100730
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200731 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200732
733 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100734 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
735 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737 cur = cur->next;
738 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200739
740 buf = &(cur->buf);
741 buf->tag = tag;
742 buf->p = *p;
743 buf->len = tag_len;
744 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745 }
746
747 /* Set final sequence entry's next pointer to NULL */
748 cur->next = NULL;
749
750 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100751 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
752 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753
754 return( 0 );
755}
756
757/*
toth92gd2df7ec2021-05-10 15:16:33 +0200758 * AuthorityKeyIdentifier ::= SEQUENCE {
759 * keyIdentifier [0] KeyIdentifier OPTIONAL,
760 * authorityCertIssuer [1] GeneralNames OPTIONAL,
761 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
762 *
763 * KeyIdentifier ::= OCTET STRING
764 */
765static int x509_get_authority_key_id( unsigned char** p,
766 unsigned char* end,
767 mbedtls_x509_authority* authority_key_id )
768{
769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
770 size_t len = 0u;
771
772 if ( ( ret = mbedtls_asn1_get_tag( p, end, &len,
773 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE | 0 ) ) != 0 )
774 {
775 return( ret );
776 }
777
778 if ( (ret = mbedtls_asn1_get_tag( p, end, &len,
779 MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
780 {
781 /* KeyIdentifier is an OPTIONAL field */
782 }
783 else
784 {
785 authority_key_id->keyIdentifier.len = len;
786 authority_key_id->keyIdentifier.p = *p;
787 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
788
789 *p += len;
790 }
791
792 if ( *p < end )
793 {
794 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
795 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) != 0)
796 {
797 /* authorityCertIssuer is an OPTIONAL field */
798 }
799 else
800 {
801 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
802 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 4 ) ) != 0)
803 {
804 return(ret);
805 }
806 else
807 {
808 /* "end" also includes the CertSerialNumber field so "len" shall be used */
809 ret = x509_get_general_names( p,
810 (*p+len),
811 &authority_key_id->authorityCertIssuer );
812 }
813 }
814 }
815
816 if ( *p < end )
817 {
818 if ( ( ret = mbedtls_asn1_get_tag( p, end, &len,
819 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_INTEGER ) ) != 0 )
820 {
821 /* authorityCertSerialNumber is an OPTIONAL field, but if there are still data it must be the serial number */
822 return( ret );
823 }
824 else
825 {
826 authority_key_id->authorityCertSerialNumber.len = len;
827 authority_key_id->authorityCertSerialNumber.p = *p;
828 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_OCTET_STRING;
829 *p += len;
830 }
831 }
832
833 if ( *p != end )
834 {
835 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
836 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
837 }
838
839 return( 0 );
840}
841
842/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200843 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
844 *
845 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
846 *
847 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
848 *
849 * PolicyInformation ::= SEQUENCE {
850 * policyIdentifier CertPolicyId,
851 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
852 * PolicyQualifierInfo OPTIONAL }
853 *
854 * CertPolicyId ::= OBJECT IDENTIFIER
855 *
856 * PolicyQualifierInfo ::= SEQUENCE {
857 * policyQualifierId PolicyQualifierId,
858 * qualifier ANY DEFINED BY policyQualifierId }
859 *
860 * -- policyQualifierIds for Internet policy qualifiers
861 *
862 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
863 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
864 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
865 *
866 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
867 *
868 * Qualifier ::= CHOICE {
869 * cPSuri CPSuri,
870 * userNotice UserNotice }
871 *
872 * CPSuri ::= IA5String
873 *
874 * UserNotice ::= SEQUENCE {
875 * noticeRef NoticeReference OPTIONAL,
876 * explicitText DisplayText OPTIONAL }
877 *
878 * NoticeReference ::= SEQUENCE {
879 * organization DisplayText,
880 * noticeNumbers SEQUENCE OF INTEGER }
881 *
882 * DisplayText ::= CHOICE {
883 * ia5String IA5String (SIZE (1..200)),
884 * visibleString VisibleString (SIZE (1..200)),
885 * bmpString BMPString (SIZE (1..200)),
886 * utf8String UTF8String (SIZE (1..200)) }
887 *
888 * NOTE: we only parse and use anyPolicy without qualifiers at this point
889 * as defined in RFC 5280.
890 */
891static int x509_get_certificate_policies( unsigned char **p,
892 const unsigned char *end,
893 mbedtls_x509_sequence *certificate_policies )
894{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300895 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200896 size_t len;
897 mbedtls_asn1_buf *buf;
898 mbedtls_asn1_sequence *cur = certificate_policies;
899
900 /* Get main sequence tag */
901 ret = mbedtls_asn1_get_tag( p, end, &len,
902 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
903 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100904 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200905
906 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100907 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
908 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200909
910 /*
911 * Cannot be an empty sequence.
912 */
913 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100914 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
915 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200916
917 while( *p < end )
918 {
919 mbedtls_x509_buf policy_oid;
920 const unsigned char *policy_end;
921
922 /*
923 * Get the policy sequence
924 */
925 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
926 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100927 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200928
929 policy_end = *p + len;
930
Ron Eldor08063792019-05-13 16:38:39 +0300931 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200932 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100933 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200934
935 policy_oid.tag = MBEDTLS_ASN1_OID;
936 policy_oid.len = len;
937 policy_oid.p = *p;
938
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300939 /*
940 * Only AnyPolicy is currently supported when enforcing policy.
941 */
942 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
943 {
944 /*
945 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200946 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300947 */
948 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
949 }
950
Ron Eldor74d9acc2019-03-21 14:00:03 +0200951 /* Allocate and assign next pointer */
952 if( cur->buf.p != NULL )
953 {
954 if( cur->next != NULL )
955 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
956
957 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
958
959 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100960 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
961 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200962
963 cur = cur->next;
964 }
965
966 buf = &( cur->buf );
967 buf->tag = policy_oid.tag;
968 buf->p = policy_oid.p;
969 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300970
971 *p += len;
972
973 /*
974 * If there is an optional qualifier, then *p < policy_end
975 * Check the Qualifier len to verify it doesn't exceed policy_end.
976 */
977 if( *p < policy_end )
978 {
979 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
980 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100981 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300982 /*
983 * Skip the optional policy qualifiers.
984 */
985 *p += len;
986 }
987
988 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100989 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
990 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200991 }
992
993 /* Set final sequence entry's next pointer to NULL */
994 cur->next = NULL;
995
996 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100997 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
998 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200999
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001000 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +02001001}
1002
1003/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004 * X.509 v3 extensions
1005 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006 */
1007static int x509_get_crt_ext( unsigned char **p,
1008 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001009 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001010 mbedtls_x509_crt_ext_cb_t cb,
1011 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012{
Janos Follath865b3eb2019-12-16 11:46:15 +00001013 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001015 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016
Hanno Becker12f62fb2019-02-12 17:22:36 +00001017 if( *p == end )
1018 return( 0 );
1019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001022
Hanno Becker12f62fb2019-02-12 17:22:36 +00001023 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024 while( *p < end )
1025 {
1026 /*
1027 * Extension ::= SEQUENCE {
1028 * extnID OBJECT IDENTIFIER,
1029 * critical BOOLEAN DEFAULT FALSE,
1030 * extnValue OCTET STRING }
1031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033 int is_critical = 0; /* DEFAULT FALSE */
1034 int ext_type = 0;
1035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
1037 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001038 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039
1040 end_ext_data = *p + len;
1041
1042 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +02001043 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +02001044 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001045 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046
k-stachowiak463928a2018-07-24 12:50:59 +02001047 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 extn_oid.p = *p;
1049 *p += extn_oid.len;
1050
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
1053 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +01001054 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055
1056 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
1058 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001059 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001061 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062 end_ext_octet = *p + len;
1063
1064 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +01001065 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1066 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067
1068 /*
1069 * Detect supported extensions
1070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072
1073 if( ret != 0 )
1074 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001075 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +02001076 if( cb != NULL )
1077 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001078 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +02001079 if( ret != 0 && is_critical )
1080 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001081 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001082 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001083 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001084
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085 /* No parser found, skip extension */
1086 *p = end_ext_octet;
1087
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088 if( is_critical )
1089 {
1090 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +01001091 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1092 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094 continue;
1095 }
1096
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001097 /* Forbid repeated extensions */
1098 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001100
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101 crt->ext_types |= ext_type;
1102
1103 switch( ext_type )
1104 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001105 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106 /* Parse basic constraints */
1107 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1108 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001109 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110 break;
1111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113 /* Parse key usage */
1114 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1115 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001116 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117 break;
1118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120 /* Parse extended key usage */
1121 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1122 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001123 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124 break;
toth92gd2df7ec2021-05-10 15:16:33 +02001125
toth92g1bbc2fe2021-02-12 16:11:17 +01001126 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
1127 /* Parse subject key identifier */
1128 if ( ( ret = x509_get_subject_key_id( p, end_ext_data,
1129 &crt->subject_key_id ) ) != 0 )
1130 {
1131 return ( ret );
1132 }
1133 break;
toth92gd2df7ec2021-05-10 15:16:33 +02001134
toth92g1bbc2fe2021-02-12 16:11:17 +01001135 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1136 /* Parse authority key identifier */
toth92gd2df7ec2021-05-10 15:16:33 +02001137 if ( ( ret = x509_get_authority_key_id( p, end_ext_octet,
1138 &crt->authority_key_id ) ) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +01001139 {
toth92gdd123902021-04-08 10:12:52 +02001140 return ( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +01001141 }
1142 break;
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001143 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92gd2df7ec2021-05-10 15:16:33 +02001144 /* Parse subject alt name
1145 * SubjectAltName ::= GeneralNames
1146 */
1147 if( ( ret = x509_get_general_names( p, end_ext_octet,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001149 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150 break;
1151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001153 /* Parse netscape certificate type */
1154 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1155 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001156 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 break;
1158
Ron Eldor74d9acc2019-03-21 14:00:03 +02001159 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1160 /* Parse certificate policies type */
1161 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1162 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001163 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001164 /* Give the callback (if any) a chance to handle the extension
1165 * if it contains unsupported policies */
1166 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1167 cb( p_ctx, crt, &extn_oid, is_critical,
1168 start_ext_octet, end_ext_octet ) == 0 )
1169 break;
1170
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001171 if( is_critical )
1172 return( ret );
1173 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001174 /*
Ron Eldora2913912019-05-16 16:17:38 +03001175 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1176 * cannot interpret or enforce the policy. However, it is up to
1177 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001178 * unless the extension is critical.
1179 */
1180 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1181 return( ret );
1182 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001183 break;
1184
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001185 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001186 /*
1187 * If this is a non-critical extension, which the oid layer
1188 * supports, but there isn't an x509 parser for it,
1189 * skip the extension.
1190 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001191 if( is_critical )
1192 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1193 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001194 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001195 }
1196 }
1197
1198 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001199 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1200 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001201
1202 return( 0 );
1203}
1204
1205/*
1206 * Parse and fill a single X.509 certificate in DER format
1207 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001208static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1209 const unsigned char *buf,
1210 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001211 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001212 mbedtls_x509_crt_ext_cb_t cb,
1213 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001214{
Janos Follath865b3eb2019-12-16 11:46:15 +00001215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001216 size_t len;
1217 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1221 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1222 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001223
1224 /*
1225 * Check for valid input
1226 */
1227 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001230 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001231 p = (unsigned char*) buf;
1232 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001233 end = p + len;
1234
1235 /*
1236 * Certificate ::= SEQUENCE {
1237 * tbsCertificate TBSCertificate,
1238 * signatureAlgorithm AlgorithmIdentifier,
1239 * signatureValue BIT STRING }
1240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1242 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 mbedtls_x509_crt_free( crt );
1245 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001246 }
1247
Janos Follathcc0e49d2016-02-17 14:34:12 +00001248 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001249 crt->raw.len = crt_end - buf;
1250 if( make_copy != 0 )
1251 {
1252 /* Create and populate a new buffer for the raw field. */
1253 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1254 if( crt->raw.p == NULL )
1255 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1256
1257 memcpy( crt->raw.p, buf, crt->raw.len );
1258 crt->own_buffer = 1;
1259
1260 p += crt->raw.len - len;
1261 end = crt_end = p + len;
1262 }
1263 else
1264 {
1265 crt->raw.p = (unsigned char*) buf;
1266 crt->own_buffer = 0;
1267 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001268
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001269 /*
1270 * TBSCertificate ::= SEQUENCE {
1271 */
1272 crt->tbs.p = p;
1273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1275 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001278 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001279 }
1280
1281 end = p + len;
1282 crt->tbs.len = end - crt->tbs.p;
1283
1284 /*
1285 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1286 *
1287 * CertificateSerialNumber ::= INTEGER
1288 *
1289 * signature AlgorithmIdentifier
1290 */
1291 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1293 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001294 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001297 return( ret );
1298 }
1299
Andres AG7ca4a032017-03-09 16:16:11 +00001300 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 mbedtls_x509_crt_free( crt );
1303 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 }
1305
Andres AG7ca4a032017-03-09 16:16:11 +00001306 crt->version++;
1307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001309 &crt->sig_md, &crt->sig_pk,
1310 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001313 return( ret );
1314 }
1315
1316 /*
1317 * issuer Name
1318 */
1319 crt->issuer_raw.p = p;
1320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1322 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001325 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001326 }
1327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001331 return( ret );
1332 }
1333
1334 crt->issuer_raw.len = p - crt->issuer_raw.p;
1335
1336 /*
1337 * Validity ::= SEQUENCE {
1338 * notBefore Time,
1339 * notAfter Time }
1340 *
1341 */
1342 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1343 &crt->valid_to ) ) != 0 )
1344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001346 return( ret );
1347 }
1348
1349 /*
1350 * subject Name
1351 */
1352 crt->subject_raw.p = p;
1353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1355 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001358 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001359 }
1360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364 return( ret );
1365 }
1366
1367 crt->subject_raw.len = p - crt->subject_raw.p;
1368
1369 /*
1370 * SubjectPublicKeyInfo
1371 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001372 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376 return( ret );
1377 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001378 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001379
1380 /*
1381 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1382 * -- If present, version shall be v2 or v3
1383 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1384 * -- If present, version shall be v2 or v3
1385 * extensions [3] EXPLICIT Extensions OPTIONAL
1386 * -- If present, version shall be v3
1387 */
1388 if( crt->version == 2 || crt->version == 3 )
1389 {
1390 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1391 if( ret != 0 )
1392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001394 return( ret );
1395 }
1396 }
1397
1398 if( crt->version == 2 || crt->version == 3 )
1399 {
1400 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1401 if( ret != 0 )
1402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001404 return( ret );
1405 }
1406 }
1407
1408 if( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001409 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001410 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001411 if( ret != 0 )
1412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001414 return( ret );
1415 }
1416 }
1417
1418 if( p != end )
1419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001421 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1422 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423 }
1424
1425 end = crt_end;
1426
1427 /*
1428 * }
1429 * -- end of TBSCertificate
1430 *
1431 * signatureAlgorithm AlgorithmIdentifier,
1432 * signatureValue BIT STRING
1433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001437 return( ret );
1438 }
1439
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001440 if( crt->sig_oid.len != sig_oid2.len ||
1441 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001442 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001443 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001444 ( sig_params1.len != 0 &&
1445 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 mbedtls_x509_crt_free( crt );
1448 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449 }
1450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454 return( ret );
1455 }
1456
1457 if( p != end )
1458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001460 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1461 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001462 }
1463
1464 return( 0 );
1465}
1466
1467/*
1468 * Parse one X.509 certificate in DER format from a buffer and add them to a
1469 * chained list
1470 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001471static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1472 const unsigned char *buf,
1473 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001474 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001475 mbedtls_x509_crt_ext_cb_t cb,
1476 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001477{
Janos Follath865b3eb2019-12-16 11:46:15 +00001478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480
1481 /*
1482 * Check for valid input
1483 */
1484 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486
1487 while( crt->version != 0 && crt->next != NULL )
1488 {
1489 prev = crt;
1490 crt = crt->next;
1491 }
1492
1493 /*
1494 * Add new certificate on the end of the chain if needed.
1495 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001496 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001497 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001498 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001499
1500 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001501 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001502
1503 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001505 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506 }
1507
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001508 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001509 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510 {
1511 if( prev )
1512 prev->next = NULL;
1513
1514 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516
1517 return( ret );
1518 }
1519
1520 return( 0 );
1521}
1522
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001523int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1524 const unsigned char *buf,
1525 size_t buflen )
1526{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001527 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001528}
1529
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001530int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1531 const unsigned char *buf,
1532 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001533 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001534 mbedtls_x509_crt_ext_cb_t cb,
1535 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001536{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001537 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001538}
1539
1540int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1541 const unsigned char *buf,
1542 size_t buflen )
1543{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001544 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001545}
1546
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001548 * Parse one or more PEM certificates from a buffer and add them to the chained
1549 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001550 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001551int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1552 const unsigned char *buf,
1553 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001554{
Janos Follath98e28a72016-05-31 14:03:54 +01001555#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001556 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001558#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559
1560 /*
1561 * Check for valid input
1562 */
1563 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001565
1566 /*
1567 * Determine buffer content. Buffer contains either one DER certificate or
1568 * one or more PEM certificates.
1569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001571 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001572 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001575 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1578 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001579#else
1580 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1581#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#if defined(MBEDTLS_PEM_PARSE_C)
1584 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001586 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001588
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001589 /* 1 rather than 0 since the terminating NULL byte is counted in */
1590 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591 {
1592 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001595 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597 "-----BEGIN CERTIFICATE-----",
1598 "-----END CERTIFICATE-----",
1599 buf, NULL, 0, &use_len );
1600
1601 if( ret == 0 )
1602 {
1603 /*
1604 * Was PEM encoded
1605 */
1606 buflen -= use_len;
1607 buf += use_len;
1608 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001610 {
1611 return( ret );
1612 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616
1617 /*
1618 * PEM header and footer were found
1619 */
1620 buflen -= use_len;
1621 buf += use_len;
1622
1623 if( first_error == 0 )
1624 first_error = ret;
1625
Paul Bakker5a5fa922014-09-26 14:53:04 +02001626 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001627 continue;
1628 }
1629 else
1630 break;
1631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635
1636 if( ret != 0 )
1637 {
1638 /*
1639 * Quit parsing on a memory error
1640 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001641 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001642 return( ret );
1643
1644 if( first_error == 0 )
1645 first_error = ret;
1646
1647 total_failed++;
1648 continue;
1649 }
1650
1651 success = 1;
1652 }
1653 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001654
1655 if( success )
1656 return( total_failed );
1657 else if( first_error )
1658 return( first_error );
1659 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001661#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001662}
1663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001665/*
1666 * Load one or more certificates and add them to the chained list
1667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001669{
Janos Follath865b3eb2019-12-16 11:46:15 +00001670 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671 size_t n;
1672 unsigned char *buf;
1673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675 return( ret );
1676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001679 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001681
1682 return( ret );
1683}
1684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001686{
1687 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001688#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001689 int w_ret;
1690 WCHAR szDir[MAX_PATH];
1691 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001692 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001693 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001694
Paul Bakker9af723c2014-05-01 13:03:14 +02001695 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001696 HANDLE hFind;
1697
1698 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001700
Paul Bakker9af723c2014-05-01 13:03:14 +02001701 memset( szDir, 0, sizeof(szDir) );
1702 memset( filename, 0, MAX_PATH );
1703 memcpy( filename, path, len );
1704 filename[len++] = '\\';
1705 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001706 filename[len++] = '*';
1707
Simon B3c6b18d2016-11-03 01:11:37 +00001708 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001709 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001710 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001712
1713 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001714 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001716
1717 len = MAX_PATH - len;
1718 do
1719 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001720 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721
1722 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1723 continue;
1724
Paul Bakker9af723c2014-05-01 13:03:14 +02001725 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001726 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001727 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001728 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001729 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001730 {
1731 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1732 goto cleanup;
1733 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736 if( w_ret < 0 )
1737 ret++;
1738 else
1739 ret += w_ret;
1740 }
1741 while( FindNextFileW( hFind, &file_data ) != 0 );
1742
Paul Bakker66d5d072014-06-17 16:39:18 +02001743 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745
Ron Eldor36d90422017-01-09 15:09:16 +02001746cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001747 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001748#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001749 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001750 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001751 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001752 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001753 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001754 DIR *dir = opendir( path );
1755
Paul Bakker66d5d072014-06-17 16:39:18 +02001756 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001758
Ron Eldor63140682017-01-09 19:27:59 +02001759#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001760 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001761 {
1762 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001763 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001764 }
Ron Eldor63140682017-01-09 19:27:59 +02001765#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001766
Paul Elliottfb91a482021-03-05 14:17:51 +00001767 memset( &sb, 0, sizeof( sb ) );
1768
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001769 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001770 {
Andres AGf9113192016-09-02 14:06:04 +01001771 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1772 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001773
Andres AGf9113192016-09-02 14:06:04 +01001774 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001775 {
Andres AGf9113192016-09-02 14:06:04 +01001776 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1777 goto cleanup;
1778 }
1779 else if( stat( entry_name, &sb ) == -1 )
1780 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001781 if( errno == ENOENT )
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001782 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001783 /* Broken symbolic link - ignore this entry.
1784 stat(2) will return this error for either (a) a dangling
1785 symlink or (b) a missing file.
1786 Given that we have just obtained the filename from readdir,
1787 assume that it does exist and therefore treat this as a
1788 dangling symlink. */
1789 continue;
1790 }
1791 else
1792 {
1793 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001794 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1795 goto cleanup;
1796 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001797 }
1798
1799 if( !S_ISREG( sb.st_mode ) )
1800 continue;
1801
1802 // Ignore parse errors
1803 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001805 if( t_ret < 0 )
1806 ret++;
1807 else
1808 ret += t_ret;
1809 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001810
1811cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001812 closedir( dir );
1813
Ron Eldor63140682017-01-09 19:27:59 +02001814#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001815 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001817#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001818
Paul Bakkerbe089b02013-10-14 15:51:50 +02001819#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001820
1821 return( ret );
1822}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001824
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001825/*
1826 * OtherName ::= SEQUENCE {
1827 * type-id OBJECT IDENTIFIER,
1828 * value [0] EXPLICIT ANY DEFINED BY type-id }
1829 *
1830 * HardwareModuleName ::= SEQUENCE {
1831 * hwType OBJECT IDENTIFIER,
1832 * hwSerialNum OCTET STRING }
1833 *
1834 * NOTE: we currently only parse and use otherName of type HwModuleName,
1835 * as defined in RFC 4108.
1836 */
1837static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1838 mbedtls_x509_san_other_name *other_name )
1839{
Janos Follathab23cd12019-05-09 13:53:57 +01001840 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001841 size_t len;
1842 unsigned char *p = subject_alt_name->p;
1843 const unsigned char *end = p + subject_alt_name->len;
1844 mbedtls_x509_buf cur_oid;
1845
1846 if( ( subject_alt_name->tag &
1847 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1848 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1849 {
1850 /*
1851 * The given subject alternative name is not of type "othername".
1852 */
1853 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1854 }
1855
1856 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1857 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001858 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001859
1860 cur_oid.tag = MBEDTLS_ASN1_OID;
1861 cur_oid.p = p;
1862 cur_oid.len = len;
1863
1864 /*
1865 * Only HwModuleName is currently supported.
1866 */
1867 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1868 {
1869 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1870 }
1871
Ron Eldor890819a2019-05-13 19:03:04 +03001872 if( p + len >= end )
1873 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001874 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001875 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1876 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001877 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001878 p += len;
1879 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1880 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001881 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001882
1883 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1884 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001885 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001886
1887 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001888 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001889
1890 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1891 other_name->value.hardware_module_name.oid.p = p;
1892 other_name->value.hardware_module_name.oid.len = len;
1893
Ron Eldor890819a2019-05-13 19:03:04 +03001894 if( p + len >= end )
1895 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001896 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001897 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1898 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001899 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001900 p += len;
1901 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1902 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001903 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001904
1905 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1906 other_name->value.hardware_module_name.val.p = p;
1907 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001908 p += len;
1909 if( p != end )
1910 {
Ron Eldor890819a2019-05-13 19:03:04 +03001911 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001912 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001913 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1914 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001915 }
1916 return( 0 );
1917}
1918
Peter Kolbus9a969b62018-12-11 13:55:56 -06001919int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1920 mbedtls_x509_subject_alternative_name *san )
1921{
1922 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1923 switch( san_buf->tag &
1924 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1925 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1926 {
1927 /*
1928 * otherName
1929 */
1930 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1931 {
1932 mbedtls_x509_san_other_name other_name;
1933
1934 ret = x509_get_other_name( san_buf, &other_name );
1935 if( ret != 0 )
1936 return( ret );
1937
1938 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1939 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1940 memcpy( &san->san.other_name,
1941 &other_name, sizeof( other_name ) );
1942
1943 }
1944 break;
1945
1946 /*
1947 * dNSName
1948 */
1949 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1950 {
1951 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1952 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1953
1954 memcpy( &san->san.unstructured_name,
1955 san_buf, sizeof( *san_buf ) );
1956
1957 }
1958 break;
1959
1960 /*
1961 * Type not supported
1962 */
1963 default:
1964 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1965 }
1966 return( 0 );
1967}
1968
1969#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001970static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001971 const mbedtls_x509_sequence
1972 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001973 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001974{
Janos Follath865b3eb2019-12-16 11:46:15 +00001975 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03001976 size_t i;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001977 size_t n = *size;
1978 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001980 mbedtls_x509_subject_alternative_name san;
1981 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001982
1983 while( cur != NULL )
1984 {
Ron Eldor890819a2019-05-13 19:03:04 +03001985 memset( &san, 0, sizeof( san ) );
toth92gd2df7ec2021-05-10 15:16:33 +02001986 parse_ret = mbedtls_x509_parse_general_name( &cur->buf, &san );
Ron Eldor890819a2019-05-13 19:03:04 +03001987 if( parse_ret != 0 )
1988 {
1989 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1990 {
1991 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1992 MBEDTLS_X509_SAFE_SNPRINTF;
1993 }
1994 else
1995 {
1996 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1997 MBEDTLS_X509_SAFE_SNPRINTF;
1998 }
1999 cur = cur->next;
2000 continue;
2001 }
2002
2003 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002004 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002005 /*
2006 * otherName
2007 */
Ron Eldora2913912019-05-16 16:17:38 +03002008 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03002009 {
2010 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
2011
2012 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
2013 MBEDTLS_X509_SAFE_SNPRINTF;
2014
2015 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
2016 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01002017 {
Ron Eldor890819a2019-05-13 19:03:04 +03002018 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
2019 MBEDTLS_X509_SAFE_SNPRINTF;
2020 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01002021 MBEDTLS_X509_SAFE_SNPRINTF;
2022
Ron Eldor890819a2019-05-13 19:03:04 +03002023 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
2024 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01002025
Ron Eldor890819a2019-05-13 19:03:04 +03002026 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
2027 MBEDTLS_X509_SAFE_SNPRINTF;
2028
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03002029 for( i = 0; i < other_name->value.hardware_module_name.val.len; i++ )
Ron Eldor890819a2019-05-13 19:03:04 +03002030 {
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03002031 ret = mbedtls_snprintf( p, n, "%02X", other_name->value.hardware_module_name.val.p[i] );
2032 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath22f605f2019-05-10 10:37:17 +01002033 }
Ron Eldor890819a2019-05-13 19:03:04 +03002034 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
2035 }
2036 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002037
2038 /*
2039 * dNSName
2040 */
Ron Eldora2913912019-05-16 16:17:38 +03002041 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03002042 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002043 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
2044 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03002045 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002046 {
2047 *p = '\0';
2048 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
2049 }
Ron Eldora2913912019-05-16 16:17:38 +03002050
2051 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
2052 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03002053 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03002054 }
2055 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002056
2057 /*
Ron Eldor890819a2019-05-13 19:03:04 +03002058 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002059 */
2060 default:
Janos Follath22f605f2019-05-10 10:37:17 +01002061 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
2062 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002063 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002064 }
2065
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002066 cur = cur->next;
2067 }
2068
2069 *p = '\0';
2070
2071 *size = n;
2072 *buf = p;
2073
2074 return( 0 );
2075}
2076
toth92gd2df7ec2021-05-10 15:16:33 +02002077int mbedtls_x509_parse_general_name( const mbedtls_x509_buf *san_buf,
2078 mbedtls_x509_subject_alternative_name *san )
2079{
2080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2081 switch( san_buf->tag &
2082 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
2083 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
2084 {
2085 /*
2086 * otherName
2087 */
2088 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
2089 {
2090 mbedtls_x509_san_other_name other_name;
2091
2092 ret = x509_get_other_name( san_buf, &other_name );
2093 if( ret != 0 )
2094 return( ret );
2095
2096 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
2097 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
2098 memcpy( &san->san.other_name,
2099 &other_name, sizeof( other_name ) );
2100
2101 }
2102 break;
2103
2104 /*
2105 * dNSName
2106 */
2107 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
2108 {
2109 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
2110 san->type = MBEDTLS_X509_SAN_DNS_NAME;
2111
2112 memcpy( &san->san.unstructured_name,
2113 san_buf, sizeof( *san_buf ) );
2114
2115 }
2116 break;
2117
2118 /*
2119 * RFC822 Name
2120 */
2121 case( MBEDTLS_ASN1_SEQUENCE | MBEDTLS_X509_SAN_RFC822_NAME ):
2122 {
2123 mbedtls_x509_name rfc822Name;
2124 unsigned char* bufferPointer = san_buf->p;
2125 unsigned char** p = &bufferPointer;
2126 const unsigned char* end = san_buf->p + san_buf->len;
2127
2128 /* The leading ASN1 tag and length has been processed. Stepping back with 2 bytes, because mbedtls_x509_get_name expects the beginning of the SET tag */
2129 *p = *p - 2;
2130
2131 ret = mbedtls_x509_get_name( p, end, &rfc822Name );
2132 if ( ret != 0 )
2133 return( ret );
2134
2135 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
2136 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
2137 memcpy( &san->san.unstructured_name,
2138 &rfc822Name, sizeof( rfc822Name ) );
2139 }
2140 break;
2141
2142 /*
2143 * Type not supported
2144 */
2145 default:
2146 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
2147 }
2148 return( 0 );
2149}
2150
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002151#define PRINT_ITEM(i) \
2152 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002154 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002155 sep = ", "; \
2156 }
2157
2158#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01002159 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002160 PRINT_ITEM( name );
2161
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002162static int x509_info_cert_type( char **buf, size_t *size,
2163 unsigned char ns_cert_type )
2164{
Janos Follath865b3eb2019-12-16 11:46:15 +00002165 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002166 size_t n = *size;
2167 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002168 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002171 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
2173 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
2174 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002175 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
2176 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
2177 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002178
2179 *size = n;
2180 *buf = p;
2181
2182 return( 0 );
2183}
2184
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002185#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01002186 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002187 PRINT_ITEM( name );
2188
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002189static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002190 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002191{
Janos Follath865b3eb2019-12-16 11:46:15 +00002192 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002193 size_t n = *size;
2194 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002195 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2198 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002199 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2200 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2201 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2203 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002204 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2205 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002206
2207 *size = n;
2208 *buf = p;
2209
2210 return( 0 );
2211}
2212
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002213static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002215{
Janos Follath865b3eb2019-12-16 11:46:15 +00002216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002217 const char *desc;
2218 size_t n = *size;
2219 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002221 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002222
2223 while( cur != NULL )
2224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002226 desc = "???";
2227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002229 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002230
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002231 sep = ", ";
2232
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002233 cur = cur->next;
2234 }
2235
2236 *size = n;
2237 *buf = p;
2238
2239 return( 0 );
2240}
2241
Ron Eldor74d9acc2019-03-21 14:00:03 +02002242static int x509_info_cert_policies( char **buf, size_t *size,
2243 const mbedtls_x509_sequence *certificate_policies )
2244{
Janos Follath865b3eb2019-12-16 11:46:15 +00002245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002246 const char *desc;
2247 size_t n = *size;
2248 char *p = *buf;
2249 const mbedtls_x509_sequence *cur = certificate_policies;
2250 const char *sep = "";
2251
2252 while( cur != NULL )
2253 {
2254 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2255 desc = "???";
2256
2257 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2258 MBEDTLS_X509_SAFE_SNPRINTF;
2259
2260 sep = ", ";
2261
2262 cur = cur->next;
2263 }
2264
2265 *size = n;
2266 *buf = p;
2267
2268 return( 0 );
2269}
2270
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002271/*
2272 * Return an informational string about the certificate.
2273 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002274#define BEFORE_COLON 18
2275#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2277 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002278{
Janos Follath865b3eb2019-12-16 11:46:15 +00002279 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002280 size_t n;
2281 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002282 char key_size_str[BEFORE_COLON];
2283
2284 p = buf;
2285 n = size;
2286
Janos Follath98e28a72016-05-31 14:03:54 +01002287 if( NULL == crt )
2288 {
2289 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2290 MBEDTLS_X509_SAFE_SNPRINTF;
2291
2292 return( (int) ( size - n ) );
2293 }
2294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002296 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002297 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002299 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002300 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002303 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002306 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002308 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002311 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002313 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002316 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2317 crt->valid_from.year, crt->valid_from.mon,
2318 crt->valid_from.day, crt->valid_from.hour,
2319 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002320 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002323 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2324 crt->valid_to.year, crt->valid_to.mon,
2325 crt->valid_to.day, crt->valid_to.hour,
2326 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002327 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002330 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002333 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002334 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002335
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002336 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2338 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002339 {
2340 return( ret );
2341 }
2342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002344 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002345 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002346
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002347 /*
2348 * Optional extensions
2349 */
2350
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002351 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002354 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002355 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002356
2357 if( crt->max_pathlen > 0 )
2358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002360 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002361 }
2362 }
2363
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002364 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002365 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002366 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002367 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002368
2369 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002370 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002371 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002372 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002373 }
2374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002378 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002379
2380 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2381 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002382 }
2383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002387 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002388
2389 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2390 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002391 }
2392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002396 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002397
2398 if( ( ret = x509_info_ext_key_usage( &p, &n,
2399 &crt->ext_key_usage ) ) != 0 )
2400 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002401 }
2402
Ron Eldor74d9acc2019-03-21 14:00:03 +02002403 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2404 {
2405 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2406 MBEDTLS_X509_SAFE_SNPRINTF;
2407
2408 if( ( ret = x509_info_cert_policies( &p, &n,
2409 &crt->certificate_policies ) ) != 0 )
2410 return( ret );
2411 }
2412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002414 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002415
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002416 return( (int) ( size - n ) );
2417}
2418
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002419struct x509_crt_verify_string {
2420 int code;
2421 const char *string;
2422};
2423
Hanno Becker7ac83f92020-10-09 10:48:22 +01002424#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002425static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002426 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002427 { 0, NULL }
2428};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002429#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002430
2431int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002432 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002433{
Janos Follath865b3eb2019-12-16 11:46:15 +00002434 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002435 const struct x509_crt_verify_string *cur;
2436 char *p = buf;
2437 size_t n = size;
2438
2439 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2440 {
2441 if( ( flags & cur->code ) == 0 )
2442 continue;
2443
2444 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002445 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002446 flags ^= cur->code;
2447 }
2448
2449 if( flags != 0 )
2450 {
2451 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2452 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002453 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002454 }
2455
2456 return( (int) ( size - n ) );
2457}
Hanno Becker612a2f12020-10-09 09:19:39 +01002458#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002459
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002460int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2461 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002462{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002463 unsigned int usage_must, usage_may;
2464 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2465 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2466
2467 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2468 return( 0 );
2469
2470 usage_must = usage & ~may_mask;
2471
2472 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2473 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2474
2475 usage_may = usage & may_mask;
2476
2477 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002479
2480 return( 0 );
2481}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002484 const char *usage_oid,
2485 size_t usage_len )
2486{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002488
2489 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002491 return( 0 );
2492
2493 /*
2494 * Look for the requested usage (or wildcard ANY) in our list
2495 */
2496 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002499
2500 if( cur_oid->len == usage_len &&
2501 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2502 {
2503 return( 0 );
2504 }
2505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002507 return( 0 );
2508 }
2509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002511}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002514/*
2515 * Return 1 if the certificate is revoked, or 0 otherwise.
2516 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002517int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002518{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002520
2521 while( cur != NULL && cur->serial.len != 0 )
2522 {
2523 if( crt->serial.len == cur->serial.len &&
2524 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2525 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002526 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002527 }
2528
2529 cur = cur->next;
2530 }
2531
2532 return( 0 );
2533}
2534
2535/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002536 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002537 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002540 mbedtls_x509_crl *crl_list,
2541 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002542{
2543 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002544 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002545#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002546 psa_algorithm_t psa_algorithm;
2547#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002549#endif /* MBEDTLS_USE_PSA_CRYPTO */
2550 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002551
2552 if( ca == NULL )
2553 return( flags );
2554
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002555 while( crl_list != NULL )
2556 {
2557 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002558 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002559 {
2560 crl_list = crl_list->next;
2561 continue;
2562 }
2563
2564 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002565 * Check if the CA is configured to sign CRLs
2566 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002567 if( mbedtls_x509_crt_check_key_usage( ca,
2568 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002571 break;
2572 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002573
2574 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002575 * Check if CRL is correctly signed by the trusted CA
2576 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002577 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2578 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2579
2580 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2581 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2582
pespacek7599a772022-02-07 14:40:55 +01002583#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002584 psa_algorithm = mbedtls_hash_info_psa_from_md( crl_list->sig_md );
pespacekd924e552022-02-28 11:49:54 +01002585 if( psa_hash_compute( psa_algorithm,
2586 crl_list->tbs.p,
2587 crl_list->tbs.len,
2588 hash,
2589 sizeof( hash ),
2590 &hash_length ) != PSA_SUCCESS )
pespaceka7a64692022-02-14 15:18:43 +01002591 {
2592 /* Note: this can't happen except after an internal error */
2593 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2594 break;
2595 }
pespacek7599a772022-02-07 14:40:55 +01002596#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002598 hash_length = mbedtls_md_get_size( md_info );
2599 if( mbedtls_md( md_info,
2600 crl_list->tbs.p,
2601 crl_list->tbs.len,
2602 hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002603 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002604 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002606 break;
2607 }
pespaceka7a64692022-02-14 15:18:43 +01002608#endif /* MBEDTLS_USE_PSA_CRYPTO */
2609
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002610 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002611 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
pespacek7599a772022-02-07 14:40:55 +01002614 crl_list->sig_md, hash, hash_length,
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002615 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002618 break;
2619 }
2620
2621 /*
2622 * Check for validity of CRL (Do not drop out)
2623 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002624 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002626
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002627 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002628 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002629
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002630 /*
2631 * Check if certificate is revoked
2632 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002633 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002636 break;
2637 }
2638
2639 crl_list = crl_list->next;
2640 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002641
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002642 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002643}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002645
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002646/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002647 * Check the signature of a certificate by its parent
2648 */
2649static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002650 mbedtls_x509_crt *parent,
2651 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002652{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002653 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02002654 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002655#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2656 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002657 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002658 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002659
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002660 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002661 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002662 return( -1 );
2663#else
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002664 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( child->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002665 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002666
pespacek7599a772022-02-07 14:40:55 +01002667 status = psa_hash_compute( hash_alg,
2668 child->tbs.p,
2669 child->tbs.len,
2670 hash,
pespacekb9f07a72022-02-14 15:13:26 +01002671 sizeof( hash ),
pespacek7599a772022-02-07 14:40:55 +01002672 &hash_len );
2673 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002674 {
pespacek3110c7b2022-02-14 15:07:41 +01002675 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002676 }
2677
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002678#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002679 /* Skip expensive computation on obvious mismatch */
2680 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002681 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002682
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002683#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002684 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2685 {
2686 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002687 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002688 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002689 }
2690#else
2691 (void) rs_ctx;
2692#endif
2693
2694 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002695 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002696 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002697}
2698
2699/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002700 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2701 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002702 *
2703 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2706 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002707 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002708{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002709 int need_ca_bit;
2710
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002711 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002712 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002713 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002714
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002715 /* Parent must have the basicConstraints CA bit set as a general rule */
2716 need_ca_bit = 1;
2717
2718 /* Exception: v1/v2 certificates that are locally trusted. */
2719 if( top && parent->version < 3 )
2720 need_ca_bit = 0;
2721
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002722 if( need_ca_bit && ! parent->ca_istrue )
2723 return( -1 );
2724
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002725 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002727 {
2728 return( -1 );
2729 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002730
2731 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002732}
2733
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002734/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002735 * Find a suitable parent for child in candidates, or return NULL.
2736 *
2737 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002738 * 1. subject name matches child's issuer
2739 * 2. if necessary, the CA bit is set and key usage allows signing certs
2740 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002741 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002742 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002743 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002744 * If there's a suitable candidate which is also time-valid, return the first
2745 * such. Otherwise, return the first suitable candidate (or NULL if there is
2746 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002747 *
2748 * The rationale for this rule is that someone could have a list of trusted
2749 * roots with two versions on the same root with different validity periods.
2750 * (At least one user reported having such a list and wanted it to just work.)
2751 * The reason we don't just require time-validity is that generally there is
2752 * only one version, and if it's expired we want the flags to state that
2753 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002754 *
2755 * The rationale for rule 3 (signature for trusted roots) is that users might
2756 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002757 * way we select the correct one is by checking the signature (as we don't
2758 * rely on key identifier extensions). (This is one way users might choose to
2759 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002760 *
2761 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002762 * - [in] child: certificate for which we're looking for a parent
2763 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002764 * - [out] r_parent: parent found (or NULL)
2765 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002766 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2767 * of the chain, 0 otherwise
2768 * - [in] path_cnt: number of intermediates seen so far
2769 * - [in] self_cnt: number of self-signed intermediates seen so far
2770 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002771 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002772 *
2773 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002774 * - 0 on success
2775 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002776 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002777static int x509_crt_find_parent_in(
2778 mbedtls_x509_crt *child,
2779 mbedtls_x509_crt *candidates,
2780 mbedtls_x509_crt **r_parent,
2781 int *r_signature_is_good,
2782 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002783 unsigned path_cnt,
2784 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002785 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002786{
Janos Follath865b3eb2019-12-16 11:46:15 +00002787 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002788 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002789 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002790
2791#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002792 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002793 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2794 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002795 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002796 parent = rs_ctx->parent;
2797 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002798 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002799
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002800 /* clear saved state */
2801 rs_ctx->parent = NULL;
2802 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002803 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002804
2805 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002806 goto check_signature;
2807 }
2808#endif
2809
2810 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002811 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002812
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002813 for( parent = candidates; parent != NULL; parent = parent->next )
2814 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002815 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002816 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002817 continue;
2818
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002819 /* +1 because stored max_pathlen is 1 higher that the actual value */
2820 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002821 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002822 {
2823 continue;
2824 }
2825
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002826 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002827#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2828check_signature:
2829#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002830 ret = x509_crt_check_signature( child, parent, rs_ctx );
2831
2832#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002833 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2834 {
2835 /* save state */
2836 rs_ctx->parent = parent;
2837 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002838 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002839
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002840 return( ret );
2841 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002842#else
2843 (void) ret;
2844#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002845
2846 signature_is_good = ret == 0;
2847 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002848 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002849
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002850 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002851 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2852 mbedtls_x509_time_is_future( &parent->valid_from ) )
2853 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002854 if( fallback_parent == NULL )
2855 {
2856 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002857 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002858 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002859
2860 continue;
2861 }
2862
Andy Gross1f627142019-01-30 10:25:53 -06002863 *r_parent = parent;
2864 *r_signature_is_good = signature_is_good;
2865
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002866 break;
2867 }
2868
Andy Gross1f627142019-01-30 10:25:53 -06002869 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002870 {
2871 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002872 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002873 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002874
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002875 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002876}
2877
2878/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002879 * Find a parent in trusted CAs or the provided chain, or return NULL.
2880 *
2881 * Searches in trusted CAs first, and return the first suitable parent found
2882 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002883 *
2884 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002885 * - [in] child: certificate for which we're looking for a parent, followed
2886 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002887 * - [in] trust_ca: list of locally trusted certificates
2888 * - [out] parent: parent found (or NULL)
2889 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2890 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2891 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2892 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002893 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002894 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002895 *
2896 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002897 * - 0 on success
2898 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002899 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002900static int x509_crt_find_parent(
2901 mbedtls_x509_crt *child,
2902 mbedtls_x509_crt *trust_ca,
2903 mbedtls_x509_crt **parent,
2904 int *parent_is_trusted,
2905 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002906 unsigned path_cnt,
2907 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002908 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002909{
Janos Follath865b3eb2019-12-16 11:46:15 +00002910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002911 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002912
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002913 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002914
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002915#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002916 /* restore then clear saved state if we have some stored */
2917 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2918 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002919 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002920 rs_ctx->parent_is_trusted = -1;
2921 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002922#endif
2923
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002924 while( 1 ) {
2925 search_list = *parent_is_trusted ? trust_ca : child->next;
2926
2927 ret = x509_crt_find_parent_in( child, search_list,
2928 parent, signature_is_good,
2929 *parent_is_trusted,
2930 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002931
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002932#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002933 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2934 {
2935 /* save state */
2936 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002937 return( ret );
2938 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002939#else
2940 (void) ret;
2941#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002942
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002943 /* stop here if found or already in second iteration */
2944 if( *parent != NULL || *parent_is_trusted == 0 )
2945 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002946
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002947 /* prepare second iteration */
2948 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002949 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002950
2951 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002952 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002953 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002954 *parent_is_trusted = 0;
2955 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002956 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002957
2958 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002959}
2960
2961/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002962 * Check if an end-entity certificate is locally trusted
2963 *
2964 * Currently we require such certificates to be self-signed (actually only
2965 * check for self-issued as self-signatures are not checked)
2966 */
2967static int x509_crt_check_ee_locally_trusted(
2968 mbedtls_x509_crt *crt,
2969 mbedtls_x509_crt *trust_ca )
2970{
2971 mbedtls_x509_crt *cur;
2972
2973 /* must be self-issued */
2974 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2975 return( -1 );
2976
2977 /* look for an exact match with trusted cert */
2978 for( cur = trust_ca; cur != NULL; cur = cur->next )
2979 {
2980 if( crt->raw.len == cur->raw.len &&
2981 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2982 {
2983 return( 0 );
2984 }
2985 }
2986
2987 /* too bad */
2988 return( -1 );
2989}
2990
2991/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002992 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002993 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002994 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2995 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002996 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002997 * such that every cert in the chain is a child of the next one,
2998 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002999 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003000 * Verify that chain and return it with flags for all issues found.
3001 *
3002 * Special cases:
3003 * - EE == Rj -> return a one-element list containing it
3004 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
3005 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003006 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003007 * Tests for (aspects of) this function should include at least:
3008 * - trusted EE
3009 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01003010 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003011 * - if relevant: EE untrusted
3012 * - if relevant: EE -> intermediate, untrusted
3013 * with the aspect under test checked at each relevant level (EE, int, root).
3014 * For some aspects longer chains are required, but usually length 2 is
3015 * enough (but length 1 is not in general).
3016 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003017 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003018 * - [in] crt: the cert list EE, C1, ..., Cn
3019 * - [in] trust_ca: the trusted list R1, ..., Rp
3020 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003021 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003022 * Only valid when return value is 0, may contain garbage otherwise!
3023 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003024 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003025 *
3026 * Return value:
3027 * - non-zero if the chain could not be fully built and examined
3028 * - 0 is the chain was successfully built and examined,
3029 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003030 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003031static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003032 mbedtls_x509_crt *crt,
3033 mbedtls_x509_crt *trust_ca,
3034 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003035 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3036 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003037 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003038 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003039 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003040{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003041 /* Don't initialize any of those variables here, so that the compiler can
3042 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00003043 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003044 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003045 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003046 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02003047 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003048 int parent_is_trusted;
3049 int child_is_trusted;
3050 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003051 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00003052 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003053
3054#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3055 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003056 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003057 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003058 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003059 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003060 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003061
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003062 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003063 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003064 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003065 flags = &cur->flags;
3066
3067 goto find_parent;
3068 }
3069#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003070
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003071 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003072 self_cnt = 0;
3073 parent_is_trusted = 0;
3074 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003075
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003076 while( 1 ) {
3077 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003078 cur = &ver_chain->items[ver_chain->len];
3079 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003080 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003081 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003082 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003083
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003084 /* Check time-validity (all certificates) */
3085 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3086 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003087
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003088 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3089 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02003090
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003091 /* Stop here for trusted roots (but not for trusted EE certs) */
3092 if( child_is_trusted )
3093 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003094
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003095 /* Check signature algorithm: MD & PK algs */
3096 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3097 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003098
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003099 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3100 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003101
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003102 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003103 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003104 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3105 {
3106 return( 0 );
3107 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003108
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003109#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3110find_parent:
3111#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00003112
3113 /* Obtain list of potential trusted signers from CA callback,
3114 * or use statically provided list. */
3115#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3116 if( f_ca_cb != NULL )
3117 {
3118 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
3119 mbedtls_free( ver_chain->trust_ca_cb_result );
3120 ver_chain->trust_ca_cb_result = NULL;
3121
3122 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
3123 if( ret != 0 )
3124 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3125
3126 cur_trust_ca = ver_chain->trust_ca_cb_result;
3127 }
3128 else
3129#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3130 {
3131 ((void) f_ca_cb);
3132 ((void) p_ca_cb);
3133 cur_trust_ca = trust_ca;
3134 }
3135
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003136 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00003137 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003138 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003139 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003140
3141#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003142 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3143 {
3144 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003145 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003146 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003147 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003148
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003149 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003150 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003151#else
3152 (void) ret;
3153#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003154
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003155 /* No parent? We're done here */
3156 if( parent == NULL )
3157 {
3158 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3159 return( 0 );
3160 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003161
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003162 /* Count intermediate self-issued (not necessarily self-signed) certs.
3163 * These can occur with some strategies for key rollover, see [SIRO],
3164 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003165 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02003166 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
3167 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003168 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02003169 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003170
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003171 /* path_cnt is 0 for the first intermediate CA,
3172 * and if parent is trusted it's not an intermediate CA */
3173 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003174 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003175 {
3176 /* return immediately to avoid overflow the chain array */
3177 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3178 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003179
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003180 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003181 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003182 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3183
3184 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003185 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003186 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003189 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003190 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003191#else
3192 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003193#endif
3194
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003195 /* prepare for next iteration */
3196 child = parent;
3197 parent = NULL;
3198 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003199 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003200 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003201}
3202
3203/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003204 * Check for CN match
3205 */
3206static int x509_crt_check_cn( const mbedtls_x509_buf *name,
3207 const char *cn, size_t cn_len )
3208{
3209 /* try exact match */
3210 if( name->len == cn_len &&
3211 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3212 {
3213 return( 0 );
3214 }
3215
3216 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003217 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003218 {
3219 return( 0 );
3220 }
3221
3222 return( -1 );
3223}
3224
3225/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003226 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3227 */
3228static int x509_crt_check_san( const mbedtls_x509_buf *name,
3229 const char *cn, size_t cn_len )
3230{
3231 const unsigned char san_type = (unsigned char) name->tag &
3232 MBEDTLS_ASN1_TAG_VALUE_MASK;
3233
3234 /* dNSName */
3235 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3236 return( x509_crt_check_cn( name, cn, cn_len ) );
3237
3238 /* (We may handle other types here later.) */
3239
3240 /* Unrecognized type */
3241 return( -1 );
3242}
3243
3244/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003245 * Verify the requested CN - only call this if cn is not NULL!
3246 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003247static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003248 const char *cn,
3249 uint32_t *flags )
3250{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003251 const mbedtls_x509_name *name;
3252 const mbedtls_x509_sequence *cur;
3253 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003254
3255 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3256 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003257 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003258 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003259 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003260 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003261 }
3262
3263 if( cur == NULL )
3264 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3265 }
3266 else
3267 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003268 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003269 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003270 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3271 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003272 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003273 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003274 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003275 }
3276
3277 if( name == NULL )
3278 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3279 }
3280}
3281
3282/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003283 * Merge the flags for all certs in the chain, after calling callback
3284 */
3285static int x509_crt_merge_flags_with_cb(
3286 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003287 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003288 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3289 void *p_vrfy )
3290{
Janos Follath865b3eb2019-12-16 11:46:15 +00003291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003292 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003293 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003294 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003295
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003296 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003297 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003298 cur = &ver_chain->items[i-1];
3299 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003300
3301 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003302 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003303 return( ret );
3304
3305 *flags |= cur_flags;
3306 }
3307
3308 return( 0 );
3309}
3310
3311/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003312 * Verify the certificate validity, with profile, restartable version
3313 *
3314 * This function:
3315 * - checks the requested CN (if any)
3316 * - checks the type and size of the EE cert's key,
3317 * as that isn't done as part of chain building/verification currently
3318 * - builds and verifies the chain
3319 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003320 *
3321 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3322 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3323 * verification routine to search for trusted signers, and CRLs will
3324 * be disabled. Otherwise, `trust_ca` will be used as the static list
3325 * of trusted signers, and `ca_crl` will be use as the static list
3326 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003327 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003328static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003329 mbedtls_x509_crt *trust_ca,
3330 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003331 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3332 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003333 const mbedtls_x509_crt_profile *profile,
3334 const char *cn, uint32_t *flags,
3335 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3336 void *p_vrfy,
3337 mbedtls_x509_crt_restart_ctx *rs_ctx )
3338{
Janos Follath865b3eb2019-12-16 11:46:15 +00003339 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003340 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003341 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003342 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003343
3344 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003345 ee_flags = 0;
3346 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003347
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003348 if( profile == NULL )
3349 {
3350 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3351 goto exit;
3352 }
3353
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003354 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003355 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003356 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003357
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003358 /* Check the type and size of the key */
3359 pk_type = mbedtls_pk_get_type( &crt->pk );
3360
3361 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003362 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003363
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003364 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003365 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003366
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003367 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003368 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3369 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003370 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003371
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003372 if( ret != 0 )
3373 goto exit;
3374
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003375 /* Merge end-entity flags */
3376 ver_chain.items[0].flags |= ee_flags;
3377
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003378 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003379 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003380
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003381exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003382
3383#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3384 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3385 mbedtls_free( ver_chain.trust_ca_cb_result );
3386 ver_chain.trust_ca_cb_result = NULL;
3387#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3388
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003389#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3390 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3391 mbedtls_x509_crt_restart_free( rs_ctx );
3392#endif
3393
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003394 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3395 * the SSL module for authmode optional, but non-zero return from the
3396 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003397 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3398 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3399
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003400 if( ret != 0 )
3401 {
3402 *flags = (uint32_t) -1;
3403 return( ret );
3404 }
3405
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003406 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003408
3409 return( 0 );
3410}
3411
Hanno Becker3116fb32019-03-28 13:34:42 +00003412
3413/*
3414 * Verify the certificate validity (default profile, not restartable)
3415 */
3416int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3417 mbedtls_x509_crt *trust_ca,
3418 mbedtls_x509_crl *ca_crl,
3419 const char *cn, uint32_t *flags,
3420 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3421 void *p_vrfy )
3422{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003423 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003424 NULL, NULL,
3425 &mbedtls_x509_crt_profile_default,
3426 cn, flags,
3427 f_vrfy, p_vrfy, NULL ) );
3428}
3429
3430/*
3431 * Verify the certificate validity (user-chosen profile, not restartable)
3432 */
3433int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3434 mbedtls_x509_crt *trust_ca,
3435 mbedtls_x509_crl *ca_crl,
3436 const mbedtls_x509_crt_profile *profile,
3437 const char *cn, uint32_t *flags,
3438 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3439 void *p_vrfy )
3440{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003441 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003442 NULL, NULL,
3443 profile, cn, flags,
3444 f_vrfy, p_vrfy, NULL ) );
3445}
3446
3447#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3448/*
3449 * Verify the certificate validity (user-chosen profile, CA callback,
3450 * not restartable).
3451 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003452int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003453 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3454 void *p_ca_cb,
3455 const mbedtls_x509_crt_profile *profile,
3456 const char *cn, uint32_t *flags,
3457 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3458 void *p_vrfy )
3459{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003460 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003461 f_ca_cb, p_ca_cb,
3462 profile, cn, flags,
3463 f_vrfy, p_vrfy, NULL ) );
3464}
3465#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3466
3467int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3468 mbedtls_x509_crt *trust_ca,
3469 mbedtls_x509_crl *ca_crl,
3470 const mbedtls_x509_crt_profile *profile,
3471 const char *cn, uint32_t *flags,
3472 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3473 void *p_vrfy,
3474 mbedtls_x509_crt_restart_ctx *rs_ctx )
3475{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003476 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003477 NULL, NULL,
3478 profile, cn, flags,
3479 f_vrfy, p_vrfy, rs_ctx ) );
3480}
3481
3482
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003483/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003484 * Initialize a certificate chain
3485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003487{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003489}
3490
3491/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003492 * Unallocate all certificate data
3493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003495{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 mbedtls_x509_crt *cert_cur = crt;
3497 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003498
Glenn Straussa4b40412022-06-26 19:32:09 -04003499 while( cert_cur != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3504 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003505#endif
3506
Glenn Straussa4b40412022-06-26 19:32:09 -04003507 mbedtls_asn1_free_named_data_list_shallow( cert_cur->issuer.next );
3508 mbedtls_asn1_free_named_data_list_shallow( cert_cur->subject.next );
3509 mbedtls_asn1_sequence_free( cert_cur->ext_key_usage.next );
3510 mbedtls_asn1_sequence_free( cert_cur->subject_alt_names.next );
3511 mbedtls_asn1_sequence_free( cert_cur->certificate_policies.next );
Ron Eldor74d9acc2019-03-21 14:00:03 +02003512
toth92gd2df7ec2021-05-10 15:16:33 +02003513 seq_cur = cert_cur->authority_key_id.authorityCertIssuer.next;
3514 while ( seq_cur != NULL )
toth92g1bbc2fe2021-02-12 16:11:17 +01003515 {
toth92gd2df7ec2021-05-10 15:16:33 +02003516 seq_prv = seq_cur;
3517 seq_cur = seq_cur->next;
3518 mbedtls_platform_zeroize( seq_prv,
3519 sizeof( mbedtls_x509_sequence ) );
3520 mbedtls_free( seq_prv );
toth92g1bbc2fe2021-02-12 16:11:17 +01003521 }
3522
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003523 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003524 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003525 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003527 }
3528
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003529 cert_prv = cert_cur;
3530 cert_cur = cert_cur->next;
3531
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003532 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003533 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003535 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003536}
3537
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003538#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3539/*
3540 * Initialize a restart context
3541 */
3542void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3543{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003544 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003545
3546 ctx->parent = NULL;
3547 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003548 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003549
3550 ctx->parent_is_trusted = -1;
3551
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003552 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003553 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003554 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003555}
3556
3557/*
3558 * Free the components of a restart context
3559 */
3560void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3561{
3562 if( ctx == NULL )
3563 return;
3564
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003565 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003566 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003567}
3568#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570#endif /* MBEDTLS_X509_CRT_PARSE_C */