blob: d17a952b7c9fc0e5c2f4c1d9f84529e5cd5b0d7a [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#else
Simon Butcherd2642582018-10-03 15:11:19 +010056#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000057#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020059#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#endif
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010065#endif
66
Daniel Axtensf0710242020-05-28 11:43:41 +100067#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010068#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069#include <windows.h>
70#else
71#include <time.h>
72#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100073#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000076#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020077#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078#include <sys/types.h>
79#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020080#if defined(__MBED__)
81#include <platform/mbed_retarget.h>
82#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020084#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060085#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000086#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087#endif
88
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020089/*
90 * Item in a verification chain: cert and flags for it
91 */
92typedef struct {
93 mbedtls_x509_crt *crt;
94 uint32_t flags;
95} x509_crt_verify_chain_item;
96
97/*
98 * Max size of verification chain: end-entity + intermediates + trusted root
99 */
100#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200101
Gilles Peskineffb92da2021-06-02 00:03:26 +0200102/* Default profile. Do not remove items unless there are serious security
103 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200104const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
105{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200106 /* Hashes from SHA-256 and above. Note that this selection
107 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200108 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
110 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
111 0xFFFFFFF, /* Any PK alg */
112#if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200113 /* Curves at or above 128-bit security level. Note that this selection
114 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
120 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
Gilles Peskine39957502021-06-17 23:17:52 +0200121 0,
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200122#else
123 0,
124#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200125 2048,
126};
127
Gilles Peskineffb92da2021-06-02 00:03:26 +0200128/* Next-generation profile. Currently identical to the default, but may
129 * be tightened at any time. */
130const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200131{
132 /* Hashes from SHA-256 and above. */
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
136 0xFFFFFFF, /* Any PK alg */
137#if defined(MBEDTLS_ECP_C)
138 /* Curves at or above 128-bit security level. */
139 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
140 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
142 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
143 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
144 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
145 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
146#else
147 0,
148#endif
149 2048,
150};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200151
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200152/*
153 * NSA Suite B Profile
154 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200155const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
156{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200157 /* Only SHA-256 and 384 */
158 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
159 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
160 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200161 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
162 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200163#if defined(MBEDTLS_ECP_C)
164 /* Only NIST P-256 and P-384 */
165 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
166 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
167#else
168 0,
169#endif
170 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200171};
172
173/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200174 * Empty / all-forbidden profile
175 */
176const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
177{
178 0,
179 0,
180 0,
181 (uint32_t) -1,
182};
183
184/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200185 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200186 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200187 */
188static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
189 mbedtls_md_type_t md_alg )
190{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200191 if( md_alg == MBEDTLS_MD_NONE )
192 return( -1 );
193
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200194 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
195 return( 0 );
196
197 return( -1 );
198}
199
200/*
201 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200202 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200203 */
204static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
205 mbedtls_pk_type_t pk_alg )
206{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200207 if( pk_alg == MBEDTLS_PK_NONE )
208 return( -1 );
209
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200210 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
211 return( 0 );
212
213 return( -1 );
214}
215
216/*
217 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200218 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200219 */
220static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200221 const mbedtls_pk_context *pk )
222{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200223 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200224
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200225#if defined(MBEDTLS_RSA_C)
226 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
227 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200228 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200229 return( 0 );
230
231 return( -1 );
232 }
233#endif
234
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200235#if defined(MBEDTLS_ECP_C)
236 if( pk_alg == MBEDTLS_PK_ECDSA ||
237 pk_alg == MBEDTLS_PK_ECKEY ||
238 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200239 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200240 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200241
Philippe Antoineb5b25432018-05-11 11:06:29 +0200242 if( gid == MBEDTLS_ECP_DP_NONE )
243 return( -1 );
244
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200245 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
246 return( 0 );
247
248 return( -1 );
249 }
250#endif
251
252 return( -1 );
253}
254
255/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000256 * Like memcmp, but case-insensitive and always returns -1 if different
257 */
258static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
259{
260 size_t i;
261 unsigned char diff;
262 const unsigned char *n1 = s1, *n2 = s2;
263
264 for( i = 0; i < len; i++ )
265 {
266 diff = n1[i] ^ n2[i];
267
268 if( diff == 0 )
269 continue;
270
271 if( diff == 32 &&
272 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
273 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
274 {
275 continue;
276 }
277
278 return( -1 );
279 }
280
281 return( 0 );
282}
283
284/*
285 * Return 0 if name matches wildcard, -1 otherwise
286 */
287static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
288{
289 size_t i;
290 size_t cn_idx = 0, cn_len = strlen( cn );
291
292 /* We can't have a match if there is no wildcard to match */
293 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
294 return( -1 );
295
296 for( i = 0; i < cn_len; ++i )
297 {
298 if( cn[i] == '.' )
299 {
300 cn_idx = i;
301 break;
302 }
303 }
304
305 if( cn_idx == 0 )
306 return( -1 );
307
308 if( cn_len - cn_idx == name->len - 1 &&
309 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
310 {
311 return( 0 );
312 }
313
314 return( -1 );
315}
316
317/*
318 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
319 * variations (but not all).
320 *
321 * Return 0 if equal, -1 otherwise.
322 */
323static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
324{
325 if( a->tag == b->tag &&
326 a->len == b->len &&
327 memcmp( a->p, b->p, b->len ) == 0 )
328 {
329 return( 0 );
330 }
331
332 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
333 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
334 a->len == b->len &&
335 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
336 {
337 return( 0 );
338 }
339
340 return( -1 );
341}
342
343/*
344 * Compare two X.509 Names (aka rdnSequence).
345 *
346 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
347 * we sometimes return unequal when the full algorithm would return equal,
348 * but never the other way. (In particular, we don't do Unicode normalisation
349 * or space folding.)
350 *
351 * Return 0 if equal, -1 otherwise.
352 */
353static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
354{
355 /* Avoid recursion, it might not be optimised by the compiler */
356 while( a != NULL || b != NULL )
357 {
358 if( a == NULL || b == NULL )
359 return( -1 );
360
361 /* type */
362 if( a->oid.tag != b->oid.tag ||
363 a->oid.len != b->oid.len ||
364 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
365 {
366 return( -1 );
367 }
368
369 /* value */
370 if( x509_string_cmp( &a->val, &b->val ) != 0 )
371 return( -1 );
372
373 /* structure of the list of sets */
374 if( a->next_merged != b->next_merged )
375 return( -1 );
376
377 a = a->next;
378 b = b->next;
379 }
380
381 /* a == NULL == b */
382 return( 0 );
383}
384
385/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200386 * Reset (init or clear) a verify_chain
387 */
388static void x509_crt_verify_chain_reset(
389 mbedtls_x509_crt_verify_chain *ver_chain )
390{
391 size_t i;
392
393 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
394 {
395 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000396 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200397 }
398
399 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000400
401#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
402 ver_chain->trust_ca_cb_result = NULL;
403#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200404}
405
406/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
408 */
409static int x509_get_version( unsigned char **p,
410 const unsigned char *end,
411 int *ver )
412{
Janos Follath865b3eb2019-12-16 11:46:15 +0000413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414 size_t len;
415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
417 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420 {
421 *ver = 0;
422 return( 0 );
423 }
424
Chris Jones9f7a6932021-04-14 12:12:09 +0100425 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426 }
427
428 end = *p + len;
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100431 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100434 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
435 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436
437 return( 0 );
438}
439
440/*
441 * Validity ::= SEQUENCE {
442 * notBefore Time,
443 * notAfter Time }
444 */
445static int x509_get_dates( unsigned char **p,
446 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_x509_time *from,
448 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449{
Janos Follath865b3eb2019-12-16 11:46:15 +0000450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451 size_t len;
452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
454 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100455 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456
457 end = *p + len;
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460 return( ret );
461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463 return( ret );
464
465 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100466 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
467 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468
469 return( 0 );
470}
471
472/*
473 * X.509 v2/v3 unique identifier (not parsed)
474 */
475static int x509_get_uid( unsigned char **p,
476 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478{
Janos Follath865b3eb2019-12-16 11:46:15 +0000479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480
481 if( *p == end )
482 return( 0 );
483
484 uid->tag = **p;
485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
487 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 return( 0 );
491
Chris Jones9f7a6932021-04-14 12:12:09 +0100492 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 }
494
495 uid->p = *p;
496 *p += uid->len;
497
498 return( 0 );
499}
500
501static int x509_get_basic_constraints( unsigned char **p,
502 const unsigned char *end,
503 int *ca_istrue,
504 int *max_pathlen )
505{
Janos Follath865b3eb2019-12-16 11:46:15 +0000506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 size_t len;
508
509 /*
510 * BasicConstraints ::= SEQUENCE {
511 * cA BOOLEAN DEFAULT FALSE,
512 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
513 */
514 *ca_istrue = 0; /* DEFAULT FALSE */
515 *max_pathlen = 0; /* endless */
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
518 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100519 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520
521 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200522 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
527 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528
529 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100530 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531
532 if( *ca_istrue != 0 )
533 *ca_istrue = 1;
534 }
535
536 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200537 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100540 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541
542 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100543 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
544 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545
Andrzej Kurek16050742020-04-14 09:49:52 -0400546 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
547 * overflow, which is an undefined behavior. */
548 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100549 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
550 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400551
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552 (*max_pathlen)++;
553
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200554 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555}
556
557static int x509_get_ns_cert_type( unsigned char **p,
558 const unsigned char *end,
559 unsigned char *ns_cert_type)
560{
Janos Follath865b3eb2019-12-16 11:46:15 +0000561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100565 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566
567 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100568 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
569 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200570
571 /* Get actual bitstring */
572 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200573 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574}
575
576static int x509_get_key_usage( unsigned char **p,
577 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100578 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579{
Janos Follath865b3eb2019-12-16 11:46:15 +0000580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200581 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100585 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586
587 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100588 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
589 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590
591 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200592 *key_usage = 0;
593 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
594 {
595 *key_usage |= (unsigned int) bs.p[i] << (8*i);
596 }
597
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200598 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599}
600
601/*
602 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
603 *
604 * KeyPurposeId ::= OBJECT IDENTIFIER
605 */
606static int x509_get_ext_key_usage( unsigned char **p,
607 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200609{
Janos Follath865b3eb2019-12-16 11:46:15 +0000610 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100613 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614
615 /* Sequence length must be >= 1 */
616 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100617 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
618 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200620 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621}
622
623/*
624 * SubjectAltName ::= GeneralNames
625 *
626 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
627 *
628 * GeneralName ::= CHOICE {
629 * otherName [0] OtherName,
630 * rfc822Name [1] IA5String,
631 * dNSName [2] IA5String,
632 * x400Address [3] ORAddress,
633 * directoryName [4] Name,
634 * ediPartyName [5] EDIPartyName,
635 * uniformResourceIdentifier [6] IA5String,
636 * iPAddress [7] OCTET STRING,
637 * registeredID [8] OBJECT IDENTIFIER }
638 *
639 * OtherName ::= SEQUENCE {
640 * type-id OBJECT IDENTIFIER,
641 * value [0] EXPLICIT ANY DEFINED BY type-id }
642 *
643 * EDIPartyName ::= SEQUENCE {
644 * nameAssigner [0] DirectoryString OPTIONAL,
645 * partyName [1] DirectoryString }
646 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300647 * NOTE: we list all types, but only use dNSName and otherName
648 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649 */
650static int x509_get_subject_alt_name( unsigned char **p,
651 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653{
Janos Follath865b3eb2019-12-16 11:46:15 +0000654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659
660 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
662 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100663 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664
665 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100666 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
667 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668
669 while( *p < end )
670 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300671 mbedtls_x509_subject_alternative_name dummy_san_buf;
672 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
673
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 tag = **p;
675 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100677 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200678
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100679 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
680 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
681 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100682 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
683 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100684 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685
Ron Eldordbbd9662019-05-15 15:46:03 +0300686 /*
irwir74aee1c2019-09-21 18:21:48 +0300687 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300688 */
689 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
690 /*
691 * In case the extension is malformed, return an error,
692 * and clear the allocated sequences.
693 */
694 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
695 {
696 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
697 mbedtls_x509_sequence *seq_prv;
698 while( seq_cur != NULL )
699 {
700 seq_prv = seq_cur;
701 seq_cur = seq_cur->next;
702 mbedtls_platform_zeroize( seq_prv,
703 sizeof( mbedtls_x509_sequence ) );
704 mbedtls_free( seq_prv );
705 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300706 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300707 return( ret );
708 }
709
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200711 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200712 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100713 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100715
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200716 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717
718 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100719 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
720 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722 cur = cur->next;
723 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200724
725 buf = &(cur->buf);
726 buf->tag = tag;
727 buf->p = *p;
728 buf->len = tag_len;
729 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730 }
731
732 /* Set final sequence entry's next pointer to NULL */
733 cur->next = NULL;
734
735 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100736 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
737 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738
739 return( 0 );
740}
741
742/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200743 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
744 *
745 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
746 *
747 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
748 *
749 * PolicyInformation ::= SEQUENCE {
750 * policyIdentifier CertPolicyId,
751 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
752 * PolicyQualifierInfo OPTIONAL }
753 *
754 * CertPolicyId ::= OBJECT IDENTIFIER
755 *
756 * PolicyQualifierInfo ::= SEQUENCE {
757 * policyQualifierId PolicyQualifierId,
758 * qualifier ANY DEFINED BY policyQualifierId }
759 *
760 * -- policyQualifierIds for Internet policy qualifiers
761 *
762 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
763 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
764 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
765 *
766 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
767 *
768 * Qualifier ::= CHOICE {
769 * cPSuri CPSuri,
770 * userNotice UserNotice }
771 *
772 * CPSuri ::= IA5String
773 *
774 * UserNotice ::= SEQUENCE {
775 * noticeRef NoticeReference OPTIONAL,
776 * explicitText DisplayText OPTIONAL }
777 *
778 * NoticeReference ::= SEQUENCE {
779 * organization DisplayText,
780 * noticeNumbers SEQUENCE OF INTEGER }
781 *
782 * DisplayText ::= CHOICE {
783 * ia5String IA5String (SIZE (1..200)),
784 * visibleString VisibleString (SIZE (1..200)),
785 * bmpString BMPString (SIZE (1..200)),
786 * utf8String UTF8String (SIZE (1..200)) }
787 *
788 * NOTE: we only parse and use anyPolicy without qualifiers at this point
789 * as defined in RFC 5280.
790 */
791static int x509_get_certificate_policies( unsigned char **p,
792 const unsigned char *end,
793 mbedtls_x509_sequence *certificate_policies )
794{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300795 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200796 size_t len;
797 mbedtls_asn1_buf *buf;
798 mbedtls_asn1_sequence *cur = certificate_policies;
799
800 /* Get main sequence tag */
801 ret = mbedtls_asn1_get_tag( p, end, &len,
802 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
803 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100804 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200805
806 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100807 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
808 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200809
810 /*
811 * Cannot be an empty sequence.
812 */
813 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100814 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
815 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200816
817 while( *p < end )
818 {
819 mbedtls_x509_buf policy_oid;
820 const unsigned char *policy_end;
821
822 /*
823 * Get the policy sequence
824 */
825 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
826 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100827 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200828
829 policy_end = *p + len;
830
Ron Eldor08063792019-05-13 16:38:39 +0300831 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200832 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100833 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200834
835 policy_oid.tag = MBEDTLS_ASN1_OID;
836 policy_oid.len = len;
837 policy_oid.p = *p;
838
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300839 /*
840 * Only AnyPolicy is currently supported when enforcing policy.
841 */
842 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
843 {
844 /*
845 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200846 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300847 */
848 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
849 }
850
Ron Eldor74d9acc2019-03-21 14:00:03 +0200851 /* Allocate and assign next pointer */
852 if( cur->buf.p != NULL )
853 {
854 if( cur->next != NULL )
855 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
856
857 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
858
859 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100860 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
861 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200862
863 cur = cur->next;
864 }
865
866 buf = &( cur->buf );
867 buf->tag = policy_oid.tag;
868 buf->p = policy_oid.p;
869 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300870
871 *p += len;
872
873 /*
874 * If there is an optional qualifier, then *p < policy_end
875 * Check the Qualifier len to verify it doesn't exceed policy_end.
876 */
877 if( *p < policy_end )
878 {
879 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
880 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100881 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300882 /*
883 * Skip the optional policy qualifiers.
884 */
885 *p += len;
886 }
887
888 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100889 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
890 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200891 }
892
893 /* Set final sequence entry's next pointer to NULL */
894 cur->next = NULL;
895
896 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100897 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
898 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200899
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300900 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200901}
902
903/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 * X.509 v3 extensions
905 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906 */
907static int x509_get_crt_ext( unsigned char **p,
908 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200909 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200910 mbedtls_x509_crt_ext_cb_t cb,
911 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912{
Janos Follath865b3eb2019-12-16 11:46:15 +0000913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200915 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916
Hanno Becker12f62fb2019-02-12 17:22:36 +0000917 if( *p == end )
918 return( 0 );
919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922
Hanno Becker12f62fb2019-02-12 17:22:36 +0000923 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924 while( *p < end )
925 {
926 /*
927 * Extension ::= SEQUENCE {
928 * extnID OBJECT IDENTIFIER,
929 * critical BOOLEAN DEFAULT FALSE,
930 * extnValue OCTET STRING }
931 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 int is_critical = 0; /* DEFAULT FALSE */
934 int ext_type = 0;
935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
937 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100938 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939
940 end_ext_data = *p + len;
941
942 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200943 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200944 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100945 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946
k-stachowiak463928a2018-07-24 12:50:59 +0200947 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948 extn_oid.p = *p;
949 *p += extn_oid.len;
950
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
953 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100954 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955
956 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
958 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100959 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200961 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 end_ext_octet = *p + len;
963
964 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100965 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
966 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967
968 /*
969 * Detect supported extensions
970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972
973 if( ret != 0 )
974 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200975 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200976 if( cb != NULL )
977 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200978 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200979 if( ret != 0 && is_critical )
980 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200981 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200982 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200983 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200984
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 /* No parser found, skip extension */
986 *p = end_ext_octet;
987
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988 if( is_critical )
989 {
990 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100991 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
992 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994 continue;
995 }
996
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100997 /* Forbid repeated extensions */
998 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001000
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001 crt->ext_types |= ext_type;
1002
1003 switch( ext_type )
1004 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001005 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006 /* Parse basic constraints */
1007 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1008 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001009 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 break;
1011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 /* Parse key usage */
1014 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1015 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001016 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017 break;
1018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 /* Parse extended key usage */
1021 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1022 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001023 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024 break;
1025
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001026 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 /* Parse subject alt name */
1028 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1029 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001030 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 break;
1032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 /* Parse netscape certificate type */
1035 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1036 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001037 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038 break;
1039
Ron Eldor74d9acc2019-03-21 14:00:03 +02001040 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1041 /* Parse certificate policies type */
1042 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1043 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001044 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001045 /* Give the callback (if any) a chance to handle the extension
1046 * if it contains unsupported policies */
1047 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1048 cb( p_ctx, crt, &extn_oid, is_critical,
1049 start_ext_octet, end_ext_octet ) == 0 )
1050 break;
1051
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001052 if( is_critical )
1053 return( ret );
1054 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001055 /*
Ron Eldora2913912019-05-16 16:17:38 +03001056 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1057 * cannot interpret or enforce the policy. However, it is up to
1058 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001059 * unless the extension is critical.
1060 */
1061 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1062 return( ret );
1063 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001064 break;
1065
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001066 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001067 /*
1068 * If this is a non-critical extension, which the oid layer
1069 * supports, but there isn't an x509 parser for it,
1070 * skip the extension.
1071 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001072 if( is_critical )
1073 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1074 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001075 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076 }
1077 }
1078
1079 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001080 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1081 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001082
1083 return( 0 );
1084}
1085
1086/*
1087 * Parse and fill a single X.509 certificate in DER format
1088 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001089static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1090 const unsigned char *buf,
1091 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001092 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001093 mbedtls_x509_crt_ext_cb_t cb,
1094 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095{
Janos Follath865b3eb2019-12-16 11:46:15 +00001096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097 size_t len;
1098 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1102 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1103 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104
1105 /*
1106 * Check for valid input
1107 */
1108 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001111 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001112 p = (unsigned char*) buf;
1113 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114 end = p + len;
1115
1116 /*
1117 * Certificate ::= SEQUENCE {
1118 * tbsCertificate TBSCertificate,
1119 * signatureAlgorithm AlgorithmIdentifier,
1120 * signatureValue BIT STRING }
1121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1123 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 mbedtls_x509_crt_free( crt );
1126 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127 }
1128
Janos Follathcc0e49d2016-02-17 14:34:12 +00001129 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001130 crt->raw.len = crt_end - buf;
1131 if( make_copy != 0 )
1132 {
1133 /* Create and populate a new buffer for the raw field. */
1134 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1135 if( crt->raw.p == NULL )
1136 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1137
1138 memcpy( crt->raw.p, buf, crt->raw.len );
1139 crt->own_buffer = 1;
1140
1141 p += crt->raw.len - len;
1142 end = crt_end = p + len;
1143 }
1144 else
1145 {
1146 crt->raw.p = (unsigned char*) buf;
1147 crt->own_buffer = 0;
1148 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001149
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150 /*
1151 * TBSCertificate ::= SEQUENCE {
1152 */
1153 crt->tbs.p = p;
1154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1156 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001159 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160 }
1161
1162 end = p + len;
1163 crt->tbs.len = end - crt->tbs.p;
1164
1165 /*
1166 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1167 *
1168 * CertificateSerialNumber ::= INTEGER
1169 *
1170 * signature AlgorithmIdentifier
1171 */
1172 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1174 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001175 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001178 return( ret );
1179 }
1180
Andres AG7ca4a032017-03-09 16:16:11 +00001181 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 mbedtls_x509_crt_free( crt );
1184 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001185 }
1186
Andres AG7ca4a032017-03-09 16:16:11 +00001187 crt->version++;
1188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001190 &crt->sig_md, &crt->sig_pk,
1191 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 return( ret );
1195 }
1196
1197 /*
1198 * issuer Name
1199 */
1200 crt->issuer_raw.p = p;
1201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1203 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001206 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001207 }
1208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001212 return( ret );
1213 }
1214
1215 crt->issuer_raw.len = p - crt->issuer_raw.p;
1216
1217 /*
1218 * Validity ::= SEQUENCE {
1219 * notBefore Time,
1220 * notAfter Time }
1221 *
1222 */
1223 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1224 &crt->valid_to ) ) != 0 )
1225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227 return( ret );
1228 }
1229
1230 /*
1231 * subject Name
1232 */
1233 crt->subject_raw.p = p;
1234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1236 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001239 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001240 }
1241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245 return( ret );
1246 }
1247
1248 crt->subject_raw.len = p - crt->subject_raw.p;
1249
1250 /*
1251 * SubjectPublicKeyInfo
1252 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001253 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001257 return( ret );
1258 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001259 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001260
1261 /*
1262 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1263 * -- If present, version shall be v2 or v3
1264 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1265 * -- If present, version shall be v2 or v3
1266 * extensions [3] EXPLICIT Extensions OPTIONAL
1267 * -- If present, version shall be v3
1268 */
1269 if( crt->version == 2 || crt->version == 3 )
1270 {
1271 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1272 if( ret != 0 )
1273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001275 return( ret );
1276 }
1277 }
1278
1279 if( crt->version == 2 || crt->version == 3 )
1280 {
1281 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1282 if( ret != 0 )
1283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001285 return( ret );
1286 }
1287 }
1288
1289 if( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001290 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001291 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001292 if( ret != 0 )
1293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001295 return( ret );
1296 }
1297 }
1298
1299 if( p != end )
1300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001302 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1303 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 }
1305
1306 end = crt_end;
1307
1308 /*
1309 * }
1310 * -- end of TBSCertificate
1311 *
1312 * signatureAlgorithm AlgorithmIdentifier,
1313 * signatureValue BIT STRING
1314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001318 return( ret );
1319 }
1320
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001321 if( crt->sig_oid.len != sig_oid2.len ||
1322 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001323 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001324 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001325 ( sig_params1.len != 0 &&
1326 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 mbedtls_x509_crt_free( crt );
1329 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001330 }
1331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001335 return( ret );
1336 }
1337
1338 if( p != end )
1339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001341 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1342 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343 }
1344
1345 return( 0 );
1346}
1347
1348/*
1349 * Parse one X.509 certificate in DER format from a buffer and add them to a
1350 * chained list
1351 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001352static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1353 const unsigned char *buf,
1354 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001355 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001356 mbedtls_x509_crt_ext_cb_t cb,
1357 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001358{
Janos Follath865b3eb2019-12-16 11:46:15 +00001359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361
1362 /*
1363 * Check for valid input
1364 */
1365 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001367
1368 while( crt->version != 0 && crt->next != NULL )
1369 {
1370 prev = crt;
1371 crt = crt->next;
1372 }
1373
1374 /*
1375 * Add new certificate on the end of the chain if needed.
1376 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001377 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001379 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380
1381 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001382 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383
1384 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001386 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001387 }
1388
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001389 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001390 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001391 {
1392 if( prev )
1393 prev->next = NULL;
1394
1395 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001397
1398 return( ret );
1399 }
1400
1401 return( 0 );
1402}
1403
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001404int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1405 const unsigned char *buf,
1406 size_t buflen )
1407{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001408 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001409}
1410
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001411int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1412 const unsigned char *buf,
1413 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001414 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001415 mbedtls_x509_crt_ext_cb_t cb,
1416 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001417{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001418 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001419}
1420
1421int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1422 const unsigned char *buf,
1423 size_t buflen )
1424{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001425 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001426}
1427
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001428/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001429 * Parse one or more PEM certificates from a buffer and add them to the chained
1430 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001432int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1433 const unsigned char *buf,
1434 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001435{
Janos Follath98e28a72016-05-31 14:03:54 +01001436#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001437 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001439#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440
1441 /*
1442 * Check for valid input
1443 */
1444 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446
1447 /*
1448 * Determine buffer content. Buffer contains either one DER certificate or
1449 * one or more PEM certificates.
1450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001452 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001453 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001456 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1459 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001460#else
1461 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1462#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_PEM_PARSE_C)
1465 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001469
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001470 /* 1 rather than 0 since the terminating NULL byte is counted in */
1471 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472 {
1473 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001476 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001478 "-----BEGIN CERTIFICATE-----",
1479 "-----END CERTIFICATE-----",
1480 buf, NULL, 0, &use_len );
1481
1482 if( ret == 0 )
1483 {
1484 /*
1485 * Was PEM encoded
1486 */
1487 buflen -= use_len;
1488 buf += use_len;
1489 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491 {
1492 return( ret );
1493 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001497
1498 /*
1499 * PEM header and footer were found
1500 */
1501 buflen -= use_len;
1502 buf += use_len;
1503
1504 if( first_error == 0 )
1505 first_error = ret;
1506
Paul Bakker5a5fa922014-09-26 14:53:04 +02001507 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508 continue;
1509 }
1510 else
1511 break;
1512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516
1517 if( ret != 0 )
1518 {
1519 /*
1520 * Quit parsing on a memory error
1521 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001522 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001523 return( ret );
1524
1525 if( first_error == 0 )
1526 first_error = ret;
1527
1528 total_failed++;
1529 continue;
1530 }
1531
1532 success = 1;
1533 }
1534 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001535
1536 if( success )
1537 return( total_failed );
1538 else if( first_error )
1539 return( first_error );
1540 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001542#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543}
1544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546/*
1547 * Load one or more certificates and add them to the chained list
1548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001550{
Janos Follath865b3eb2019-12-16 11:46:15 +00001551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552 size_t n;
1553 unsigned char *buf;
1554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556 return( ret );
1557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001560 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562
1563 return( ret );
1564}
1565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567{
1568 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001569#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570 int w_ret;
1571 WCHAR szDir[MAX_PATH];
1572 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001573 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001574 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575
Paul Bakker9af723c2014-05-01 13:03:14 +02001576 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577 HANDLE hFind;
1578
1579 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581
Paul Bakker9af723c2014-05-01 13:03:14 +02001582 memset( szDir, 0, sizeof(szDir) );
1583 memset( filename, 0, MAX_PATH );
1584 memcpy( filename, path, len );
1585 filename[len++] = '\\';
1586 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587 filename[len++] = '*';
1588
Simon B3c6b18d2016-11-03 01:11:37 +00001589 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001590 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001591 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593
1594 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001595 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597
1598 len = MAX_PATH - len;
1599 do
1600 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001601 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602
1603 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1604 continue;
1605
Paul Bakker9af723c2014-05-01 13:03:14 +02001606 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001607 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001608 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001609 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001610 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001611 {
1612 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1613 goto cleanup;
1614 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617 if( w_ret < 0 )
1618 ret++;
1619 else
1620 ret += w_ret;
1621 }
1622 while( FindNextFileW( hFind, &file_data ) != 0 );
1623
Paul Bakker66d5d072014-06-17 16:39:18 +02001624 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001626
Ron Eldor36d90422017-01-09 15:09:16 +02001627cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001629#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001630 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001631 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001632 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001633 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001634 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635 DIR *dir = opendir( path );
1636
Paul Bakker66d5d072014-06-17 16:39:18 +02001637 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639
Ron Eldor63140682017-01-09 19:27:59 +02001640#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001641 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001642 {
1643 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001644 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001645 }
Ron Eldor63140682017-01-09 19:27:59 +02001646#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001647
Paul Elliottfb91a482021-03-05 14:17:51 +00001648 memset( &sb, 0, sizeof( sb ) );
1649
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001650 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001651 {
Andres AGf9113192016-09-02 14:06:04 +01001652 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1653 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001654
Andres AGf9113192016-09-02 14:06:04 +01001655 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001656 {
Andres AGf9113192016-09-02 14:06:04 +01001657 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1658 goto cleanup;
1659 }
1660 else if( stat( entry_name, &sb ) == -1 )
1661 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001662 if( errno == ENOENT )
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001663 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001664 /* Broken symbolic link - ignore this entry.
1665 stat(2) will return this error for either (a) a dangling
1666 symlink or (b) a missing file.
1667 Given that we have just obtained the filename from readdir,
1668 assume that it does exist and therefore treat this as a
1669 dangling symlink. */
1670 continue;
1671 }
1672 else
1673 {
1674 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001675 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1676 goto cleanup;
1677 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678 }
1679
1680 if( !S_ISREG( sb.st_mode ) )
1681 continue;
1682
1683 // Ignore parse errors
1684 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001686 if( t_ret < 0 )
1687 ret++;
1688 else
1689 ret += t_ret;
1690 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001691
1692cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001693 closedir( dir );
1694
Ron Eldor63140682017-01-09 19:27:59 +02001695#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001696 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001698#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001699
Paul Bakkerbe089b02013-10-14 15:51:50 +02001700#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001701
1702 return( ret );
1703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001706/*
1707 * OtherName ::= SEQUENCE {
1708 * type-id OBJECT IDENTIFIER,
1709 * value [0] EXPLICIT ANY DEFINED BY type-id }
1710 *
1711 * HardwareModuleName ::= SEQUENCE {
1712 * hwType OBJECT IDENTIFIER,
1713 * hwSerialNum OCTET STRING }
1714 *
1715 * NOTE: we currently only parse and use otherName of type HwModuleName,
1716 * as defined in RFC 4108.
1717 */
1718static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1719 mbedtls_x509_san_other_name *other_name )
1720{
Janos Follathab23cd12019-05-09 13:53:57 +01001721 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001722 size_t len;
1723 unsigned char *p = subject_alt_name->p;
1724 const unsigned char *end = p + subject_alt_name->len;
1725 mbedtls_x509_buf cur_oid;
1726
1727 if( ( subject_alt_name->tag &
1728 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1729 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1730 {
1731 /*
1732 * The given subject alternative name is not of type "othername".
1733 */
1734 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1735 }
1736
1737 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1738 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001739 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001740
1741 cur_oid.tag = MBEDTLS_ASN1_OID;
1742 cur_oid.p = p;
1743 cur_oid.len = len;
1744
1745 /*
1746 * Only HwModuleName is currently supported.
1747 */
1748 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1749 {
1750 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1751 }
1752
Ron Eldor890819a2019-05-13 19:03:04 +03001753 if( p + len >= end )
1754 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001755 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001756 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1757 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001758 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001759 p += len;
1760 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1761 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001762 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001763
1764 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1765 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001766 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001767
1768 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001769 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001770
1771 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1772 other_name->value.hardware_module_name.oid.p = p;
1773 other_name->value.hardware_module_name.oid.len = len;
1774
Ron Eldor890819a2019-05-13 19:03:04 +03001775 if( p + len >= end )
1776 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001777 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001778 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1779 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001780 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001781 p += len;
1782 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1783 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001784 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001785
1786 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1787 other_name->value.hardware_module_name.val.p = p;
1788 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001789 p += len;
1790 if( p != end )
1791 {
Ron Eldor890819a2019-05-13 19:03:04 +03001792 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001793 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001794 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1795 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001796 }
1797 return( 0 );
1798}
1799
Peter Kolbus9a969b62018-12-11 13:55:56 -06001800int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1801 mbedtls_x509_subject_alternative_name *san )
1802{
1803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1804 switch( san_buf->tag &
1805 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1806 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1807 {
1808 /*
1809 * otherName
1810 */
1811 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1812 {
1813 mbedtls_x509_san_other_name other_name;
1814
1815 ret = x509_get_other_name( san_buf, &other_name );
1816 if( ret != 0 )
1817 return( ret );
1818
1819 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1820 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1821 memcpy( &san->san.other_name,
1822 &other_name, sizeof( other_name ) );
1823
1824 }
1825 break;
1826
1827 /*
1828 * dNSName
1829 */
1830 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1831 {
1832 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1833 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1834
1835 memcpy( &san->san.unstructured_name,
1836 san_buf, sizeof( *san_buf ) );
1837
1838 }
1839 break;
1840
1841 /*
1842 * Type not supported
1843 */
1844 default:
1845 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1846 }
1847 return( 0 );
1848}
1849
1850#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001851static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001852 const mbedtls_x509_sequence
1853 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001854 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001855{
Janos Follath865b3eb2019-12-16 11:46:15 +00001856 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03001857 size_t i;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001858 size_t n = *size;
1859 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001861 mbedtls_x509_subject_alternative_name san;
1862 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001863
1864 while( cur != NULL )
1865 {
Ron Eldor890819a2019-05-13 19:03:04 +03001866 memset( &san, 0, sizeof( san ) );
1867 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1868 if( parse_ret != 0 )
1869 {
1870 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1871 {
1872 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1873 MBEDTLS_X509_SAFE_SNPRINTF;
1874 }
1875 else
1876 {
1877 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1878 MBEDTLS_X509_SAFE_SNPRINTF;
1879 }
1880 cur = cur->next;
1881 continue;
1882 }
1883
1884 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001885 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001886 /*
1887 * otherName
1888 */
Ron Eldora2913912019-05-16 16:17:38 +03001889 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001890 {
1891 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1892
1893 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1894 MBEDTLS_X509_SAFE_SNPRINTF;
1895
1896 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1897 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001898 {
Ron Eldor890819a2019-05-13 19:03:04 +03001899 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1900 MBEDTLS_X509_SAFE_SNPRINTF;
1901 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001902 MBEDTLS_X509_SAFE_SNPRINTF;
1903
Ron Eldor890819a2019-05-13 19:03:04 +03001904 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1905 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001906
Ron Eldor890819a2019-05-13 19:03:04 +03001907 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1908 MBEDTLS_X509_SAFE_SNPRINTF;
1909
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03001910 for( i = 0; i < other_name->value.hardware_module_name.val.len; i++ )
Ron Eldor890819a2019-05-13 19:03:04 +03001911 {
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03001912 ret = mbedtls_snprintf( p, n, "%02X", other_name->value.hardware_module_name.val.p[i] );
1913 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath22f605f2019-05-10 10:37:17 +01001914 }
Ron Eldor890819a2019-05-13 19:03:04 +03001915 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1916 }
1917 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001918
1919 /*
1920 * dNSName
1921 */
Ron Eldora2913912019-05-16 16:17:38 +03001922 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001923 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001924 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1925 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001926 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001927 {
1928 *p = '\0';
1929 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1930 }
Ron Eldora2913912019-05-16 16:17:38 +03001931
1932 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1933 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001934 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001935 }
1936 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001937
1938 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001939 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001940 */
1941 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001942 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1943 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001944 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001945 }
1946
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001947 cur = cur->next;
1948 }
1949
1950 *p = '\0';
1951
1952 *size = n;
1953 *buf = p;
1954
1955 return( 0 );
1956}
1957
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001958#define PRINT_ITEM(i) \
1959 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001961 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001962 sep = ", "; \
1963 }
1964
1965#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001966 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001967 PRINT_ITEM( name );
1968
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001969static int x509_info_cert_type( char **buf, size_t *size,
1970 unsigned char ns_cert_type )
1971{
Janos Follath865b3eb2019-12-16 11:46:15 +00001972 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001973 size_t n = *size;
1974 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001975 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001978 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1980 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1981 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001982 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1983 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1984 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001985
1986 *size = n;
1987 *buf = p;
1988
1989 return( 0 );
1990}
1991
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001992#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001993 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001994 PRINT_ITEM( name );
1995
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001996static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001997 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001998{
Janos Follath865b3eb2019-12-16 11:46:15 +00001999 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002000 size_t n = *size;
2001 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002002 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2005 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002006 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2007 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2008 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2010 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002011 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2012 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002013
2014 *size = n;
2015 *buf = p;
2016
2017 return( 0 );
2018}
2019
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002020static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002022{
Janos Follath865b3eb2019-12-16 11:46:15 +00002023 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002024 const char *desc;
2025 size_t n = *size;
2026 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002028 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002029
2030 while( cur != NULL )
2031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002033 desc = "???";
2034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002036 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002037
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002038 sep = ", ";
2039
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002040 cur = cur->next;
2041 }
2042
2043 *size = n;
2044 *buf = p;
2045
2046 return( 0 );
2047}
2048
Ron Eldor74d9acc2019-03-21 14:00:03 +02002049static int x509_info_cert_policies( char **buf, size_t *size,
2050 const mbedtls_x509_sequence *certificate_policies )
2051{
Janos Follath865b3eb2019-12-16 11:46:15 +00002052 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002053 const char *desc;
2054 size_t n = *size;
2055 char *p = *buf;
2056 const mbedtls_x509_sequence *cur = certificate_policies;
2057 const char *sep = "";
2058
2059 while( cur != NULL )
2060 {
2061 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2062 desc = "???";
2063
2064 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2065 MBEDTLS_X509_SAFE_SNPRINTF;
2066
2067 sep = ", ";
2068
2069 cur = cur->next;
2070 }
2071
2072 *size = n;
2073 *buf = p;
2074
2075 return( 0 );
2076}
2077
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002078/*
2079 * Return an informational string about the certificate.
2080 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002081#define BEFORE_COLON 18
2082#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2084 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002085{
Janos Follath865b3eb2019-12-16 11:46:15 +00002086 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002087 size_t n;
2088 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002089 char key_size_str[BEFORE_COLON];
2090
2091 p = buf;
2092 n = size;
2093
Janos Follath98e28a72016-05-31 14:03:54 +01002094 if( NULL == crt )
2095 {
2096 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2097 MBEDTLS_X509_SAFE_SNPRINTF;
2098
2099 return( (int) ( size - n ) );
2100 }
2101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002103 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002104 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002106 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002107 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002110 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002113 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002115 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002118 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002120 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002123 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2124 crt->valid_from.year, crt->valid_from.mon,
2125 crt->valid_from.day, crt->valid_from.hour,
2126 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002127 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002130 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2131 crt->valid_to.year, crt->valid_to.mon,
2132 crt->valid_to.day, crt->valid_to.hour,
2133 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002134 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002137 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002140 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002141 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002142
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002143 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2145 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002146 {
2147 return( ret );
2148 }
2149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002151 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002152 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002153
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002154 /*
2155 * Optional extensions
2156 */
2157
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002158 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002161 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002162 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002163
2164 if( crt->max_pathlen > 0 )
2165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002167 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002168 }
2169 }
2170
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002171 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002172 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002173 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002174 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002175
2176 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002177 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002178 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002179 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002180 }
2181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002185 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002186
2187 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2188 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002189 }
2190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002194 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002195
2196 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2197 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002198 }
2199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002203 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002204
2205 if( ( ret = x509_info_ext_key_usage( &p, &n,
2206 &crt->ext_key_usage ) ) != 0 )
2207 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002208 }
2209
Ron Eldor74d9acc2019-03-21 14:00:03 +02002210 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2211 {
2212 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2213 MBEDTLS_X509_SAFE_SNPRINTF;
2214
2215 if( ( ret = x509_info_cert_policies( &p, &n,
2216 &crt->certificate_policies ) ) != 0 )
2217 return( ret );
2218 }
2219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002221 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002222
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002223 return( (int) ( size - n ) );
2224}
2225
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002226struct x509_crt_verify_string {
2227 int code;
2228 const char *string;
2229};
2230
Hanno Becker7ac83f92020-10-09 10:48:22 +01002231#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002232static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002233 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002234 { 0, NULL }
2235};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002236#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002237
2238int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002239 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002240{
Janos Follath865b3eb2019-12-16 11:46:15 +00002241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002242 const struct x509_crt_verify_string *cur;
2243 char *p = buf;
2244 size_t n = size;
2245
2246 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2247 {
2248 if( ( flags & cur->code ) == 0 )
2249 continue;
2250
2251 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002252 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002253 flags ^= cur->code;
2254 }
2255
2256 if( flags != 0 )
2257 {
2258 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2259 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002260 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002261 }
2262
2263 return( (int) ( size - n ) );
2264}
Hanno Becker612a2f12020-10-09 09:19:39 +01002265#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002266
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002267int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2268 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002269{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002270 unsigned int usage_must, usage_may;
2271 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2272 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2273
2274 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2275 return( 0 );
2276
2277 usage_must = usage & ~may_mask;
2278
2279 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2280 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2281
2282 usage_may = usage & may_mask;
2283
2284 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002286
2287 return( 0 );
2288}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002291 const char *usage_oid,
2292 size_t usage_len )
2293{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002295
2296 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002298 return( 0 );
2299
2300 /*
2301 * Look for the requested usage (or wildcard ANY) in our list
2302 */
2303 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002306
2307 if( cur_oid->len == usage_len &&
2308 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2309 {
2310 return( 0 );
2311 }
2312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002314 return( 0 );
2315 }
2316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002318}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002321/*
2322 * Return 1 if the certificate is revoked, or 0 otherwise.
2323 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002324int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002325{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002327
2328 while( cur != NULL && cur->serial.len != 0 )
2329 {
2330 if( crt->serial.len == cur->serial.len &&
2331 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2332 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002333 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002334 }
2335
2336 cur = cur->next;
2337 }
2338
2339 return( 0 );
2340}
2341
2342/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002343 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002344 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002347 mbedtls_x509_crl *crl_list,
2348 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002349{
2350 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002351 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002352#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002353 psa_algorithm_t psa_algorithm;
2354#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002356#endif /* MBEDTLS_USE_PSA_CRYPTO */
2357 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002358
2359 if( ca == NULL )
2360 return( flags );
2361
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002362 while( crl_list != NULL )
2363 {
2364 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002365 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002366 {
2367 crl_list = crl_list->next;
2368 continue;
2369 }
2370
2371 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002372 * Check if the CA is configured to sign CRLs
2373 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002374 if( mbedtls_x509_crt_check_key_usage( ca,
2375 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002378 break;
2379 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002380
2381 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002382 * Check if CRL is correctly signed by the trusted CA
2383 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002384 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2385 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2386
2387 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2388 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2389
pespacek7599a772022-02-07 14:40:55 +01002390#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002391 psa_algorithm = mbedtls_hash_info_psa_from_md( crl_list->sig_md );
pespacekd924e552022-02-28 11:49:54 +01002392 if( psa_hash_compute( psa_algorithm,
2393 crl_list->tbs.p,
2394 crl_list->tbs.len,
2395 hash,
2396 sizeof( hash ),
2397 &hash_length ) != PSA_SUCCESS )
pespaceka7a64692022-02-14 15:18:43 +01002398 {
2399 /* Note: this can't happen except after an internal error */
2400 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2401 break;
2402 }
pespacek7599a772022-02-07 14:40:55 +01002403#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002405 hash_length = mbedtls_md_get_size( md_info );
2406 if( mbedtls_md( md_info,
2407 crl_list->tbs.p,
2408 crl_list->tbs.len,
2409 hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002410 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002411 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002413 break;
2414 }
pespaceka7a64692022-02-14 15:18:43 +01002415#endif /* MBEDTLS_USE_PSA_CRYPTO */
2416
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002417 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002418 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
pespacek7599a772022-02-07 14:40:55 +01002421 crl_list->sig_md, hash, hash_length,
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002422 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002425 break;
2426 }
2427
2428 /*
2429 * Check for validity of CRL (Do not drop out)
2430 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002431 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002433
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002434 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002435 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002436
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002437 /*
2438 * Check if certificate is revoked
2439 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002440 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002443 break;
2444 }
2445
2446 crl_list = crl_list->next;
2447 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002448
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002449 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002450}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002452
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002453/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002454 * Check the signature of a certificate by its parent
2455 */
2456static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002457 mbedtls_x509_crt *parent,
2458 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002459{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002460 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02002461 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002462#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2463 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002464 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002465 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002466
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002467 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002468 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002469 return( -1 );
2470#else
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002471 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( child->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002472 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002473
pespacek7599a772022-02-07 14:40:55 +01002474 status = psa_hash_compute( hash_alg,
2475 child->tbs.p,
2476 child->tbs.len,
2477 hash,
pespacekb9f07a72022-02-14 15:13:26 +01002478 sizeof( hash ),
pespacek7599a772022-02-07 14:40:55 +01002479 &hash_len );
2480 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002481 {
pespacek3110c7b2022-02-14 15:07:41 +01002482 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002483 }
2484
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002485#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002486 /* Skip expensive computation on obvious mismatch */
2487 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002488 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002489
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002490#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002491 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2492 {
2493 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002494 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002495 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002496 }
2497#else
2498 (void) rs_ctx;
2499#endif
2500
2501 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002502 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002503 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002504}
2505
2506/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002507 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2508 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002509 *
2510 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2513 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002514 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002515{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002516 int need_ca_bit;
2517
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002518 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002519 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002520 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002521
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002522 /* Parent must have the basicConstraints CA bit set as a general rule */
2523 need_ca_bit = 1;
2524
2525 /* Exception: v1/v2 certificates that are locally trusted. */
2526 if( top && parent->version < 3 )
2527 need_ca_bit = 0;
2528
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002529 if( need_ca_bit && ! parent->ca_istrue )
2530 return( -1 );
2531
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002532 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002534 {
2535 return( -1 );
2536 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002537
2538 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002539}
2540
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002541/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002542 * Find a suitable parent for child in candidates, or return NULL.
2543 *
2544 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002545 * 1. subject name matches child's issuer
2546 * 2. if necessary, the CA bit is set and key usage allows signing certs
2547 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002548 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002549 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002550 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002551 * If there's a suitable candidate which is also time-valid, return the first
2552 * such. Otherwise, return the first suitable candidate (or NULL if there is
2553 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002554 *
2555 * The rationale for this rule is that someone could have a list of trusted
2556 * roots with two versions on the same root with different validity periods.
2557 * (At least one user reported having such a list and wanted it to just work.)
2558 * The reason we don't just require time-validity is that generally there is
2559 * only one version, and if it's expired we want the flags to state that
2560 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002561 *
2562 * The rationale for rule 3 (signature for trusted roots) is that users might
2563 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002564 * way we select the correct one is by checking the signature (as we don't
2565 * rely on key identifier extensions). (This is one way users might choose to
2566 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002567 *
2568 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002569 * - [in] child: certificate for which we're looking for a parent
2570 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002571 * - [out] r_parent: parent found (or NULL)
2572 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002573 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2574 * of the chain, 0 otherwise
2575 * - [in] path_cnt: number of intermediates seen so far
2576 * - [in] self_cnt: number of self-signed intermediates seen so far
2577 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002578 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002579 *
2580 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002581 * - 0 on success
2582 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002583 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002584static int x509_crt_find_parent_in(
2585 mbedtls_x509_crt *child,
2586 mbedtls_x509_crt *candidates,
2587 mbedtls_x509_crt **r_parent,
2588 int *r_signature_is_good,
2589 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002590 unsigned path_cnt,
2591 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002592 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002593{
Janos Follath865b3eb2019-12-16 11:46:15 +00002594 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002595 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002596 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002597
2598#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002599 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002600 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2601 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002602 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002603 parent = rs_ctx->parent;
2604 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002605 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002606
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002607 /* clear saved state */
2608 rs_ctx->parent = NULL;
2609 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002610 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002611
2612 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002613 goto check_signature;
2614 }
2615#endif
2616
2617 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002618 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002619
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002620 for( parent = candidates; parent != NULL; parent = parent->next )
2621 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002622 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002623 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002624 continue;
2625
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002626 /* +1 because stored max_pathlen is 1 higher that the actual value */
2627 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002628 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002629 {
2630 continue;
2631 }
2632
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002633 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002634#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2635check_signature:
2636#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002637 ret = x509_crt_check_signature( child, parent, rs_ctx );
2638
2639#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002640 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2641 {
2642 /* save state */
2643 rs_ctx->parent = parent;
2644 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002645 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002646
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002647 return( ret );
2648 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002649#else
2650 (void) ret;
2651#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002652
2653 signature_is_good = ret == 0;
2654 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002655 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002656
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002657 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002658 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2659 mbedtls_x509_time_is_future( &parent->valid_from ) )
2660 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002661 if( fallback_parent == NULL )
2662 {
2663 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002664 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002665 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002666
2667 continue;
2668 }
2669
Andy Gross1f627142019-01-30 10:25:53 -06002670 *r_parent = parent;
2671 *r_signature_is_good = signature_is_good;
2672
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002673 break;
2674 }
2675
Andy Gross1f627142019-01-30 10:25:53 -06002676 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002677 {
2678 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002679 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002680 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002681
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002682 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002683}
2684
2685/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002686 * Find a parent in trusted CAs or the provided chain, or return NULL.
2687 *
2688 * Searches in trusted CAs first, and return the first suitable parent found
2689 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002690 *
2691 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002692 * - [in] child: certificate for which we're looking for a parent, followed
2693 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002694 * - [in] trust_ca: list of locally trusted certificates
2695 * - [out] parent: parent found (or NULL)
2696 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2697 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2698 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2699 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002700 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002701 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002702 *
2703 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002704 * - 0 on success
2705 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002706 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002707static int x509_crt_find_parent(
2708 mbedtls_x509_crt *child,
2709 mbedtls_x509_crt *trust_ca,
2710 mbedtls_x509_crt **parent,
2711 int *parent_is_trusted,
2712 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002713 unsigned path_cnt,
2714 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002715 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002716{
Janos Follath865b3eb2019-12-16 11:46:15 +00002717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002718 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002719
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002720 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002721
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002722#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002723 /* restore then clear saved state if we have some stored */
2724 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2725 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002726 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002727 rs_ctx->parent_is_trusted = -1;
2728 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002729#endif
2730
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002731 while( 1 ) {
2732 search_list = *parent_is_trusted ? trust_ca : child->next;
2733
2734 ret = x509_crt_find_parent_in( child, search_list,
2735 parent, signature_is_good,
2736 *parent_is_trusted,
2737 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002738
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002739#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002740 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2741 {
2742 /* save state */
2743 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002744 return( ret );
2745 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002746#else
2747 (void) ret;
2748#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002749
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002750 /* stop here if found or already in second iteration */
2751 if( *parent != NULL || *parent_is_trusted == 0 )
2752 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002753
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002754 /* prepare second iteration */
2755 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002756 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002757
2758 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002759 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002760 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002761 *parent_is_trusted = 0;
2762 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002763 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002764
2765 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002766}
2767
2768/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002769 * Check if an end-entity certificate is locally trusted
2770 *
2771 * Currently we require such certificates to be self-signed (actually only
2772 * check for self-issued as self-signatures are not checked)
2773 */
2774static int x509_crt_check_ee_locally_trusted(
2775 mbedtls_x509_crt *crt,
2776 mbedtls_x509_crt *trust_ca )
2777{
2778 mbedtls_x509_crt *cur;
2779
2780 /* must be self-issued */
2781 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2782 return( -1 );
2783
2784 /* look for an exact match with trusted cert */
2785 for( cur = trust_ca; cur != NULL; cur = cur->next )
2786 {
2787 if( crt->raw.len == cur->raw.len &&
2788 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2789 {
2790 return( 0 );
2791 }
2792 }
2793
2794 /* too bad */
2795 return( -1 );
2796}
2797
2798/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002799 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002800 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002801 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2802 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002803 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002804 * such that every cert in the chain is a child of the next one,
2805 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002806 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002807 * Verify that chain and return it with flags for all issues found.
2808 *
2809 * Special cases:
2810 * - EE == Rj -> return a one-element list containing it
2811 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2812 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002813 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002814 * Tests for (aspects of) this function should include at least:
2815 * - trusted EE
2816 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002817 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002818 * - if relevant: EE untrusted
2819 * - if relevant: EE -> intermediate, untrusted
2820 * with the aspect under test checked at each relevant level (EE, int, root).
2821 * For some aspects longer chains are required, but usually length 2 is
2822 * enough (but length 1 is not in general).
2823 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002824 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002825 * - [in] crt: the cert list EE, C1, ..., Cn
2826 * - [in] trust_ca: the trusted list R1, ..., Rp
2827 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002828 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002829 * Only valid when return value is 0, may contain garbage otherwise!
2830 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002831 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002832 *
2833 * Return value:
2834 * - non-zero if the chain could not be fully built and examined
2835 * - 0 is the chain was successfully built and examined,
2836 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002837 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002838static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002839 mbedtls_x509_crt *crt,
2840 mbedtls_x509_crt *trust_ca,
2841 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002842 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2843 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002844 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002845 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002846 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002847{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002848 /* Don't initialize any of those variables here, so that the compiler can
2849 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002851 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002852 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002853 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002854 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002855 int parent_is_trusted;
2856 int child_is_trusted;
2857 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002858 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002859 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002860
2861#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2862 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002863 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002864 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002865 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002866 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002867 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002868
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002869 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002870 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002871 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002872 flags = &cur->flags;
2873
2874 goto find_parent;
2875 }
2876#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002877
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002878 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002879 self_cnt = 0;
2880 parent_is_trusted = 0;
2881 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002882
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002883 while( 1 ) {
2884 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002885 cur = &ver_chain->items[ver_chain->len];
2886 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002887 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002888 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002889 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002890
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002891 /* Check time-validity (all certificates) */
2892 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2893 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002894
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002895 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2896 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002897
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002898 /* Stop here for trusted roots (but not for trusted EE certs) */
2899 if( child_is_trusted )
2900 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002901
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002902 /* Check signature algorithm: MD & PK algs */
2903 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2904 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002905
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002906 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2907 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002908
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002909 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002910 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002911 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2912 {
2913 return( 0 );
2914 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002915
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002916#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2917find_parent:
2918#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002919
2920 /* Obtain list of potential trusted signers from CA callback,
2921 * or use statically provided list. */
2922#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2923 if( f_ca_cb != NULL )
2924 {
2925 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2926 mbedtls_free( ver_chain->trust_ca_cb_result );
2927 ver_chain->trust_ca_cb_result = NULL;
2928
2929 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2930 if( ret != 0 )
2931 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2932
2933 cur_trust_ca = ver_chain->trust_ca_cb_result;
2934 }
2935 else
2936#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2937 {
2938 ((void) f_ca_cb);
2939 ((void) p_ca_cb);
2940 cur_trust_ca = trust_ca;
2941 }
2942
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002943 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002944 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002945 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002946 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002947
2948#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002949 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2950 {
2951 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002952 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002953 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002954 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002955
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002956 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002957 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002958#else
2959 (void) ret;
2960#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002961
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002962 /* No parent? We're done here */
2963 if( parent == NULL )
2964 {
2965 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2966 return( 0 );
2967 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002968
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002969 /* Count intermediate self-issued (not necessarily self-signed) certs.
2970 * These can occur with some strategies for key rollover, see [SIRO],
2971 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002972 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002973 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2974 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002975 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002976 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002977
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002978 /* path_cnt is 0 for the first intermediate CA,
2979 * and if parent is trusted it's not an intermediate CA */
2980 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002981 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002982 {
2983 /* return immediately to avoid overflow the chain array */
2984 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2985 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002986
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002987 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002988 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002989 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2990
2991 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002992 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002993 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002996 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002997 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002998#else
2999 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003000#endif
3001
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003002 /* prepare for next iteration */
3003 child = parent;
3004 parent = NULL;
3005 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003006 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003007 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003008}
3009
3010/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003011 * Check for CN match
3012 */
3013static int x509_crt_check_cn( const mbedtls_x509_buf *name,
3014 const char *cn, size_t cn_len )
3015{
3016 /* try exact match */
3017 if( name->len == cn_len &&
3018 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3019 {
3020 return( 0 );
3021 }
3022
3023 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003024 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003025 {
3026 return( 0 );
3027 }
3028
3029 return( -1 );
3030}
3031
3032/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003033 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3034 */
3035static int x509_crt_check_san( const mbedtls_x509_buf *name,
3036 const char *cn, size_t cn_len )
3037{
3038 const unsigned char san_type = (unsigned char) name->tag &
3039 MBEDTLS_ASN1_TAG_VALUE_MASK;
3040
3041 /* dNSName */
3042 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3043 return( x509_crt_check_cn( name, cn, cn_len ) );
3044
3045 /* (We may handle other types here later.) */
3046
3047 /* Unrecognized type */
3048 return( -1 );
3049}
3050
3051/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003052 * Verify the requested CN - only call this if cn is not NULL!
3053 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003054static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003055 const char *cn,
3056 uint32_t *flags )
3057{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003058 const mbedtls_x509_name *name;
3059 const mbedtls_x509_sequence *cur;
3060 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003061
3062 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3063 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003064 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003065 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003066 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003067 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003068 }
3069
3070 if( cur == NULL )
3071 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3072 }
3073 else
3074 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003075 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003076 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003077 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3078 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003079 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003080 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003081 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003082 }
3083
3084 if( name == NULL )
3085 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3086 }
3087}
3088
3089/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003090 * Merge the flags for all certs in the chain, after calling callback
3091 */
3092static int x509_crt_merge_flags_with_cb(
3093 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003094 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003095 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3096 void *p_vrfy )
3097{
Janos Follath865b3eb2019-12-16 11:46:15 +00003098 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003099 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003100 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003101 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003102
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003103 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003104 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003105 cur = &ver_chain->items[i-1];
3106 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003107
3108 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003109 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003110 return( ret );
3111
3112 *flags |= cur_flags;
3113 }
3114
3115 return( 0 );
3116}
3117
3118/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003119 * Verify the certificate validity, with profile, restartable version
3120 *
3121 * This function:
3122 * - checks the requested CN (if any)
3123 * - checks the type and size of the EE cert's key,
3124 * as that isn't done as part of chain building/verification currently
3125 * - builds and verifies the chain
3126 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003127 *
3128 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3129 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3130 * verification routine to search for trusted signers, and CRLs will
3131 * be disabled. Otherwise, `trust_ca` will be used as the static list
3132 * of trusted signers, and `ca_crl` will be use as the static list
3133 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003134 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003135static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003136 mbedtls_x509_crt *trust_ca,
3137 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003138 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3139 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003140 const mbedtls_x509_crt_profile *profile,
3141 const char *cn, uint32_t *flags,
3142 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3143 void *p_vrfy,
3144 mbedtls_x509_crt_restart_ctx *rs_ctx )
3145{
Janos Follath865b3eb2019-12-16 11:46:15 +00003146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003147 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003148 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003149 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003150
3151 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003152 ee_flags = 0;
3153 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003154
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003155 if( profile == NULL )
3156 {
3157 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3158 goto exit;
3159 }
3160
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003161 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003162 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003163 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003164
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003165 /* Check the type and size of the key */
3166 pk_type = mbedtls_pk_get_type( &crt->pk );
3167
3168 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003169 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003170
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003171 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003172 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003173
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003174 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003175 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3176 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003177 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003178
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003179 if( ret != 0 )
3180 goto exit;
3181
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003182 /* Merge end-entity flags */
3183 ver_chain.items[0].flags |= ee_flags;
3184
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003185 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003186 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003187
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003188exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003189
3190#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3191 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3192 mbedtls_free( ver_chain.trust_ca_cb_result );
3193 ver_chain.trust_ca_cb_result = NULL;
3194#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3195
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003196#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3197 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3198 mbedtls_x509_crt_restart_free( rs_ctx );
3199#endif
3200
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003201 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3202 * the SSL module for authmode optional, but non-zero return from the
3203 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003204 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3205 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3206
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003207 if( ret != 0 )
3208 {
3209 *flags = (uint32_t) -1;
3210 return( ret );
3211 }
3212
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003213 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003215
3216 return( 0 );
3217}
3218
Hanno Becker3116fb32019-03-28 13:34:42 +00003219
3220/*
3221 * Verify the certificate validity (default profile, not restartable)
3222 */
3223int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3224 mbedtls_x509_crt *trust_ca,
3225 mbedtls_x509_crl *ca_crl,
3226 const char *cn, uint32_t *flags,
3227 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3228 void *p_vrfy )
3229{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003230 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003231 NULL, NULL,
3232 &mbedtls_x509_crt_profile_default,
3233 cn, flags,
3234 f_vrfy, p_vrfy, NULL ) );
3235}
3236
3237/*
3238 * Verify the certificate validity (user-chosen profile, not restartable)
3239 */
3240int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3241 mbedtls_x509_crt *trust_ca,
3242 mbedtls_x509_crl *ca_crl,
3243 const mbedtls_x509_crt_profile *profile,
3244 const char *cn, uint32_t *flags,
3245 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3246 void *p_vrfy )
3247{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003248 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003249 NULL, NULL,
3250 profile, cn, flags,
3251 f_vrfy, p_vrfy, NULL ) );
3252}
3253
3254#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3255/*
3256 * Verify the certificate validity (user-chosen profile, CA callback,
3257 * not restartable).
3258 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003259int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003260 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3261 void *p_ca_cb,
3262 const mbedtls_x509_crt_profile *profile,
3263 const char *cn, uint32_t *flags,
3264 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3265 void *p_vrfy )
3266{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003267 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003268 f_ca_cb, p_ca_cb,
3269 profile, cn, flags,
3270 f_vrfy, p_vrfy, NULL ) );
3271}
3272#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3273
3274int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3275 mbedtls_x509_crt *trust_ca,
3276 mbedtls_x509_crl *ca_crl,
3277 const mbedtls_x509_crt_profile *profile,
3278 const char *cn, uint32_t *flags,
3279 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3280 void *p_vrfy,
3281 mbedtls_x509_crt_restart_ctx *rs_ctx )
3282{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003283 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003284 NULL, NULL,
3285 profile, cn, flags,
3286 f_vrfy, p_vrfy, rs_ctx ) );
3287}
3288
3289
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003290/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003291 * Initialize a certificate chain
3292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003294{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003296}
3297
3298/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003299 * Unallocate all certificate data
3300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 mbedtls_x509_crt *cert_cur = crt;
3304 mbedtls_x509_crt *cert_prv;
3305 mbedtls_x509_name *name_cur;
3306 mbedtls_x509_name *name_prv;
3307 mbedtls_x509_sequence *seq_cur;
3308 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003309
3310 if( crt == NULL )
3311 return;
3312
3313 do
3314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3318 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003319#endif
3320
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003321 name_cur = cert_cur->issuer.next;
3322 while( name_cur != NULL )
3323 {
3324 name_prv = name_cur;
3325 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003326 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003328 }
3329
3330 name_cur = cert_cur->subject.next;
3331 while( name_cur != NULL )
3332 {
3333 name_prv = name_cur;
3334 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003335 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003337 }
3338
3339 seq_cur = cert_cur->ext_key_usage.next;
3340 while( seq_cur != NULL )
3341 {
3342 seq_prv = seq_cur;
3343 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003344 mbedtls_platform_zeroize( seq_prv,
3345 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003347 }
3348
3349 seq_cur = cert_cur->subject_alt_names.next;
3350 while( seq_cur != NULL )
3351 {
3352 seq_prv = seq_cur;
3353 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003354 mbedtls_platform_zeroize( seq_prv,
3355 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003357 }
3358
Ron Eldor74d9acc2019-03-21 14:00:03 +02003359 seq_cur = cert_cur->certificate_policies.next;
3360 while( seq_cur != NULL )
3361 {
3362 seq_prv = seq_cur;
3363 seq_cur = seq_cur->next;
3364 mbedtls_platform_zeroize( seq_prv,
3365 sizeof( mbedtls_x509_sequence ) );
3366 mbedtls_free( seq_prv );
3367 }
3368
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003369 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003370 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003371 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003373 }
3374
3375 cert_cur = cert_cur->next;
3376 }
3377 while( cert_cur != NULL );
3378
3379 cert_cur = crt;
3380 do
3381 {
3382 cert_prv = cert_cur;
3383 cert_cur = cert_cur->next;
3384
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003385 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003386 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003388 }
3389 while( cert_cur != NULL );
3390}
3391
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003392#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3393/*
3394 * Initialize a restart context
3395 */
3396void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3397{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003398 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003399
3400 ctx->parent = NULL;
3401 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003402 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003403
3404 ctx->parent_is_trusted = -1;
3405
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003406 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003407 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003408 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003409}
3410
3411/*
3412 * Free the components of a restart context
3413 */
3414void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3415{
3416 if( ctx == NULL )
3417 return;
3418
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003419 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003420 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003421}
3422#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424#endif /* MBEDTLS_X509_CRT_PARSE_C */