blob: 5395f455dfce3240307a5eddf6771f2958950e00 [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
toth92gdd123902021-04-08 10:12:52 +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 }
639
toth92gdd123902021-04-08 10:12:52 +0200640 return( 0 );
toth92g1bbc2fe2021-02-12 16:11:17 +0100641}
642
643/*
644 * AuthorityKeyIdentifier ::= SEQUENCE {
645 * keyIdentifier [0] KeyIdentifier OPTIONAL,
646 * authorityCertIssuer [1] GeneralNames OPTIONAL,
647 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
648 *
649 * KeyIdentifier ::= OCTET STRING
650 */
toth92gdd123902021-04-08 10:12:52 +0200651static int x509_get_authority_key_id( unsigned char** p,
toth92g1bbc2fe2021-02-12 16:11:17 +0100652 unsigned char* end,
toth92gdd123902021-04-08 10:12:52 +0200653 mbedtls_x509_authority* authority_key_id )
toth92g1bbc2fe2021-02-12 16:11:17 +0100654{
655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
656 size_t len = 0u;
657
toth92gdd123902021-04-08 10:12:52 +0200658 if ( (ret = mbedtls_asn1_get_tag(p, end, &len,
659 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100660 {
toth92gdd123902021-04-08 10:12:52 +0200661 return( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +0100662 }
663
toth92gdd123902021-04-08 10:12:52 +0200664 if ( (ret = mbedtls_asn1_get_tag(p, end, &len,
665 MBEDTLS_ASN1_CONTEXT_SPECIFIC)) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100666 {
667 /* KeyIdentifier is an OPTIONAL field */
668 }
669 else
670 {
671 authority_key_id->keyIdentifier.len = len;
672 authority_key_id->keyIdentifier.p = *p;
673 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
674
675 *p += len;
676 }
677
678 if ( *p < end )
679 {
toth92gdd123902021-04-08 10:12:52 +0200680 if ( (ret = mbedtls_asn1_get_tag(p, end, &len,
681 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_BOOLEAN)) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100682 {
683 /* authorityCertIssuer is an OPTIONAL field */
684 }
685 else
686 {
toth92gdd123902021-04-08 10:12:52 +0200687 if ( (ret = mbedtls_asn1_get_tag(p, end, &len,
688 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_OCTET_STRING)) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100689 {
toth92gdd123902021-04-08 10:12:52 +0200690 return( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +0100691 }
692 else
693 {
694 authority_key_id->raw.p = *p;
695
toth92gdd123902021-04-08 10:12:52 +0200696 if ( (ret = mbedtls_asn1_get_tag(p, end, &len,
697 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100698 {
toth92gdd123902021-04-08 10:12:52 +0200699 return( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +0100700 }
701
toth92gdd123902021-04-08 10:12:52 +0200702 if ( (ret = mbedtls_x509_get_name(p, *p + len, &authority_key_id->authorityCertIssuer)) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100703 {
toth92gdd123902021-04-08 10:12:52 +0200704 return( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +0100705 }
706
707 authority_key_id->raw.len = *p - authority_key_id->raw.p;
708 }
709 }
710 }
711
712 if ( *p < end )
713 {
toth92gdd123902021-04-08 10:12:52 +0200714 if ( (ret = mbedtls_asn1_get_tag(p, end, &len,
715 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_INTEGER)) != 0 )
toth92g1bbc2fe2021-02-12 16:11:17 +0100716 {
717 /* authorityCertSerialNumber is an OPTIONAL field, but if there are still data it must be the serial number */
toth92gdd123902021-04-08 10:12:52 +0200718 return( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +0100719 }
720 else
721 {
722 authority_key_id->authorityCertSerialNumber.len = len;
723 authority_key_id->authorityCertSerialNumber.p = *p;
724 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_OCTET_STRING;
725 *p += len;
726 }
727 }
728
toth92gdd123902021-04-08 10:12:52 +0200729 if ( *p != end )
toth92g1bbc2fe2021-02-12 16:11:17 +0100730 {
toth92gdd123902021-04-08 10:12:52 +0200731 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
732 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
toth92g1bbc2fe2021-02-12 16:11:17 +0100733 }
734
toth92gdd123902021-04-08 10:12:52 +0200735 return( 0 );
toth92g1bbc2fe2021-02-12 16:11:17 +0100736}
737
738/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 * SubjectAltName ::= GeneralNames
740 *
741 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
742 *
743 * GeneralName ::= CHOICE {
744 * otherName [0] OtherName,
745 * rfc822Name [1] IA5String,
746 * dNSName [2] IA5String,
747 * x400Address [3] ORAddress,
748 * directoryName [4] Name,
749 * ediPartyName [5] EDIPartyName,
750 * uniformResourceIdentifier [6] IA5String,
751 * iPAddress [7] OCTET STRING,
752 * registeredID [8] OBJECT IDENTIFIER }
753 *
754 * OtherName ::= SEQUENCE {
755 * type-id OBJECT IDENTIFIER,
756 * value [0] EXPLICIT ANY DEFINED BY type-id }
757 *
758 * EDIPartyName ::= SEQUENCE {
759 * nameAssigner [0] DirectoryString OPTIONAL,
760 * partyName [1] DirectoryString }
761 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300762 * NOTE: we list all types, but only use dNSName and otherName
763 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764 */
765static int x509_get_subject_alt_name( unsigned char **p,
766 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768{
Janos Follath865b3eb2019-12-16 11:46:15 +0000769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200770 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774
775 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
777 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100778 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779
780 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100781 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
782 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200783
784 while( *p < end )
785 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300786 mbedtls_x509_subject_alternative_name dummy_san_buf;
787 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
788
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789 tag = **p;
790 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100792 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100794 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
795 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
796 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100797 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
798 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100799 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800
Ron Eldordbbd9662019-05-15 15:46:03 +0300801 /*
irwir74aee1c2019-09-21 18:21:48 +0300802 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300803 */
804 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
805 /*
806 * In case the extension is malformed, return an error,
807 * and clear the allocated sequences.
808 */
809 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
810 {
Glenn Straussa4b40412022-06-26 19:32:09 -0400811 mbedtls_asn1_sequence_free( subject_alt_name->next );
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300812 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300813 return( ret );
814 }
815
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200817 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100819 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100821
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200822 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823
824 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100825 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
826 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200828 cur = cur->next;
829 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200830
831 buf = &(cur->buf);
832 buf->tag = tag;
833 buf->p = *p;
834 buf->len = tag_len;
835 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836 }
837
838 /* Set final sequence entry's next pointer to NULL */
839 cur->next = NULL;
840
841 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100842 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
843 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200844
845 return( 0 );
846}
847
848/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200849 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
850 *
851 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
852 *
853 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
854 *
855 * PolicyInformation ::= SEQUENCE {
856 * policyIdentifier CertPolicyId,
857 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
858 * PolicyQualifierInfo OPTIONAL }
859 *
860 * CertPolicyId ::= OBJECT IDENTIFIER
861 *
862 * PolicyQualifierInfo ::= SEQUENCE {
863 * policyQualifierId PolicyQualifierId,
864 * qualifier ANY DEFINED BY policyQualifierId }
865 *
866 * -- policyQualifierIds for Internet policy qualifiers
867 *
868 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
869 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
870 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
871 *
872 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
873 *
874 * Qualifier ::= CHOICE {
875 * cPSuri CPSuri,
876 * userNotice UserNotice }
877 *
878 * CPSuri ::= IA5String
879 *
880 * UserNotice ::= SEQUENCE {
881 * noticeRef NoticeReference OPTIONAL,
882 * explicitText DisplayText OPTIONAL }
883 *
884 * NoticeReference ::= SEQUENCE {
885 * organization DisplayText,
886 * noticeNumbers SEQUENCE OF INTEGER }
887 *
888 * DisplayText ::= CHOICE {
889 * ia5String IA5String (SIZE (1..200)),
890 * visibleString VisibleString (SIZE (1..200)),
891 * bmpString BMPString (SIZE (1..200)),
892 * utf8String UTF8String (SIZE (1..200)) }
893 *
894 * NOTE: we only parse and use anyPolicy without qualifiers at this point
895 * as defined in RFC 5280.
896 */
897static int x509_get_certificate_policies( unsigned char **p,
898 const unsigned char *end,
899 mbedtls_x509_sequence *certificate_policies )
900{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300901 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200902 size_t len;
903 mbedtls_asn1_buf *buf;
904 mbedtls_asn1_sequence *cur = certificate_policies;
905
906 /* Get main sequence tag */
907 ret = mbedtls_asn1_get_tag( p, end, &len,
908 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
909 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100910 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200911
912 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100913 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
914 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200915
916 /*
917 * Cannot be an empty sequence.
918 */
919 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100920 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
921 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200922
923 while( *p < end )
924 {
925 mbedtls_x509_buf policy_oid;
926 const unsigned char *policy_end;
927
928 /*
929 * Get the policy sequence
930 */
931 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
932 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 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_end = *p + len;
936
Ron Eldor08063792019-05-13 16:38:39 +0300937 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200938 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100939 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200940
941 policy_oid.tag = MBEDTLS_ASN1_OID;
942 policy_oid.len = len;
943 policy_oid.p = *p;
944
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300945 /*
946 * Only AnyPolicy is currently supported when enforcing policy.
947 */
948 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
949 {
950 /*
951 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200952 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300953 */
954 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
955 }
956
Ron Eldor74d9acc2019-03-21 14:00:03 +0200957 /* Allocate and assign next pointer */
958 if( cur->buf.p != NULL )
959 {
960 if( cur->next != NULL )
961 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
962
963 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
964
965 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100966 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
967 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200968
969 cur = cur->next;
970 }
971
972 buf = &( cur->buf );
973 buf->tag = policy_oid.tag;
974 buf->p = policy_oid.p;
975 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300976
977 *p += len;
978
979 /*
980 * If there is an optional qualifier, then *p < policy_end
981 * Check the Qualifier len to verify it doesn't exceed policy_end.
982 */
983 if( *p < policy_end )
984 {
985 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
986 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100987 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300988 /*
989 * Skip the optional policy qualifiers.
990 */
991 *p += len;
992 }
993
994 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100995 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
996 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200997 }
998
999 /* Set final sequence entry's next pointer to NULL */
1000 cur->next = NULL;
1001
1002 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001003 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1004 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +02001005
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001006 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +02001007}
1008
1009/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 * X.509 v3 extensions
1011 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012 */
1013static int x509_get_crt_ext( unsigned char **p,
1014 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001015 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001016 mbedtls_x509_crt_ext_cb_t cb,
1017 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018{
Janos Follath865b3eb2019-12-16 11:46:15 +00001019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001021 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001022
Hanno Becker12f62fb2019-02-12 17:22:36 +00001023 if( *p == end )
1024 return( 0 );
1025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028
Hanno Becker12f62fb2019-02-12 17:22:36 +00001029 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 while( *p < end )
1031 {
1032 /*
1033 * Extension ::= SEQUENCE {
1034 * extnID OBJECT IDENTIFIER,
1035 * critical BOOLEAN DEFAULT FALSE,
1036 * extnValue OCTET STRING }
1037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039 int is_critical = 0; /* DEFAULT FALSE */
1040 int ext_type = 0;
1041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
1043 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001044 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045
1046 end_ext_data = *p + len;
1047
1048 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +02001049 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +02001050 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001051 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001052
k-stachowiak463928a2018-07-24 12:50:59 +02001053 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001054 extn_oid.p = *p;
1055 *p += extn_oid.len;
1056
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001057 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
1059 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +01001060 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001061
1062 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
1064 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001065 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001066
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001067 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068 end_ext_octet = *p + len;
1069
1070 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +01001071 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1072 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001073
1074 /*
1075 * Detect supported extensions
1076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078
1079 if( ret != 0 )
1080 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001081 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +02001082 if( cb != NULL )
1083 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001084 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +02001085 if( ret != 0 && is_critical )
1086 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001087 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001088 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001089 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001090
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091 /* No parser found, skip extension */
1092 *p = end_ext_octet;
1093
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094 if( is_critical )
1095 {
1096 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +01001097 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1098 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100 continue;
1101 }
1102
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001103 /* Forbid repeated extensions */
1104 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001106
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001107 crt->ext_types |= ext_type;
1108
1109 switch( ext_type )
1110 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001111 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112 /* Parse basic constraints */
1113 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1114 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001115 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001116 break;
1117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119 /* Parse key usage */
1120 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1121 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001122 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123 break;
1124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001126 /* Parse extended key usage */
1127 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1128 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001129 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001130 break;
toth92g1bbc2fe2021-02-12 16:11:17 +01001131 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
1132 /* Parse subject key identifier */
1133 if ( ( ret = x509_get_subject_key_id( p, end_ext_data,
1134 &crt->subject_key_id ) ) != 0 )
1135 {
1136 return ( ret );
1137 }
1138 break;
1139 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1140 /* Parse authority key identifier */
1141 if ( (ret = x509_get_authority_key_id(p, end_ext_octet,
1142 &crt->authority_key_id)) != 0 )
1143 {
toth92gdd123902021-04-08 10:12:52 +02001144 return ( ret );
toth92g1bbc2fe2021-02-12 16:11:17 +01001145 }
1146 break;
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001147 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 /* Parse subject alt name */
1149 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1150 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001151 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152 break;
1153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155 /* Parse netscape certificate type */
1156 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1157 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001158 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001159 break;
1160
Ron Eldor74d9acc2019-03-21 14:00:03 +02001161 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1162 /* Parse certificate policies type */
1163 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1164 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001165 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001166 /* Give the callback (if any) a chance to handle the extension
1167 * if it contains unsupported policies */
1168 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1169 cb( p_ctx, crt, &extn_oid, is_critical,
1170 start_ext_octet, end_ext_octet ) == 0 )
1171 break;
1172
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001173 if( is_critical )
1174 return( ret );
1175 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001176 /*
Ron Eldora2913912019-05-16 16:17:38 +03001177 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1178 * cannot interpret or enforce the policy. However, it is up to
1179 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001180 * unless the extension is critical.
1181 */
1182 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1183 return( ret );
1184 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001185 break;
1186
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001187 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001188 /*
1189 * If this is a non-critical extension, which the oid layer
1190 * supports, but there isn't an x509 parser for it,
1191 * skip the extension.
1192 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001193 if( is_critical )
1194 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1195 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001196 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001197 }
1198 }
1199
1200 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001201 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1202 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203
1204 return( 0 );
1205}
1206
1207/*
1208 * Parse and fill a single X.509 certificate in DER format
1209 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001210static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1211 const unsigned char *buf,
1212 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001213 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001214 mbedtls_x509_crt_ext_cb_t cb,
1215 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001216{
Janos Follath865b3eb2019-12-16 11:46:15 +00001217 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001218 size_t len;
1219 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1223 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1224 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001225
1226 /*
1227 * Check for valid input
1228 */
1229 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001231
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001232 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001233 p = (unsigned char*) buf;
1234 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235 end = p + len;
1236
1237 /*
1238 * Certificate ::= SEQUENCE {
1239 * tbsCertificate TBSCertificate,
1240 * signatureAlgorithm AlgorithmIdentifier,
1241 * signatureValue BIT STRING }
1242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1244 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_x509_crt_free( crt );
1247 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001248 }
1249
Janos Follathcc0e49d2016-02-17 14:34:12 +00001250 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001251 crt->raw.len = crt_end - buf;
1252 if( make_copy != 0 )
1253 {
1254 /* Create and populate a new buffer for the raw field. */
1255 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1256 if( crt->raw.p == NULL )
1257 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1258
1259 memcpy( crt->raw.p, buf, crt->raw.len );
1260 crt->own_buffer = 1;
1261
1262 p += crt->raw.len - len;
1263 end = crt_end = p + len;
1264 }
1265 else
1266 {
1267 crt->raw.p = (unsigned char*) buf;
1268 crt->own_buffer = 0;
1269 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001270
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001271 /*
1272 * TBSCertificate ::= SEQUENCE {
1273 */
1274 crt->tbs.p = p;
1275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1277 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001280 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 }
1282
1283 end = p + len;
1284 crt->tbs.len = end - crt->tbs.p;
1285
1286 /*
1287 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1288 *
1289 * CertificateSerialNumber ::= INTEGER
1290 *
1291 * signature AlgorithmIdentifier
1292 */
1293 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1295 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001296 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001299 return( ret );
1300 }
1301
Andres AG7ca4a032017-03-09 16:16:11 +00001302 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 mbedtls_x509_crt_free( crt );
1305 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001306 }
1307
Andres AG7ca4a032017-03-09 16:16:11 +00001308 crt->version++;
1309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001311 &crt->sig_md, &crt->sig_pk,
1312 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001315 return( ret );
1316 }
1317
1318 /*
1319 * issuer Name
1320 */
1321 crt->issuer_raw.p = p;
1322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1324 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001327 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328 }
1329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001333 return( ret );
1334 }
1335
1336 crt->issuer_raw.len = p - crt->issuer_raw.p;
1337
1338 /*
1339 * Validity ::= SEQUENCE {
1340 * notBefore Time,
1341 * notAfter Time }
1342 *
1343 */
1344 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1345 &crt->valid_to ) ) != 0 )
1346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001348 return( ret );
1349 }
1350
1351 /*
1352 * subject Name
1353 */
1354 crt->subject_raw.p = p;
1355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1357 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001360 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361 }
1362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366 return( ret );
1367 }
1368
1369 crt->subject_raw.len = p - crt->subject_raw.p;
1370
1371 /*
1372 * SubjectPublicKeyInfo
1373 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001374 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378 return( ret );
1379 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001380 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001381
1382 /*
1383 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1384 * -- If present, version shall be v2 or v3
1385 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1386 * -- If present, version shall be v2 or v3
1387 * extensions [3] EXPLICIT Extensions OPTIONAL
1388 * -- If present, version shall be v3
1389 */
1390 if( crt->version == 2 || crt->version == 3 )
1391 {
1392 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1393 if( ret != 0 )
1394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001396 return( ret );
1397 }
1398 }
1399
1400 if( crt->version == 2 || crt->version == 3 )
1401 {
1402 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1403 if( ret != 0 )
1404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406 return( ret );
1407 }
1408 }
1409
1410 if( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001411 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001412 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001413 if( ret != 0 )
1414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001416 return( ret );
1417 }
1418 }
1419
1420 if( p != end )
1421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001423 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1424 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425 }
1426
1427 end = crt_end;
1428
1429 /*
1430 * }
1431 * -- end of TBSCertificate
1432 *
1433 * signatureAlgorithm AlgorithmIdentifier,
1434 * signatureValue BIT STRING
1435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439 return( ret );
1440 }
1441
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001442 if( crt->sig_oid.len != sig_oid2.len ||
1443 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001444 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001445 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001446 ( sig_params1.len != 0 &&
1447 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 mbedtls_x509_crt_free( crt );
1450 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451 }
1452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001456 return( ret );
1457 }
1458
1459 if( p != end )
1460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001462 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1463 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001464 }
1465
1466 return( 0 );
1467}
1468
1469/*
1470 * Parse one X.509 certificate in DER format from a buffer and add them to a
1471 * chained list
1472 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001473static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1474 const unsigned char *buf,
1475 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001476 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001477 mbedtls_x509_crt_ext_cb_t cb,
1478 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001479{
Janos Follath865b3eb2019-12-16 11:46:15 +00001480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482
1483 /*
1484 * Check for valid input
1485 */
1486 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488
1489 while( crt->version != 0 && crt->next != NULL )
1490 {
1491 prev = crt;
1492 crt = crt->next;
1493 }
1494
1495 /*
1496 * Add new certificate on the end of the chain if needed.
1497 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001498 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001499 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001500 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001501
1502 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001503 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504
1505 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508 }
1509
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001510 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001511 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512 {
1513 if( prev )
1514 prev->next = NULL;
1515
1516 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001518
1519 return( ret );
1520 }
1521
1522 return( 0 );
1523}
1524
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001525int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1526 const unsigned char *buf,
1527 size_t buflen )
1528{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001529 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001530}
1531
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001532int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1533 const unsigned char *buf,
1534 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001535 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001536 mbedtls_x509_crt_ext_cb_t cb,
1537 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001538{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001539 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001540}
1541
1542int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1543 const unsigned char *buf,
1544 size_t buflen )
1545{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001546 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001547}
1548
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001550 * Parse one or more PEM certificates from a buffer and add them to the chained
1551 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001553int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1554 const unsigned char *buf,
1555 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556{
Janos Follath98e28a72016-05-31 14:03:54 +01001557#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001558 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001560#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561
1562 /*
1563 * Check for valid input
1564 */
1565 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567
1568 /*
1569 * Determine buffer content. Buffer contains either one DER certificate or
1570 * one or more PEM certificates.
1571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001573 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001574 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001577 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1580 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001581#else
1582 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1583#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#if defined(MBEDTLS_PEM_PARSE_C)
1586 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001591 /* 1 rather than 0 since the terminating NULL byte is counted in */
1592 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593 {
1594 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001597 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599 "-----BEGIN CERTIFICATE-----",
1600 "-----END CERTIFICATE-----",
1601 buf, NULL, 0, &use_len );
1602
1603 if( ret == 0 )
1604 {
1605 /*
1606 * Was PEM encoded
1607 */
1608 buflen -= use_len;
1609 buf += use_len;
1610 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612 {
1613 return( ret );
1614 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001618
1619 /*
1620 * PEM header and footer were found
1621 */
1622 buflen -= use_len;
1623 buf += use_len;
1624
1625 if( first_error == 0 )
1626 first_error = ret;
1627
Paul Bakker5a5fa922014-09-26 14:53:04 +02001628 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001629 continue;
1630 }
1631 else
1632 break;
1633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637
1638 if( ret != 0 )
1639 {
1640 /*
1641 * Quit parsing on a memory error
1642 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001643 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644 return( ret );
1645
1646 if( first_error == 0 )
1647 first_error = ret;
1648
1649 total_failed++;
1650 continue;
1651 }
1652
1653 success = 1;
1654 }
1655 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001656
1657 if( success )
1658 return( total_failed );
1659 else if( first_error )
1660 return( first_error );
1661 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001663#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001664}
1665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001667/*
1668 * Load one or more certificates and add them to the chained list
1669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671{
Janos Follath865b3eb2019-12-16 11:46:15 +00001672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001673 size_t n;
1674 unsigned char *buf;
1675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001677 return( ret );
1678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001681 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001683
1684 return( ret );
1685}
1686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001688{
1689 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001690#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001691 int w_ret;
1692 WCHAR szDir[MAX_PATH];
1693 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001694 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001695 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001696
Paul Bakker9af723c2014-05-01 13:03:14 +02001697 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001698 HANDLE hFind;
1699
1700 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702
Paul Bakker9af723c2014-05-01 13:03:14 +02001703 memset( szDir, 0, sizeof(szDir) );
1704 memset( filename, 0, MAX_PATH );
1705 memcpy( filename, path, len );
1706 filename[len++] = '\\';
1707 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001708 filename[len++] = '*';
1709
Simon B3c6b18d2016-11-03 01:11:37 +00001710 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001711 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001712 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001714
1715 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001716 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001718
1719 len = MAX_PATH - len;
1720 do
1721 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001722 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001723
1724 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1725 continue;
1726
Paul Bakker9af723c2014-05-01 13:03:14 +02001727 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001728 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001729 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001730 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001731 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001732 {
1733 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1734 goto cleanup;
1735 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001738 if( w_ret < 0 )
1739 ret++;
1740 else
1741 ret += w_ret;
1742 }
1743 while( FindNextFileW( hFind, &file_data ) != 0 );
1744
Paul Bakker66d5d072014-06-17 16:39:18 +02001745 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001747
Ron Eldor36d90422017-01-09 15:09:16 +02001748cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001749 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001750#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001751 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001752 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001753 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001754 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001755 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001756 DIR *dir = opendir( path );
1757
Paul Bakker66d5d072014-06-17 16:39:18 +02001758 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001760
Ron Eldor63140682017-01-09 19:27:59 +02001761#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001762 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001763 {
1764 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001765 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001766 }
Ron Eldor63140682017-01-09 19:27:59 +02001767#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001768
Paul Elliottfb91a482021-03-05 14:17:51 +00001769 memset( &sb, 0, sizeof( sb ) );
1770
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001771 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001772 {
Andres AGf9113192016-09-02 14:06:04 +01001773 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1774 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001775
Andres AGf9113192016-09-02 14:06:04 +01001776 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001777 {
Andres AGf9113192016-09-02 14:06:04 +01001778 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1779 goto cleanup;
1780 }
1781 else if( stat( entry_name, &sb ) == -1 )
1782 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001783 if( errno == ENOENT )
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001784 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001785 /* Broken symbolic link - ignore this entry.
1786 stat(2) will return this error for either (a) a dangling
1787 symlink or (b) a missing file.
1788 Given that we have just obtained the filename from readdir,
1789 assume that it does exist and therefore treat this as a
1790 dangling symlink. */
1791 continue;
1792 }
1793 else
1794 {
1795 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001796 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1797 goto cleanup;
1798 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001799 }
1800
1801 if( !S_ISREG( sb.st_mode ) )
1802 continue;
1803
1804 // Ignore parse errors
1805 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001807 if( t_ret < 0 )
1808 ret++;
1809 else
1810 ret += t_ret;
1811 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001812
1813cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001814 closedir( dir );
1815
Ron Eldor63140682017-01-09 19:27:59 +02001816#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001817 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001819#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001820
Paul Bakkerbe089b02013-10-14 15:51:50 +02001821#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001822
1823 return( ret );
1824}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001826
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001827/*
1828 * OtherName ::= SEQUENCE {
1829 * type-id OBJECT IDENTIFIER,
1830 * value [0] EXPLICIT ANY DEFINED BY type-id }
1831 *
1832 * HardwareModuleName ::= SEQUENCE {
1833 * hwType OBJECT IDENTIFIER,
1834 * hwSerialNum OCTET STRING }
1835 *
1836 * NOTE: we currently only parse and use otherName of type HwModuleName,
1837 * as defined in RFC 4108.
1838 */
1839static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1840 mbedtls_x509_san_other_name *other_name )
1841{
Janos Follathab23cd12019-05-09 13:53:57 +01001842 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001843 size_t len;
1844 unsigned char *p = subject_alt_name->p;
1845 const unsigned char *end = p + subject_alt_name->len;
1846 mbedtls_x509_buf cur_oid;
1847
1848 if( ( subject_alt_name->tag &
1849 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1850 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1851 {
1852 /*
1853 * The given subject alternative name is not of type "othername".
1854 */
1855 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1856 }
1857
1858 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1859 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001860 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001861
1862 cur_oid.tag = MBEDTLS_ASN1_OID;
1863 cur_oid.p = p;
1864 cur_oid.len = len;
1865
1866 /*
1867 * Only HwModuleName is currently supported.
1868 */
1869 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1870 {
1871 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1872 }
1873
Ron Eldor890819a2019-05-13 19:03:04 +03001874 if( p + len >= end )
1875 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001876 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001877 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1878 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001879 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001880 p += len;
1881 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1882 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001883 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001884
1885 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1886 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001887 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001888
1889 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001890 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001891
1892 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1893 other_name->value.hardware_module_name.oid.p = p;
1894 other_name->value.hardware_module_name.oid.len = len;
1895
Ron Eldor890819a2019-05-13 19:03:04 +03001896 if( p + len >= end )
1897 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001898 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001899 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1900 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001901 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001902 p += len;
1903 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1904 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001905 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001906
1907 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1908 other_name->value.hardware_module_name.val.p = p;
1909 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001910 p += len;
1911 if( p != end )
1912 {
Ron Eldor890819a2019-05-13 19:03:04 +03001913 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001914 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001915 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1916 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001917 }
1918 return( 0 );
1919}
1920
Peter Kolbus9a969b62018-12-11 13:55:56 -06001921int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1922 mbedtls_x509_subject_alternative_name *san )
1923{
1924 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1925 switch( san_buf->tag &
1926 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1927 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1928 {
1929 /*
1930 * otherName
1931 */
1932 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1933 {
1934 mbedtls_x509_san_other_name other_name;
1935
1936 ret = x509_get_other_name( san_buf, &other_name );
1937 if( ret != 0 )
1938 return( ret );
1939
1940 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1941 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1942 memcpy( &san->san.other_name,
1943 &other_name, sizeof( other_name ) );
1944
1945 }
1946 break;
1947
1948 /*
1949 * dNSName
1950 */
1951 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1952 {
1953 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1954 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1955
1956 memcpy( &san->san.unstructured_name,
1957 san_buf, sizeof( *san_buf ) );
1958
1959 }
1960 break;
1961
1962 /*
1963 * Type not supported
1964 */
1965 default:
1966 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1967 }
1968 return( 0 );
1969}
1970
1971#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001972static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001973 const mbedtls_x509_sequence
1974 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001975 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001976{
Janos Follath865b3eb2019-12-16 11:46:15 +00001977 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03001978 size_t i;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001979 size_t n = *size;
1980 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001982 mbedtls_x509_subject_alternative_name san;
1983 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001984
1985 while( cur != NULL )
1986 {
Ron Eldor890819a2019-05-13 19:03:04 +03001987 memset( &san, 0, sizeof( san ) );
1988 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1989 if( parse_ret != 0 )
1990 {
1991 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1992 {
1993 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1994 MBEDTLS_X509_SAFE_SNPRINTF;
1995 }
1996 else
1997 {
1998 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1999 MBEDTLS_X509_SAFE_SNPRINTF;
2000 }
2001 cur = cur->next;
2002 continue;
2003 }
2004
2005 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002006 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002007 /*
2008 * otherName
2009 */
Ron Eldora2913912019-05-16 16:17:38 +03002010 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03002011 {
2012 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
2013
2014 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
2015 MBEDTLS_X509_SAFE_SNPRINTF;
2016
2017 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
2018 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01002019 {
Ron Eldor890819a2019-05-13 19:03:04 +03002020 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
2021 MBEDTLS_X509_SAFE_SNPRINTF;
2022 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01002023 MBEDTLS_X509_SAFE_SNPRINTF;
2024
Ron Eldor890819a2019-05-13 19:03:04 +03002025 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
2026 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01002027
Ron Eldor890819a2019-05-13 19:03:04 +03002028 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
2029 MBEDTLS_X509_SAFE_SNPRINTF;
2030
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03002031 for( i = 0; i < other_name->value.hardware_module_name.val.len; i++ )
Ron Eldor890819a2019-05-13 19:03:04 +03002032 {
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03002033 ret = mbedtls_snprintf( p, n, "%02X", other_name->value.hardware_module_name.val.p[i] );
2034 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath22f605f2019-05-10 10:37:17 +01002035 }
Ron Eldor890819a2019-05-13 19:03:04 +03002036 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
2037 }
2038 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002039
2040 /*
2041 * dNSName
2042 */
Ron Eldora2913912019-05-16 16:17:38 +03002043 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03002044 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002045 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
2046 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03002047 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002048 {
2049 *p = '\0';
2050 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
2051 }
Ron Eldora2913912019-05-16 16:17:38 +03002052
2053 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
2054 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03002055 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03002056 }
2057 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002058
2059 /*
Ron Eldor890819a2019-05-13 19:03:04 +03002060 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002061 */
2062 default:
Janos Follath22f605f2019-05-10 10:37:17 +01002063 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
2064 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002065 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002066 }
2067
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002068 cur = cur->next;
2069 }
2070
2071 *p = '\0';
2072
2073 *size = n;
2074 *buf = p;
2075
2076 return( 0 );
2077}
2078
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002079#define PRINT_ITEM(i) \
2080 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002082 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002083 sep = ", "; \
2084 }
2085
2086#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01002087 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002088 PRINT_ITEM( name );
2089
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002090static int x509_info_cert_type( char **buf, size_t *size,
2091 unsigned char ns_cert_type )
2092{
Janos Follath865b3eb2019-12-16 11:46:15 +00002093 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002094 size_t n = *size;
2095 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002096 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002099 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
2101 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
2102 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002103 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
2104 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
2105 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002106
2107 *size = n;
2108 *buf = p;
2109
2110 return( 0 );
2111}
2112
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002113#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01002114 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002115 PRINT_ITEM( name );
2116
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002117static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002118 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002119{
Janos Follath865b3eb2019-12-16 11:46:15 +00002120 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002121 size_t n = *size;
2122 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002123 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2126 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002127 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2128 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2129 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2131 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002132 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2133 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002134
2135 *size = n;
2136 *buf = p;
2137
2138 return( 0 );
2139}
2140
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002141static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002143{
Janos Follath865b3eb2019-12-16 11:46:15 +00002144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002145 const char *desc;
2146 size_t n = *size;
2147 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002149 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002150
2151 while( cur != NULL )
2152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002154 desc = "???";
2155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002157 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002158
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002159 sep = ", ";
2160
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002161 cur = cur->next;
2162 }
2163
2164 *size = n;
2165 *buf = p;
2166
2167 return( 0 );
2168}
2169
Ron Eldor74d9acc2019-03-21 14:00:03 +02002170static int x509_info_cert_policies( char **buf, size_t *size,
2171 const mbedtls_x509_sequence *certificate_policies )
2172{
Janos Follath865b3eb2019-12-16 11:46:15 +00002173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002174 const char *desc;
2175 size_t n = *size;
2176 char *p = *buf;
2177 const mbedtls_x509_sequence *cur = certificate_policies;
2178 const char *sep = "";
2179
2180 while( cur != NULL )
2181 {
2182 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2183 desc = "???";
2184
2185 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2186 MBEDTLS_X509_SAFE_SNPRINTF;
2187
2188 sep = ", ";
2189
2190 cur = cur->next;
2191 }
2192
2193 *size = n;
2194 *buf = p;
2195
2196 return( 0 );
2197}
2198
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002199/*
2200 * Return an informational string about the certificate.
2201 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002202#define BEFORE_COLON 18
2203#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2205 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002206{
Janos Follath865b3eb2019-12-16 11:46:15 +00002207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002208 size_t n;
2209 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002210 char key_size_str[BEFORE_COLON];
2211
2212 p = buf;
2213 n = size;
2214
Janos Follath98e28a72016-05-31 14:03:54 +01002215 if( NULL == crt )
2216 {
2217 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2218 MBEDTLS_X509_SAFE_SNPRINTF;
2219
2220 return( (int) ( size - n ) );
2221 }
2222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002224 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002225 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002227 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002228 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002231 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002234 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002236 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002239 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002241 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002244 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2245 crt->valid_from.year, crt->valid_from.mon,
2246 crt->valid_from.day, crt->valid_from.hour,
2247 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002248 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002251 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2252 crt->valid_to.year, crt->valid_to.mon,
2253 crt->valid_to.day, crt->valid_to.hour,
2254 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002255 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002258 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002261 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002262 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002263
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002264 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2266 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002267 {
2268 return( ret );
2269 }
2270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002272 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002273 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002274
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002275 /*
2276 * Optional extensions
2277 */
2278
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002279 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002282 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002283 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002284
2285 if( crt->max_pathlen > 0 )
2286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002288 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002289 }
2290 }
2291
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002292 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002293 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002294 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002295 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002296
2297 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002298 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002299 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002300 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002301 }
2302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002306 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002307
2308 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2309 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002310 }
2311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002315 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002316
2317 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2318 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002319 }
2320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002324 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002325
2326 if( ( ret = x509_info_ext_key_usage( &p, &n,
2327 &crt->ext_key_usage ) ) != 0 )
2328 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002329 }
2330
Ron Eldor74d9acc2019-03-21 14:00:03 +02002331 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2332 {
2333 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2334 MBEDTLS_X509_SAFE_SNPRINTF;
2335
2336 if( ( ret = x509_info_cert_policies( &p, &n,
2337 &crt->certificate_policies ) ) != 0 )
2338 return( ret );
2339 }
2340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002342 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002343
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002344 return( (int) ( size - n ) );
2345}
2346
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002347struct x509_crt_verify_string {
2348 int code;
2349 const char *string;
2350};
2351
Hanno Becker7ac83f92020-10-09 10:48:22 +01002352#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002353static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002354 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002355 { 0, NULL }
2356};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002357#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002358
2359int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002360 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002361{
Janos Follath865b3eb2019-12-16 11:46:15 +00002362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002363 const struct x509_crt_verify_string *cur;
2364 char *p = buf;
2365 size_t n = size;
2366
2367 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2368 {
2369 if( ( flags & cur->code ) == 0 )
2370 continue;
2371
2372 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002373 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002374 flags ^= cur->code;
2375 }
2376
2377 if( flags != 0 )
2378 {
2379 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2380 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002381 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002382 }
2383
2384 return( (int) ( size - n ) );
2385}
Hanno Becker612a2f12020-10-09 09:19:39 +01002386#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002387
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002388int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2389 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002390{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002391 unsigned int usage_must, usage_may;
2392 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2393 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2394
2395 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2396 return( 0 );
2397
2398 usage_must = usage & ~may_mask;
2399
2400 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2401 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2402
2403 usage_may = usage & may_mask;
2404
2405 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002407
2408 return( 0 );
2409}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002412 const char *usage_oid,
2413 size_t usage_len )
2414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002416
2417 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002419 return( 0 );
2420
2421 /*
2422 * Look for the requested usage (or wildcard ANY) in our list
2423 */
2424 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002427
2428 if( cur_oid->len == usage_len &&
2429 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2430 {
2431 return( 0 );
2432 }
2433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002435 return( 0 );
2436 }
2437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002439}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002442/*
2443 * Return 1 if the certificate is revoked, or 0 otherwise.
2444 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002445int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002446{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002448
2449 while( cur != NULL && cur->serial.len != 0 )
2450 {
2451 if( crt->serial.len == cur->serial.len &&
2452 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2453 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002454 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002455 }
2456
2457 cur = cur->next;
2458 }
2459
2460 return( 0 );
2461}
2462
2463/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002464 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002465 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002468 mbedtls_x509_crl *crl_list,
2469 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002470{
2471 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002472 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002473#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002474 psa_algorithm_t psa_algorithm;
2475#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002477#endif /* MBEDTLS_USE_PSA_CRYPTO */
2478 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002479
2480 if( ca == NULL )
2481 return( flags );
2482
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002483 while( crl_list != NULL )
2484 {
2485 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002486 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002487 {
2488 crl_list = crl_list->next;
2489 continue;
2490 }
2491
2492 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002493 * Check if the CA is configured to sign CRLs
2494 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002495 if( mbedtls_x509_crt_check_key_usage( ca,
2496 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002499 break;
2500 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002501
2502 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002503 * Check if CRL is correctly signed by the trusted CA
2504 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002505 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2506 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2507
2508 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2509 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2510
pespacek7599a772022-02-07 14:40:55 +01002511#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002512 psa_algorithm = mbedtls_hash_info_psa_from_md( crl_list->sig_md );
pespacekd924e552022-02-28 11:49:54 +01002513 if( psa_hash_compute( psa_algorithm,
2514 crl_list->tbs.p,
2515 crl_list->tbs.len,
2516 hash,
2517 sizeof( hash ),
2518 &hash_length ) != PSA_SUCCESS )
pespaceka7a64692022-02-14 15:18:43 +01002519 {
2520 /* Note: this can't happen except after an internal error */
2521 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2522 break;
2523 }
pespacek7599a772022-02-07 14:40:55 +01002524#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002526 hash_length = mbedtls_md_get_size( md_info );
2527 if( mbedtls_md( md_info,
2528 crl_list->tbs.p,
2529 crl_list->tbs.len,
2530 hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002531 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002532 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002534 break;
2535 }
pespaceka7a64692022-02-14 15:18:43 +01002536#endif /* MBEDTLS_USE_PSA_CRYPTO */
2537
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002538 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002539 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
pespacek7599a772022-02-07 14:40:55 +01002542 crl_list->sig_md, hash, hash_length,
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002543 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002546 break;
2547 }
2548
2549 /*
2550 * Check for validity of CRL (Do not drop out)
2551 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002552 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002554
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002555 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002556 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002557
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002558 /*
2559 * Check if certificate is revoked
2560 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002561 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002564 break;
2565 }
2566
2567 crl_list = crl_list->next;
2568 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002569
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002570 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002573
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002574/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002575 * Check the signature of a certificate by its parent
2576 */
2577static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002578 mbedtls_x509_crt *parent,
2579 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002580{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002581 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02002582 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002583#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2584 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002585 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002586 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002587
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002588 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002589 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002590 return( -1 );
2591#else
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002592 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( child->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002593 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002594
pespacek7599a772022-02-07 14:40:55 +01002595 status = psa_hash_compute( hash_alg,
2596 child->tbs.p,
2597 child->tbs.len,
2598 hash,
pespacekb9f07a72022-02-14 15:13:26 +01002599 sizeof( hash ),
pespacek7599a772022-02-07 14:40:55 +01002600 &hash_len );
2601 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002602 {
pespacek3110c7b2022-02-14 15:07:41 +01002603 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002604 }
2605
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002606#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002607 /* Skip expensive computation on obvious mismatch */
2608 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002609 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002610
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002611#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002612 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2613 {
2614 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002615 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002616 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002617 }
2618#else
2619 (void) rs_ctx;
2620#endif
2621
2622 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002623 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002624 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002625}
2626
2627/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002628 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2629 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002630 *
2631 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2634 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002635 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002636{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002637 int need_ca_bit;
2638
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002639 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002640 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002641 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002642
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002643 /* Parent must have the basicConstraints CA bit set as a general rule */
2644 need_ca_bit = 1;
2645
2646 /* Exception: v1/v2 certificates that are locally trusted. */
2647 if( top && parent->version < 3 )
2648 need_ca_bit = 0;
2649
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002650 if( need_ca_bit && ! parent->ca_istrue )
2651 return( -1 );
2652
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002653 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002655 {
2656 return( -1 );
2657 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002658
2659 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002660}
2661
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002662/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002663 * Find a suitable parent for child in candidates, or return NULL.
2664 *
2665 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002666 * 1. subject name matches child's issuer
2667 * 2. if necessary, the CA bit is set and key usage allows signing certs
2668 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002669 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002670 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002671 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002672 * If there's a suitable candidate which is also time-valid, return the first
2673 * such. Otherwise, return the first suitable candidate (or NULL if there is
2674 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002675 *
2676 * The rationale for this rule is that someone could have a list of trusted
2677 * roots with two versions on the same root with different validity periods.
2678 * (At least one user reported having such a list and wanted it to just work.)
2679 * The reason we don't just require time-validity is that generally there is
2680 * only one version, and if it's expired we want the flags to state that
2681 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002682 *
2683 * The rationale for rule 3 (signature for trusted roots) is that users might
2684 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002685 * way we select the correct one is by checking the signature (as we don't
2686 * rely on key identifier extensions). (This is one way users might choose to
2687 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002688 *
2689 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002690 * - [in] child: certificate for which we're looking for a parent
2691 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002692 * - [out] r_parent: parent found (or NULL)
2693 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002694 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2695 * of the chain, 0 otherwise
2696 * - [in] path_cnt: number of intermediates seen so far
2697 * - [in] self_cnt: number of self-signed intermediates seen so far
2698 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002699 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002700 *
2701 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002702 * - 0 on success
2703 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002704 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002705static int x509_crt_find_parent_in(
2706 mbedtls_x509_crt *child,
2707 mbedtls_x509_crt *candidates,
2708 mbedtls_x509_crt **r_parent,
2709 int *r_signature_is_good,
2710 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002711 unsigned path_cnt,
2712 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002713 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002714{
Janos Follath865b3eb2019-12-16 11:46:15 +00002715 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002716 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002717 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002718
2719#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002720 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002721 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2722 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002723 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002724 parent = rs_ctx->parent;
2725 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002726 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002727
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002728 /* clear saved state */
2729 rs_ctx->parent = NULL;
2730 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002731 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002732
2733 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002734 goto check_signature;
2735 }
2736#endif
2737
2738 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002739 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002740
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002741 for( parent = candidates; parent != NULL; parent = parent->next )
2742 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002743 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002744 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002745 continue;
2746
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002747 /* +1 because stored max_pathlen is 1 higher that the actual value */
2748 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002749 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002750 {
2751 continue;
2752 }
2753
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002754 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002755#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2756check_signature:
2757#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002758 ret = x509_crt_check_signature( child, parent, rs_ctx );
2759
2760#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002761 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2762 {
2763 /* save state */
2764 rs_ctx->parent = parent;
2765 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002766 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002767
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002768 return( ret );
2769 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002770#else
2771 (void) ret;
2772#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002773
2774 signature_is_good = ret == 0;
2775 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002776 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002777
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002778 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002779 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2780 mbedtls_x509_time_is_future( &parent->valid_from ) )
2781 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002782 if( fallback_parent == NULL )
2783 {
2784 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002785 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002786 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002787
2788 continue;
2789 }
2790
Andy Gross1f627142019-01-30 10:25:53 -06002791 *r_parent = parent;
2792 *r_signature_is_good = signature_is_good;
2793
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002794 break;
2795 }
2796
Andy Gross1f627142019-01-30 10:25:53 -06002797 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002798 {
2799 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002800 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002801 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002802
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002803 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002804}
2805
2806/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002807 * Find a parent in trusted CAs or the provided chain, or return NULL.
2808 *
2809 * Searches in trusted CAs first, and return the first suitable parent found
2810 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002811 *
2812 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002813 * - [in] child: certificate for which we're looking for a parent, followed
2814 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002815 * - [in] trust_ca: list of locally trusted certificates
2816 * - [out] parent: parent found (or NULL)
2817 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2818 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2819 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2820 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002821 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002822 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002823 *
2824 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002825 * - 0 on success
2826 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002827 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002828static int x509_crt_find_parent(
2829 mbedtls_x509_crt *child,
2830 mbedtls_x509_crt *trust_ca,
2831 mbedtls_x509_crt **parent,
2832 int *parent_is_trusted,
2833 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002834 unsigned path_cnt,
2835 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002836 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002837{
Janos Follath865b3eb2019-12-16 11:46:15 +00002838 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002839 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002840
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002841 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002842
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002843#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002844 /* restore then clear saved state if we have some stored */
2845 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2846 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002847 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002848 rs_ctx->parent_is_trusted = -1;
2849 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002850#endif
2851
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002852 while( 1 ) {
2853 search_list = *parent_is_trusted ? trust_ca : child->next;
2854
2855 ret = x509_crt_find_parent_in( child, search_list,
2856 parent, signature_is_good,
2857 *parent_is_trusted,
2858 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002859
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002860#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002861 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2862 {
2863 /* save state */
2864 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002865 return( ret );
2866 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002867#else
2868 (void) ret;
2869#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002870
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002871 /* stop here if found or already in second iteration */
2872 if( *parent != NULL || *parent_is_trusted == 0 )
2873 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002874
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002875 /* prepare second iteration */
2876 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002877 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002878
2879 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002880 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002881 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002882 *parent_is_trusted = 0;
2883 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002884 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002885
2886 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002887}
2888
2889/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002890 * Check if an end-entity certificate is locally trusted
2891 *
2892 * Currently we require such certificates to be self-signed (actually only
2893 * check for self-issued as self-signatures are not checked)
2894 */
2895static int x509_crt_check_ee_locally_trusted(
2896 mbedtls_x509_crt *crt,
2897 mbedtls_x509_crt *trust_ca )
2898{
2899 mbedtls_x509_crt *cur;
2900
2901 /* must be self-issued */
2902 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2903 return( -1 );
2904
2905 /* look for an exact match with trusted cert */
2906 for( cur = trust_ca; cur != NULL; cur = cur->next )
2907 {
2908 if( crt->raw.len == cur->raw.len &&
2909 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2910 {
2911 return( 0 );
2912 }
2913 }
2914
2915 /* too bad */
2916 return( -1 );
2917}
2918
2919/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002920 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002921 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002922 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2923 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002924 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002925 * such that every cert in the chain is a child of the next one,
2926 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002927 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002928 * Verify that chain and return it with flags for all issues found.
2929 *
2930 * Special cases:
2931 * - EE == Rj -> return a one-element list containing it
2932 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2933 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002934 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002935 * Tests for (aspects of) this function should include at least:
2936 * - trusted EE
2937 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002938 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002939 * - if relevant: EE untrusted
2940 * - if relevant: EE -> intermediate, untrusted
2941 * with the aspect under test checked at each relevant level (EE, int, root).
2942 * For some aspects longer chains are required, but usually length 2 is
2943 * enough (but length 1 is not in general).
2944 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002945 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002946 * - [in] crt: the cert list EE, C1, ..., Cn
2947 * - [in] trust_ca: the trusted list R1, ..., Rp
2948 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002949 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002950 * Only valid when return value is 0, may contain garbage otherwise!
2951 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002952 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002953 *
2954 * Return value:
2955 * - non-zero if the chain could not be fully built and examined
2956 * - 0 is the chain was successfully built and examined,
2957 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002958 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002959static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002960 mbedtls_x509_crt *crt,
2961 mbedtls_x509_crt *trust_ca,
2962 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002963 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2964 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002965 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002966 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002967 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002968{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002969 /* Don't initialize any of those variables here, so that the compiler can
2970 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002971 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002972 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002973 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002974 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002975 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002976 int parent_is_trusted;
2977 int child_is_trusted;
2978 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002979 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002980 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002981
2982#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2983 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002984 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002985 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002986 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002987 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002988 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002989
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002990 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002991 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002992 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002993 flags = &cur->flags;
2994
2995 goto find_parent;
2996 }
2997#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002998
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002999 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003000 self_cnt = 0;
3001 parent_is_trusted = 0;
3002 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003003
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003004 while( 1 ) {
3005 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003006 cur = &ver_chain->items[ver_chain->len];
3007 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003008 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003009 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003010 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003011
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003012 /* Check time-validity (all certificates) */
3013 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3014 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003015
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003016 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3017 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02003018
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003019 /* Stop here for trusted roots (but not for trusted EE certs) */
3020 if( child_is_trusted )
3021 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003022
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003023 /* Check signature algorithm: MD & PK algs */
3024 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3025 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003026
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003027 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3028 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003029
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003030 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003031 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003032 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3033 {
3034 return( 0 );
3035 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003036
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003037#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3038find_parent:
3039#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00003040
3041 /* Obtain list of potential trusted signers from CA callback,
3042 * or use statically provided list. */
3043#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3044 if( f_ca_cb != NULL )
3045 {
3046 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
3047 mbedtls_free( ver_chain->trust_ca_cb_result );
3048 ver_chain->trust_ca_cb_result = NULL;
3049
3050 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
3051 if( ret != 0 )
3052 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3053
3054 cur_trust_ca = ver_chain->trust_ca_cb_result;
3055 }
3056 else
3057#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3058 {
3059 ((void) f_ca_cb);
3060 ((void) p_ca_cb);
3061 cur_trust_ca = trust_ca;
3062 }
3063
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003064 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00003065 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003066 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003067 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003068
3069#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003070 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3071 {
3072 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003073 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003074 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003075 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003076
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003077 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003078 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003079#else
3080 (void) ret;
3081#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003082
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003083 /* No parent? We're done here */
3084 if( parent == NULL )
3085 {
3086 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3087 return( 0 );
3088 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003089
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003090 /* Count intermediate self-issued (not necessarily self-signed) certs.
3091 * These can occur with some strategies for key rollover, see [SIRO],
3092 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003093 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02003094 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
3095 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003096 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02003097 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003098
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003099 /* path_cnt is 0 for the first intermediate CA,
3100 * and if parent is trusted it's not an intermediate CA */
3101 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003102 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003103 {
3104 /* return immediately to avoid overflow the chain array */
3105 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3106 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003107
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003108 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003109 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003110 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3111
3112 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003113 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003114 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003117 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003118 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003119#else
3120 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003121#endif
3122
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003123 /* prepare for next iteration */
3124 child = parent;
3125 parent = NULL;
3126 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003127 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003128 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003129}
3130
3131/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003132 * Check for CN match
3133 */
3134static int x509_crt_check_cn( const mbedtls_x509_buf *name,
3135 const char *cn, size_t cn_len )
3136{
3137 /* try exact match */
3138 if( name->len == cn_len &&
3139 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3140 {
3141 return( 0 );
3142 }
3143
3144 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003145 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003146 {
3147 return( 0 );
3148 }
3149
3150 return( -1 );
3151}
3152
3153/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003154 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3155 */
3156static int x509_crt_check_san( const mbedtls_x509_buf *name,
3157 const char *cn, size_t cn_len )
3158{
3159 const unsigned char san_type = (unsigned char) name->tag &
3160 MBEDTLS_ASN1_TAG_VALUE_MASK;
3161
3162 /* dNSName */
3163 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3164 return( x509_crt_check_cn( name, cn, cn_len ) );
3165
3166 /* (We may handle other types here later.) */
3167
3168 /* Unrecognized type */
3169 return( -1 );
3170}
3171
3172/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003173 * Verify the requested CN - only call this if cn is not NULL!
3174 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003175static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003176 const char *cn,
3177 uint32_t *flags )
3178{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003179 const mbedtls_x509_name *name;
3180 const mbedtls_x509_sequence *cur;
3181 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003182
3183 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3184 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003185 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003186 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003187 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003188 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003189 }
3190
3191 if( cur == NULL )
3192 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3193 }
3194 else
3195 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003196 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003197 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003198 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3199 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003200 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003201 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003202 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003203 }
3204
3205 if( name == NULL )
3206 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3207 }
3208}
3209
3210/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003211 * Merge the flags for all certs in the chain, after calling callback
3212 */
3213static int x509_crt_merge_flags_with_cb(
3214 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003215 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003216 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3217 void *p_vrfy )
3218{
Janos Follath865b3eb2019-12-16 11:46:15 +00003219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003220 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003221 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003222 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003223
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003224 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003225 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003226 cur = &ver_chain->items[i-1];
3227 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003228
3229 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003230 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003231 return( ret );
3232
3233 *flags |= cur_flags;
3234 }
3235
3236 return( 0 );
3237}
3238
3239/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003240 * Verify the certificate validity, with profile, restartable version
3241 *
3242 * This function:
3243 * - checks the requested CN (if any)
3244 * - checks the type and size of the EE cert's key,
3245 * as that isn't done as part of chain building/verification currently
3246 * - builds and verifies the chain
3247 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003248 *
3249 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3250 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3251 * verification routine to search for trusted signers, and CRLs will
3252 * be disabled. Otherwise, `trust_ca` will be used as the static list
3253 * of trusted signers, and `ca_crl` will be use as the static list
3254 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003255 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003256static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003257 mbedtls_x509_crt *trust_ca,
3258 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003259 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3260 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003261 const mbedtls_x509_crt_profile *profile,
3262 const char *cn, uint32_t *flags,
3263 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3264 void *p_vrfy,
3265 mbedtls_x509_crt_restart_ctx *rs_ctx )
3266{
Janos Follath865b3eb2019-12-16 11:46:15 +00003267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003268 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003269 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003270 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003271
3272 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003273 ee_flags = 0;
3274 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003275
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003276 if( profile == NULL )
3277 {
3278 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3279 goto exit;
3280 }
3281
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003282 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003283 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003284 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003285
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003286 /* Check the type and size of the key */
3287 pk_type = mbedtls_pk_get_type( &crt->pk );
3288
3289 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003290 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003291
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003292 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003293 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003294
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003295 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003296 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3297 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003298 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003299
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003300 if( ret != 0 )
3301 goto exit;
3302
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003303 /* Merge end-entity flags */
3304 ver_chain.items[0].flags |= ee_flags;
3305
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003306 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003307 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003308
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003309exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003310
3311#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3312 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3313 mbedtls_free( ver_chain.trust_ca_cb_result );
3314 ver_chain.trust_ca_cb_result = NULL;
3315#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3316
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003317#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3318 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3319 mbedtls_x509_crt_restart_free( rs_ctx );
3320#endif
3321
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003322 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3323 * the SSL module for authmode optional, but non-zero return from the
3324 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003325 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3326 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3327
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003328 if( ret != 0 )
3329 {
3330 *flags = (uint32_t) -1;
3331 return( ret );
3332 }
3333
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003334 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003336
3337 return( 0 );
3338}
3339
Hanno Becker3116fb32019-03-28 13:34:42 +00003340
3341/*
3342 * Verify the certificate validity (default profile, not restartable)
3343 */
3344int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3345 mbedtls_x509_crt *trust_ca,
3346 mbedtls_x509_crl *ca_crl,
3347 const char *cn, uint32_t *flags,
3348 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3349 void *p_vrfy )
3350{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003351 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003352 NULL, NULL,
3353 &mbedtls_x509_crt_profile_default,
3354 cn, flags,
3355 f_vrfy, p_vrfy, NULL ) );
3356}
3357
3358/*
3359 * Verify the certificate validity (user-chosen profile, not restartable)
3360 */
3361int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3362 mbedtls_x509_crt *trust_ca,
3363 mbedtls_x509_crl *ca_crl,
3364 const mbedtls_x509_crt_profile *profile,
3365 const char *cn, uint32_t *flags,
3366 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3367 void *p_vrfy )
3368{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003369 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003370 NULL, NULL,
3371 profile, cn, flags,
3372 f_vrfy, p_vrfy, NULL ) );
3373}
3374
3375#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3376/*
3377 * Verify the certificate validity (user-chosen profile, CA callback,
3378 * not restartable).
3379 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003380int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003381 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3382 void *p_ca_cb,
3383 const mbedtls_x509_crt_profile *profile,
3384 const char *cn, uint32_t *flags,
3385 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3386 void *p_vrfy )
3387{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003388 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003389 f_ca_cb, p_ca_cb,
3390 profile, cn, flags,
3391 f_vrfy, p_vrfy, NULL ) );
3392}
3393#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3394
3395int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3396 mbedtls_x509_crt *trust_ca,
3397 mbedtls_x509_crl *ca_crl,
3398 const mbedtls_x509_crt_profile *profile,
3399 const char *cn, uint32_t *flags,
3400 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3401 void *p_vrfy,
3402 mbedtls_x509_crt_restart_ctx *rs_ctx )
3403{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003404 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003405 NULL, NULL,
3406 profile, cn, flags,
3407 f_vrfy, p_vrfy, rs_ctx ) );
3408}
3409
3410
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003411/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003412 * Initialize a certificate chain
3413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003417}
3418
3419/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003420 * Unallocate all certificate data
3421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003423{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 mbedtls_x509_crt *cert_cur = crt;
3425 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003426
Glenn Straussa4b40412022-06-26 19:32:09 -04003427 while( cert_cur != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3432 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003433#endif
3434
Glenn Straussa4b40412022-06-26 19:32:09 -04003435 mbedtls_asn1_free_named_data_list_shallow( cert_cur->issuer.next );
3436 mbedtls_asn1_free_named_data_list_shallow( cert_cur->subject.next );
3437 mbedtls_asn1_sequence_free( cert_cur->ext_key_usage.next );
3438 mbedtls_asn1_sequence_free( cert_cur->subject_alt_names.next );
3439 mbedtls_asn1_sequence_free( cert_cur->certificate_policies.next );
Ron Eldor74d9acc2019-03-21 14:00:03 +02003440
toth92g1bbc2fe2021-02-12 16:11:17 +01003441 name_cur = cert_cur->authority_key_id.authorityCertIssuer.next;
toth92gdd123902021-04-08 10:12:52 +02003442 while ( name_cur != NULL )
toth92g1bbc2fe2021-02-12 16:11:17 +01003443 {
3444 name_prv = name_cur;
3445 name_cur = name_cur->next;
toth92gdd123902021-04-08 10:12:52 +02003446 mbedtls_platform_zeroize( name_prv, sizeof(mbedtls_x509_name) );
3447 mbedtls_free( name_prv );
toth92g1bbc2fe2021-02-12 16:11:17 +01003448 }
3449
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003450 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003451 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003452 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003454 }
3455
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003456 cert_prv = cert_cur;
3457 cert_cur = cert_cur->next;
3458
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003459 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003460 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003462 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003463}
3464
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003465#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3466/*
3467 * Initialize a restart context
3468 */
3469void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3470{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003471 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003472
3473 ctx->parent = NULL;
3474 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003475 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003476
3477 ctx->parent_is_trusted = -1;
3478
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003479 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003480 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003481 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003482}
3483
3484/*
3485 * Free the components of a restart context
3486 */
3487void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3488{
3489 if( ctx == NULL )
3490 return;
3491
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003492 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003493 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003494}
3495#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497#endif /* MBEDTLS_X509_CRT_PARSE_C */