blob: 53cdcf026629987675920519d27add7a8e20795d [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/*
2 * X.509 certificate parsing and verification
3 *
Jerome Forissier79013242021-07-28 10:24:04 +02004 * Copyright The Mbed TLS Contributors
Tom Van Eyckb0563632024-06-13 16:20:14 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Jens Wiklander817466c2018-05-22 13:49:31 +02006 */
7/*
8 * The ITU-T X.509 standard defines a certificate format for PKI.
9 *
10 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
11 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
12 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
13 *
14 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
15 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Jens Wiklander3d3b0592019-03-20 15:30:29 +010016 *
17 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Jens Wiklander817466c2018-05-22 13:49:31 +020018 */
19
Jerome Forissier79013242021-07-28 10:24:04 +020020#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020021
22#if defined(MBEDTLS_X509_CRT_PARSE_C)
23
24#include "mbedtls/x509_crt.h"
Tom Van Eyckb0563632024-06-13 16:20:14 +020025#include "x509_internal.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020026#include "mbedtls/error.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020027#include "mbedtls/oid.h"
Jens Wiklander3d3b0592019-03-20 15:30:29 +010028#include "mbedtls/platform_util.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020029
Jens Wiklander817466c2018-05-22 13:49:31 +020030#include <string.h>
31
32#if defined(MBEDTLS_PEM_PARSE_C)
33#include "mbedtls/pem.h"
34#endif
35
Jerome Forissier11fa71b2020-04-20 17:17:56 +020036#if defined(MBEDTLS_USE_PSA_CRYPTO)
37#include "psa/crypto.h"
Tom Van Eyckb0563632024-06-13 16:20:14 +020038#include "psa_util_internal.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020039#include "mbedtls/psa_util.h"
Jens Wiklander32b31802023-10-06 16:59:46 +020040#endif /* MBEDTLS_USE_PSA_CRYPTO */
Tom Van Eyckb0563632024-06-13 16:20:14 +020041#include "pk_internal.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020042
Jens Wiklander817466c2018-05-22 13:49:31 +020043#include "mbedtls/platform.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020044
45#if defined(MBEDTLS_THREADING_C)
46#include "mbedtls/threading.h"
47#endif
48
Jerome Forissier039e02d2022-08-09 17:10:15 +020049#if defined(MBEDTLS_HAVE_TIME)
Jens Wiklander817466c2018-05-22 13:49:31 +020050#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Sungbae Yoo4d211f32024-11-19 02:47:55 +000051#ifndef WIN32_LEAN_AND_MEAN
Tom Van Eyckb0563632024-06-13 16:20:14 +020052#define WIN32_LEAN_AND_MEAN
Sungbae Yoo4d211f32024-11-19 02:47:55 +000053#endif
Jens Wiklander817466c2018-05-22 13:49:31 +020054#include <windows.h>
55#else
56#include <time.h>
57#endif
Jerome Forissier039e02d2022-08-09 17:10:15 +020058#endif
Jens Wiklander817466c2018-05-22 13:49:31 +020059
60#if defined(MBEDTLS_FS_IO)
61#include <stdio.h>
62#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
63#include <sys/types.h>
64#include <sys/stat.h>
Jens Wiklander32b31802023-10-06 16:59:46 +020065#if defined(__MBED__)
66#include <platform/mbed_retarget.h>
67#else
Jens Wiklander817466c2018-05-22 13:49:31 +020068#include <dirent.h>
Jens Wiklander32b31802023-10-06 16:59:46 +020069#endif /* __MBED__ */
70#include <errno.h>
Jens Wiklander817466c2018-05-22 13:49:31 +020071#endif /* !_WIN32 || EFIX64 || EFI32 */
72#endif
73
Jens Wiklander3d3b0592019-03-20 15:30:29 +010074/*
75 * Item in a verification chain: cert and flags for it
76 */
77typedef struct {
78 mbedtls_x509_crt *crt;
79 uint32_t flags;
80} x509_crt_verify_chain_item;
81
82/*
83 * Max size of verification chain: end-entity + intermediates + trusted root
84 */
Jens Wiklander32b31802023-10-06 16:59:46 +020085#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Jens Wiklander817466c2018-05-22 13:49:31 +020086
Jerome Forissier79013242021-07-28 10:24:04 +020087/* Default profile. Do not remove items unless there are serious security
88 * concerns. */
Jens Wiklander817466c2018-05-22 13:49:31 +020089const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
90{
Jens Wiklander32b31802023-10-06 16:59:46 +020091 /* Hashes from SHA-256 and above. Note that this selection
92 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
93 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
94 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
95 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Jens Wiklander817466c2018-05-22 13:49:31 +020096 0xFFFFFFF, /* Any PK alg */
Tom Van Eyckb0563632024-06-13 16:20:14 +020097#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jens Wiklander32b31802023-10-06 16:59:46 +020098 /* Curves at or above 128-bit security level. Note that this selection
99 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
100 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
101 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
102 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
103 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
104 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
105 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
106 0,
Tom Van Eyckb0563632024-06-13 16:20:14 +0200107#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Jens Wiklander32b31802023-10-06 16:59:46 +0200108 0,
Tom Van Eyckb0563632024-06-13 16:20:14 +0200109#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Jens Wiklander817466c2018-05-22 13:49:31 +0200110 2048,
111};
112
Jens Wiklander32b31802023-10-06 16:59:46 +0200113/* Next-generation profile. Currently identical to the default, but may
114 * be tightened at any time. */
Jens Wiklander817466c2018-05-22 13:49:31 +0200115const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
116{
Jens Wiklander32b31802023-10-06 16:59:46 +0200117 /* Hashes from SHA-256 and above. */
118 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
119 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
120 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Jens Wiklander817466c2018-05-22 13:49:31 +0200121 0xFFFFFFF, /* Any PK alg */
122#if defined(MBEDTLS_ECP_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200123 /* Curves at or above 128-bit security level. */
124 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
125 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
126 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
127 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
128 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
129 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
130 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Jens Wiklander817466c2018-05-22 13:49:31 +0200131#else
132 0,
133#endif
134 2048,
135};
136
137/*
138 * NSA Suite B Profile
139 */
140const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
141{
142 /* Only SHA-256 and 384 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200143 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
144 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Jens Wiklander817466c2018-05-22 13:49:31 +0200145 /* Only ECDSA */
Jens Wiklander32b31802023-10-06 16:59:46 +0200146 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
147 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Tom Van Eyckb0563632024-06-13 16:20:14 +0200148#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jens Wiklander817466c2018-05-22 13:49:31 +0200149 /* Only NIST P-256 and P-384 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200150 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
151 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Tom Van Eyckb0563632024-06-13 16:20:14 +0200152#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Jens Wiklander817466c2018-05-22 13:49:31 +0200153 0,
Tom Van Eyckb0563632024-06-13 16:20:14 +0200154#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Jens Wiklander817466c2018-05-22 13:49:31 +0200155 0,
156};
157
158/*
Jens Wiklander32b31802023-10-06 16:59:46 +0200159 * Empty / all-forbidden profile
160 */
161const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
162{
163 0,
164 0,
165 0,
166 (uint32_t) -1,
167};
168
169/*
Jens Wiklander817466c2018-05-22 13:49:31 +0200170 * Check md_alg against profile
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100171 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Jens Wiklander817466c2018-05-22 13:49:31 +0200172 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200173static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
174 mbedtls_md_type_t md_alg)
Jens Wiklander817466c2018-05-22 13:49:31 +0200175{
Jens Wiklander32b31802023-10-06 16:59:46 +0200176 if (md_alg == MBEDTLS_MD_NONE) {
177 return -1;
178 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100179
Jens Wiklander32b31802023-10-06 16:59:46 +0200180 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
181 return 0;
182 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200183
Jens Wiklander32b31802023-10-06 16:59:46 +0200184 return -1;
Jens Wiklander817466c2018-05-22 13:49:31 +0200185}
186
187/*
188 * Check pk_alg against profile
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100189 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Jens Wiklander817466c2018-05-22 13:49:31 +0200190 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200191static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
192 mbedtls_pk_type_t pk_alg)
Jens Wiklander817466c2018-05-22 13:49:31 +0200193{
Jens Wiklander32b31802023-10-06 16:59:46 +0200194 if (pk_alg == MBEDTLS_PK_NONE) {
195 return -1;
196 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100197
Jens Wiklander32b31802023-10-06 16:59:46 +0200198 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
199 return 0;
200 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200201
Jens Wiklander32b31802023-10-06 16:59:46 +0200202 return -1;
Jens Wiklander817466c2018-05-22 13:49:31 +0200203}
204
205/*
206 * Check key against profile
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100207 * Return 0 if pk is acceptable for this profile, -1 otherwise
Jens Wiklander817466c2018-05-22 13:49:31 +0200208 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200209static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
210 const mbedtls_pk_context *pk)
Jens Wiklander817466c2018-05-22 13:49:31 +0200211{
Jens Wiklander32b31802023-10-06 16:59:46 +0200212 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100213
Jens Wiklander817466c2018-05-22 13:49:31 +0200214#if defined(MBEDTLS_RSA_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200215 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
216 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
217 return 0;
218 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200219
Jens Wiklander32b31802023-10-06 16:59:46 +0200220 return -1;
Jens Wiklander817466c2018-05-22 13:49:31 +0200221 }
Tom Van Eyckb0563632024-06-13 16:20:14 +0200222#endif /* MBEDTLS_RSA_C */
Jens Wiklander817466c2018-05-22 13:49:31 +0200223
Tom Van Eyckb0563632024-06-13 16:20:14 +0200224#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jens Wiklander32b31802023-10-06 16:59:46 +0200225 if (pk_alg == MBEDTLS_PK_ECDSA ||
Jens Wiklander817466c2018-05-22 13:49:31 +0200226 pk_alg == MBEDTLS_PK_ECKEY ||
Jens Wiklander32b31802023-10-06 16:59:46 +0200227 pk_alg == MBEDTLS_PK_ECKEY_DH) {
Tom Van Eyckb0563632024-06-13 16:20:14 +0200228 const mbedtls_ecp_group_id gid = mbedtls_pk_get_ec_group_id(pk);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100229
Jens Wiklander32b31802023-10-06 16:59:46 +0200230 if (gid == MBEDTLS_ECP_DP_NONE) {
231 return -1;
232 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200233
Jens Wiklander32b31802023-10-06 16:59:46 +0200234 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
235 return 0;
236 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200237
Jens Wiklander32b31802023-10-06 16:59:46 +0200238 return -1;
Jens Wiklander817466c2018-05-22 13:49:31 +0200239 }
Tom Van Eyckb0563632024-06-13 16:20:14 +0200240#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Jens Wiklander817466c2018-05-22 13:49:31 +0200241
Jens Wiklander32b31802023-10-06 16:59:46 +0200242 return -1;
Jens Wiklander817466c2018-05-22 13:49:31 +0200243}
244
245/*
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100246 * Like memcmp, but case-insensitive and always returns -1 if different
247 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200248static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100249{
250 size_t i;
251 unsigned char diff;
252 const unsigned char *n1 = s1, *n2 = s2;
253
Jens Wiklander32b31802023-10-06 16:59:46 +0200254 for (i = 0; i < len; i++) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100255 diff = n1[i] ^ n2[i];
256
Jens Wiklander32b31802023-10-06 16:59:46 +0200257 if (diff == 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100258 continue;
259 }
260
Jens Wiklander32b31802023-10-06 16:59:46 +0200261 if (diff == 32 &&
262 ((n1[i] >= 'a' && n1[i] <= 'z') ||
263 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
264 continue;
265 }
266
267 return -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100268 }
269
Jens Wiklander32b31802023-10-06 16:59:46 +0200270 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100271}
272
273/*
274 * Return 0 if name matches wildcard, -1 otherwise
275 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200276static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100277{
278 size_t i;
Jens Wiklander32b31802023-10-06 16:59:46 +0200279 size_t cn_idx = 0, cn_len = strlen(cn);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100280
281 /* We can't have a match if there is no wildcard to match */
Jens Wiklander32b31802023-10-06 16:59:46 +0200282 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
283 return -1;
284 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100285
Jens Wiklander32b31802023-10-06 16:59:46 +0200286 for (i = 0; i < cn_len; ++i) {
287 if (cn[i] == '.') {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100288 cn_idx = i;
289 break;
290 }
291 }
292
Jens Wiklander32b31802023-10-06 16:59:46 +0200293 if (cn_idx == 0) {
294 return -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100295 }
296
Jens Wiklander32b31802023-10-06 16:59:46 +0200297 if (cn_len - cn_idx == name->len - 1 &&
298 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
299 return 0;
300 }
301
302 return -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100303}
304
305/*
306 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
307 * variations (but not all).
308 *
309 * Return 0 if equal, -1 otherwise.
310 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200311static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100312{
Jens Wiklander32b31802023-10-06 16:59:46 +0200313 if (a->tag == b->tag &&
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100314 a->len == b->len &&
Jens Wiklander32b31802023-10-06 16:59:46 +0200315 memcmp(a->p, b->p, b->len) == 0) {
316 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100317 }
318
Jens Wiklander32b31802023-10-06 16:59:46 +0200319 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
320 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100321 a->len == b->len &&
Jens Wiklander32b31802023-10-06 16:59:46 +0200322 x509_memcasecmp(a->p, b->p, b->len) == 0) {
323 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100324 }
325
Jens Wiklander32b31802023-10-06 16:59:46 +0200326 return -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100327}
328
329/*
330 * Compare two X.509 Names (aka rdnSequence).
331 *
332 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
333 * we sometimes return unequal when the full algorithm would return equal,
334 * but never the other way. (In particular, we don't do Unicode normalisation
335 * or space folding.)
336 *
337 * Return 0 if equal, -1 otherwise.
338 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200339static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100340{
341 /* Avoid recursion, it might not be optimised by the compiler */
Jens Wiklander32b31802023-10-06 16:59:46 +0200342 while (a != NULL || b != NULL) {
343 if (a == NULL || b == NULL) {
344 return -1;
345 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100346
347 /* type */
Jens Wiklander32b31802023-10-06 16:59:46 +0200348 if (a->oid.tag != b->oid.tag ||
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100349 a->oid.len != b->oid.len ||
Jens Wiklander32b31802023-10-06 16:59:46 +0200350 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
351 return -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100352 }
353
354 /* value */
Jens Wiklander32b31802023-10-06 16:59:46 +0200355 if (x509_string_cmp(&a->val, &b->val) != 0) {
356 return -1;
357 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100358
359 /* structure of the list of sets */
Jens Wiklander32b31802023-10-06 16:59:46 +0200360 if (a->next_merged != b->next_merged) {
361 return -1;
362 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100363
364 a = a->next;
365 b = b->next;
366 }
367
368 /* a == NULL == b */
Jens Wiklander32b31802023-10-06 16:59:46 +0200369 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100370}
371
372/*
373 * Reset (init or clear) a verify_chain
374 */
375static void x509_crt_verify_chain_reset(
Jens Wiklander32b31802023-10-06 16:59:46 +0200376 mbedtls_x509_crt_verify_chain *ver_chain)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100377{
378 size_t i;
379
Jens Wiklander32b31802023-10-06 16:59:46 +0200380 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100381 ver_chain->items[i].crt = NULL;
Jerome Forissier5b25c762020-04-07 11:18:49 +0200382 ver_chain->items[i].flags = (uint32_t) -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100383 }
384
385 ver_chain->len = 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200386
387#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
388 ver_chain->trust_ca_cb_result = NULL;
389#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100390}
391
392/*
Jens Wiklander817466c2018-05-22 13:49:31 +0200393 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
394 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200395static int x509_get_version(unsigned char **p,
396 const unsigned char *end,
397 int *ver)
Jens Wiklander817466c2018-05-22 13:49:31 +0200398{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200400 size_t len;
401
Jens Wiklander32b31802023-10-06 16:59:46 +0200402 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
403 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
404 0)) != 0) {
405 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200406 *ver = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +0200407 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200408 }
409
Jens Wiklander32b31802023-10-06 16:59:46 +0200410 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Jens Wiklander817466c2018-05-22 13:49:31 +0200411 }
412
413 end = *p + len;
414
Jens Wiklander32b31802023-10-06 16:59:46 +0200415 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
416 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
417 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200418
Jens Wiklander32b31802023-10-06 16:59:46 +0200419 if (*p != end) {
420 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
421 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
422 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200423
Jens Wiklander32b31802023-10-06 16:59:46 +0200424 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200425}
426
427/*
428 * Validity ::= SEQUENCE {
429 * notBefore Time,
430 * notAfter Time }
431 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200432static int x509_get_dates(unsigned char **p,
433 const unsigned char *end,
434 mbedtls_x509_time *from,
435 mbedtls_x509_time *to)
Jens Wiklander817466c2018-05-22 13:49:31 +0200436{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200438 size_t len;
439
Jens Wiklander32b31802023-10-06 16:59:46 +0200440 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
441 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
442 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
443 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200444
445 end = *p + len;
446
Jens Wiklander32b31802023-10-06 16:59:46 +0200447 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
448 return ret;
449 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200450
Jens Wiklander32b31802023-10-06 16:59:46 +0200451 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
452 return ret;
453 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200454
Jens Wiklander32b31802023-10-06 16:59:46 +0200455 if (*p != end) {
456 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
457 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
458 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200459
Jens Wiklander32b31802023-10-06 16:59:46 +0200460 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200461}
462
463/*
464 * X.509 v2/v3 unique identifier (not parsed)
465 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200466static int x509_get_uid(unsigned char **p,
467 const unsigned char *end,
468 mbedtls_x509_buf *uid, int n)
Jens Wiklander817466c2018-05-22 13:49:31 +0200469{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200471
Jens Wiklander32b31802023-10-06 16:59:46 +0200472 if (*p == end) {
473 return 0;
474 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200475
476 uid->tag = **p;
477
Jens Wiklander32b31802023-10-06 16:59:46 +0200478 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
479 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
480 n)) != 0) {
481 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
482 return 0;
483 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200484
Jens Wiklander32b31802023-10-06 16:59:46 +0200485 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Jens Wiklander817466c2018-05-22 13:49:31 +0200486 }
487
488 uid->p = *p;
489 *p += uid->len;
490
Jens Wiklander32b31802023-10-06 16:59:46 +0200491 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200492}
493
Jens Wiklander32b31802023-10-06 16:59:46 +0200494static int x509_get_basic_constraints(unsigned char **p,
495 const unsigned char *end,
496 int *ca_istrue,
497 int *max_pathlen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200498{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200500 size_t len;
501
502 /*
503 * BasicConstraints ::= SEQUENCE {
504 * cA BOOLEAN DEFAULT FALSE,
505 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
506 */
507 *ca_istrue = 0; /* DEFAULT FALSE */
508 *max_pathlen = 0; /* endless */
509
Jens Wiklander32b31802023-10-06 16:59:46 +0200510 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
511 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
512 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Wiklander817466c2018-05-22 13:49:31 +0200513 }
514
Jens Wiklander32b31802023-10-06 16:59:46 +0200515 if (*p == end) {
516 return 0;
517 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200518
Jens Wiklander32b31802023-10-06 16:59:46 +0200519 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
520 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
521 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
522 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200523
Jens Wiklander32b31802023-10-06 16:59:46 +0200524 if (ret != 0) {
525 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
526 }
527
528 if (*ca_istrue != 0) {
529 *ca_istrue = 1;
530 }
531 }
532
533 if (*p == end) {
534 return 0;
535 }
536
537 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
538 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
539 }
540
541 if (*p != end) {
542 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
543 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
544 }
Jerome Forissier79013242021-07-28 10:24:04 +0200545
546 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
547 * overflow, which is an undefined behavior. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200548 if (*max_pathlen == INT_MAX) {
549 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
550 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
551 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200552
553 (*max_pathlen)++;
554
Jens Wiklander32b31802023-10-06 16:59:46 +0200555 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200556}
557
558/*
559 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
560 *
561 * KeyPurposeId ::= OBJECT IDENTIFIER
562 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200563static int x509_get_ext_key_usage(unsigned char **p,
564 const unsigned char *end,
565 mbedtls_x509_sequence *ext_key_usage)
Jens Wiklander817466c2018-05-22 13:49:31 +0200566{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200567 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200568
Jens Wiklander32b31802023-10-06 16:59:46 +0200569 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
570 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Wiklander817466c2018-05-22 13:49:31 +0200571 }
572
Jens Wiklander32b31802023-10-06 16:59:46 +0200573 /* Sequence length must be >= 1 */
574 if (ext_key_usage->buf.p == NULL) {
575 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
576 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
577 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200578
Jens Wiklander32b31802023-10-06 16:59:46 +0200579 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200580}
581
582/*
Tom Van Eyckb0563632024-06-13 16:20:14 +0200583 * SubjectKeyIdentifier ::= KeyIdentifier
584 *
585 * KeyIdentifier ::= OCTET STRING
586 */
587static int x509_get_subject_key_id(unsigned char **p,
588 const unsigned char *end,
589 mbedtls_x509_buf *subject_key_id)
590{
591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
592 size_t len = 0u;
593
594 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
595 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
596 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
597 }
598
599 subject_key_id->len = len;
600 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
601 subject_key_id->p = *p;
602 *p += len;
603
604 if (*p != end) {
605 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
606 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
607 }
608
609 return 0;
610}
611
612/*
613 * AuthorityKeyIdentifier ::= SEQUENCE {
614 * keyIdentifier [0] KeyIdentifier OPTIONAL,
615 * authorityCertIssuer [1] GeneralNames OPTIONAL,
616 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
617 *
618 * KeyIdentifier ::= OCTET STRING
619 */
620static int x509_get_authority_key_id(unsigned char **p,
621 unsigned char *end,
622 mbedtls_x509_authority *authority_key_id)
623{
624 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
625 size_t len = 0u;
626
627 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
628 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
629 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
630 }
631
632 if (*p + len != end) {
633 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
634 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
635 }
636
637 ret = mbedtls_asn1_get_tag(p, end, &len,
638 MBEDTLS_ASN1_CONTEXT_SPECIFIC);
639
640 /* KeyIdentifier is an OPTIONAL field */
641 if (ret == 0) {
642 authority_key_id->keyIdentifier.len = len;
643 authority_key_id->keyIdentifier.p = *p;
644 /* Setting tag of the keyIdentfier intentionally to 0x04.
645 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
646 * its tag with the content is the payload of on OCTET STRING primitive */
647 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
648
649 *p += len;
650 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
651 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
652 }
653
654 if (*p < end) {
655 /* Getting authorityCertIssuer using the required specific class tag [1] */
656 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
657 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
658 1)) != 0) {
659 /* authorityCertIssuer and authorityCertSerialNumber MUST both
660 be present or both be absent. At this point we expect to have both. */
661 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
662 }
663 /* "end" also includes the CertSerialNumber field so "len" shall be used */
664 ret = mbedtls_x509_get_subject_alt_name_ext(p,
665 (*p+len),
666 &authority_key_id->authorityCertIssuer);
667 if (ret != 0) {
668 return ret;
669 }
670
671 /* Getting authorityCertSerialNumber using the required specific class tag [2] */
672 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
673 MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) {
674 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
675 }
676 authority_key_id->authorityCertSerialNumber.len = len;
677 authority_key_id->authorityCertSerialNumber.p = *p;
678 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER;
679 *p += len;
680 }
681
682 if (*p != end) {
683 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
684 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
685 }
686
687 return 0;
688}
689
690/*
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200691 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
692 *
693 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
694 *
695 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
696 *
697 * PolicyInformation ::= SEQUENCE {
698 * policyIdentifier CertPolicyId,
699 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
700 * PolicyQualifierInfo OPTIONAL }
701 *
702 * CertPolicyId ::= OBJECT IDENTIFIER
703 *
704 * PolicyQualifierInfo ::= SEQUENCE {
705 * policyQualifierId PolicyQualifierId,
706 * qualifier ANY DEFINED BY policyQualifierId }
707 *
708 * -- policyQualifierIds for Internet policy qualifiers
709 *
710 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
711 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
712 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
713 *
714 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
715 *
716 * Qualifier ::= CHOICE {
717 * cPSuri CPSuri,
718 * userNotice UserNotice }
719 *
720 * CPSuri ::= IA5String
721 *
722 * UserNotice ::= SEQUENCE {
723 * noticeRef NoticeReference OPTIONAL,
724 * explicitText DisplayText OPTIONAL }
725 *
726 * NoticeReference ::= SEQUENCE {
727 * organization DisplayText,
728 * noticeNumbers SEQUENCE OF INTEGER }
729 *
730 * DisplayText ::= CHOICE {
731 * ia5String IA5String (SIZE (1..200)),
732 * visibleString VisibleString (SIZE (1..200)),
733 * bmpString BMPString (SIZE (1..200)),
734 * utf8String UTF8String (SIZE (1..200)) }
735 *
736 * NOTE: we only parse and use anyPolicy without qualifiers at this point
737 * as defined in RFC 5280.
738 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200739static int x509_get_certificate_policies(unsigned char **p,
740 const unsigned char *end,
741 mbedtls_x509_sequence *certificate_policies)
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200742{
743 int ret, parse_ret = 0;
744 size_t len;
745 mbedtls_asn1_buf *buf;
746 mbedtls_asn1_sequence *cur = certificate_policies;
747
748 /* Get main sequence tag */
Jens Wiklander32b31802023-10-06 16:59:46 +0200749 ret = mbedtls_asn1_get_tag(p, end, &len,
750 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
751 if (ret != 0) {
752 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
753 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200754
Jens Wiklander32b31802023-10-06 16:59:46 +0200755 if (*p + len != end) {
756 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
757 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
758 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200759
760 /*
761 * Cannot be an empty sequence.
762 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200763 if (len == 0) {
764 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
765 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
766 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200767
Jens Wiklander32b31802023-10-06 16:59:46 +0200768 while (*p < end) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200769 mbedtls_x509_buf policy_oid;
770 const unsigned char *policy_end;
771
772 /*
773 * Get the policy sequence
774 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200775 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
776 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
777 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
778 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200779
780 policy_end = *p + len;
781
Jens Wiklander32b31802023-10-06 16:59:46 +0200782 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
783 MBEDTLS_ASN1_OID)) != 0) {
784 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
785 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200786
787 policy_oid.tag = MBEDTLS_ASN1_OID;
788 policy_oid.len = len;
789 policy_oid.p = *p;
790
791 /*
792 * Only AnyPolicy is currently supported when enforcing policy.
793 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200794 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200795 /*
796 * Set the parsing return code but continue parsing, in case this
Jens Wiklander32b31802023-10-06 16:59:46 +0200797 * extension is critical.
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200798 */
799 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
800 }
801
802 /* Allocate and assign next pointer */
Jens Wiklander32b31802023-10-06 16:59:46 +0200803 if (cur->buf.p != NULL) {
804 if (cur->next != NULL) {
805 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
806 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200807
Jens Wiklander32b31802023-10-06 16:59:46 +0200808 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200809
Jens Wiklander32b31802023-10-06 16:59:46 +0200810 if (cur->next == NULL) {
811 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
812 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
813 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200814
815 cur = cur->next;
816 }
817
Jens Wiklander32b31802023-10-06 16:59:46 +0200818 buf = &(cur->buf);
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200819 buf->tag = policy_oid.tag;
820 buf->p = policy_oid.p;
821 buf->len = policy_oid.len;
822
823 *p += len;
824
Jens Wiklander32b31802023-10-06 16:59:46 +0200825 /*
826 * If there is an optional qualifier, then *p < policy_end
827 * Check the Qualifier len to verify it doesn't exceed policy_end.
828 */
829 if (*p < policy_end) {
830 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
831 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
832 0) {
833 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
834 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200835 /*
836 * Skip the optional policy qualifiers.
837 */
838 *p += len;
839 }
840
Jens Wiklander32b31802023-10-06 16:59:46 +0200841 if (*p != policy_end) {
842 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
843 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
844 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200845 }
846
847 /* Set final sequence entry's next pointer to NULL */
848 cur->next = NULL;
849
Jens Wiklander32b31802023-10-06 16:59:46 +0200850 if (*p != end) {
851 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
852 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
853 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200854
Jens Wiklander32b31802023-10-06 16:59:46 +0200855 return parse_ret;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200856}
857
858/*
Jens Wiklander817466c2018-05-22 13:49:31 +0200859 * X.509 v3 extensions
860 *
861 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200862static int x509_get_crt_ext(unsigned char **p,
863 const unsigned char *end,
864 mbedtls_x509_crt *crt,
865 mbedtls_x509_crt_ext_cb_t cb,
866 void *p_ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200867{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200869 size_t len;
Jerome Forissier79013242021-07-28 10:24:04 +0200870 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Jens Wiklander817466c2018-05-22 13:49:31 +0200871
Jens Wiklander32b31802023-10-06 16:59:46 +0200872 if (*p == end) {
873 return 0;
874 }
Jerome Forissier5b25c762020-04-07 11:18:49 +0200875
Jens Wiklander32b31802023-10-06 16:59:46 +0200876 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
877 return ret;
878 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200879
Jerome Forissier5b25c762020-04-07 11:18:49 +0200880 end = crt->v3_ext.p + crt->v3_ext.len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200881 while (*p < end) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200882 /*
883 * Extension ::= SEQUENCE {
884 * extnID OBJECT IDENTIFIER,
885 * critical BOOLEAN DEFAULT FALSE,
886 * extnValue OCTET STRING }
887 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200888 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Jens Wiklander817466c2018-05-22 13:49:31 +0200889 int is_critical = 0; /* DEFAULT FALSE */
890 int ext_type = 0;
891
Jens Wiklander32b31802023-10-06 16:59:46 +0200892 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
893 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
894 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
895 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200896
897 end_ext_data = *p + len;
898
899 /* Get extension ID */
Jens Wiklander32b31802023-10-06 16:59:46 +0200900 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
901 MBEDTLS_ASN1_OID)) != 0) {
902 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
903 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200904
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100905 extn_oid.tag = MBEDTLS_ASN1_OID;
Jens Wiklander817466c2018-05-22 13:49:31 +0200906 extn_oid.p = *p;
907 *p += extn_oid.len;
908
Jens Wiklander817466c2018-05-22 13:49:31 +0200909 /* Get optional critical */
Jens Wiklander32b31802023-10-06 16:59:46 +0200910 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
911 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
912 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
913 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200914
915 /* Data should be octet string type */
Jens Wiklander32b31802023-10-06 16:59:46 +0200916 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
917 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
918 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
919 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200920
Jerome Forissier79013242021-07-28 10:24:04 +0200921 start_ext_octet = *p;
Jens Wiklander817466c2018-05-22 13:49:31 +0200922 end_ext_octet = *p + len;
923
Jens Wiklander32b31802023-10-06 16:59:46 +0200924 if (end_ext_octet != end_ext_data) {
925 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
926 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
927 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200928
929 /*
930 * Detect supported extensions
931 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200932 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Jens Wiklander817466c2018-05-22 13:49:31 +0200933
Jens Wiklander32b31802023-10-06 16:59:46 +0200934 if (ret != 0) {
Jerome Forissier79013242021-07-28 10:24:04 +0200935 /* Give the callback (if any) a chance to handle the extension */
Jens Wiklander32b31802023-10-06 16:59:46 +0200936 if (cb != NULL) {
937 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
938 if (ret != 0 && is_critical) {
939 return ret;
940 }
Jerome Forissier79013242021-07-28 10:24:04 +0200941 *p = end_ext_octet;
942 continue;
943 }
944
Jens Wiklander817466c2018-05-22 13:49:31 +0200945 /* No parser found, skip extension */
946 *p = end_ext_octet;
947
Jens Wiklander32b31802023-10-06 16:59:46 +0200948 if (is_critical) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200949 /* Data is marked as critical: fail */
Jens Wiklander32b31802023-10-06 16:59:46 +0200950 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
951 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Jens Wiklander817466c2018-05-22 13:49:31 +0200952 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200953 continue;
954 }
955
956 /* Forbid repeated extensions */
Jens Wiklander32b31802023-10-06 16:59:46 +0200957 if ((crt->ext_types & ext_type) != 0) {
958 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
959 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200960
961 crt->ext_types |= ext_type;
962
Jens Wiklander32b31802023-10-06 16:59:46 +0200963 switch (ext_type) {
964 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
965 /* Parse basic constraints */
966 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
967 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
968 return ret;
969 }
970 break;
Jens Wiklander817466c2018-05-22 13:49:31 +0200971
Jens Wiklander32b31802023-10-06 16:59:46 +0200972 case MBEDTLS_X509_EXT_KEY_USAGE:
973 /* Parse key usage */
974 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
975 &crt->key_usage)) != 0) {
976 return ret;
977 }
978 break;
Jens Wiklander817466c2018-05-22 13:49:31 +0200979
Jens Wiklander32b31802023-10-06 16:59:46 +0200980 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
981 /* Parse extended key usage */
982 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
983 &crt->ext_key_usage)) != 0) {
984 return ret;
985 }
986 break;
Jens Wiklander817466c2018-05-22 13:49:31 +0200987
Tom Van Eyckb0563632024-06-13 16:20:14 +0200988 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
989 /* Parse subject key identifier */
990 if ((ret = x509_get_subject_key_id(p, end_ext_data,
991 &crt->subject_key_id)) != 0) {
992 return ret;
993 }
994 break;
995
996 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
997 /* Parse authority key identifier */
998 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
999 &crt->authority_key_id)) != 0) {
1000 return ret;
1001 }
1002 break;
Jens Wiklander32b31802023-10-06 16:59:46 +02001003 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Tom Van Eyckb0563632024-06-13 16:20:14 +02001004 /* Parse subject alt name
1005 * SubjectAltName ::= GeneralNames
1006 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001007 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1008 &crt->subject_alt_names)) != 0) {
1009 return ret;
1010 }
1011 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001012
Jens Wiklander32b31802023-10-06 16:59:46 +02001013 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1014 /* Parse netscape certificate type */
1015 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1016 &crt->ns_cert_type)) != 0) {
1017 return ret;
1018 }
1019 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001020
Jens Wiklander32b31802023-10-06 16:59:46 +02001021 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1022 /* Parse certificate policies type */
1023 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1024 &crt->certificate_policies)) != 0) {
1025 /* Give the callback (if any) a chance to handle the extension
1026 * if it contains unsupported policies */
1027 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1028 cb(p_ctx, crt, &extn_oid, is_critical,
1029 start_ext_octet, end_ext_octet) == 0) {
1030 break;
1031 }
Jerome Forissier79013242021-07-28 10:24:04 +02001032
Jens Wiklander32b31802023-10-06 16:59:46 +02001033 if (is_critical) {
1034 return ret;
1035 } else
1036 /*
1037 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1038 * cannot interpret or enforce the policy. However, it is up to
1039 * the user to choose how to enforce the policies,
1040 * unless the extension is critical.
1041 */
1042 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1043 return ret;
1044 }
1045 }
1046 break;
1047
1048 default:
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001049 /*
Jens Wiklander32b31802023-10-06 16:59:46 +02001050 * If this is a non-critical extension, which the oid layer
1051 * supports, but there isn't an x509 parser for it,
1052 * skip the extension.
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001053 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001054 if (is_critical) {
1055 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1056 } else {
1057 *p = end_ext_octet;
1058 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001059 }
1060 }
1061
Jens Wiklander32b31802023-10-06 16:59:46 +02001062 if (*p != end) {
1063 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1064 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1065 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001066
Jens Wiklander32b31802023-10-06 16:59:46 +02001067 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001068}
1069
1070/*
1071 * Parse and fill a single X.509 certificate in DER format
1072 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001073static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1074 const unsigned char *buf,
1075 size_t buflen,
1076 int make_copy,
1077 mbedtls_x509_crt_ext_cb_t cb,
1078 void *p_ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +02001079{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001081 size_t len;
1082 unsigned char *p, *end, *crt_end;
1083 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
1084
Jens Wiklander32b31802023-10-06 16:59:46 +02001085 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1086 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1087 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Jens Wiklander817466c2018-05-22 13:49:31 +02001088
1089 /*
1090 * Check for valid input
1091 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001092 if (crt == NULL || buf == NULL) {
1093 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1094 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001095
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001096 /* Use the original buffer until we figure out actual length. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001097 p = (unsigned char *) buf;
Jens Wiklander817466c2018-05-22 13:49:31 +02001098 len = buflen;
1099 end = p + len;
1100
1101 /*
1102 * Certificate ::= SEQUENCE {
1103 * tbsCertificate TBSCertificate,
1104 * signatureAlgorithm AlgorithmIdentifier,
1105 * signatureValue BIT STRING }
1106 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001107 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1108 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1109 mbedtls_x509_crt_free(crt);
1110 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Jens Wiklander817466c2018-05-22 13:49:31 +02001111 }
1112
Jens Wiklander817466c2018-05-22 13:49:31 +02001113 end = crt_end = p + len;
Tom Van Eyckb0563632024-06-13 16:20:14 +02001114 crt->raw.len = (size_t) (crt_end - buf);
Jens Wiklander32b31802023-10-06 16:59:46 +02001115 if (make_copy != 0) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001116 /* Create and populate a new buffer for the raw field. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001117 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1118 if (crt->raw.p == NULL) {
1119 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1120 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001121
Jens Wiklander32b31802023-10-06 16:59:46 +02001122 memcpy(crt->raw.p, buf, crt->raw.len);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001123 crt->own_buffer = 1;
1124
1125 p += crt->raw.len - len;
1126 end = crt_end = p + len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001127 } else {
1128 crt->raw.p = (unsigned char *) buf;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001129 crt->own_buffer = 0;
1130 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001131
1132 /*
1133 * TBSCertificate ::= SEQUENCE {
1134 */
1135 crt->tbs.p = p;
1136
Jens Wiklander32b31802023-10-06 16:59:46 +02001137 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1138 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1139 mbedtls_x509_crt_free(crt);
1140 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Jens Wiklander817466c2018-05-22 13:49:31 +02001141 }
1142
1143 end = p + len;
Tom Van Eyckb0563632024-06-13 16:20:14 +02001144 crt->tbs.len = (size_t) (end - crt->tbs.p);
Jens Wiklander817466c2018-05-22 13:49:31 +02001145
1146 /*
1147 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1148 *
1149 * CertificateSerialNumber ::= INTEGER
1150 *
1151 * signature AlgorithmIdentifier
1152 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001153 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1154 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1155 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1156 &sig_params1)) != 0) {
1157 mbedtls_x509_crt_free(crt);
1158 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001159 }
1160
Jens Wiklander32b31802023-10-06 16:59:46 +02001161 if (crt->version < 0 || crt->version > 2) {
1162 mbedtls_x509_crt_free(crt);
1163 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Jens Wiklander817466c2018-05-22 13:49:31 +02001164 }
1165
1166 crt->version++;
1167
Jens Wiklander32b31802023-10-06 16:59:46 +02001168 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1169 &crt->sig_md, &crt->sig_pk,
1170 &crt->sig_opts)) != 0) {
1171 mbedtls_x509_crt_free(crt);
1172 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001173 }
1174
1175 /*
1176 * issuer Name
1177 */
1178 crt->issuer_raw.p = p;
1179
Jens Wiklander32b31802023-10-06 16:59:46 +02001180 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1181 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1182 mbedtls_x509_crt_free(crt);
1183 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Jens Wiklander817466c2018-05-22 13:49:31 +02001184 }
1185
Jens Wiklander32b31802023-10-06 16:59:46 +02001186 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1187 mbedtls_x509_crt_free(crt);
1188 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001189 }
1190
Tom Van Eyckb0563632024-06-13 16:20:14 +02001191 crt->issuer_raw.len = (size_t) (p - crt->issuer_raw.p);
Jens Wiklander817466c2018-05-22 13:49:31 +02001192
1193 /*
1194 * Validity ::= SEQUENCE {
1195 * notBefore Time,
1196 * notAfter Time }
1197 *
1198 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001199 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1200 &crt->valid_to)) != 0) {
1201 mbedtls_x509_crt_free(crt);
1202 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001203 }
1204
1205 /*
1206 * subject Name
1207 */
1208 crt->subject_raw.p = p;
1209
Jens Wiklander32b31802023-10-06 16:59:46 +02001210 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1211 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1212 mbedtls_x509_crt_free(crt);
1213 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Jens Wiklander817466c2018-05-22 13:49:31 +02001214 }
1215
Jens Wiklander32b31802023-10-06 16:59:46 +02001216 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1217 mbedtls_x509_crt_free(crt);
1218 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001219 }
1220
Tom Van Eyckb0563632024-06-13 16:20:14 +02001221 crt->subject_raw.len = (size_t) (p - crt->subject_raw.p);
Jens Wiklander817466c2018-05-22 13:49:31 +02001222
1223 /*
1224 * SubjectPublicKeyInfo
1225 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001226 crt->pk_raw.p = p;
Jens Wiklander32b31802023-10-06 16:59:46 +02001227 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1228 mbedtls_x509_crt_free(crt);
1229 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001230 }
Tom Van Eyckb0563632024-06-13 16:20:14 +02001231 crt->pk_raw.len = (size_t) (p - crt->pk_raw.p);
Jens Wiklander817466c2018-05-22 13:49:31 +02001232
1233 /*
1234 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1235 * -- If present, version shall be v2 or v3
1236 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1237 * -- If present, version shall be v2 or v3
1238 * extensions [3] EXPLICIT Extensions OPTIONAL
1239 * -- If present, version shall be v3
1240 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001241 if (crt->version == 2 || crt->version == 3) {
1242 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1243 if (ret != 0) {
1244 mbedtls_x509_crt_free(crt);
1245 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001246 }
1247 }
1248
Jens Wiklander32b31802023-10-06 16:59:46 +02001249 if (crt->version == 2 || crt->version == 3) {
1250 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1251 if (ret != 0) {
1252 mbedtls_x509_crt_free(crt);
1253 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001254 }
1255 }
1256
Jens Wiklander32b31802023-10-06 16:59:46 +02001257 if (crt->version == 3) {
1258 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1259 if (ret != 0) {
1260 mbedtls_x509_crt_free(crt);
1261 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001262 }
1263 }
1264
Jens Wiklander32b31802023-10-06 16:59:46 +02001265 if (p != end) {
1266 mbedtls_x509_crt_free(crt);
1267 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1268 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Wiklander817466c2018-05-22 13:49:31 +02001269 }
1270
1271 end = crt_end;
1272
1273 /*
1274 * }
1275 * -- end of TBSCertificate
1276 *
1277 * signatureAlgorithm AlgorithmIdentifier,
1278 * signatureValue BIT STRING
1279 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001280 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1281 mbedtls_x509_crt_free(crt);
1282 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001283 }
1284
Jens Wiklander32b31802023-10-06 16:59:46 +02001285 if (crt->sig_oid.len != sig_oid2.len ||
1286 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Jerome Forissier79013242021-07-28 10:24:04 +02001287 sig_params1.tag != sig_params2.tag ||
Jens Wiklander817466c2018-05-22 13:49:31 +02001288 sig_params1.len != sig_params2.len ||
Jens Wiklander32b31802023-10-06 16:59:46 +02001289 (sig_params1.len != 0 &&
1290 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1291 mbedtls_x509_crt_free(crt);
1292 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Jens Wiklander817466c2018-05-22 13:49:31 +02001293 }
1294
Jens Wiklander32b31802023-10-06 16:59:46 +02001295 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1296 mbedtls_x509_crt_free(crt);
1297 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001298 }
1299
Jens Wiklander32b31802023-10-06 16:59:46 +02001300 if (p != end) {
1301 mbedtls_x509_crt_free(crt);
1302 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1303 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Wiklander817466c2018-05-22 13:49:31 +02001304 }
1305
Jens Wiklander32b31802023-10-06 16:59:46 +02001306 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001307}
1308
1309/*
1310 * Parse one X.509 certificate in DER format from a buffer and add them to a
1311 * chained list
1312 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001313static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1314 const unsigned char *buf,
1315 size_t buflen,
1316 int make_copy,
1317 mbedtls_x509_crt_ext_cb_t cb,
1318 void *p_ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +02001319{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001320 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001321 mbedtls_x509_crt *crt = chain, *prev = NULL;
1322
1323 /*
1324 * Check for valid input
1325 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001326 if (crt == NULL || buf == NULL) {
1327 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1328 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001329
Jens Wiklander32b31802023-10-06 16:59:46 +02001330 while (crt->version != 0 && crt->next != NULL) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001331 prev = crt;
1332 crt = crt->next;
1333 }
1334
1335 /*
1336 * Add new certificate on the end of the chain if needed.
1337 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001338 if (crt->version != 0 && crt->next == NULL) {
1339 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jens Wiklander817466c2018-05-22 13:49:31 +02001340
Jens Wiklander32b31802023-10-06 16:59:46 +02001341 if (crt->next == NULL) {
1342 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1343 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001344
1345 prev = crt;
Jens Wiklander32b31802023-10-06 16:59:46 +02001346 mbedtls_x509_crt_init(crt->next);
Jens Wiklander817466c2018-05-22 13:49:31 +02001347 crt = crt->next;
1348 }
1349
Jens Wiklander32b31802023-10-06 16:59:46 +02001350 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1351 if (ret != 0) {
1352 if (prev) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001353 prev->next = NULL;
Jens Wiklander32b31802023-10-06 16:59:46 +02001354 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001355
Jens Wiklander32b31802023-10-06 16:59:46 +02001356 if (crt != chain) {
1357 mbedtls_free(crt);
1358 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001359
Jens Wiklander32b31802023-10-06 16:59:46 +02001360 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001361 }
1362
Jens Wiklander32b31802023-10-06 16:59:46 +02001363 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001364}
1365
Jens Wiklander32b31802023-10-06 16:59:46 +02001366int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1367 const unsigned char *buf,
1368 size_t buflen)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001369{
Jens Wiklander32b31802023-10-06 16:59:46 +02001370 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Jerome Forissier79013242021-07-28 10:24:04 +02001371}
1372
Jens Wiklander32b31802023-10-06 16:59:46 +02001373int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1374 const unsigned char *buf,
1375 size_t buflen,
1376 int make_copy,
1377 mbedtls_x509_crt_ext_cb_t cb,
1378 void *p_ctx)
Jerome Forissier79013242021-07-28 10:24:04 +02001379{
Jens Wiklander32b31802023-10-06 16:59:46 +02001380 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001381}
1382
Jens Wiklander32b31802023-10-06 16:59:46 +02001383int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1384 const unsigned char *buf,
1385 size_t buflen)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001386{
Jens Wiklander32b31802023-10-06 16:59:46 +02001387 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001388}
1389
Jens Wiklander817466c2018-05-22 13:49:31 +02001390/*
1391 * Parse one or more PEM certificates from a buffer and add them to the chained
1392 * list
1393 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001394int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1395 const unsigned char *buf,
1396 size_t buflen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001397{
1398#if defined(MBEDTLS_PEM_PARSE_C)
1399 int success = 0, first_error = 0, total_failed = 0;
1400 int buf_format = MBEDTLS_X509_FORMAT_DER;
1401#endif
1402
1403 /*
1404 * Check for valid input
1405 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001406 if (chain == NULL || buf == NULL) {
1407 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1408 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001409
1410 /*
1411 * Determine buffer content. Buffer contains either one DER certificate or
1412 * one or more PEM certificates.
1413 */
1414#if defined(MBEDTLS_PEM_PARSE_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001415 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1416 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001417 buf_format = MBEDTLS_X509_FORMAT_PEM;
1418 }
1419
Jens Wiklander32b31802023-10-06 16:59:46 +02001420 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1421 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1422 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001423#else
Jens Wiklander32b31802023-10-06 16:59:46 +02001424 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Jens Wiklander817466c2018-05-22 13:49:31 +02001425#endif
1426
1427#if defined(MBEDTLS_PEM_PARSE_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001428 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001430 mbedtls_pem_context pem;
1431
1432 /* 1 rather than 0 since the terminating NULL byte is counted in */
Jens Wiklander32b31802023-10-06 16:59:46 +02001433 while (buflen > 1) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001434 size_t use_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001435 mbedtls_pem_init(&pem);
Jens Wiklander817466c2018-05-22 13:49:31 +02001436
1437 /* If we get there, we know the string is null-terminated */
Jens Wiklander32b31802023-10-06 16:59:46 +02001438 ret = mbedtls_pem_read_buffer(&pem,
1439 "-----BEGIN CERTIFICATE-----",
1440 "-----END CERTIFICATE-----",
1441 buf, NULL, 0, &use_len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001442
Jens Wiklander32b31802023-10-06 16:59:46 +02001443 if (ret == 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001444 /*
1445 * Was PEM encoded
1446 */
1447 buflen -= use_len;
1448 buf += use_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001449 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1450 return ret;
1451 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1452 mbedtls_pem_free(&pem);
Jens Wiklander817466c2018-05-22 13:49:31 +02001453
1454 /*
1455 * PEM header and footer were found
1456 */
1457 buflen -= use_len;
1458 buf += use_len;
1459
Jens Wiklander32b31802023-10-06 16:59:46 +02001460 if (first_error == 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001461 first_error = ret;
Jens Wiklander32b31802023-10-06 16:59:46 +02001462 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001463
1464 total_failed++;
1465 continue;
Jens Wiklander32b31802023-10-06 16:59:46 +02001466 } else {
Jens Wiklander817466c2018-05-22 13:49:31 +02001467 break;
Jens Wiklander32b31802023-10-06 16:59:46 +02001468 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001469
Jens Wiklander32b31802023-10-06 16:59:46 +02001470 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Jens Wiklander817466c2018-05-22 13:49:31 +02001471
Jens Wiklander32b31802023-10-06 16:59:46 +02001472 mbedtls_pem_free(&pem);
Jens Wiklander817466c2018-05-22 13:49:31 +02001473
Jens Wiklander32b31802023-10-06 16:59:46 +02001474 if (ret != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001475 /*
1476 * Quit parsing on a memory error
1477 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001478 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1479 return ret;
1480 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001481
Jens Wiklander32b31802023-10-06 16:59:46 +02001482 if (first_error == 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001483 first_error = ret;
Jens Wiklander32b31802023-10-06 16:59:46 +02001484 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001485
1486 total_failed++;
1487 continue;
1488 }
1489
1490 success = 1;
1491 }
1492 }
1493
Jens Wiklander32b31802023-10-06 16:59:46 +02001494 if (success) {
1495 return total_failed;
1496 } else if (first_error) {
1497 return first_error;
1498 } else {
1499 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1500 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001501#endif /* MBEDTLS_PEM_PARSE_C */
1502}
1503
1504#if defined(MBEDTLS_FS_IO)
1505/*
1506 * Load one or more certificates and add them to the chained list
1507 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001508int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Jens Wiklander817466c2018-05-22 13:49:31 +02001509{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001511 size_t n;
1512 unsigned char *buf;
1513
Jens Wiklander32b31802023-10-06 16:59:46 +02001514 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1515 return ret;
1516 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001517
Jens Wiklander32b31802023-10-06 16:59:46 +02001518 ret = mbedtls_x509_crt_parse(chain, buf, n);
Jens Wiklander817466c2018-05-22 13:49:31 +02001519
Tom Van Eyckb0563632024-06-13 16:20:14 +02001520 mbedtls_zeroize_and_free(buf, n);
Jens Wiklander817466c2018-05-22 13:49:31 +02001521
Jens Wiklander32b31802023-10-06 16:59:46 +02001522 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001523}
1524
Jens Wiklander32b31802023-10-06 16:59:46 +02001525int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Jens Wiklander817466c2018-05-22 13:49:31 +02001526{
1527 int ret = 0;
1528#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
1529 int w_ret;
1530 WCHAR szDir[MAX_PATH];
1531 char filename[MAX_PATH];
1532 char *p;
Jens Wiklander32b31802023-10-06 16:59:46 +02001533 size_t len = strlen(path);
Jens Wiklander817466c2018-05-22 13:49:31 +02001534
1535 WIN32_FIND_DATAW file_data;
1536 HANDLE hFind;
1537
Jens Wiklander32b31802023-10-06 16:59:46 +02001538 if (len > MAX_PATH - 3) {
1539 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1540 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001541
Jens Wiklander32b31802023-10-06 16:59:46 +02001542 memset(szDir, 0, sizeof(szDir));
1543 memset(filename, 0, MAX_PATH);
1544 memcpy(filename, path, len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001545 filename[len++] = '\\';
1546 p = filename + len;
1547 filename[len++] = '*';
1548
Tom Van Eyckb0563632024-06-13 16:20:14 +02001549 /*
1550 * Note this function uses the code page CP_ACP which is the system default
1551 * ANSI codepage. The input string is always described in BYTES and the
1552 * output length is described in WCHARs.
1553 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001554 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1555 MAX_PATH - 3);
1556 if (w_ret == 0) {
1557 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1558 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001559
Jens Wiklander32b31802023-10-06 16:59:46 +02001560 hFind = FindFirstFileW(szDir, &file_data);
1561 if (hFind == INVALID_HANDLE_VALUE) {
1562 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1563 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001564
1565 len = MAX_PATH - len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001566 do {
1567 memset(p, 0, len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001568
Jens Wiklander32b31802023-10-06 16:59:46 +02001569 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001570 continue;
Jens Wiklander32b31802023-10-06 16:59:46 +02001571 }
Jens Wiklander32b31802023-10-06 16:59:46 +02001572 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Van Eyckb0563632024-06-13 16:20:14 +02001573 -1, p, (int) len, NULL, NULL);
Jens Wiklander32b31802023-10-06 16:59:46 +02001574 if (w_ret == 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001575 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1576 goto cleanup;
1577 }
1578
Jens Wiklander32b31802023-10-06 16:59:46 +02001579 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1580 if (w_ret < 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001581 ret++;
Jens Wiklander32b31802023-10-06 16:59:46 +02001582 } else {
Jens Wiklander817466c2018-05-22 13:49:31 +02001583 ret += w_ret;
Jens Wiklander32b31802023-10-06 16:59:46 +02001584 }
1585 } while (FindNextFileW(hFind, &file_data) != 0);
Jens Wiklander817466c2018-05-22 13:49:31 +02001586
Jens Wiklander32b31802023-10-06 16:59:46 +02001587 if (GetLastError() != ERROR_NO_MORE_FILES) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001588 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Jens Wiklander32b31802023-10-06 16:59:46 +02001589 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001590
1591cleanup:
Jens Wiklander32b31802023-10-06 16:59:46 +02001592 FindClose(hFind);
Jens Wiklander817466c2018-05-22 13:49:31 +02001593#else /* _WIN32 */
1594 int t_ret;
1595 int snp_ret;
1596 struct stat sb;
1597 struct dirent *entry;
1598 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Jens Wiklander32b31802023-10-06 16:59:46 +02001599 DIR *dir = opendir(path);
Jens Wiklander817466c2018-05-22 13:49:31 +02001600
Jens Wiklander32b31802023-10-06 16:59:46 +02001601 if (dir == NULL) {
1602 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1603 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001604
1605#if defined(MBEDTLS_THREADING_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001606 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1607 closedir(dir);
1608 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001609 }
1610#endif /* MBEDTLS_THREADING_C */
1611
Jens Wiklander32b31802023-10-06 16:59:46 +02001612 memset(&sb, 0, sizeof(sb));
Jerome Forissier79013242021-07-28 10:24:04 +02001613
Jens Wiklander32b31802023-10-06 16:59:46 +02001614 while ((entry = readdir(dir)) != NULL) {
1615 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
1616 "%s/%s", path, entry->d_name);
Jens Wiklander817466c2018-05-22 13:49:31 +02001617
Jens Wiklander32b31802023-10-06 16:59:46 +02001618 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001619 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1620 goto cleanup;
Jens Wiklander32b31802023-10-06 16:59:46 +02001621 } else if (stat(entry_name, &sb) == -1) {
1622 if (errno == ENOENT) {
1623 /* Broken symbolic link - ignore this entry.
1624 stat(2) will return this error for either (a) a dangling
1625 symlink or (b) a missing file.
1626 Given that we have just obtained the filename from readdir,
1627 assume that it does exist and therefore treat this as a
1628 dangling symlink. */
1629 continue;
1630 } else {
1631 /* Some other file error; report the error. */
1632 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1633 goto cleanup;
1634 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001635 }
1636
Jens Wiklander32b31802023-10-06 16:59:46 +02001637 if (!S_ISREG(sb.st_mode)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001638 continue;
Jens Wiklander32b31802023-10-06 16:59:46 +02001639 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001640
1641 // Ignore parse errors
1642 //
Jens Wiklander32b31802023-10-06 16:59:46 +02001643 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1644 if (t_ret < 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001645 ret++;
Jens Wiklander32b31802023-10-06 16:59:46 +02001646 } else {
Jens Wiklander817466c2018-05-22 13:49:31 +02001647 ret += t_ret;
Jens Wiklander32b31802023-10-06 16:59:46 +02001648 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001649 }
1650
1651cleanup:
Jens Wiklander32b31802023-10-06 16:59:46 +02001652 closedir(dir);
Jens Wiklander817466c2018-05-22 13:49:31 +02001653
1654#if defined(MBEDTLS_THREADING_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001655 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001656 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Jens Wiklander32b31802023-10-06 16:59:46 +02001657 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001658#endif /* MBEDTLS_THREADING_C */
1659
1660#endif /* _WIN32 */
1661
Jens Wiklander32b31802023-10-06 16:59:46 +02001662 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001663}
1664#endif /* MBEDTLS_FS_IO */
1665
Jens Wiklander32b31802023-10-06 16:59:46 +02001666#if !defined(MBEDTLS_X509_REMOVE_INFO)
Tom Van Eyckb0563632024-06-13 16:20:14 +02001667#define PRINT_ITEM(i) \
1668 do { \
1669 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1670 MBEDTLS_X509_SAFE_SNPRINTF; \
1671 sep = ", "; \
1672 } while (0)
1673
1674#define CERT_TYPE(type, name) \
1675 do { \
1676 if (ns_cert_type & (type)) { \
1677 PRINT_ITEM(name); \
1678 } \
1679 } while (0)
1680
1681#define KEY_USAGE(code, name) \
1682 do { \
1683 if (key_usage & (code)) { \
1684 PRINT_ITEM(name); \
1685 } \
1686 } while (0)
1687
Jens Wiklander32b31802023-10-06 16:59:46 +02001688static int x509_info_ext_key_usage(char **buf, size_t *size,
1689 const mbedtls_x509_sequence *extended_key_usage)
Jens Wiklander817466c2018-05-22 13:49:31 +02001690{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001692 const char *desc;
1693 size_t n = *size;
1694 char *p = *buf;
1695 const mbedtls_x509_sequence *cur = extended_key_usage;
1696 const char *sep = "";
1697
Jens Wiklander32b31802023-10-06 16:59:46 +02001698 while (cur != NULL) {
1699 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001700 desc = "???";
Jens Wiklander32b31802023-10-06 16:59:46 +02001701 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001702
Jens Wiklander32b31802023-10-06 16:59:46 +02001703 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Jens Wiklander817466c2018-05-22 13:49:31 +02001704 MBEDTLS_X509_SAFE_SNPRINTF;
1705
1706 sep = ", ";
1707
1708 cur = cur->next;
1709 }
1710
1711 *size = n;
1712 *buf = p;
1713
Jens Wiklander32b31802023-10-06 16:59:46 +02001714 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001715}
1716
Jens Wiklander32b31802023-10-06 16:59:46 +02001717static int x509_info_cert_policies(char **buf, size_t *size,
1718 const mbedtls_x509_sequence *certificate_policies)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001719{
1720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1721 const char *desc;
1722 size_t n = *size;
1723 char *p = *buf;
1724 const mbedtls_x509_sequence *cur = certificate_policies;
1725 const char *sep = "";
1726
Jens Wiklander32b31802023-10-06 16:59:46 +02001727 while (cur != NULL) {
1728 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001729 desc = "???";
Jens Wiklander32b31802023-10-06 16:59:46 +02001730 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001731
Jens Wiklander32b31802023-10-06 16:59:46 +02001732 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001733 MBEDTLS_X509_SAFE_SNPRINTF;
1734
1735 sep = ", ";
1736
1737 cur = cur->next;
1738 }
1739
1740 *size = n;
1741 *buf = p;
1742
Jens Wiklander32b31802023-10-06 16:59:46 +02001743 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001744}
1745
Jens Wiklander817466c2018-05-22 13:49:31 +02001746/*
1747 * Return an informational string about the certificate.
1748 */
1749#define BEFORE_COLON 18
1750#define BC "18"
Jens Wiklander32b31802023-10-06 16:59:46 +02001751int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1752 const mbedtls_x509_crt *crt)
Jens Wiklander817466c2018-05-22 13:49:31 +02001753{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001754 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001755 size_t n;
1756 char *p;
1757 char key_size_str[BEFORE_COLON];
1758
1759 p = buf;
1760 n = size;
1761
Jens Wiklander32b31802023-10-06 16:59:46 +02001762 if (NULL == crt) {
1763 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Jens Wiklander817466c2018-05-22 13:49:31 +02001764 MBEDTLS_X509_SAFE_SNPRINTF;
1765
Jens Wiklander32b31802023-10-06 16:59:46 +02001766 return (int) (size - n);
Jens Wiklander817466c2018-05-22 13:49:31 +02001767 }
1768
Jens Wiklander32b31802023-10-06 16:59:46 +02001769 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1770 prefix, crt->version);
Jens Wiklander817466c2018-05-22 13:49:31 +02001771 MBEDTLS_X509_SAFE_SNPRINTF;
Jens Wiklander32b31802023-10-06 16:59:46 +02001772 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1773 prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001774 MBEDTLS_X509_SAFE_SNPRINTF;
1775
Jens Wiklander32b31802023-10-06 16:59:46 +02001776 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Jens Wiklander817466c2018-05-22 13:49:31 +02001777 MBEDTLS_X509_SAFE_SNPRINTF;
1778
Jens Wiklander32b31802023-10-06 16:59:46 +02001779 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001780 MBEDTLS_X509_SAFE_SNPRINTF;
Jens Wiklander32b31802023-10-06 16:59:46 +02001781 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Jens Wiklander817466c2018-05-22 13:49:31 +02001782 MBEDTLS_X509_SAFE_SNPRINTF;
1783
Jens Wiklander32b31802023-10-06 16:59:46 +02001784 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001785 MBEDTLS_X509_SAFE_SNPRINTF;
Jens Wiklander32b31802023-10-06 16:59:46 +02001786 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Jens Wiklander817466c2018-05-22 13:49:31 +02001787 MBEDTLS_X509_SAFE_SNPRINTF;
1788
Jens Wiklander32b31802023-10-06 16:59:46 +02001789 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1790 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1791 crt->valid_from.year, crt->valid_from.mon,
1792 crt->valid_from.day, crt->valid_from.hour,
1793 crt->valid_from.min, crt->valid_from.sec);
Jens Wiklander817466c2018-05-22 13:49:31 +02001794 MBEDTLS_X509_SAFE_SNPRINTF;
1795
Jens Wiklander32b31802023-10-06 16:59:46 +02001796 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1797 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1798 crt->valid_to.year, crt->valid_to.mon,
1799 crt->valid_to.day, crt->valid_to.hour,
1800 crt->valid_to.min, crt->valid_to.sec);
Jens Wiklander817466c2018-05-22 13:49:31 +02001801 MBEDTLS_X509_SAFE_SNPRINTF;
1802
Jens Wiklander32b31802023-10-06 16:59:46 +02001803 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001804 MBEDTLS_X509_SAFE_SNPRINTF;
1805
Jens Wiklander32b31802023-10-06 16:59:46 +02001806 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1807 crt->sig_md, crt->sig_opts);
Jens Wiklander817466c2018-05-22 13:49:31 +02001808 MBEDTLS_X509_SAFE_SNPRINTF;
1809
1810 /* Key size */
Jens Wiklander32b31802023-10-06 16:59:46 +02001811 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1812 mbedtls_pk_get_name(&crt->pk))) != 0) {
1813 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001814 }
1815
Jens Wiklander32b31802023-10-06 16:59:46 +02001816 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1817 (int) mbedtls_pk_get_bitlen(&crt->pk));
Jens Wiklander817466c2018-05-22 13:49:31 +02001818 MBEDTLS_X509_SAFE_SNPRINTF;
1819
1820 /*
1821 * Optional extensions
1822 */
1823
Jens Wiklander32b31802023-10-06 16:59:46 +02001824 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1825 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1826 crt->ca_istrue ? "true" : "false");
Jens Wiklander817466c2018-05-22 13:49:31 +02001827 MBEDTLS_X509_SAFE_SNPRINTF;
1828
Jens Wiklander32b31802023-10-06 16:59:46 +02001829 if (crt->max_pathlen > 0) {
1830 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Jens Wiklander817466c2018-05-22 13:49:31 +02001831 MBEDTLS_X509_SAFE_SNPRINTF;
1832 }
1833 }
1834
Jens Wiklander32b31802023-10-06 16:59:46 +02001835 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1836 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001837 MBEDTLS_X509_SAFE_SNPRINTF;
1838
Jens Wiklander32b31802023-10-06 16:59:46 +02001839 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1840 &crt->subject_alt_names,
1841 prefix)) != 0) {
1842 return ret;
1843 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001844 }
1845
Jens Wiklander32b31802023-10-06 16:59:46 +02001846 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1847 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001848 MBEDTLS_X509_SAFE_SNPRINTF;
1849
Jens Wiklander32b31802023-10-06 16:59:46 +02001850 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
1851 return ret;
1852 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001853 }
1854
Jens Wiklander32b31802023-10-06 16:59:46 +02001855 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1856 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001857 MBEDTLS_X509_SAFE_SNPRINTF;
1858
Jens Wiklander32b31802023-10-06 16:59:46 +02001859 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
1860 return ret;
1861 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001862 }
1863
Jens Wiklander32b31802023-10-06 16:59:46 +02001864 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1865 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001866 MBEDTLS_X509_SAFE_SNPRINTF;
1867
Jens Wiklander32b31802023-10-06 16:59:46 +02001868 if ((ret = x509_info_ext_key_usage(&p, &n,
1869 &crt->ext_key_usage)) != 0) {
1870 return ret;
1871 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001872 }
1873
Jens Wiklander32b31802023-10-06 16:59:46 +02001874 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1875 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001876 MBEDTLS_X509_SAFE_SNPRINTF;
1877
Jens Wiklander32b31802023-10-06 16:59:46 +02001878 if ((ret = x509_info_cert_policies(&p, &n,
1879 &crt->certificate_policies)) != 0) {
1880 return ret;
1881 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001882 }
1883
Jens Wiklander32b31802023-10-06 16:59:46 +02001884 ret = mbedtls_snprintf(p, n, "\n");
Jens Wiklander817466c2018-05-22 13:49:31 +02001885 MBEDTLS_X509_SAFE_SNPRINTF;
1886
Jens Wiklander32b31802023-10-06 16:59:46 +02001887 return (int) (size - n);
Jens Wiklander817466c2018-05-22 13:49:31 +02001888}
1889
1890struct x509_crt_verify_string {
1891 int code;
1892 const char *string;
1893};
1894
Jens Wiklander32b31802023-10-06 16:59:46 +02001895#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Jens Wiklander817466c2018-05-22 13:49:31 +02001896static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Jens Wiklander32b31802023-10-06 16:59:46 +02001897 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Jens Wiklander817466c2018-05-22 13:49:31 +02001898 { 0, NULL }
1899};
Jens Wiklander32b31802023-10-06 16:59:46 +02001900#undef X509_CRT_ERROR_INFO
Jens Wiklander817466c2018-05-22 13:49:31 +02001901
Jens Wiklander32b31802023-10-06 16:59:46 +02001902int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1903 uint32_t flags)
Jens Wiklander817466c2018-05-22 13:49:31 +02001904{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001906 const struct x509_crt_verify_string *cur;
1907 char *p = buf;
1908 size_t n = size;
1909
Jens Wiklander32b31802023-10-06 16:59:46 +02001910 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1911 if ((flags & cur->code) == 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001912 continue;
Jens Wiklander32b31802023-10-06 16:59:46 +02001913 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001914
Jens Wiklander32b31802023-10-06 16:59:46 +02001915 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Jens Wiklander817466c2018-05-22 13:49:31 +02001916 MBEDTLS_X509_SAFE_SNPRINTF;
1917 flags ^= cur->code;
1918 }
1919
Jens Wiklander32b31802023-10-06 16:59:46 +02001920 if (flags != 0) {
1921 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1922 "(this should not happen)\n", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001923 MBEDTLS_X509_SAFE_SNPRINTF;
1924 }
1925
Jens Wiklander32b31802023-10-06 16:59:46 +02001926 return (int) (size - n);
Jens Wiklander817466c2018-05-22 13:49:31 +02001927}
Jens Wiklander32b31802023-10-06 16:59:46 +02001928#endif /* MBEDTLS_X509_REMOVE_INFO */
Jens Wiklander817466c2018-05-22 13:49:31 +02001929
Jens Wiklander32b31802023-10-06 16:59:46 +02001930int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1931 unsigned int usage)
Jens Wiklander817466c2018-05-22 13:49:31 +02001932{
1933 unsigned int usage_must, usage_may;
1934 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Jens Wiklander32b31802023-10-06 16:59:46 +02001935 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Jens Wiklander817466c2018-05-22 13:49:31 +02001936
Jens Wiklander32b31802023-10-06 16:59:46 +02001937 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1938 return 0;
1939 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001940
1941 usage_must = usage & ~may_mask;
1942
Jens Wiklander32b31802023-10-06 16:59:46 +02001943 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1944 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1945 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001946
1947 usage_may = usage & may_mask;
1948
Jens Wiklander32b31802023-10-06 16:59:46 +02001949 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1950 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1951 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001952
Jens Wiklander32b31802023-10-06 16:59:46 +02001953 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001954}
Jens Wiklander817466c2018-05-22 13:49:31 +02001955
Jens Wiklander32b31802023-10-06 16:59:46 +02001956int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1957 const char *usage_oid,
1958 size_t usage_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001959{
1960 const mbedtls_x509_sequence *cur;
1961
1962 /* Extension is not mandatory, absent means no restriction */
Jens Wiklander32b31802023-10-06 16:59:46 +02001963 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1964 return 0;
1965 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001966
1967 /*
1968 * Look for the requested usage (or wildcard ANY) in our list
1969 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001970 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001971 const mbedtls_x509_buf *cur_oid = &cur->buf;
1972
Jens Wiklander32b31802023-10-06 16:59:46 +02001973 if (cur_oid->len == usage_len &&
1974 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1975 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001976 }
1977
Jens Wiklander32b31802023-10-06 16:59:46 +02001978 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1979 return 0;
1980 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001981 }
1982
Jens Wiklander32b31802023-10-06 16:59:46 +02001983 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +02001984}
Jens Wiklander817466c2018-05-22 13:49:31 +02001985
1986#if defined(MBEDTLS_X509_CRL_PARSE_C)
1987/*
1988 * Return 1 if the certificate is revoked, or 0 otherwise.
1989 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001990int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Jens Wiklander817466c2018-05-22 13:49:31 +02001991{
1992 const mbedtls_x509_crl_entry *cur = &crl->entry;
1993
Jens Wiklander32b31802023-10-06 16:59:46 +02001994 while (cur != NULL && cur->serial.len != 0) {
1995 if (crt->serial.len == cur->serial.len &&
1996 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
1997 return 1;
Jens Wiklander817466c2018-05-22 13:49:31 +02001998 }
1999
2000 cur = cur->next;
2001 }
2002
Jens Wiklander32b31802023-10-06 16:59:46 +02002003 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02002004}
2005
2006/*
2007 * Check that the given certificate is not revoked according to the CRL.
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002008 * Skip validation if no CRL for the given CA is present.
Jens Wiklander817466c2018-05-22 13:49:31 +02002009 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002010static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2011 mbedtls_x509_crl *crl_list,
Tom Van Eyckb0563632024-06-13 16:20:14 +02002012 const mbedtls_x509_crt_profile *profile,
2013 const mbedtls_x509_time *now)
Jens Wiklander817466c2018-05-22 13:49:31 +02002014{
2015 int flags = 0;
Tom Van Eyckb0563632024-06-13 16:20:14 +02002016 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Jens Wiklander32b31802023-10-06 16:59:46 +02002017#if defined(MBEDTLS_USE_PSA_CRYPTO)
2018 psa_algorithm_t psa_algorithm;
2019#else
Jens Wiklander817466c2018-05-22 13:49:31 +02002020 const mbedtls_md_info_t *md_info;
Jens Wiklander32b31802023-10-06 16:59:46 +02002021#endif /* MBEDTLS_USE_PSA_CRYPTO */
2022 size_t hash_length;
Jens Wiklander817466c2018-05-22 13:49:31 +02002023
Jens Wiklander32b31802023-10-06 16:59:46 +02002024 if (ca == NULL) {
2025 return flags;
2026 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002027
Jens Wiklander32b31802023-10-06 16:59:46 +02002028 while (crl_list != NULL) {
2029 if (crl_list->version == 0 ||
2030 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002031 crl_list = crl_list->next;
2032 continue;
2033 }
2034
2035 /*
2036 * Check if the CA is configured to sign CRLs
2037 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002038 if (mbedtls_x509_crt_check_key_usage(ca,
2039 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002040 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2041 break;
2042 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002043
2044 /*
2045 * Check if CRL is correctly signed by the trusted CA
2046 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002047 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002048 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Jens Wiklander32b31802023-10-06 16:59:46 +02002049 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002050
Jens Wiklander32b31802023-10-06 16:59:46 +02002051 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002052 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Jens Wiklander32b31802023-10-06 16:59:46 +02002053 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002054
Jens Wiklander32b31802023-10-06 16:59:46 +02002055#if defined(MBEDTLS_USE_PSA_CRYPTO)
Tom Van Eyckb0563632024-06-13 16:20:14 +02002056 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Jens Wiklander32b31802023-10-06 16:59:46 +02002057 if (psa_hash_compute(psa_algorithm,
2058 crl_list->tbs.p,
2059 crl_list->tbs.len,
2060 hash,
2061 sizeof(hash),
2062 &hash_length) != PSA_SUCCESS) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002063 /* Note: this can't happen except after an internal error */
Jens Wiklander817466c2018-05-22 13:49:31 +02002064 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2065 break;
2066 }
Jens Wiklander32b31802023-10-06 16:59:46 +02002067#else
2068 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2069 hash_length = mbedtls_md_get_size(md_info);
2070 if (mbedtls_md(md_info,
2071 crl_list->tbs.p,
2072 crl_list->tbs.len,
2073 hash) != 0) {
2074 /* Note: this can't happen except after an internal error */
2075 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2076 break;
2077 }
2078#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jens Wiklander817466c2018-05-22 13:49:31 +02002079
Jens Wiklander32b31802023-10-06 16:59:46 +02002080 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002081 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Jens Wiklander32b31802023-10-06 16:59:46 +02002082 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002083
Jens Wiklander32b31802023-10-06 16:59:46 +02002084 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2085 crl_list->sig_md, hash, hash_length,
2086 crl_list->sig.p, crl_list->sig.len) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002087 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2088 break;
2089 }
2090
Tom Van Eyckb0563632024-06-13 16:20:14 +02002091#if defined(MBEDTLS_HAVE_TIME_DATE)
Jens Wiklander817466c2018-05-22 13:49:31 +02002092 /*
2093 * Check for validity of CRL (Do not drop out)
2094 */
Tom Van Eyckb0563632024-06-13 16:20:14 +02002095 if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002096 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Jens Wiklander32b31802023-10-06 16:59:46 +02002097 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002098
Tom Van Eyckb0563632024-06-13 16:20:14 +02002099 if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002100 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Jens Wiklander32b31802023-10-06 16:59:46 +02002101 }
Tom Van Eyckb0563632024-06-13 16:20:14 +02002102#else
2103 ((void) now);
2104#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02002105
2106 /*
2107 * Check if certificate is revoked
2108 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002109 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002110 flags |= MBEDTLS_X509_BADCERT_REVOKED;
2111 break;
2112 }
2113
2114 crl_list = crl_list->next;
2115 }
2116
Jens Wiklander32b31802023-10-06 16:59:46 +02002117 return flags;
Jens Wiklander817466c2018-05-22 13:49:31 +02002118}
2119#endif /* MBEDTLS_X509_CRL_PARSE_C */
2120
2121/*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002122 * Check the signature of a certificate by its parent
Jens Wiklander817466c2018-05-22 13:49:31 +02002123 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002124static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2125 mbedtls_x509_crt *parent,
2126 mbedtls_x509_crt_restart_ctx *rs_ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +02002127{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002128 size_t hash_len;
Tom Van Eyckb0563632024-06-13 16:20:14 +02002129 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002130#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2131 const mbedtls_md_info_t *md_info;
Jens Wiklander32b31802023-10-06 16:59:46 +02002132 md_info = mbedtls_md_info_from_type(child->sig_md);
2133 hash_len = mbedtls_md_get_size(md_info);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002134
2135 /* Note: hash errors can happen only after an internal error */
Jens Wiklander32b31802023-10-06 16:59:46 +02002136 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2137 return -1;
2138 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002139#else
Tom Van Eyckb0563632024-06-13 16:20:14 +02002140 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
Jens Wiklander32b31802023-10-06 16:59:46 +02002141 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002142
Jens Wiklander32b31802023-10-06 16:59:46 +02002143 status = psa_hash_compute(hash_alg,
2144 child->tbs.p,
2145 child->tbs.len,
2146 hash,
2147 sizeof(hash),
2148 &hash_len);
2149 if (status != PSA_SUCCESS) {
2150 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jens Wiklander817466c2018-05-22 13:49:31 +02002151 }
2152
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002153#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002154 /* Skip expensive computation on obvious mismatch */
Jens Wiklander32b31802023-10-06 16:59:46 +02002155 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2156 return -1;
2157 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002158
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002159#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Jens Wiklander32b31802023-10-06 16:59:46 +02002160 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2161 return mbedtls_pk_verify_restartable(&parent->pk,
2162 child->sig_md, hash, hash_len,
2163 child->sig.p, child->sig.len, &rs_ctx->pk);
Jens Wiklander817466c2018-05-22 13:49:31 +02002164 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002165#else
2166 (void) rs_ctx;
2167#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02002168
Jens Wiklander32b31802023-10-06 16:59:46 +02002169 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2170 child->sig_md, hash, hash_len,
2171 child->sig.p, child->sig.len);
Jens Wiklander817466c2018-05-22 13:49:31 +02002172}
2173
2174/*
2175 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2176 * Return 0 if yes, -1 if not.
2177 *
2178 * top means parent is a locally-trusted certificate
Jens Wiklander817466c2018-05-22 13:49:31 +02002179 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002180static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2181 const mbedtls_x509_crt *parent,
2182 int top)
Jens Wiklander817466c2018-05-22 13:49:31 +02002183{
2184 int need_ca_bit;
2185
2186 /* Parent must be the issuer */
Jens Wiklander32b31802023-10-06 16:59:46 +02002187 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2188 return -1;
2189 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002190
2191 /* Parent must have the basicConstraints CA bit set as a general rule */
2192 need_ca_bit = 1;
2193
2194 /* Exception: v1/v2 certificates that are locally trusted. */
Jens Wiklander32b31802023-10-06 16:59:46 +02002195 if (top && parent->version < 3) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002196 need_ca_bit = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02002197 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002198
Jens Wiklander32b31802023-10-06 16:59:46 +02002199 if (need_ca_bit && !parent->ca_istrue) {
2200 return -1;
2201 }
2202
2203 if (need_ca_bit &&
2204 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2205 return -1;
2206 }
2207
2208 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02002209}
2210
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002211/*
2212 * Find a suitable parent for child in candidates, or return NULL.
2213 *
2214 * Here suitable is defined as:
2215 * 1. subject name matches child's issuer
2216 * 2. if necessary, the CA bit is set and key usage allows signing certs
2217 * 3. for trusted roots, the signature is correct
2218 * (for intermediates, the signature is checked and the result reported)
2219 * 4. pathlen constraints are satisfied
2220 *
2221 * If there's a suitable candidate which is also time-valid, return the first
2222 * such. Otherwise, return the first suitable candidate (or NULL if there is
2223 * none).
2224 *
2225 * The rationale for this rule is that someone could have a list of trusted
2226 * roots with two versions on the same root with different validity periods.
2227 * (At least one user reported having such a list and wanted it to just work.)
2228 * The reason we don't just require time-validity is that generally there is
2229 * only one version, and if it's expired we want the flags to state that
2230 * rather than NOT_TRUSTED, as would be the case if we required it here.
2231 *
2232 * The rationale for rule 3 (signature for trusted roots) is that users might
2233 * have two versions of the same CA with different keys in their list, and the
2234 * way we select the correct one is by checking the signature (as we don't
2235 * rely on key identifier extensions). (This is one way users might choose to
2236 * handle key rollover, another relies on self-issued certs, see [SIRO].)
2237 *
2238 * Arguments:
2239 * - [in] child: certificate for which we're looking for a parent
2240 * - [in] candidates: chained list of potential parents
2241 * - [out] r_parent: parent found (or NULL)
2242 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
2243 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2244 * of the chain, 0 otherwise
2245 * - [in] path_cnt: number of intermediates seen so far
2246 * - [in] self_cnt: number of self-signed intermediates seen so far
2247 * (will never be greater than path_cnt)
2248 * - [in-out] rs_ctx: context for restarting operations
2249 *
2250 * Return value:
2251 * - 0 on success
2252 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
2253 */
2254static int x509_crt_find_parent_in(
Jens Wiklander32b31802023-10-06 16:59:46 +02002255 mbedtls_x509_crt *child,
2256 mbedtls_x509_crt *candidates,
2257 mbedtls_x509_crt **r_parent,
2258 int *r_signature_is_good,
2259 int top,
2260 unsigned path_cnt,
2261 unsigned self_cnt,
Tom Van Eyckb0563632024-06-13 16:20:14 +02002262 mbedtls_x509_crt_restart_ctx *rs_ctx,
2263 const mbedtls_x509_time *now)
Jens Wiklander817466c2018-05-22 13:49:31 +02002264{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002266 mbedtls_x509_crt *parent, *fallback_parent;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002267 int signature_is_good = 0, fallback_signature_is_good;
Jens Wiklander817466c2018-05-22 13:49:31 +02002268
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002269#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2270 /* did we have something in progress? */
Jens Wiklander32b31802023-10-06 16:59:46 +02002271 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002272 /* restore saved state */
2273 parent = rs_ctx->parent;
2274 fallback_parent = rs_ctx->fallback_parent;
2275 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
2276
2277 /* clear saved state */
2278 rs_ctx->parent = NULL;
2279 rs_ctx->fallback_parent = NULL;
2280 rs_ctx->fallback_signature_is_good = 0;
2281
2282 /* resume where we left */
2283 goto check_signature;
Jens Wiklander817466c2018-05-22 13:49:31 +02002284 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002285#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02002286
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002287 fallback_parent = NULL;
2288 fallback_signature_is_good = 0;
2289
Jens Wiklander32b31802023-10-06 16:59:46 +02002290 for (parent = candidates; parent != NULL; parent = parent->next) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002291 /* basic parenting skills (name, CA bit, key usage) */
Jens Wiklander32b31802023-10-06 16:59:46 +02002292 if (x509_crt_check_parent(child, parent, top) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002293 continue;
Jens Wiklander32b31802023-10-06 16:59:46 +02002294 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002295
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002296 /* +1 because stored max_pathlen is 1 higher that the actual value */
Jens Wiklander32b31802023-10-06 16:59:46 +02002297 if (parent->max_pathlen > 0 &&
2298 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002299 continue;
2300 }
2301
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002302 /* Signature */
2303#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2304check_signature:
2305#endif
Jens Wiklander32b31802023-10-06 16:59:46 +02002306 ret = x509_crt_check_signature(child, parent, rs_ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +02002307
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002308#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Jens Wiklander32b31802023-10-06 16:59:46 +02002309 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002310 /* save state */
2311 rs_ctx->parent = parent;
2312 rs_ctx->fallback_parent = fallback_parent;
2313 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
2314
Jens Wiklander32b31802023-10-06 16:59:46 +02002315 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002316 }
2317#else
2318 (void) ret;
2319#endif
2320
2321 signature_is_good = ret == 0;
Jens Wiklander32b31802023-10-06 16:59:46 +02002322 if (top && !signature_is_good) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002323 continue;
Jens Wiklander32b31802023-10-06 16:59:46 +02002324 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002325
Tom Van Eyckb0563632024-06-13 16:20:14 +02002326#if defined(MBEDTLS_HAVE_TIME_DATE)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002327 /* optional time check */
Tom Van Eyckb0563632024-06-13 16:20:14 +02002328 if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2329 mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
Jens Wiklander32b31802023-10-06 16:59:46 +02002330 if (fallback_parent == NULL) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002331 fallback_parent = parent;
2332 fallback_signature_is_good = signature_is_good;
2333 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002334
2335 continue;
2336 }
Tom Van Eyckb0563632024-06-13 16:20:14 +02002337#else
2338 ((void) now);
2339#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02002340
Jerome Forissier5b25c762020-04-07 11:18:49 +02002341 *r_parent = parent;
2342 *r_signature_is_good = signature_is_good;
2343
Jens Wiklander817466c2018-05-22 13:49:31 +02002344 break;
2345 }
2346
Jens Wiklander32b31802023-10-06 16:59:46 +02002347 if (parent == NULL) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002348 *r_parent = fallback_parent;
2349 *r_signature_is_good = fallback_signature_is_good;
Jens Wiklander817466c2018-05-22 13:49:31 +02002350 }
2351
Jens Wiklander32b31802023-10-06 16:59:46 +02002352 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02002353}
2354
2355/*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002356 * Find a parent in trusted CAs or the provided chain, or return NULL.
2357 *
2358 * Searches in trusted CAs first, and return the first suitable parent found
2359 * (see find_parent_in() for definition of suitable).
2360 *
2361 * Arguments:
2362 * - [in] child: certificate for which we're looking for a parent, followed
2363 * by a chain of possible intermediates
2364 * - [in] trust_ca: list of locally trusted certificates
2365 * - [out] parent: parent found (or NULL)
2366 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2367 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2368 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2369 * - [in] self_cnt: number of self-signed certs in the chain so far
2370 * (will always be no greater than path_cnt)
2371 * - [in-out] rs_ctx: context for restarting operations
2372 *
2373 * Return value:
2374 * - 0 on success
2375 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
2376 */
2377static int x509_crt_find_parent(
Jens Wiklander32b31802023-10-06 16:59:46 +02002378 mbedtls_x509_crt *child,
2379 mbedtls_x509_crt *trust_ca,
2380 mbedtls_x509_crt **parent,
2381 int *parent_is_trusted,
2382 int *signature_is_good,
2383 unsigned path_cnt,
2384 unsigned self_cnt,
Tom Van Eyckb0563632024-06-13 16:20:14 +02002385 mbedtls_x509_crt_restart_ctx *rs_ctx,
2386 const mbedtls_x509_time *now)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002387{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002389 mbedtls_x509_crt *search_list;
2390
2391 *parent_is_trusted = 1;
2392
2393#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2394 /* restore then clear saved state if we have some stored */
Jens Wiklander32b31802023-10-06 16:59:46 +02002395 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002396 *parent_is_trusted = rs_ctx->parent_is_trusted;
2397 rs_ctx->parent_is_trusted = -1;
2398 }
2399#endif
2400
Jens Wiklander32b31802023-10-06 16:59:46 +02002401 while (1) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002402 search_list = *parent_is_trusted ? trust_ca : child->next;
2403
Jens Wiklander32b31802023-10-06 16:59:46 +02002404 ret = x509_crt_find_parent_in(child, search_list,
2405 parent, signature_is_good,
2406 *parent_is_trusted,
Tom Van Eyckb0563632024-06-13 16:20:14 +02002407 path_cnt, self_cnt, rs_ctx, now);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002408
2409#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Jens Wiklander32b31802023-10-06 16:59:46 +02002410 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002411 /* save state */
2412 rs_ctx->parent_is_trusted = *parent_is_trusted;
Jens Wiklander32b31802023-10-06 16:59:46 +02002413 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002414 }
2415#else
2416 (void) ret;
2417#endif
2418
2419 /* stop here if found or already in second iteration */
Jens Wiklander32b31802023-10-06 16:59:46 +02002420 if (*parent != NULL || *parent_is_trusted == 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002421 break;
Jens Wiklander32b31802023-10-06 16:59:46 +02002422 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002423
2424 /* prepare second iteration */
2425 *parent_is_trusted = 0;
2426 }
2427
2428 /* extra precaution against mistakes in the caller */
Jens Wiklander32b31802023-10-06 16:59:46 +02002429 if (*parent == NULL) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002430 *parent_is_trusted = 0;
2431 *signature_is_good = 0;
2432 }
2433
Jens Wiklander32b31802023-10-06 16:59:46 +02002434 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002435}
2436
2437/*
2438 * Check if an end-entity certificate is locally trusted
2439 *
2440 * Currently we require such certificates to be self-signed (actually only
2441 * check for self-issued as self-signatures are not checked)
2442 */
2443static int x509_crt_check_ee_locally_trusted(
Jens Wiklander32b31802023-10-06 16:59:46 +02002444 mbedtls_x509_crt *crt,
2445 mbedtls_x509_crt *trust_ca)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002446{
2447 mbedtls_x509_crt *cur;
2448
2449 /* must be self-issued */
Jens Wiklander32b31802023-10-06 16:59:46 +02002450 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2451 return -1;
2452 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002453
2454 /* look for an exact match with trusted cert */
Jens Wiklander32b31802023-10-06 16:59:46 +02002455 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2456 if (crt->raw.len == cur->raw.len &&
2457 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2458 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002459 }
2460 }
2461
2462 /* too bad */
Jens Wiklander32b31802023-10-06 16:59:46 +02002463 return -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002464}
2465
2466/*
2467 * Build and verify a certificate chain
2468 *
2469 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2470 * a list of trusted certs R1, ... Rp, try to build and verify a chain
2471 * EE, Ci1, ... Ciq [, Rj]
2472 * such that every cert in the chain is a child of the next one,
2473 * jumping to a trusted root as early as possible.
2474 *
2475 * Verify that chain and return it with flags for all issues found.
2476 *
2477 * Special cases:
2478 * - EE == Rj -> return a one-element list containing it
2479 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2480 * -> return that chain with NOT_TRUSTED set on Ciq
2481 *
2482 * Tests for (aspects of) this function should include at least:
2483 * - trusted EE
2484 * - EE -> trusted root
Jerome Forissier5b25c762020-04-07 11:18:49 +02002485 * - EE -> intermediate CA -> trusted root
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002486 * - if relevant: EE untrusted
2487 * - if relevant: EE -> intermediate, untrusted
2488 * with the aspect under test checked at each relevant level (EE, int, root).
2489 * For some aspects longer chains are required, but usually length 2 is
2490 * enough (but length 1 is not in general).
2491 *
2492 * Arguments:
2493 * - [in] crt: the cert list EE, C1, ..., Cn
2494 * - [in] trust_ca: the trusted list R1, ..., Rp
2495 * - [in] ca_crl, profile: as in verify_with_profile()
2496 * - [out] ver_chain: the built and verified chain
2497 * Only valid when return value is 0, may contain garbage otherwise!
2498 * Restart note: need not be the same when calling again to resume.
2499 * - [in-out] rs_ctx: context for restarting operations
2500 *
2501 * Return value:
2502 * - non-zero if the chain could not be fully built and examined
2503 * - 0 is the chain was successfully built and examined,
2504 * even if it was found to be invalid
2505 */
2506static int x509_crt_verify_chain(
Jens Wiklander32b31802023-10-06 16:59:46 +02002507 mbedtls_x509_crt *crt,
2508 mbedtls_x509_crt *trust_ca,
2509 mbedtls_x509_crl *ca_crl,
2510 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2511 void *p_ca_cb,
2512 const mbedtls_x509_crt_profile *profile,
2513 mbedtls_x509_crt_verify_chain *ver_chain,
2514 mbedtls_x509_crt_restart_ctx *rs_ctx)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002515{
2516 /* Don't initialize any of those variables here, so that the compiler can
2517 * catch potential issues with jumping ahead when restarting */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002519 uint32_t *flags;
2520 mbedtls_x509_crt_verify_chain_item *cur;
2521 mbedtls_x509_crt *child;
2522 mbedtls_x509_crt *parent;
2523 int parent_is_trusted;
2524 int child_is_trusted;
2525 int signature_is_good;
2526 unsigned self_cnt;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002527 mbedtls_x509_crt *cur_trust_ca = NULL;
Tom Van Eyckb0563632024-06-13 16:20:14 +02002528 mbedtls_x509_time now;
2529
2530#if defined(MBEDTLS_HAVE_TIME_DATE)
2531 if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2532 return MBEDTLS_ERR_X509_FATAL_ERROR;
2533 }
2534#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002535
2536#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2537 /* resume if we had an operation in progress */
Jens Wiklander32b31802023-10-06 16:59:46 +02002538 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002539 /* restore saved state */
2540 *ver_chain = rs_ctx->ver_chain; /* struct copy */
2541 self_cnt = rs_ctx->self_cnt;
2542
2543 /* restore derived state */
2544 cur = &ver_chain->items[ver_chain->len - 1];
2545 child = cur->crt;
2546 flags = &cur->flags;
2547
2548 goto find_parent;
2549 }
2550#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
2551
2552 child = crt;
2553 self_cnt = 0;
2554 parent_is_trusted = 0;
2555 child_is_trusted = 0;
2556
Jens Wiklander32b31802023-10-06 16:59:46 +02002557 while (1) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002558 /* Add certificate to the verification chain */
2559 cur = &ver_chain->items[ver_chain->len];
2560 cur->crt = child;
2561 cur->flags = 0;
2562 ver_chain->len++;
2563 flags = &cur->flags;
2564
Tom Van Eyckb0563632024-06-13 16:20:14 +02002565#if defined(MBEDTLS_HAVE_TIME_DATE)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002566 /* Check time-validity (all certificates) */
Tom Van Eyckb0563632024-06-13 16:20:14 +02002567 if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002568 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Jens Wiklander32b31802023-10-06 16:59:46 +02002569 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002570
Tom Van Eyckb0563632024-06-13 16:20:14 +02002571 if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002572 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Jens Wiklander32b31802023-10-06 16:59:46 +02002573 }
Tom Van Eyckb0563632024-06-13 16:20:14 +02002574#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002575
2576 /* Stop here for trusted roots (but not for trusted EE certs) */
Jens Wiklander32b31802023-10-06 16:59:46 +02002577 if (child_is_trusted) {
2578 return 0;
2579 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002580
2581 /* Check signature algorithm: MD & PK algs */
Jens Wiklander32b31802023-10-06 16:59:46 +02002582 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002583 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Jens Wiklander32b31802023-10-06 16:59:46 +02002584 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002585
Jens Wiklander32b31802023-10-06 16:59:46 +02002586 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002587 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Jens Wiklander32b31802023-10-06 16:59:46 +02002588 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002589
2590 /* Special case: EE certs that are locally trusted */
Jens Wiklander32b31802023-10-06 16:59:46 +02002591 if (ver_chain->len == 1 &&
2592 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2593 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002594 }
2595
2596#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2597find_parent:
2598#endif
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002599
2600 /* Obtain list of potential trusted signers from CA callback,
2601 * or use statically provided list. */
2602#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Jens Wiklander32b31802023-10-06 16:59:46 +02002603 if (f_ca_cb != NULL) {
2604 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2605 mbedtls_free(ver_chain->trust_ca_cb_result);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002606 ver_chain->trust_ca_cb_result = NULL;
2607
Jens Wiklander32b31802023-10-06 16:59:46 +02002608 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2609 if (ret != 0) {
2610 return MBEDTLS_ERR_X509_FATAL_ERROR;
2611 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002612
2613 cur_trust_ca = ver_chain->trust_ca_cb_result;
Jens Wiklander32b31802023-10-06 16:59:46 +02002614 } else
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002615#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2616 {
2617 ((void) f_ca_cb);
2618 ((void) p_ca_cb);
2619 cur_trust_ca = trust_ca;
2620 }
2621
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002622 /* Look for a parent in trusted CAs or up the chain */
Jens Wiklander32b31802023-10-06 16:59:46 +02002623 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2624 &parent_is_trusted, &signature_is_good,
Tom Van Eyckb0563632024-06-13 16:20:14 +02002625 ver_chain->len - 1, self_cnt, rs_ctx,
2626 &now);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002627
2628#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Jens Wiklander32b31802023-10-06 16:59:46 +02002629 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002630 /* save state */
2631 rs_ctx->in_progress = x509_crt_rs_find_parent;
2632 rs_ctx->self_cnt = self_cnt;
2633 rs_ctx->ver_chain = *ver_chain; /* struct copy */
2634
Jens Wiklander32b31802023-10-06 16:59:46 +02002635 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002636 }
2637#else
2638 (void) ret;
2639#endif
2640
2641 /* No parent? We're done here */
Jens Wiklander32b31802023-10-06 16:59:46 +02002642 if (parent == NULL) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002643 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Jens Wiklander32b31802023-10-06 16:59:46 +02002644 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002645 }
2646
2647 /* Count intermediate self-issued (not necessarily self-signed) certs.
2648 * These can occur with some strategies for key rollover, see [SIRO],
2649 * and should be excluded from max_pathlen checks. */
Jens Wiklander32b31802023-10-06 16:59:46 +02002650 if (ver_chain->len != 1 &&
2651 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002652 self_cnt++;
2653 }
2654
2655 /* path_cnt is 0 for the first intermediate CA,
2656 * and if parent is trusted it's not an intermediate CA */
Jens Wiklander32b31802023-10-06 16:59:46 +02002657 if (!parent_is_trusted &&
2658 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002659 /* return immediately to avoid overflow the chain array */
Jens Wiklander32b31802023-10-06 16:59:46 +02002660 return MBEDTLS_ERR_X509_FATAL_ERROR;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002661 }
2662
2663 /* signature was checked while searching parent */
Jens Wiklander32b31802023-10-06 16:59:46 +02002664 if (!signature_is_good) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002665 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Jens Wiklander32b31802023-10-06 16:59:46 +02002666 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002667
2668 /* check size of signing key */
Jens Wiklander32b31802023-10-06 16:59:46 +02002669 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002670 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Jens Wiklander32b31802023-10-06 16:59:46 +02002671 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002672
2673#if defined(MBEDTLS_X509_CRL_PARSE_C)
2674 /* Check trusted CA's CRL for the given crt */
Tom Van Eyckb0563632024-06-13 16:20:14 +02002675 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002676#else
2677 (void) ca_crl;
2678#endif
2679
2680 /* prepare for next iteration */
2681 child = parent;
2682 parent = NULL;
2683 child_is_trusted = parent_is_trusted;
2684 signature_is_good = 0;
2685 }
2686}
2687
Tom Van Eyckb0563632024-06-13 16:20:14 +02002688#ifdef _WIN32
2689#ifdef _MSC_VER
2690#pragma comment(lib, "ws2_32.lib")
2691#include <winsock2.h>
2692#include <ws2tcpip.h>
2693#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2694#include <winsock2.h>
2695#include <ws2tcpip.h>
2696#else
2697/* inet_pton() is not supported, fallback to software version */
2698#define MBEDTLS_TEST_SW_INET_PTON
2699#endif
2700#elif defined(__sun)
2701/* Solaris requires -lsocket -lnsl for inet_pton() */
2702#elif defined(__has_include)
2703#if __has_include(<sys/socket.h>)
2704#include <sys/socket.h>
2705#endif
2706#if __has_include(<arpa/inet.h>)
2707#include <arpa/inet.h>
2708#endif
2709#endif
2710
2711/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2712 * the platform inet_pton() or a local implementation (below). The local
2713 * implementation may be used even in cases where the platform provides
2714 * inet_pton(), e.g. when there are different includes required and/or the
2715 * platform implementation requires dependencies on additional libraries.
2716 * Specifically, Windows requires custom includes and additional link
2717 * dependencies, and Solaris requires additional link dependencies.
2718 * Also, as a coarse heuristic, use the local implementation if the compiler
2719 * does not support __has_include(), or if the definition of AF_INET6 is not
2720 * provided by headers included (or not) via __has_include() above.
2721 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2722 * despite having a platform that has inet_pton. */
2723#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2724/* Definition located further below to possibly reduce compiler inlining */
2725static int x509_inet_pton_ipv4(const char *src, void *dst);
2726
2727#define li_cton(c, n) \
2728 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2729
2730static int x509_inet_pton_ipv6(const char *src, void *dst)
2731{
2732 const unsigned char *p = (const unsigned char *) src;
2733 int nonzero_groups = 0, num_digits, zero_group_start = -1;
2734 uint16_t addr[8];
2735 do {
2736 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
2737 uint16_t group = num_digits = 0;
2738 for (uint8_t digit; num_digits < 4; num_digits++) {
2739 if (li_cton(*p, digit) == 0) {
2740 break;
2741 }
2742 group = (group << 4) | digit;
2743 p++;
2744 }
2745 if (num_digits != 0) {
2746 MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups);
2747 nonzero_groups++;
2748 if (*p == '\0') {
2749 break;
2750 } else if (*p == '.') {
2751 /* Don't accept IPv4 too early or late */
2752 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2753 nonzero_groups >= 7) {
2754 break;
2755 }
2756
2757 /* Walk back to prior ':', then parse as IPv4-mapped */
2758 int steps = 4;
2759 do {
2760 p--;
2761 steps--;
2762 } while (*p != ':' && steps > 0);
2763
2764 if (*p != ':') {
2765 break;
2766 }
2767 p++;
2768 nonzero_groups--;
2769 if (x509_inet_pton_ipv4((const char *) p,
2770 addr + nonzero_groups) != 0) {
2771 break;
2772 }
2773
2774 nonzero_groups += 2;
2775 p = (const unsigned char *) "";
2776 break;
2777 } else if (*p != ':') {
2778 return -1;
2779 }
2780 } else {
2781 /* Don't accept a second zero group or an invalid delimiter */
2782 if (zero_group_start != -1 || *p != ':') {
2783 return -1;
2784 }
2785 zero_group_start = nonzero_groups;
2786
2787 /* Accept a zero group at start, but it has to be a double colon */
2788 if (zero_group_start == 0 && *++p != ':') {
2789 return -1;
2790 }
2791
2792 if (p[1] == '\0') {
2793 ++p;
2794 break;
2795 }
2796 }
2797 ++p;
2798 } while (nonzero_groups < 8);
2799
2800 if (*p != '\0') {
2801 return -1;
2802 }
2803
2804 if (zero_group_start != -1) {
2805 if (nonzero_groups > 6) {
2806 return -1;
2807 }
2808 int zero_groups = 8 - nonzero_groups;
2809 int groups_after_zero = nonzero_groups - zero_group_start;
2810
2811 /* Move the non-zero part to after the zeroes */
2812 if (groups_after_zero) {
2813 memmove(addr + zero_group_start + zero_groups,
2814 addr + zero_group_start,
2815 groups_after_zero * sizeof(*addr));
2816 }
2817 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
2818 } else {
2819 if (nonzero_groups != 8) {
2820 return -1;
2821 }
2822 }
2823 memcpy(dst, addr, sizeof(addr));
2824 return 0;
2825}
2826
2827static int x509_inet_pton_ipv4(const char *src, void *dst)
2828{
2829 const unsigned char *p = (const unsigned char *) src;
2830 uint8_t *res = (uint8_t *) dst;
2831 uint8_t digit, num_digits = 0;
2832 uint8_t num_octets = 0;
2833 uint16_t octet;
2834
2835 do {
2836 octet = num_digits = 0;
2837 do {
2838 digit = *p - '0';
2839 if (digit > 9) {
2840 break;
2841 }
2842
2843 /* Don't allow leading zeroes. These might mean octal format,
2844 * which this implementation does not support. */
2845 if (octet == 0 && num_digits > 0) {
2846 return -1;
2847 }
2848
2849 octet = octet * 10 + digit;
2850 num_digits++;
2851 p++;
2852 } while (num_digits < 3);
2853
2854 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
2855 return -1;
2856 }
2857 *res++ = (uint8_t) octet;
2858 num_octets++;
2859 } while (num_octets < 4 && *p++ == '.');
2860 return num_octets == 4 && *p == '\0' ? 0 : -1;
2861}
2862
2863#else
2864
2865static int x509_inet_pton_ipv6(const char *src, void *dst)
2866{
2867 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2868}
2869
2870static int x509_inet_pton_ipv4(const char *src, void *dst)
2871{
2872 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2873}
2874
2875#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
2876
2877size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
2878{
2879 return strchr(cn, ':') == NULL
2880 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2881 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2882}
2883
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002884/*
2885 * Check for CN match
2886 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002887static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2888 const char *cn, size_t cn_len)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002889{
2890 /* try exact match */
Jens Wiklander32b31802023-10-06 16:59:46 +02002891 if (name->len == cn_len &&
2892 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2893 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002894 }
2895
2896 /* try wildcard match */
Jens Wiklander32b31802023-10-06 16:59:46 +02002897 if (x509_check_wildcard(cn, name) == 0) {
2898 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002899 }
2900
Jens Wiklander32b31802023-10-06 16:59:46 +02002901 return -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002902}
2903
Tom Van Eyckb0563632024-06-13 16:20:14 +02002904static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2905 const char *cn, size_t cn_len)
2906{
2907 uint32_t ip[4];
2908 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
2909 if (cn_len == 0) {
2910 return -1;
2911 }
2912
2913 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2914 const unsigned char san_type = (unsigned char) cur->buf.tag &
2915 MBEDTLS_ASN1_TAG_VALUE_MASK;
2916 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2917 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2918 return 0;
2919 }
2920 }
2921
2922 return -1;
2923}
2924
2925static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2926 const char *cn, size_t cn_len)
2927{
2928 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2929 const unsigned char san_type = (unsigned char) cur->buf.tag &
2930 MBEDTLS_ASN1_TAG_VALUE_MASK;
2931 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2932 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2933 return 0;
2934 }
2935 }
2936
2937 return -1;
2938}
2939
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002940/*
Jerome Forissier79013242021-07-28 10:24:04 +02002941 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2942 */
Tom Van Eyckb0563632024-06-13 16:20:14 +02002943static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Jens Wiklander32b31802023-10-06 16:59:46 +02002944 const char *cn, size_t cn_len)
Jerome Forissier79013242021-07-28 10:24:04 +02002945{
Tom Van Eyckb0563632024-06-13 16:20:14 +02002946 int san_ip = 0;
2947 int san_uri = 0;
2948 /* Prioritize DNS name over other subtypes due to popularity */
2949 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2950 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
2951 case MBEDTLS_X509_SAN_DNS_NAME:
2952 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2953 return 0;
2954 }
2955 break;
2956 case MBEDTLS_X509_SAN_IP_ADDRESS:
2957 san_ip = 1;
2958 break;
2959 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
2960 san_uri = 1;
2961 break;
2962 /* (We may handle other types here later.) */
2963 default: /* Unrecognized type */
2964 break;
2965 }
2966 }
2967 if (san_ip) {
2968 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
2969 return 0;
2970 }
2971 }
2972 if (san_uri) {
2973 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
2974 return 0;
2975 }
Jens Wiklander32b31802023-10-06 16:59:46 +02002976 }
Jerome Forissier79013242021-07-28 10:24:04 +02002977
Jens Wiklander32b31802023-10-06 16:59:46 +02002978 return -1;
Jerome Forissier79013242021-07-28 10:24:04 +02002979}
2980
2981/*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002982 * Verify the requested CN - only call this if cn is not NULL!
2983 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002984static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2985 const char *cn,
2986 uint32_t *flags)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002987{
2988 const mbedtls_x509_name *name;
Jens Wiklander32b31802023-10-06 16:59:46 +02002989 size_t cn_len = strlen(cn);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002990
Jens Wiklander32b31802023-10-06 16:59:46 +02002991 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Tom Van Eyckb0563632024-06-13 16:20:14 +02002992 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
2993 return;
Jens Wiklander32b31802023-10-06 16:59:46 +02002994 }
2995 } else {
2996 for (name = &crt->subject; name != NULL; name = name->next) {
2997 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2998 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Tom Van Eyckb0563632024-06-13 16:20:14 +02002999 return;
Jens Wiklander32b31802023-10-06 16:59:46 +02003000 }
3001 }
3002
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003003 }
Tom Van Eyckb0563632024-06-13 16:20:14 +02003004
3005 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003006}
3007
3008/*
3009 * Merge the flags for all certs in the chain, after calling callback
3010 */
3011static int x509_crt_merge_flags_with_cb(
Jens Wiklander32b31802023-10-06 16:59:46 +02003012 uint32_t *flags,
3013 const mbedtls_x509_crt_verify_chain *ver_chain,
3014 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3015 void *p_vrfy)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003016{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003017 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003018 unsigned i;
3019 uint32_t cur_flags;
3020 const mbedtls_x509_crt_verify_chain_item *cur;
3021
Jens Wiklander32b31802023-10-06 16:59:46 +02003022 for (i = ver_chain->len; i != 0; --i) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003023 cur = &ver_chain->items[i-1];
3024 cur_flags = cur->flags;
3025
Jens Wiklander32b31802023-10-06 16:59:46 +02003026 if (NULL != f_vrfy) {
3027 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3028 return ret;
3029 }
3030 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003031
3032 *flags |= cur_flags;
3033 }
3034
Jens Wiklander32b31802023-10-06 16:59:46 +02003035 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003036}
3037
3038/*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003039 * Verify the certificate validity, with profile, restartable version
3040 *
3041 * This function:
3042 * - checks the requested CN (if any)
3043 * - checks the type and size of the EE cert's key,
3044 * as that isn't done as part of chain building/verification currently
3045 * - builds and verifies the chain
3046 * - then calls the callback and merges the flags
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003047 *
3048 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3049 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3050 * verification routine to search for trusted signers, and CRLs will
3051 * be disabled. Otherwise, `trust_ca` will be used as the static list
3052 * of trusted signers, and `ca_crl` will be use as the static list
3053 * of CRLs.
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003054 */
Jens Wiklander32b31802023-10-06 16:59:46 +02003055static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3056 mbedtls_x509_crt *trust_ca,
3057 mbedtls_x509_crl *ca_crl,
3058 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3059 void *p_ca_cb,
3060 const mbedtls_x509_crt_profile *profile,
3061 const char *cn, uint32_t *flags,
3062 int (*f_vrfy)(void *,
3063 mbedtls_x509_crt *,
3064 int,
3065 uint32_t *),
3066 void *p_vrfy,
3067 mbedtls_x509_crt_restart_ctx *rs_ctx)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003068{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003069 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02003070 mbedtls_pk_type_t pk_type;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003071 mbedtls_x509_crt_verify_chain ver_chain;
3072 uint32_t ee_flags;
Jens Wiklander817466c2018-05-22 13:49:31 +02003073
3074 *flags = 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003075 ee_flags = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +02003076 x509_crt_verify_chain_reset(&ver_chain);
Jens Wiklander817466c2018-05-22 13:49:31 +02003077
Jens Wiklander32b31802023-10-06 16:59:46 +02003078 if (profile == NULL) {
Jens Wiklander817466c2018-05-22 13:49:31 +02003079 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3080 goto exit;
3081 }
3082
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003083 /* check name if requested */
Jens Wiklander32b31802023-10-06 16:59:46 +02003084 if (cn != NULL) {
3085 x509_crt_verify_name(crt, cn, &ee_flags);
3086 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003087
3088 /* Check the type and size of the key */
Jens Wiklander32b31802023-10-06 16:59:46 +02003089 pk_type = mbedtls_pk_get_type(&crt->pk);
Jens Wiklander817466c2018-05-22 13:49:31 +02003090
Jens Wiklander32b31802023-10-06 16:59:46 +02003091 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003092 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Jens Wiklander32b31802023-10-06 16:59:46 +02003093 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003094
Jens Wiklander32b31802023-10-06 16:59:46 +02003095 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003096 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Jens Wiklander32b31802023-10-06 16:59:46 +02003097 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003098
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003099 /* Check the chain */
Jens Wiklander32b31802023-10-06 16:59:46 +02003100 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3101 f_ca_cb, p_ca_cb, profile,
3102 &ver_chain, rs_ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +02003103
Jens Wiklander32b31802023-10-06 16:59:46 +02003104 if (ret != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003105 goto exit;
Jens Wiklander32b31802023-10-06 16:59:46 +02003106 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003107
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003108 /* Merge end-entity flags */
3109 ver_chain.items[0].flags |= ee_flags;
3110
3111 /* Build final flags, calling callback on the way if any */
Jens Wiklander32b31802023-10-06 16:59:46 +02003112 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Jens Wiklander817466c2018-05-22 13:49:31 +02003113
3114exit:
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003115
3116#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Jens Wiklander32b31802023-10-06 16:59:46 +02003117 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3118 mbedtls_free(ver_chain.trust_ca_cb_result);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003119 ver_chain.trust_ca_cb_result = NULL;
3120#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3121
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003122#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Jens Wiklander32b31802023-10-06 16:59:46 +02003123 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3124 mbedtls_x509_crt_restart_free(rs_ctx);
3125 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003126#endif
3127
Jens Wiklander817466c2018-05-22 13:49:31 +02003128 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3129 * the SSL module for authmode optional, but non-zero return from the
3130 * callback means a fatal error so it shouldn't be ignored */
Jens Wiklander32b31802023-10-06 16:59:46 +02003131 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Jens Wiklander817466c2018-05-22 13:49:31 +02003132 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Jens Wiklander817466c2018-05-22 13:49:31 +02003133 }
3134
Jens Wiklander32b31802023-10-06 16:59:46 +02003135 if (ret != 0) {
3136 *flags = (uint32_t) -1;
3137 return ret;
3138 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003139
Jens Wiklander32b31802023-10-06 16:59:46 +02003140 if (*flags != 0) {
3141 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3142 }
3143
3144 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02003145}
3146
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003147
3148/*
3149 * Verify the certificate validity (default profile, not restartable)
3150 */
Jens Wiklander32b31802023-10-06 16:59:46 +02003151int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3152 mbedtls_x509_crt *trust_ca,
3153 mbedtls_x509_crl *ca_crl,
3154 const char *cn, uint32_t *flags,
3155 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3156 void *p_vrfy)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003157{
Jens Wiklander32b31802023-10-06 16:59:46 +02003158 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3159 NULL, NULL,
3160 &mbedtls_x509_crt_profile_default,
3161 cn, flags,
3162 f_vrfy, p_vrfy, NULL);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003163}
3164
3165/*
3166 * Verify the certificate validity (user-chosen profile, not restartable)
3167 */
Jens Wiklander32b31802023-10-06 16:59:46 +02003168int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3169 mbedtls_x509_crt *trust_ca,
3170 mbedtls_x509_crl *ca_crl,
3171 const mbedtls_x509_crt_profile *profile,
3172 const char *cn, uint32_t *flags,
3173 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3174 void *p_vrfy)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003175{
Jens Wiklander32b31802023-10-06 16:59:46 +02003176 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3177 NULL, NULL,
3178 profile, cn, flags,
3179 f_vrfy, p_vrfy, NULL);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003180}
3181
3182#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3183/*
3184 * Verify the certificate validity (user-chosen profile, CA callback,
3185 * not restartable).
3186 */
Jens Wiklander32b31802023-10-06 16:59:46 +02003187int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3188 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3189 void *p_ca_cb,
3190 const mbedtls_x509_crt_profile *profile,
3191 const char *cn, uint32_t *flags,
3192 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3193 void *p_vrfy)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003194{
Jens Wiklander32b31802023-10-06 16:59:46 +02003195 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3196 f_ca_cb, p_ca_cb,
3197 profile, cn, flags,
3198 f_vrfy, p_vrfy, NULL);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003199}
3200#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3201
Jens Wiklander32b31802023-10-06 16:59:46 +02003202int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3203 mbedtls_x509_crt *trust_ca,
3204 mbedtls_x509_crl *ca_crl,
3205 const mbedtls_x509_crt_profile *profile,
3206 const char *cn, uint32_t *flags,
3207 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3208 void *p_vrfy,
3209 mbedtls_x509_crt_restart_ctx *rs_ctx)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003210{
Jens Wiklander32b31802023-10-06 16:59:46 +02003211 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3212 NULL, NULL,
3213 profile, cn, flags,
3214 f_vrfy, p_vrfy, rs_ctx);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003215}
3216
3217
Jens Wiklander817466c2018-05-22 13:49:31 +02003218/*
3219 * Initialize a certificate chain
3220 */
Jens Wiklander32b31802023-10-06 16:59:46 +02003221void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Jens Wiklander817466c2018-05-22 13:49:31 +02003222{
Jens Wiklander32b31802023-10-06 16:59:46 +02003223 memset(crt, 0, sizeof(mbedtls_x509_crt));
Jens Wiklander817466c2018-05-22 13:49:31 +02003224}
3225
3226/*
3227 * Unallocate all certificate data
3228 */
Jens Wiklander32b31802023-10-06 16:59:46 +02003229void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Jens Wiklander817466c2018-05-22 13:49:31 +02003230{
3231 mbedtls_x509_crt *cert_cur = crt;
3232 mbedtls_x509_crt *cert_prv;
Jens Wiklander817466c2018-05-22 13:49:31 +02003233
Jens Wiklander32b31802023-10-06 16:59:46 +02003234 while (cert_cur != NULL) {
3235 mbedtls_pk_free(&cert_cur->pk);
Jens Wiklander817466c2018-05-22 13:49:31 +02003236
3237#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Jens Wiklander32b31802023-10-06 16:59:46 +02003238 mbedtls_free(cert_cur->sig_opts);
Jens Wiklander817466c2018-05-22 13:49:31 +02003239#endif
3240
Jens Wiklander32b31802023-10-06 16:59:46 +02003241 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3242 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3243 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3244 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3245 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Tom Van Eyckb0563632024-06-13 16:20:14 +02003246 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Jens Wiklander32b31802023-10-06 16:59:46 +02003247
3248 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
Tom Van Eyckb0563632024-06-13 16:20:14 +02003249 mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len);
Jens Wiklander817466c2018-05-22 13:49:31 +02003250 }
3251
Jens Wiklander817466c2018-05-22 13:49:31 +02003252 cert_prv = cert_cur;
3253 cert_cur = cert_cur->next;
3254
Jens Wiklander32b31802023-10-06 16:59:46 +02003255 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3256 if (cert_prv != crt) {
3257 mbedtls_free(cert_prv);
3258 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003259 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003260}
3261
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003262#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3263/*
3264 * Initialize a restart context
3265 */
Jens Wiklander32b31802023-10-06 16:59:46 +02003266void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003267{
Jens Wiklander32b31802023-10-06 16:59:46 +02003268 mbedtls_pk_restart_init(&ctx->pk);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003269
3270 ctx->parent = NULL;
3271 ctx->fallback_parent = NULL;
3272 ctx->fallback_signature_is_good = 0;
3273
3274 ctx->parent_is_trusted = -1;
3275
3276 ctx->in_progress = x509_crt_rs_none;
3277 ctx->self_cnt = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +02003278 x509_crt_verify_chain_reset(&ctx->ver_chain);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003279}
3280
3281/*
3282 * Free the components of a restart context
3283 */
Jens Wiklander32b31802023-10-06 16:59:46 +02003284void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003285{
Jens Wiklander32b31802023-10-06 16:59:46 +02003286 if (ctx == NULL) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003287 return;
Jens Wiklander32b31802023-10-06 16:59:46 +02003288 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003289
Jens Wiklander32b31802023-10-06 16:59:46 +02003290 mbedtls_pk_restart_free(&ctx->pk);
3291 mbedtls_x509_crt_restart_init(ctx);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003292}
3293#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3294
Tom Van Eyckb0563632024-06-13 16:20:14 +02003295int mbedtls_x509_crt_get_ca_istrue(const mbedtls_x509_crt *crt)
3296{
3297 if ((crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) != 0) {
3298 return crt->MBEDTLS_PRIVATE(ca_istrue);
3299 }
3300 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
3301}
3302
Jens Wiklander817466c2018-05-22 13:49:31 +02003303#endif /* MBEDTLS_X509_CRT_PARSE_C */