blob: cf62532f28b5373f2af57fb9120e2c8e974e514f [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
5 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02006 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Jens Wiklander817466c2018-05-22 13:49:31 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
22 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
25 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Jens Wiklander3d3b0592019-03-20 15:30:29 +010028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Jens Wiklander817466c2018-05-22 13:49:31 +020030 */
31
Jerome Forissier79013242021-07-28 10:24:04 +020032#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020033
34#if defined(MBEDTLS_X509_CRT_PARSE_C)
35
36#include "mbedtls/x509_crt.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020037#include "mbedtls/error.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020038#include "mbedtls/oid.h"
Jens Wiklander3d3b0592019-03-20 15:30:29 +010039#include "mbedtls/platform_util.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020040
Jens Wiklander817466c2018-05-22 13:49:31 +020041#include <string.h>
42
43#if defined(MBEDTLS_PEM_PARSE_C)
44#include "mbedtls/pem.h"
45#endif
46
Jerome Forissier11fa71b2020-04-20 17:17:56 +020047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
49#include "mbedtls/psa_util.h"
Jens Wiklander32b31802023-10-06 16:59:46 +020050#endif /* MBEDTLS_USE_PSA_CRYPTO */
51#include "hash_info.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020052
Jens Wiklander817466c2018-05-22 13:49:31 +020053#include "mbedtls/platform.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020054
55#if defined(MBEDTLS_THREADING_C)
56#include "mbedtls/threading.h"
57#endif
58
Jerome Forissier039e02d2022-08-09 17:10:15 +020059#if defined(MBEDTLS_HAVE_TIME)
Jens Wiklander817466c2018-05-22 13:49:31 +020060#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
61#include <windows.h>
62#else
63#include <time.h>
64#endif
Jerome Forissier039e02d2022-08-09 17:10:15 +020065#endif
Jens Wiklander817466c2018-05-22 13:49:31 +020066
67#if defined(MBEDTLS_FS_IO)
68#include <stdio.h>
69#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
70#include <sys/types.h>
71#include <sys/stat.h>
Jens Wiklander32b31802023-10-06 16:59:46 +020072#if defined(__MBED__)
73#include <platform/mbed_retarget.h>
74#else
Jens Wiklander817466c2018-05-22 13:49:31 +020075#include <dirent.h>
Jens Wiklander32b31802023-10-06 16:59:46 +020076#endif /* __MBED__ */
77#include <errno.h>
Jens Wiklander817466c2018-05-22 13:49:31 +020078#endif /* !_WIN32 || EFIX64 || EFI32 */
79#endif
80
Jens Wiklander3d3b0592019-03-20 15:30:29 +010081/*
82 * Item in a verification chain: cert and flags for it
83 */
84typedef struct {
85 mbedtls_x509_crt *crt;
86 uint32_t flags;
87} x509_crt_verify_chain_item;
88
89/*
90 * Max size of verification chain: end-entity + intermediates + trusted root
91 */
Jens Wiklander32b31802023-10-06 16:59:46 +020092#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Jens Wiklander817466c2018-05-22 13:49:31 +020093
Jerome Forissier79013242021-07-28 10:24:04 +020094/* Default profile. Do not remove items unless there are serious security
95 * concerns. */
Jens Wiklander817466c2018-05-22 13:49:31 +020096const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
97{
Jens Wiklander32b31802023-10-06 16:59:46 +020098 /* Hashes from SHA-256 and above. Note that this selection
99 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
100 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
101 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
102 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Jens Wiklander817466c2018-05-22 13:49:31 +0200103 0xFFFFFFF, /* Any PK alg */
Jens Wiklander32b31802023-10-06 16:59:46 +0200104#if defined(MBEDTLS_ECP_C)
105 /* Curves at or above 128-bit security level. Note that this selection
106 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
107 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
108 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
109 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
110 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
111 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
112 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
113 0,
114#else
115 0,
116#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200117 2048,
118};
119
Jens Wiklander32b31802023-10-06 16:59:46 +0200120/* Next-generation profile. Currently identical to the default, but may
121 * be tightened at any time. */
Jens Wiklander817466c2018-05-22 13:49:31 +0200122const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
123{
Jens Wiklander32b31802023-10-06 16:59:46 +0200124 /* Hashes from SHA-256 and above. */
125 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
126 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
127 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Jens Wiklander817466c2018-05-22 13:49:31 +0200128 0xFFFFFFF, /* Any PK alg */
129#if defined(MBEDTLS_ECP_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200130 /* Curves at or above 128-bit security level. */
131 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
132 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
133 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
134 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
135 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
136 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
137 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Jens Wiklander817466c2018-05-22 13:49:31 +0200138#else
139 0,
140#endif
141 2048,
142};
143
144/*
145 * NSA Suite B Profile
146 */
147const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
148{
149 /* Only SHA-256 and 384 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200150 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
151 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Jens Wiklander817466c2018-05-22 13:49:31 +0200152 /* Only ECDSA */
Jens Wiklander32b31802023-10-06 16:59:46 +0200153 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
154 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Jens Wiklander817466c2018-05-22 13:49:31 +0200155#if defined(MBEDTLS_ECP_C)
156 /* Only NIST P-256 and P-384 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200157 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
158 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Jens Wiklander817466c2018-05-22 13:49:31 +0200159#else
160 0,
161#endif
162 0,
163};
164
165/*
Jens Wiklander32b31802023-10-06 16:59:46 +0200166 * Empty / all-forbidden profile
167 */
168const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
169{
170 0,
171 0,
172 0,
173 (uint32_t) -1,
174};
175
176/*
Jens Wiklander817466c2018-05-22 13:49:31 +0200177 * Check md_alg against profile
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100178 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Jens Wiklander817466c2018-05-22 13:49:31 +0200179 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200180static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
181 mbedtls_md_type_t md_alg)
Jens Wiklander817466c2018-05-22 13:49:31 +0200182{
Jens Wiklander32b31802023-10-06 16:59:46 +0200183 if (md_alg == MBEDTLS_MD_NONE) {
184 return -1;
185 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100186
Jens Wiklander32b31802023-10-06 16:59:46 +0200187 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
188 return 0;
189 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200190
Jens Wiklander32b31802023-10-06 16:59:46 +0200191 return -1;
Jens Wiklander817466c2018-05-22 13:49:31 +0200192}
193
194/*
195 * Check pk_alg against profile
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100196 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Jens Wiklander817466c2018-05-22 13:49:31 +0200197 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200198static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
199 mbedtls_pk_type_t pk_alg)
Jens Wiklander817466c2018-05-22 13:49:31 +0200200{
Jens Wiklander32b31802023-10-06 16:59:46 +0200201 if (pk_alg == MBEDTLS_PK_NONE) {
202 return -1;
203 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100204
Jens Wiklander32b31802023-10-06 16:59:46 +0200205 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
206 return 0;
207 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200208
Jens Wiklander32b31802023-10-06 16:59:46 +0200209 return -1;
Jens Wiklander817466c2018-05-22 13:49:31 +0200210}
211
212/*
213 * Check key against profile
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100214 * Return 0 if pk is acceptable for this profile, -1 otherwise
Jens Wiklander817466c2018-05-22 13:49:31 +0200215 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200216static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
217 const mbedtls_pk_context *pk)
Jens Wiklander817466c2018-05-22 13:49:31 +0200218{
Jens Wiklander32b31802023-10-06 16:59:46 +0200219 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100220
Jens Wiklander817466c2018-05-22 13:49:31 +0200221#if defined(MBEDTLS_RSA_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200222 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
223 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
224 return 0;
225 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200226
Jens Wiklander32b31802023-10-06 16:59:46 +0200227 return -1;
Jens Wiklander817466c2018-05-22 13:49:31 +0200228 }
229#endif
230
231#if defined(MBEDTLS_ECP_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200232 if (pk_alg == MBEDTLS_PK_ECDSA ||
Jens Wiklander817466c2018-05-22 13:49:31 +0200233 pk_alg == MBEDTLS_PK_ECKEY ||
Jens Wiklander32b31802023-10-06 16:59:46 +0200234 pk_alg == MBEDTLS_PK_ECKEY_DH) {
235 const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100236
Jens Wiklander32b31802023-10-06 16:59:46 +0200237 if (gid == MBEDTLS_ECP_DP_NONE) {
238 return -1;
239 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200240
Jens Wiklander32b31802023-10-06 16:59:46 +0200241 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
242 return 0;
243 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200244
Jens Wiklander32b31802023-10-06 16:59:46 +0200245 return -1;
Jens Wiklander817466c2018-05-22 13:49:31 +0200246 }
247#endif
248
Jens Wiklander32b31802023-10-06 16:59:46 +0200249 return -1;
Jens Wiklander817466c2018-05-22 13:49:31 +0200250}
251
252/*
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100253 * Like memcmp, but case-insensitive and always returns -1 if different
254 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200255static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100256{
257 size_t i;
258 unsigned char diff;
259 const unsigned char *n1 = s1, *n2 = s2;
260
Jens Wiklander32b31802023-10-06 16:59:46 +0200261 for (i = 0; i < len; i++) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100262 diff = n1[i] ^ n2[i];
263
Jens Wiklander32b31802023-10-06 16:59:46 +0200264 if (diff == 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100265 continue;
266 }
267
Jens Wiklander32b31802023-10-06 16:59:46 +0200268 if (diff == 32 &&
269 ((n1[i] >= 'a' && n1[i] <= 'z') ||
270 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
271 continue;
272 }
273
274 return -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100275 }
276
Jens Wiklander32b31802023-10-06 16:59:46 +0200277 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100278}
279
280/*
281 * Return 0 if name matches wildcard, -1 otherwise
282 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200283static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100284{
285 size_t i;
Jens Wiklander32b31802023-10-06 16:59:46 +0200286 size_t cn_idx = 0, cn_len = strlen(cn);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100287
288 /* We can't have a match if there is no wildcard to match */
Jens Wiklander32b31802023-10-06 16:59:46 +0200289 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
290 return -1;
291 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100292
Jens Wiklander32b31802023-10-06 16:59:46 +0200293 for (i = 0; i < cn_len; ++i) {
294 if (cn[i] == '.') {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100295 cn_idx = i;
296 break;
297 }
298 }
299
Jens Wiklander32b31802023-10-06 16:59:46 +0200300 if (cn_idx == 0) {
301 return -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100302 }
303
Jens Wiklander32b31802023-10-06 16:59:46 +0200304 if (cn_len - cn_idx == name->len - 1 &&
305 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
306 return 0;
307 }
308
309 return -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100310}
311
312/*
313 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
314 * variations (but not all).
315 *
316 * Return 0 if equal, -1 otherwise.
317 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200318static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100319{
Jens Wiklander32b31802023-10-06 16:59:46 +0200320 if (a->tag == b->tag &&
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100321 a->len == b->len &&
Jens Wiklander32b31802023-10-06 16:59:46 +0200322 memcmp(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 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
327 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100328 a->len == b->len &&
Jens Wiklander32b31802023-10-06 16:59:46 +0200329 x509_memcasecmp(a->p, b->p, b->len) == 0) {
330 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100331 }
332
Jens Wiklander32b31802023-10-06 16:59:46 +0200333 return -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100334}
335
336/*
337 * Compare two X.509 Names (aka rdnSequence).
338 *
339 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
340 * we sometimes return unequal when the full algorithm would return equal,
341 * but never the other way. (In particular, we don't do Unicode normalisation
342 * or space folding.)
343 *
344 * Return 0 if equal, -1 otherwise.
345 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200346static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100347{
348 /* Avoid recursion, it might not be optimised by the compiler */
Jens Wiklander32b31802023-10-06 16:59:46 +0200349 while (a != NULL || b != NULL) {
350 if (a == NULL || b == NULL) {
351 return -1;
352 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100353
354 /* type */
Jens Wiklander32b31802023-10-06 16:59:46 +0200355 if (a->oid.tag != b->oid.tag ||
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100356 a->oid.len != b->oid.len ||
Jens Wiklander32b31802023-10-06 16:59:46 +0200357 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
358 return -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100359 }
360
361 /* value */
Jens Wiklander32b31802023-10-06 16:59:46 +0200362 if (x509_string_cmp(&a->val, &b->val) != 0) {
363 return -1;
364 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100365
366 /* structure of the list of sets */
Jens Wiklander32b31802023-10-06 16:59:46 +0200367 if (a->next_merged != b->next_merged) {
368 return -1;
369 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100370
371 a = a->next;
372 b = b->next;
373 }
374
375 /* a == NULL == b */
Jens Wiklander32b31802023-10-06 16:59:46 +0200376 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100377}
378
379/*
380 * Reset (init or clear) a verify_chain
381 */
382static void x509_crt_verify_chain_reset(
Jens Wiklander32b31802023-10-06 16:59:46 +0200383 mbedtls_x509_crt_verify_chain *ver_chain)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100384{
385 size_t i;
386
Jens Wiklander32b31802023-10-06 16:59:46 +0200387 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100388 ver_chain->items[i].crt = NULL;
Jerome Forissier5b25c762020-04-07 11:18:49 +0200389 ver_chain->items[i].flags = (uint32_t) -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100390 }
391
392 ver_chain->len = 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200393
394#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
395 ver_chain->trust_ca_cb_result = NULL;
396#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100397}
398
399/*
Jens Wiklander817466c2018-05-22 13:49:31 +0200400 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
401 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200402static int x509_get_version(unsigned char **p,
403 const unsigned char *end,
404 int *ver)
Jens Wiklander817466c2018-05-22 13:49:31 +0200405{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200407 size_t len;
408
Jens Wiklander32b31802023-10-06 16:59:46 +0200409 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
410 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
411 0)) != 0) {
412 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200413 *ver = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +0200414 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200415 }
416
Jens Wiklander32b31802023-10-06 16:59:46 +0200417 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Jens Wiklander817466c2018-05-22 13:49:31 +0200418 }
419
420 end = *p + len;
421
Jens Wiklander32b31802023-10-06 16:59:46 +0200422 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
423 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
424 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200425
Jens Wiklander32b31802023-10-06 16:59:46 +0200426 if (*p != end) {
427 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
428 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
429 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200430
Jens Wiklander32b31802023-10-06 16:59:46 +0200431 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200432}
433
434/*
435 * Validity ::= SEQUENCE {
436 * notBefore Time,
437 * notAfter Time }
438 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200439static int x509_get_dates(unsigned char **p,
440 const unsigned char *end,
441 mbedtls_x509_time *from,
442 mbedtls_x509_time *to)
Jens Wiklander817466c2018-05-22 13:49:31 +0200443{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200445 size_t len;
446
Jens Wiklander32b31802023-10-06 16:59:46 +0200447 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
448 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
449 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
450 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200451
452 end = *p + len;
453
Jens Wiklander32b31802023-10-06 16:59:46 +0200454 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
455 return ret;
456 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200457
Jens Wiklander32b31802023-10-06 16:59:46 +0200458 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
459 return ret;
460 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200461
Jens Wiklander32b31802023-10-06 16:59:46 +0200462 if (*p != end) {
463 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
464 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
465 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200466
Jens Wiklander32b31802023-10-06 16:59:46 +0200467 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200468}
469
470/*
471 * X.509 v2/v3 unique identifier (not parsed)
472 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200473static int x509_get_uid(unsigned char **p,
474 const unsigned char *end,
475 mbedtls_x509_buf *uid, int n)
Jens Wiklander817466c2018-05-22 13:49:31 +0200476{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200478
Jens Wiklander32b31802023-10-06 16:59:46 +0200479 if (*p == end) {
480 return 0;
481 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200482
483 uid->tag = **p;
484
Jens Wiklander32b31802023-10-06 16:59:46 +0200485 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
486 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
487 n)) != 0) {
488 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
489 return 0;
490 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200491
Jens Wiklander32b31802023-10-06 16:59:46 +0200492 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Jens Wiklander817466c2018-05-22 13:49:31 +0200493 }
494
495 uid->p = *p;
496 *p += uid->len;
497
Jens Wiklander32b31802023-10-06 16:59:46 +0200498 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200499}
500
Jens Wiklander32b31802023-10-06 16:59:46 +0200501static int x509_get_basic_constraints(unsigned char **p,
502 const unsigned char *end,
503 int *ca_istrue,
504 int *max_pathlen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200505{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200507 size_t len;
508
509 /*
510 * BasicConstraints ::= SEQUENCE {
511 * cA BOOLEAN DEFAULT FALSE,
512 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
513 */
514 *ca_istrue = 0; /* DEFAULT FALSE */
515 *max_pathlen = 0; /* endless */
516
Jens Wiklander32b31802023-10-06 16:59:46 +0200517 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
518 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
519 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Wiklander817466c2018-05-22 13:49:31 +0200520 }
521
Jens Wiklander32b31802023-10-06 16:59:46 +0200522 if (*p == end) {
523 return 0;
524 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200525
Jens Wiklander32b31802023-10-06 16:59:46 +0200526 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
527 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
528 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
529 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200530
Jens Wiklander32b31802023-10-06 16:59:46 +0200531 if (ret != 0) {
532 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
533 }
534
535 if (*ca_istrue != 0) {
536 *ca_istrue = 1;
537 }
538 }
539
540 if (*p == end) {
541 return 0;
542 }
543
544 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
545 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
546 }
547
548 if (*p != end) {
549 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
550 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
551 }
Jerome Forissier79013242021-07-28 10:24:04 +0200552
553 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
554 * overflow, which is an undefined behavior. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200555 if (*max_pathlen == INT_MAX) {
556 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
557 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
558 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200559
560 (*max_pathlen)++;
561
Jens Wiklander32b31802023-10-06 16:59:46 +0200562 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200563}
564
565/*
566 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
567 *
568 * KeyPurposeId ::= OBJECT IDENTIFIER
569 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200570static int x509_get_ext_key_usage(unsigned char **p,
571 const unsigned char *end,
572 mbedtls_x509_sequence *ext_key_usage)
Jens Wiklander817466c2018-05-22 13:49:31 +0200573{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200574 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200575
Jens Wiklander32b31802023-10-06 16:59:46 +0200576 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
577 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Wiklander817466c2018-05-22 13:49:31 +0200578 }
579
Jens Wiklander32b31802023-10-06 16:59:46 +0200580 /* Sequence length must be >= 1 */
581 if (ext_key_usage->buf.p == NULL) {
582 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
583 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
584 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200585
Jens Wiklander32b31802023-10-06 16:59:46 +0200586 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200587}
588
589/*
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200590 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
591 *
592 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
593 *
594 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
595 *
596 * PolicyInformation ::= SEQUENCE {
597 * policyIdentifier CertPolicyId,
598 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
599 * PolicyQualifierInfo OPTIONAL }
600 *
601 * CertPolicyId ::= OBJECT IDENTIFIER
602 *
603 * PolicyQualifierInfo ::= SEQUENCE {
604 * policyQualifierId PolicyQualifierId,
605 * qualifier ANY DEFINED BY policyQualifierId }
606 *
607 * -- policyQualifierIds for Internet policy qualifiers
608 *
609 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
610 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
611 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
612 *
613 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
614 *
615 * Qualifier ::= CHOICE {
616 * cPSuri CPSuri,
617 * userNotice UserNotice }
618 *
619 * CPSuri ::= IA5String
620 *
621 * UserNotice ::= SEQUENCE {
622 * noticeRef NoticeReference OPTIONAL,
623 * explicitText DisplayText OPTIONAL }
624 *
625 * NoticeReference ::= SEQUENCE {
626 * organization DisplayText,
627 * noticeNumbers SEQUENCE OF INTEGER }
628 *
629 * DisplayText ::= CHOICE {
630 * ia5String IA5String (SIZE (1..200)),
631 * visibleString VisibleString (SIZE (1..200)),
632 * bmpString BMPString (SIZE (1..200)),
633 * utf8String UTF8String (SIZE (1..200)) }
634 *
635 * NOTE: we only parse and use anyPolicy without qualifiers at this point
636 * as defined in RFC 5280.
637 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200638static int x509_get_certificate_policies(unsigned char **p,
639 const unsigned char *end,
640 mbedtls_x509_sequence *certificate_policies)
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200641{
642 int ret, parse_ret = 0;
643 size_t len;
644 mbedtls_asn1_buf *buf;
645 mbedtls_asn1_sequence *cur = certificate_policies;
646
647 /* Get main sequence tag */
Jens Wiklander32b31802023-10-06 16:59:46 +0200648 ret = mbedtls_asn1_get_tag(p, end, &len,
649 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
650 if (ret != 0) {
651 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
652 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200653
Jens Wiklander32b31802023-10-06 16:59:46 +0200654 if (*p + len != end) {
655 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
656 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
657 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200658
659 /*
660 * Cannot be an empty sequence.
661 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200662 if (len == 0) {
663 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
664 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
665 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200666
Jens Wiklander32b31802023-10-06 16:59:46 +0200667 while (*p < end) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200668 mbedtls_x509_buf policy_oid;
669 const unsigned char *policy_end;
670
671 /*
672 * Get the policy sequence
673 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200674 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
675 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
676 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
677 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200678
679 policy_end = *p + len;
680
Jens Wiklander32b31802023-10-06 16:59:46 +0200681 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
682 MBEDTLS_ASN1_OID)) != 0) {
683 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
684 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200685
686 policy_oid.tag = MBEDTLS_ASN1_OID;
687 policy_oid.len = len;
688 policy_oid.p = *p;
689
690 /*
691 * Only AnyPolicy is currently supported when enforcing policy.
692 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200693 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200694 /*
695 * Set the parsing return code but continue parsing, in case this
Jens Wiklander32b31802023-10-06 16:59:46 +0200696 * extension is critical.
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200697 */
698 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
699 }
700
701 /* Allocate and assign next pointer */
Jens Wiklander32b31802023-10-06 16:59:46 +0200702 if (cur->buf.p != NULL) {
703 if (cur->next != NULL) {
704 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
705 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200706
Jens Wiklander32b31802023-10-06 16:59:46 +0200707 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200708
Jens Wiklander32b31802023-10-06 16:59:46 +0200709 if (cur->next == NULL) {
710 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
711 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
712 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200713
714 cur = cur->next;
715 }
716
Jens Wiklander32b31802023-10-06 16:59:46 +0200717 buf = &(cur->buf);
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200718 buf->tag = policy_oid.tag;
719 buf->p = policy_oid.p;
720 buf->len = policy_oid.len;
721
722 *p += len;
723
Jens Wiklander32b31802023-10-06 16:59:46 +0200724 /*
725 * If there is an optional qualifier, then *p < policy_end
726 * Check the Qualifier len to verify it doesn't exceed policy_end.
727 */
728 if (*p < policy_end) {
729 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
730 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
731 0) {
732 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
733 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200734 /*
735 * Skip the optional policy qualifiers.
736 */
737 *p += len;
738 }
739
Jens Wiklander32b31802023-10-06 16:59:46 +0200740 if (*p != policy_end) {
741 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
742 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
743 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200744 }
745
746 /* Set final sequence entry's next pointer to NULL */
747 cur->next = NULL;
748
Jens Wiklander32b31802023-10-06 16:59:46 +0200749 if (*p != end) {
750 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
751 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
752 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200753
Jens Wiklander32b31802023-10-06 16:59:46 +0200754 return parse_ret;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200755}
756
757/*
Jens Wiklander817466c2018-05-22 13:49:31 +0200758 * X.509 v3 extensions
759 *
760 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200761static int x509_get_crt_ext(unsigned char **p,
762 const unsigned char *end,
763 mbedtls_x509_crt *crt,
764 mbedtls_x509_crt_ext_cb_t cb,
765 void *p_ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200766{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200767 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200768 size_t len;
Jerome Forissier79013242021-07-28 10:24:04 +0200769 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Jens Wiklander817466c2018-05-22 13:49:31 +0200770
Jens Wiklander32b31802023-10-06 16:59:46 +0200771 if (*p == end) {
772 return 0;
773 }
Jerome Forissier5b25c762020-04-07 11:18:49 +0200774
Jens Wiklander32b31802023-10-06 16:59:46 +0200775 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
776 return ret;
777 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200778
Jerome Forissier5b25c762020-04-07 11:18:49 +0200779 end = crt->v3_ext.p + crt->v3_ext.len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200780 while (*p < end) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200781 /*
782 * Extension ::= SEQUENCE {
783 * extnID OBJECT IDENTIFIER,
784 * critical BOOLEAN DEFAULT FALSE,
785 * extnValue OCTET STRING }
786 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200787 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Jens Wiklander817466c2018-05-22 13:49:31 +0200788 int is_critical = 0; /* DEFAULT FALSE */
789 int ext_type = 0;
790
Jens Wiklander32b31802023-10-06 16:59:46 +0200791 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
792 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
793 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
794 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200795
796 end_ext_data = *p + len;
797
798 /* Get extension ID */
Jens Wiklander32b31802023-10-06 16:59:46 +0200799 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
800 MBEDTLS_ASN1_OID)) != 0) {
801 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
802 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200803
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100804 extn_oid.tag = MBEDTLS_ASN1_OID;
Jens Wiklander817466c2018-05-22 13:49:31 +0200805 extn_oid.p = *p;
806 *p += extn_oid.len;
807
Jens Wiklander817466c2018-05-22 13:49:31 +0200808 /* Get optional critical */
Jens Wiklander32b31802023-10-06 16:59:46 +0200809 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
810 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
811 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
812 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200813
814 /* Data should be octet string type */
Jens Wiklander32b31802023-10-06 16:59:46 +0200815 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
816 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
817 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
818 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200819
Jerome Forissier79013242021-07-28 10:24:04 +0200820 start_ext_octet = *p;
Jens Wiklander817466c2018-05-22 13:49:31 +0200821 end_ext_octet = *p + len;
822
Jens Wiklander32b31802023-10-06 16:59:46 +0200823 if (end_ext_octet != end_ext_data) {
824 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
825 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
826 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200827
828 /*
829 * Detect supported extensions
830 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200831 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Jens Wiklander817466c2018-05-22 13:49:31 +0200832
Jens Wiklander32b31802023-10-06 16:59:46 +0200833 if (ret != 0) {
Jerome Forissier79013242021-07-28 10:24:04 +0200834 /* Give the callback (if any) a chance to handle the extension */
Jens Wiklander32b31802023-10-06 16:59:46 +0200835 if (cb != NULL) {
836 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
837 if (ret != 0 && is_critical) {
838 return ret;
839 }
Jerome Forissier79013242021-07-28 10:24:04 +0200840 *p = end_ext_octet;
841 continue;
842 }
843
Jens Wiklander817466c2018-05-22 13:49:31 +0200844 /* No parser found, skip extension */
845 *p = end_ext_octet;
846
Jens Wiklander32b31802023-10-06 16:59:46 +0200847 if (is_critical) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200848 /* Data is marked as critical: fail */
Jens Wiklander32b31802023-10-06 16:59:46 +0200849 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
850 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Jens Wiklander817466c2018-05-22 13:49:31 +0200851 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200852 continue;
853 }
854
855 /* Forbid repeated extensions */
Jens Wiklander32b31802023-10-06 16:59:46 +0200856 if ((crt->ext_types & ext_type) != 0) {
857 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
858 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200859
860 crt->ext_types |= ext_type;
861
Jens Wiklander32b31802023-10-06 16:59:46 +0200862 switch (ext_type) {
863 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
864 /* Parse basic constraints */
865 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
866 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
867 return ret;
868 }
869 break;
Jens Wiklander817466c2018-05-22 13:49:31 +0200870
Jens Wiklander32b31802023-10-06 16:59:46 +0200871 case MBEDTLS_X509_EXT_KEY_USAGE:
872 /* Parse key usage */
873 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
874 &crt->key_usage)) != 0) {
875 return ret;
876 }
877 break;
Jens Wiklander817466c2018-05-22 13:49:31 +0200878
Jens Wiklander32b31802023-10-06 16:59:46 +0200879 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
880 /* Parse extended key usage */
881 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
882 &crt->ext_key_usage)) != 0) {
883 return ret;
884 }
885 break;
Jens Wiklander817466c2018-05-22 13:49:31 +0200886
Jens Wiklander32b31802023-10-06 16:59:46 +0200887 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
888 /* Parse subject alt name */
889 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
890 &crt->subject_alt_names)) != 0) {
891 return ret;
892 }
893 break;
Jens Wiklander817466c2018-05-22 13:49:31 +0200894
Jens Wiklander32b31802023-10-06 16:59:46 +0200895 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
896 /* Parse netscape certificate type */
897 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
898 &crt->ns_cert_type)) != 0) {
899 return ret;
900 }
901 break;
Jens Wiklander817466c2018-05-22 13:49:31 +0200902
Jens Wiklander32b31802023-10-06 16:59:46 +0200903 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
904 /* Parse certificate policies type */
905 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
906 &crt->certificate_policies)) != 0) {
907 /* Give the callback (if any) a chance to handle the extension
908 * if it contains unsupported policies */
909 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
910 cb(p_ctx, crt, &extn_oid, is_critical,
911 start_ext_octet, end_ext_octet) == 0) {
912 break;
913 }
Jerome Forissier79013242021-07-28 10:24:04 +0200914
Jens Wiklander32b31802023-10-06 16:59:46 +0200915 if (is_critical) {
916 return ret;
917 } else
918 /*
919 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
920 * cannot interpret or enforce the policy. However, it is up to
921 * the user to choose how to enforce the policies,
922 * unless the extension is critical.
923 */
924 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
925 return ret;
926 }
927 }
928 break;
929
930 default:
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200931 /*
Jens Wiklander32b31802023-10-06 16:59:46 +0200932 * If this is a non-critical extension, which the oid layer
933 * supports, but there isn't an x509 parser for it,
934 * skip the extension.
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200935 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200936 if (is_critical) {
937 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
938 } else {
939 *p = end_ext_octet;
940 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200941 }
942 }
943
Jens Wiklander32b31802023-10-06 16:59:46 +0200944 if (*p != end) {
945 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
946 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
947 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200948
Jens Wiklander32b31802023-10-06 16:59:46 +0200949 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200950}
951
952/*
953 * Parse and fill a single X.509 certificate in DER format
954 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200955static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
956 const unsigned char *buf,
957 size_t buflen,
958 int make_copy,
959 mbedtls_x509_crt_ext_cb_t cb,
960 void *p_ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200961{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200962 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200963 size_t len;
964 unsigned char *p, *end, *crt_end;
965 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
966
Jens Wiklander32b31802023-10-06 16:59:46 +0200967 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
968 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
969 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Jens Wiklander817466c2018-05-22 13:49:31 +0200970
971 /*
972 * Check for valid input
973 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200974 if (crt == NULL || buf == NULL) {
975 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
976 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200977
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200978 /* Use the original buffer until we figure out actual length. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200979 p = (unsigned char *) buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200980 len = buflen;
981 end = p + len;
982
983 /*
984 * Certificate ::= SEQUENCE {
985 * tbsCertificate TBSCertificate,
986 * signatureAlgorithm AlgorithmIdentifier,
987 * signatureValue BIT STRING }
988 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200989 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
990 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
991 mbedtls_x509_crt_free(crt);
992 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Jens Wiklander817466c2018-05-22 13:49:31 +0200993 }
994
Jens Wiklander817466c2018-05-22 13:49:31 +0200995 end = crt_end = p + len;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200996 crt->raw.len = crt_end - buf;
Jens Wiklander32b31802023-10-06 16:59:46 +0200997 if (make_copy != 0) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200998 /* Create and populate a new buffer for the raw field. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200999 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1000 if (crt->raw.p == NULL) {
1001 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1002 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001003
Jens Wiklander32b31802023-10-06 16:59:46 +02001004 memcpy(crt->raw.p, buf, crt->raw.len);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001005 crt->own_buffer = 1;
1006
1007 p += crt->raw.len - len;
1008 end = crt_end = p + len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001009 } else {
1010 crt->raw.p = (unsigned char *) buf;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001011 crt->own_buffer = 0;
1012 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001013
1014 /*
1015 * TBSCertificate ::= SEQUENCE {
1016 */
1017 crt->tbs.p = p;
1018
Jens Wiklander32b31802023-10-06 16:59:46 +02001019 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1020 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1021 mbedtls_x509_crt_free(crt);
1022 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Jens Wiklander817466c2018-05-22 13:49:31 +02001023 }
1024
1025 end = p + len;
1026 crt->tbs.len = end - crt->tbs.p;
1027
1028 /*
1029 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1030 *
1031 * CertificateSerialNumber ::= INTEGER
1032 *
1033 * signature AlgorithmIdentifier
1034 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001035 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1036 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1037 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1038 &sig_params1)) != 0) {
1039 mbedtls_x509_crt_free(crt);
1040 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001041 }
1042
Jens Wiklander32b31802023-10-06 16:59:46 +02001043 if (crt->version < 0 || crt->version > 2) {
1044 mbedtls_x509_crt_free(crt);
1045 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Jens Wiklander817466c2018-05-22 13:49:31 +02001046 }
1047
1048 crt->version++;
1049
Jens Wiklander32b31802023-10-06 16:59:46 +02001050 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1051 &crt->sig_md, &crt->sig_pk,
1052 &crt->sig_opts)) != 0) {
1053 mbedtls_x509_crt_free(crt);
1054 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001055 }
1056
1057 /*
1058 * issuer Name
1059 */
1060 crt->issuer_raw.p = p;
1061
Jens Wiklander32b31802023-10-06 16:59:46 +02001062 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1063 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1064 mbedtls_x509_crt_free(crt);
1065 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Jens Wiklander817466c2018-05-22 13:49:31 +02001066 }
1067
Jens Wiklander32b31802023-10-06 16:59:46 +02001068 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1069 mbedtls_x509_crt_free(crt);
1070 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001071 }
1072
1073 crt->issuer_raw.len = p - crt->issuer_raw.p;
1074
1075 /*
1076 * Validity ::= SEQUENCE {
1077 * notBefore Time,
1078 * notAfter Time }
1079 *
1080 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001081 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1082 &crt->valid_to)) != 0) {
1083 mbedtls_x509_crt_free(crt);
1084 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001085 }
1086
1087 /*
1088 * subject Name
1089 */
1090 crt->subject_raw.p = p;
1091
Jens Wiklander32b31802023-10-06 16:59:46 +02001092 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1093 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1094 mbedtls_x509_crt_free(crt);
1095 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Jens Wiklander817466c2018-05-22 13:49:31 +02001096 }
1097
Jens Wiklander32b31802023-10-06 16:59:46 +02001098 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1099 mbedtls_x509_crt_free(crt);
1100 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001101 }
1102
1103 crt->subject_raw.len = p - crt->subject_raw.p;
1104
1105 /*
1106 * SubjectPublicKeyInfo
1107 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001108 crt->pk_raw.p = p;
Jens Wiklander32b31802023-10-06 16:59:46 +02001109 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1110 mbedtls_x509_crt_free(crt);
1111 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001112 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001113 crt->pk_raw.len = p - crt->pk_raw.p;
Jens Wiklander817466c2018-05-22 13:49:31 +02001114
1115 /*
1116 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1117 * -- If present, version shall be v2 or v3
1118 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1119 * -- If present, version shall be v2 or v3
1120 * extensions [3] EXPLICIT Extensions OPTIONAL
1121 * -- If present, version shall be v3
1122 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001123 if (crt->version == 2 || crt->version == 3) {
1124 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1125 if (ret != 0) {
1126 mbedtls_x509_crt_free(crt);
1127 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001128 }
1129 }
1130
Jens Wiklander32b31802023-10-06 16:59:46 +02001131 if (crt->version == 2 || crt->version == 3) {
1132 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1133 if (ret != 0) {
1134 mbedtls_x509_crt_free(crt);
1135 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001136 }
1137 }
1138
Jens Wiklander32b31802023-10-06 16:59:46 +02001139 if (crt->version == 3) {
1140 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1141 if (ret != 0) {
1142 mbedtls_x509_crt_free(crt);
1143 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001144 }
1145 }
1146
Jens Wiklander32b31802023-10-06 16:59:46 +02001147 if (p != end) {
1148 mbedtls_x509_crt_free(crt);
1149 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1150 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Wiklander817466c2018-05-22 13:49:31 +02001151 }
1152
1153 end = crt_end;
1154
1155 /*
1156 * }
1157 * -- end of TBSCertificate
1158 *
1159 * signatureAlgorithm AlgorithmIdentifier,
1160 * signatureValue BIT STRING
1161 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001162 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1163 mbedtls_x509_crt_free(crt);
1164 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001165 }
1166
Jens Wiklander32b31802023-10-06 16:59:46 +02001167 if (crt->sig_oid.len != sig_oid2.len ||
1168 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Jerome Forissier79013242021-07-28 10:24:04 +02001169 sig_params1.tag != sig_params2.tag ||
Jens Wiklander817466c2018-05-22 13:49:31 +02001170 sig_params1.len != sig_params2.len ||
Jens Wiklander32b31802023-10-06 16:59:46 +02001171 (sig_params1.len != 0 &&
1172 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1173 mbedtls_x509_crt_free(crt);
1174 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Jens Wiklander817466c2018-05-22 13:49:31 +02001175 }
1176
Jens Wiklander32b31802023-10-06 16:59:46 +02001177 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1178 mbedtls_x509_crt_free(crt);
1179 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001180 }
1181
Jens Wiklander32b31802023-10-06 16:59:46 +02001182 if (p != end) {
1183 mbedtls_x509_crt_free(crt);
1184 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1185 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Wiklander817466c2018-05-22 13:49:31 +02001186 }
1187
Jens Wiklander32b31802023-10-06 16:59:46 +02001188 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001189}
1190
1191/*
1192 * Parse one X.509 certificate in DER format from a buffer and add them to a
1193 * chained list
1194 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001195static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1196 const unsigned char *buf,
1197 size_t buflen,
1198 int make_copy,
1199 mbedtls_x509_crt_ext_cb_t cb,
1200 void *p_ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +02001201{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001202 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001203 mbedtls_x509_crt *crt = chain, *prev = NULL;
1204
1205 /*
1206 * Check for valid input
1207 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001208 if (crt == NULL || buf == NULL) {
1209 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1210 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001211
Jens Wiklander32b31802023-10-06 16:59:46 +02001212 while (crt->version != 0 && crt->next != NULL) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001213 prev = crt;
1214 crt = crt->next;
1215 }
1216
1217 /*
1218 * Add new certificate on the end of the chain if needed.
1219 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001220 if (crt->version != 0 && crt->next == NULL) {
1221 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jens Wiklander817466c2018-05-22 13:49:31 +02001222
Jens Wiklander32b31802023-10-06 16:59:46 +02001223 if (crt->next == NULL) {
1224 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1225 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001226
1227 prev = crt;
Jens Wiklander32b31802023-10-06 16:59:46 +02001228 mbedtls_x509_crt_init(crt->next);
Jens Wiklander817466c2018-05-22 13:49:31 +02001229 crt = crt->next;
1230 }
1231
Jens Wiklander32b31802023-10-06 16:59:46 +02001232 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1233 if (ret != 0) {
1234 if (prev) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001235 prev->next = NULL;
Jens Wiklander32b31802023-10-06 16:59:46 +02001236 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001237
Jens Wiklander32b31802023-10-06 16:59:46 +02001238 if (crt != chain) {
1239 mbedtls_free(crt);
1240 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001241
Jens Wiklander32b31802023-10-06 16:59:46 +02001242 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001243 }
1244
Jens Wiklander32b31802023-10-06 16:59:46 +02001245 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001246}
1247
Jens Wiklander32b31802023-10-06 16:59:46 +02001248int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1249 const unsigned char *buf,
1250 size_t buflen)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001251{
Jens Wiklander32b31802023-10-06 16:59:46 +02001252 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Jerome Forissier79013242021-07-28 10:24:04 +02001253}
1254
Jens Wiklander32b31802023-10-06 16:59:46 +02001255int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1256 const unsigned char *buf,
1257 size_t buflen,
1258 int make_copy,
1259 mbedtls_x509_crt_ext_cb_t cb,
1260 void *p_ctx)
Jerome Forissier79013242021-07-28 10:24:04 +02001261{
Jens Wiklander32b31802023-10-06 16:59:46 +02001262 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001263}
1264
Jens Wiklander32b31802023-10-06 16:59:46 +02001265int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1266 const unsigned char *buf,
1267 size_t buflen)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001268{
Jens Wiklander32b31802023-10-06 16:59:46 +02001269 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001270}
1271
Jens Wiklander817466c2018-05-22 13:49:31 +02001272/*
1273 * Parse one or more PEM certificates from a buffer and add them to the chained
1274 * list
1275 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001276int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1277 const unsigned char *buf,
1278 size_t buflen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001279{
1280#if defined(MBEDTLS_PEM_PARSE_C)
1281 int success = 0, first_error = 0, total_failed = 0;
1282 int buf_format = MBEDTLS_X509_FORMAT_DER;
1283#endif
1284
1285 /*
1286 * Check for valid input
1287 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001288 if (chain == NULL || buf == NULL) {
1289 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1290 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001291
1292 /*
1293 * Determine buffer content. Buffer contains either one DER certificate or
1294 * one or more PEM certificates.
1295 */
1296#if defined(MBEDTLS_PEM_PARSE_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001297 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1298 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001299 buf_format = MBEDTLS_X509_FORMAT_PEM;
1300 }
1301
Jens Wiklander32b31802023-10-06 16:59:46 +02001302 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1303 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1304 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001305#else
Jens Wiklander32b31802023-10-06 16:59:46 +02001306 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Jens Wiklander817466c2018-05-22 13:49:31 +02001307#endif
1308
1309#if defined(MBEDTLS_PEM_PARSE_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001310 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001311 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001312 mbedtls_pem_context pem;
1313
1314 /* 1 rather than 0 since the terminating NULL byte is counted in */
Jens Wiklander32b31802023-10-06 16:59:46 +02001315 while (buflen > 1) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001316 size_t use_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001317 mbedtls_pem_init(&pem);
Jens Wiklander817466c2018-05-22 13:49:31 +02001318
1319 /* If we get there, we know the string is null-terminated */
Jens Wiklander32b31802023-10-06 16:59:46 +02001320 ret = mbedtls_pem_read_buffer(&pem,
1321 "-----BEGIN CERTIFICATE-----",
1322 "-----END CERTIFICATE-----",
1323 buf, NULL, 0, &use_len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001324
Jens Wiklander32b31802023-10-06 16:59:46 +02001325 if (ret == 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001326 /*
1327 * Was PEM encoded
1328 */
1329 buflen -= use_len;
1330 buf += use_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001331 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1332 return ret;
1333 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1334 mbedtls_pem_free(&pem);
Jens Wiklander817466c2018-05-22 13:49:31 +02001335
1336 /*
1337 * PEM header and footer were found
1338 */
1339 buflen -= use_len;
1340 buf += use_len;
1341
Jens Wiklander32b31802023-10-06 16:59:46 +02001342 if (first_error == 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001343 first_error = ret;
Jens Wiklander32b31802023-10-06 16:59:46 +02001344 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001345
1346 total_failed++;
1347 continue;
Jens Wiklander32b31802023-10-06 16:59:46 +02001348 } else {
Jens Wiklander817466c2018-05-22 13:49:31 +02001349 break;
Jens Wiklander32b31802023-10-06 16:59:46 +02001350 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001351
Jens Wiklander32b31802023-10-06 16:59:46 +02001352 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Jens Wiklander817466c2018-05-22 13:49:31 +02001353
Jens Wiklander32b31802023-10-06 16:59:46 +02001354 mbedtls_pem_free(&pem);
Jens Wiklander817466c2018-05-22 13:49:31 +02001355
Jens Wiklander32b31802023-10-06 16:59:46 +02001356 if (ret != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001357 /*
1358 * Quit parsing on a memory error
1359 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001360 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1361 return ret;
1362 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001363
Jens Wiklander32b31802023-10-06 16:59:46 +02001364 if (first_error == 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001365 first_error = ret;
Jens Wiklander32b31802023-10-06 16:59:46 +02001366 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001367
1368 total_failed++;
1369 continue;
1370 }
1371
1372 success = 1;
1373 }
1374 }
1375
Jens Wiklander32b31802023-10-06 16:59:46 +02001376 if (success) {
1377 return total_failed;
1378 } else if (first_error) {
1379 return first_error;
1380 } else {
1381 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1382 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001383#endif /* MBEDTLS_PEM_PARSE_C */
1384}
1385
1386#if defined(MBEDTLS_FS_IO)
1387/*
1388 * Load one or more certificates and add them to the chained list
1389 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001390int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Jens Wiklander817466c2018-05-22 13:49:31 +02001391{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001393 size_t n;
1394 unsigned char *buf;
1395
Jens Wiklander32b31802023-10-06 16:59:46 +02001396 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1397 return ret;
1398 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001399
Jens Wiklander32b31802023-10-06 16:59:46 +02001400 ret = mbedtls_x509_crt_parse(chain, buf, n);
Jens Wiklander817466c2018-05-22 13:49:31 +02001401
Jens Wiklander32b31802023-10-06 16:59:46 +02001402 mbedtls_platform_zeroize(buf, n);
1403 mbedtls_free(buf);
Jens Wiklander817466c2018-05-22 13:49:31 +02001404
Jens Wiklander32b31802023-10-06 16:59:46 +02001405 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001406}
1407
Jens Wiklander32b31802023-10-06 16:59:46 +02001408int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Jens Wiklander817466c2018-05-22 13:49:31 +02001409{
1410 int ret = 0;
1411#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
1412 int w_ret;
1413 WCHAR szDir[MAX_PATH];
1414 char filename[MAX_PATH];
1415 char *p;
Jens Wiklander32b31802023-10-06 16:59:46 +02001416 size_t len = strlen(path);
Jens Wiklander817466c2018-05-22 13:49:31 +02001417
1418 WIN32_FIND_DATAW file_data;
1419 HANDLE hFind;
1420
Jens Wiklander32b31802023-10-06 16:59:46 +02001421 if (len > MAX_PATH - 3) {
1422 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1423 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001424
Jens Wiklander32b31802023-10-06 16:59:46 +02001425 memset(szDir, 0, sizeof(szDir));
1426 memset(filename, 0, MAX_PATH);
1427 memcpy(filename, path, len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001428 filename[len++] = '\\';
1429 p = filename + len;
1430 filename[len++] = '*';
1431
Jens Wiklander32b31802023-10-06 16:59:46 +02001432 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1433 MAX_PATH - 3);
1434 if (w_ret == 0) {
1435 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1436 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001437
Jens Wiklander32b31802023-10-06 16:59:46 +02001438 hFind = FindFirstFileW(szDir, &file_data);
1439 if (hFind == INVALID_HANDLE_VALUE) {
1440 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1441 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001442
1443 len = MAX_PATH - len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001444 do {
1445 memset(p, 0, len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001446
Jens Wiklander32b31802023-10-06 16:59:46 +02001447 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001448 continue;
Jens Wiklander32b31802023-10-06 16:59:46 +02001449 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001450
Jens Wiklander32b31802023-10-06 16:59:46 +02001451 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
1452 -1,
1453 p, (int) len,
1454 NULL, NULL);
1455 if (w_ret == 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001456 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1457 goto cleanup;
1458 }
1459
Jens Wiklander32b31802023-10-06 16:59:46 +02001460 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1461 if (w_ret < 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001462 ret++;
Jens Wiklander32b31802023-10-06 16:59:46 +02001463 } else {
Jens Wiklander817466c2018-05-22 13:49:31 +02001464 ret += w_ret;
Jens Wiklander32b31802023-10-06 16:59:46 +02001465 }
1466 } while (FindNextFileW(hFind, &file_data) != 0);
Jens Wiklander817466c2018-05-22 13:49:31 +02001467
Jens Wiklander32b31802023-10-06 16:59:46 +02001468 if (GetLastError() != ERROR_NO_MORE_FILES) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001469 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Jens Wiklander32b31802023-10-06 16:59:46 +02001470 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001471
1472cleanup:
Jens Wiklander32b31802023-10-06 16:59:46 +02001473 FindClose(hFind);
Jens Wiklander817466c2018-05-22 13:49:31 +02001474#else /* _WIN32 */
1475 int t_ret;
1476 int snp_ret;
1477 struct stat sb;
1478 struct dirent *entry;
1479 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Jens Wiklander32b31802023-10-06 16:59:46 +02001480 DIR *dir = opendir(path);
Jens Wiklander817466c2018-05-22 13:49:31 +02001481
Jens Wiklander32b31802023-10-06 16:59:46 +02001482 if (dir == NULL) {
1483 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1484 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001485
1486#if defined(MBEDTLS_THREADING_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001487 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1488 closedir(dir);
1489 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001490 }
1491#endif /* MBEDTLS_THREADING_C */
1492
Jens Wiklander32b31802023-10-06 16:59:46 +02001493 memset(&sb, 0, sizeof(sb));
Jerome Forissier79013242021-07-28 10:24:04 +02001494
Jens Wiklander32b31802023-10-06 16:59:46 +02001495 while ((entry = readdir(dir)) != NULL) {
1496 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
1497 "%s/%s", path, entry->d_name);
Jens Wiklander817466c2018-05-22 13:49:31 +02001498
Jens Wiklander32b31802023-10-06 16:59:46 +02001499 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001500 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1501 goto cleanup;
Jens Wiklander32b31802023-10-06 16:59:46 +02001502 } else if (stat(entry_name, &sb) == -1) {
1503 if (errno == ENOENT) {
1504 /* Broken symbolic link - ignore this entry.
1505 stat(2) will return this error for either (a) a dangling
1506 symlink or (b) a missing file.
1507 Given that we have just obtained the filename from readdir,
1508 assume that it does exist and therefore treat this as a
1509 dangling symlink. */
1510 continue;
1511 } else {
1512 /* Some other file error; report the error. */
1513 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1514 goto cleanup;
1515 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001516 }
1517
Jens Wiklander32b31802023-10-06 16:59:46 +02001518 if (!S_ISREG(sb.st_mode)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001519 continue;
Jens Wiklander32b31802023-10-06 16:59:46 +02001520 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001521
1522 // Ignore parse errors
1523 //
Jens Wiklander32b31802023-10-06 16:59:46 +02001524 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1525 if (t_ret < 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001526 ret++;
Jens Wiklander32b31802023-10-06 16:59:46 +02001527 } else {
Jens Wiklander817466c2018-05-22 13:49:31 +02001528 ret += t_ret;
Jens Wiklander32b31802023-10-06 16:59:46 +02001529 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001530 }
1531
1532cleanup:
Jens Wiklander32b31802023-10-06 16:59:46 +02001533 closedir(dir);
Jens Wiklander817466c2018-05-22 13:49:31 +02001534
1535#if defined(MBEDTLS_THREADING_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001536 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001537 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Jens Wiklander32b31802023-10-06 16:59:46 +02001538 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001539#endif /* MBEDTLS_THREADING_C */
1540
1541#endif /* _WIN32 */
1542
Jens Wiklander32b31802023-10-06 16:59:46 +02001543 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001544}
1545#endif /* MBEDTLS_FS_IO */
1546
Jens Wiklander32b31802023-10-06 16:59:46 +02001547#if !defined(MBEDTLS_X509_REMOVE_INFO)
1548static int x509_info_ext_key_usage(char **buf, size_t *size,
1549 const mbedtls_x509_sequence *extended_key_usage)
Jens Wiklander817466c2018-05-22 13:49:31 +02001550{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001552 const char *desc;
1553 size_t n = *size;
1554 char *p = *buf;
1555 const mbedtls_x509_sequence *cur = extended_key_usage;
1556 const char *sep = "";
1557
Jens Wiklander32b31802023-10-06 16:59:46 +02001558 while (cur != NULL) {
1559 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001560 desc = "???";
Jens Wiklander32b31802023-10-06 16:59:46 +02001561 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001562
Jens Wiklander32b31802023-10-06 16:59:46 +02001563 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Jens Wiklander817466c2018-05-22 13:49:31 +02001564 MBEDTLS_X509_SAFE_SNPRINTF;
1565
1566 sep = ", ";
1567
1568 cur = cur->next;
1569 }
1570
1571 *size = n;
1572 *buf = p;
1573
Jens Wiklander32b31802023-10-06 16:59:46 +02001574 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001575}
1576
Jens Wiklander32b31802023-10-06 16:59:46 +02001577static int x509_info_cert_policies(char **buf, size_t *size,
1578 const mbedtls_x509_sequence *certificate_policies)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001579{
1580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1581 const char *desc;
1582 size_t n = *size;
1583 char *p = *buf;
1584 const mbedtls_x509_sequence *cur = certificate_policies;
1585 const char *sep = "";
1586
Jens Wiklander32b31802023-10-06 16:59:46 +02001587 while (cur != NULL) {
1588 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001589 desc = "???";
Jens Wiklander32b31802023-10-06 16:59:46 +02001590 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001591
Jens Wiklander32b31802023-10-06 16:59:46 +02001592 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001593 MBEDTLS_X509_SAFE_SNPRINTF;
1594
1595 sep = ", ";
1596
1597 cur = cur->next;
1598 }
1599
1600 *size = n;
1601 *buf = p;
1602
Jens Wiklander32b31802023-10-06 16:59:46 +02001603 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001604}
1605
Jens Wiklander817466c2018-05-22 13:49:31 +02001606/*
1607 * Return an informational string about the certificate.
1608 */
1609#define BEFORE_COLON 18
1610#define BC "18"
Jens Wiklander32b31802023-10-06 16:59:46 +02001611int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1612 const mbedtls_x509_crt *crt)
Jens Wiklander817466c2018-05-22 13:49:31 +02001613{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001614 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001615 size_t n;
1616 char *p;
1617 char key_size_str[BEFORE_COLON];
1618
1619 p = buf;
1620 n = size;
1621
Jens Wiklander32b31802023-10-06 16:59:46 +02001622 if (NULL == crt) {
1623 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Jens Wiklander817466c2018-05-22 13:49:31 +02001624 MBEDTLS_X509_SAFE_SNPRINTF;
1625
Jens Wiklander32b31802023-10-06 16:59:46 +02001626 return (int) (size - n);
Jens Wiklander817466c2018-05-22 13:49:31 +02001627 }
1628
Jens Wiklander32b31802023-10-06 16:59:46 +02001629 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1630 prefix, crt->version);
Jens Wiklander817466c2018-05-22 13:49:31 +02001631 MBEDTLS_X509_SAFE_SNPRINTF;
Jens Wiklander32b31802023-10-06 16:59:46 +02001632 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1633 prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001634 MBEDTLS_X509_SAFE_SNPRINTF;
1635
Jens Wiklander32b31802023-10-06 16:59:46 +02001636 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Jens Wiklander817466c2018-05-22 13:49:31 +02001637 MBEDTLS_X509_SAFE_SNPRINTF;
1638
Jens Wiklander32b31802023-10-06 16:59:46 +02001639 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001640 MBEDTLS_X509_SAFE_SNPRINTF;
Jens Wiklander32b31802023-10-06 16:59:46 +02001641 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Jens Wiklander817466c2018-05-22 13:49:31 +02001642 MBEDTLS_X509_SAFE_SNPRINTF;
1643
Jens Wiklander32b31802023-10-06 16:59:46 +02001644 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001645 MBEDTLS_X509_SAFE_SNPRINTF;
Jens Wiklander32b31802023-10-06 16:59:46 +02001646 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Jens Wiklander817466c2018-05-22 13:49:31 +02001647 MBEDTLS_X509_SAFE_SNPRINTF;
1648
Jens Wiklander32b31802023-10-06 16:59:46 +02001649 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1650 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1651 crt->valid_from.year, crt->valid_from.mon,
1652 crt->valid_from.day, crt->valid_from.hour,
1653 crt->valid_from.min, crt->valid_from.sec);
Jens Wiklander817466c2018-05-22 13:49:31 +02001654 MBEDTLS_X509_SAFE_SNPRINTF;
1655
Jens Wiklander32b31802023-10-06 16:59:46 +02001656 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1657 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1658 crt->valid_to.year, crt->valid_to.mon,
1659 crt->valid_to.day, crt->valid_to.hour,
1660 crt->valid_to.min, crt->valid_to.sec);
Jens Wiklander817466c2018-05-22 13:49:31 +02001661 MBEDTLS_X509_SAFE_SNPRINTF;
1662
Jens Wiklander32b31802023-10-06 16:59:46 +02001663 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001664 MBEDTLS_X509_SAFE_SNPRINTF;
1665
Jens Wiklander32b31802023-10-06 16:59:46 +02001666 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1667 crt->sig_md, crt->sig_opts);
Jens Wiklander817466c2018-05-22 13:49:31 +02001668 MBEDTLS_X509_SAFE_SNPRINTF;
1669
1670 /* Key size */
Jens Wiklander32b31802023-10-06 16:59:46 +02001671 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1672 mbedtls_pk_get_name(&crt->pk))) != 0) {
1673 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001674 }
1675
Jens Wiklander32b31802023-10-06 16:59:46 +02001676 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1677 (int) mbedtls_pk_get_bitlen(&crt->pk));
Jens Wiklander817466c2018-05-22 13:49:31 +02001678 MBEDTLS_X509_SAFE_SNPRINTF;
1679
1680 /*
1681 * Optional extensions
1682 */
1683
Jens Wiklander32b31802023-10-06 16:59:46 +02001684 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1685 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1686 crt->ca_istrue ? "true" : "false");
Jens Wiklander817466c2018-05-22 13:49:31 +02001687 MBEDTLS_X509_SAFE_SNPRINTF;
1688
Jens Wiklander32b31802023-10-06 16:59:46 +02001689 if (crt->max_pathlen > 0) {
1690 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Jens Wiklander817466c2018-05-22 13:49:31 +02001691 MBEDTLS_X509_SAFE_SNPRINTF;
1692 }
1693 }
1694
Jens Wiklander32b31802023-10-06 16:59:46 +02001695 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1696 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001697 MBEDTLS_X509_SAFE_SNPRINTF;
1698
Jens Wiklander32b31802023-10-06 16:59:46 +02001699 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1700 &crt->subject_alt_names,
1701 prefix)) != 0) {
1702 return ret;
1703 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001704 }
1705
Jens Wiklander32b31802023-10-06 16:59:46 +02001706 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1707 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001708 MBEDTLS_X509_SAFE_SNPRINTF;
1709
Jens Wiklander32b31802023-10-06 16:59:46 +02001710 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
1711 return ret;
1712 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001713 }
1714
Jens Wiklander32b31802023-10-06 16:59:46 +02001715 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1716 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001717 MBEDTLS_X509_SAFE_SNPRINTF;
1718
Jens Wiklander32b31802023-10-06 16:59:46 +02001719 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
1720 return ret;
1721 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001722 }
1723
Jens Wiklander32b31802023-10-06 16:59:46 +02001724 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1725 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001726 MBEDTLS_X509_SAFE_SNPRINTF;
1727
Jens Wiklander32b31802023-10-06 16:59:46 +02001728 if ((ret = x509_info_ext_key_usage(&p, &n,
1729 &crt->ext_key_usage)) != 0) {
1730 return ret;
1731 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001732 }
1733
Jens Wiklander32b31802023-10-06 16:59:46 +02001734 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1735 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001736 MBEDTLS_X509_SAFE_SNPRINTF;
1737
Jens Wiklander32b31802023-10-06 16:59:46 +02001738 if ((ret = x509_info_cert_policies(&p, &n,
1739 &crt->certificate_policies)) != 0) {
1740 return ret;
1741 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001742 }
1743
Jens Wiklander32b31802023-10-06 16:59:46 +02001744 ret = mbedtls_snprintf(p, n, "\n");
Jens Wiklander817466c2018-05-22 13:49:31 +02001745 MBEDTLS_X509_SAFE_SNPRINTF;
1746
Jens Wiklander32b31802023-10-06 16:59:46 +02001747 return (int) (size - n);
Jens Wiklander817466c2018-05-22 13:49:31 +02001748}
1749
1750struct x509_crt_verify_string {
1751 int code;
1752 const char *string;
1753};
1754
Jens Wiklander32b31802023-10-06 16:59:46 +02001755#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Jens Wiklander817466c2018-05-22 13:49:31 +02001756static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Jens Wiklander32b31802023-10-06 16:59:46 +02001757 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Jens Wiklander817466c2018-05-22 13:49:31 +02001758 { 0, NULL }
1759};
Jens Wiklander32b31802023-10-06 16:59:46 +02001760#undef X509_CRT_ERROR_INFO
Jens Wiklander817466c2018-05-22 13:49:31 +02001761
Jens Wiklander32b31802023-10-06 16:59:46 +02001762int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1763 uint32_t flags)
Jens Wiklander817466c2018-05-22 13:49:31 +02001764{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001765 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001766 const struct x509_crt_verify_string *cur;
1767 char *p = buf;
1768 size_t n = size;
1769
Jens Wiklander32b31802023-10-06 16:59:46 +02001770 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1771 if ((flags & cur->code) == 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001772 continue;
Jens Wiklander32b31802023-10-06 16:59:46 +02001773 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001774
Jens Wiklander32b31802023-10-06 16:59:46 +02001775 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Jens Wiklander817466c2018-05-22 13:49:31 +02001776 MBEDTLS_X509_SAFE_SNPRINTF;
1777 flags ^= cur->code;
1778 }
1779
Jens Wiklander32b31802023-10-06 16:59:46 +02001780 if (flags != 0) {
1781 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1782 "(this should not happen)\n", prefix);
Jens Wiklander817466c2018-05-22 13:49:31 +02001783 MBEDTLS_X509_SAFE_SNPRINTF;
1784 }
1785
Jens Wiklander32b31802023-10-06 16:59:46 +02001786 return (int) (size - n);
Jens Wiklander817466c2018-05-22 13:49:31 +02001787}
Jens Wiklander32b31802023-10-06 16:59:46 +02001788#endif /* MBEDTLS_X509_REMOVE_INFO */
Jens Wiklander817466c2018-05-22 13:49:31 +02001789
Jens Wiklander32b31802023-10-06 16:59:46 +02001790int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1791 unsigned int usage)
Jens Wiklander817466c2018-05-22 13:49:31 +02001792{
1793 unsigned int usage_must, usage_may;
1794 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Jens Wiklander32b31802023-10-06 16:59:46 +02001795 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Jens Wiklander817466c2018-05-22 13:49:31 +02001796
Jens Wiklander32b31802023-10-06 16:59:46 +02001797 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1798 return 0;
1799 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001800
1801 usage_must = usage & ~may_mask;
1802
Jens Wiklander32b31802023-10-06 16:59:46 +02001803 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1804 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1805 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001806
1807 usage_may = usage & may_mask;
1808
Jens Wiklander32b31802023-10-06 16:59:46 +02001809 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1810 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1811 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001812
Jens Wiklander32b31802023-10-06 16:59:46 +02001813 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001814}
Jens Wiklander817466c2018-05-22 13:49:31 +02001815
Jens Wiklander32b31802023-10-06 16:59:46 +02001816int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1817 const char *usage_oid,
1818 size_t usage_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001819{
1820 const mbedtls_x509_sequence *cur;
1821
1822 /* Extension is not mandatory, absent means no restriction */
Jens Wiklander32b31802023-10-06 16:59:46 +02001823 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1824 return 0;
1825 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001826
1827 /*
1828 * Look for the requested usage (or wildcard ANY) in our list
1829 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001830 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001831 const mbedtls_x509_buf *cur_oid = &cur->buf;
1832
Jens Wiklander32b31802023-10-06 16:59:46 +02001833 if (cur_oid->len == usage_len &&
1834 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1835 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001836 }
1837
Jens Wiklander32b31802023-10-06 16:59:46 +02001838 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1839 return 0;
1840 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001841 }
1842
Jens Wiklander32b31802023-10-06 16:59:46 +02001843 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +02001844}
Jens Wiklander817466c2018-05-22 13:49:31 +02001845
1846#if defined(MBEDTLS_X509_CRL_PARSE_C)
1847/*
1848 * Return 1 if the certificate is revoked, or 0 otherwise.
1849 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001850int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Jens Wiklander817466c2018-05-22 13:49:31 +02001851{
1852 const mbedtls_x509_crl_entry *cur = &crl->entry;
1853
Jens Wiklander32b31802023-10-06 16:59:46 +02001854 while (cur != NULL && cur->serial.len != 0) {
1855 if (crt->serial.len == cur->serial.len &&
1856 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
1857 return 1;
Jens Wiklander817466c2018-05-22 13:49:31 +02001858 }
1859
1860 cur = cur->next;
1861 }
1862
Jens Wiklander32b31802023-10-06 16:59:46 +02001863 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001864}
1865
1866/*
1867 * Check that the given certificate is not revoked according to the CRL.
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001868 * Skip validation if no CRL for the given CA is present.
Jens Wiklander817466c2018-05-22 13:49:31 +02001869 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001870static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
1871 mbedtls_x509_crl *crl_list,
1872 const mbedtls_x509_crt_profile *profile)
Jens Wiklander817466c2018-05-22 13:49:31 +02001873{
1874 int flags = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +02001875 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
1876#if defined(MBEDTLS_USE_PSA_CRYPTO)
1877 psa_algorithm_t psa_algorithm;
1878#else
Jens Wiklander817466c2018-05-22 13:49:31 +02001879 const mbedtls_md_info_t *md_info;
Jens Wiklander32b31802023-10-06 16:59:46 +02001880#endif /* MBEDTLS_USE_PSA_CRYPTO */
1881 size_t hash_length;
Jens Wiklander817466c2018-05-22 13:49:31 +02001882
Jens Wiklander32b31802023-10-06 16:59:46 +02001883 if (ca == NULL) {
1884 return flags;
1885 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001886
Jens Wiklander32b31802023-10-06 16:59:46 +02001887 while (crl_list != NULL) {
1888 if (crl_list->version == 0 ||
1889 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001890 crl_list = crl_list->next;
1891 continue;
1892 }
1893
1894 /*
1895 * Check if the CA is configured to sign CRLs
1896 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001897 if (mbedtls_x509_crt_check_key_usage(ca,
1898 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001899 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
1900 break;
1901 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001902
1903 /*
1904 * Check if CRL is correctly signed by the trusted CA
1905 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001906 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001907 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Jens Wiklander32b31802023-10-06 16:59:46 +02001908 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001909
Jens Wiklander32b31802023-10-06 16:59:46 +02001910 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001911 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Jens Wiklander32b31802023-10-06 16:59:46 +02001912 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001913
Jens Wiklander32b31802023-10-06 16:59:46 +02001914#if defined(MBEDTLS_USE_PSA_CRYPTO)
1915 psa_algorithm = mbedtls_hash_info_psa_from_md(crl_list->sig_md);
1916 if (psa_hash_compute(psa_algorithm,
1917 crl_list->tbs.p,
1918 crl_list->tbs.len,
1919 hash,
1920 sizeof(hash),
1921 &hash_length) != PSA_SUCCESS) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001922 /* Note: this can't happen except after an internal error */
Jens Wiklander817466c2018-05-22 13:49:31 +02001923 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
1924 break;
1925 }
Jens Wiklander32b31802023-10-06 16:59:46 +02001926#else
1927 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
1928 hash_length = mbedtls_md_get_size(md_info);
1929 if (mbedtls_md(md_info,
1930 crl_list->tbs.p,
1931 crl_list->tbs.len,
1932 hash) != 0) {
1933 /* Note: this can't happen except after an internal error */
1934 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
1935 break;
1936 }
1937#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jens Wiklander817466c2018-05-22 13:49:31 +02001938
Jens Wiklander32b31802023-10-06 16:59:46 +02001939 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001940 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Jens Wiklander32b31802023-10-06 16:59:46 +02001941 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001942
Jens Wiklander32b31802023-10-06 16:59:46 +02001943 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1944 crl_list->sig_md, hash, hash_length,
1945 crl_list->sig.p, crl_list->sig.len) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001946 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
1947 break;
1948 }
1949
1950 /*
1951 * Check for validity of CRL (Do not drop out)
1952 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001953 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001954 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Jens Wiklander32b31802023-10-06 16:59:46 +02001955 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001956
Jens Wiklander32b31802023-10-06 16:59:46 +02001957 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001958 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Jens Wiklander32b31802023-10-06 16:59:46 +02001959 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001960
1961 /*
1962 * Check if certificate is revoked
1963 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001964 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001965 flags |= MBEDTLS_X509_BADCERT_REVOKED;
1966 break;
1967 }
1968
1969 crl_list = crl_list->next;
1970 }
1971
Jens Wiklander32b31802023-10-06 16:59:46 +02001972 return flags;
Jens Wiklander817466c2018-05-22 13:49:31 +02001973}
1974#endif /* MBEDTLS_X509_CRL_PARSE_C */
1975
1976/*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001977 * Check the signature of a certificate by its parent
Jens Wiklander817466c2018-05-22 13:49:31 +02001978 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001979static int x509_crt_check_signature(const mbedtls_x509_crt *child,
1980 mbedtls_x509_crt *parent,
1981 mbedtls_x509_crt_restart_ctx *rs_ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +02001982{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001983 size_t hash_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001984 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001985#if !defined(MBEDTLS_USE_PSA_CRYPTO)
1986 const mbedtls_md_info_t *md_info;
Jens Wiklander32b31802023-10-06 16:59:46 +02001987 md_info = mbedtls_md_info_from_type(child->sig_md);
1988 hash_len = mbedtls_md_get_size(md_info);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001989
1990 /* Note: hash errors can happen only after an internal error */
Jens Wiklander32b31802023-10-06 16:59:46 +02001991 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
1992 return -1;
1993 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001994#else
Jens Wiklander32b31802023-10-06 16:59:46 +02001995 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(child->sig_md);
1996 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001997
Jens Wiklander32b31802023-10-06 16:59:46 +02001998 status = psa_hash_compute(hash_alg,
1999 child->tbs.p,
2000 child->tbs.len,
2001 hash,
2002 sizeof(hash),
2003 &hash_len);
2004 if (status != PSA_SUCCESS) {
2005 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jens Wiklander817466c2018-05-22 13:49:31 +02002006 }
2007
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002008#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002009 /* Skip expensive computation on obvious mismatch */
Jens Wiklander32b31802023-10-06 16:59:46 +02002010 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2011 return -1;
2012 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002013
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002014#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Jens Wiklander32b31802023-10-06 16:59:46 +02002015 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2016 return mbedtls_pk_verify_restartable(&parent->pk,
2017 child->sig_md, hash, hash_len,
2018 child->sig.p, child->sig.len, &rs_ctx->pk);
Jens Wiklander817466c2018-05-22 13:49:31 +02002019 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002020#else
2021 (void) rs_ctx;
2022#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02002023
Jens Wiklander32b31802023-10-06 16:59:46 +02002024 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2025 child->sig_md, hash, hash_len,
2026 child->sig.p, child->sig.len);
Jens Wiklander817466c2018-05-22 13:49:31 +02002027}
2028
2029/*
2030 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2031 * Return 0 if yes, -1 if not.
2032 *
2033 * top means parent is a locally-trusted certificate
Jens Wiklander817466c2018-05-22 13:49:31 +02002034 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002035static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2036 const mbedtls_x509_crt *parent,
2037 int top)
Jens Wiklander817466c2018-05-22 13:49:31 +02002038{
2039 int need_ca_bit;
2040
2041 /* Parent must be the issuer */
Jens Wiklander32b31802023-10-06 16:59:46 +02002042 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2043 return -1;
2044 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002045
2046 /* Parent must have the basicConstraints CA bit set as a general rule */
2047 need_ca_bit = 1;
2048
2049 /* Exception: v1/v2 certificates that are locally trusted. */
Jens Wiklander32b31802023-10-06 16:59:46 +02002050 if (top && parent->version < 3) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002051 need_ca_bit = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02002052 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002053
Jens Wiklander32b31802023-10-06 16:59:46 +02002054 if (need_ca_bit && !parent->ca_istrue) {
2055 return -1;
2056 }
2057
2058 if (need_ca_bit &&
2059 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2060 return -1;
2061 }
2062
2063 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02002064}
2065
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002066/*
2067 * Find a suitable parent for child in candidates, or return NULL.
2068 *
2069 * Here suitable is defined as:
2070 * 1. subject name matches child's issuer
2071 * 2. if necessary, the CA bit is set and key usage allows signing certs
2072 * 3. for trusted roots, the signature is correct
2073 * (for intermediates, the signature is checked and the result reported)
2074 * 4. pathlen constraints are satisfied
2075 *
2076 * If there's a suitable candidate which is also time-valid, return the first
2077 * such. Otherwise, return the first suitable candidate (or NULL if there is
2078 * none).
2079 *
2080 * The rationale for this rule is that someone could have a list of trusted
2081 * roots with two versions on the same root with different validity periods.
2082 * (At least one user reported having such a list and wanted it to just work.)
2083 * The reason we don't just require time-validity is that generally there is
2084 * only one version, and if it's expired we want the flags to state that
2085 * rather than NOT_TRUSTED, as would be the case if we required it here.
2086 *
2087 * The rationale for rule 3 (signature for trusted roots) is that users might
2088 * have two versions of the same CA with different keys in their list, and the
2089 * way we select the correct one is by checking the signature (as we don't
2090 * rely on key identifier extensions). (This is one way users might choose to
2091 * handle key rollover, another relies on self-issued certs, see [SIRO].)
2092 *
2093 * Arguments:
2094 * - [in] child: certificate for which we're looking for a parent
2095 * - [in] candidates: chained list of potential parents
2096 * - [out] r_parent: parent found (or NULL)
2097 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
2098 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2099 * of the chain, 0 otherwise
2100 * - [in] path_cnt: number of intermediates seen so far
2101 * - [in] self_cnt: number of self-signed intermediates seen so far
2102 * (will never be greater than path_cnt)
2103 * - [in-out] rs_ctx: context for restarting operations
2104 *
2105 * Return value:
2106 * - 0 on success
2107 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
2108 */
2109static int x509_crt_find_parent_in(
Jens Wiklander32b31802023-10-06 16:59:46 +02002110 mbedtls_x509_crt *child,
2111 mbedtls_x509_crt *candidates,
2112 mbedtls_x509_crt **r_parent,
2113 int *r_signature_is_good,
2114 int top,
2115 unsigned path_cnt,
2116 unsigned self_cnt,
2117 mbedtls_x509_crt_restart_ctx *rs_ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +02002118{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002119 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002120 mbedtls_x509_crt *parent, *fallback_parent;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002121 int signature_is_good = 0, fallback_signature_is_good;
Jens Wiklander817466c2018-05-22 13:49:31 +02002122
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002123#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2124 /* did we have something in progress? */
Jens Wiklander32b31802023-10-06 16:59:46 +02002125 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002126 /* restore saved state */
2127 parent = rs_ctx->parent;
2128 fallback_parent = rs_ctx->fallback_parent;
2129 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
2130
2131 /* clear saved state */
2132 rs_ctx->parent = NULL;
2133 rs_ctx->fallback_parent = NULL;
2134 rs_ctx->fallback_signature_is_good = 0;
2135
2136 /* resume where we left */
2137 goto check_signature;
Jens Wiklander817466c2018-05-22 13:49:31 +02002138 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002139#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02002140
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002141 fallback_parent = NULL;
2142 fallback_signature_is_good = 0;
2143
Jens Wiklander32b31802023-10-06 16:59:46 +02002144 for (parent = candidates; parent != NULL; parent = parent->next) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002145 /* basic parenting skills (name, CA bit, key usage) */
Jens Wiklander32b31802023-10-06 16:59:46 +02002146 if (x509_crt_check_parent(child, parent, top) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002147 continue;
Jens Wiklander32b31802023-10-06 16:59:46 +02002148 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002149
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002150 /* +1 because stored max_pathlen is 1 higher that the actual value */
Jens Wiklander32b31802023-10-06 16:59:46 +02002151 if (parent->max_pathlen > 0 &&
2152 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002153 continue;
2154 }
2155
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002156 /* Signature */
2157#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2158check_signature:
2159#endif
Jens Wiklander32b31802023-10-06 16:59:46 +02002160 ret = x509_crt_check_signature(child, parent, rs_ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +02002161
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002162#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Jens Wiklander32b31802023-10-06 16:59:46 +02002163 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002164 /* save state */
2165 rs_ctx->parent = parent;
2166 rs_ctx->fallback_parent = fallback_parent;
2167 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
2168
Jens Wiklander32b31802023-10-06 16:59:46 +02002169 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002170 }
2171#else
2172 (void) ret;
2173#endif
2174
2175 signature_is_good = ret == 0;
Jens Wiklander32b31802023-10-06 16:59:46 +02002176 if (top && !signature_is_good) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002177 continue;
Jens Wiklander32b31802023-10-06 16:59:46 +02002178 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002179
2180 /* optional time check */
Jens Wiklander32b31802023-10-06 16:59:46 +02002181 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2182 mbedtls_x509_time_is_future(&parent->valid_from)) {
2183 if (fallback_parent == NULL) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002184 fallback_parent = parent;
2185 fallback_signature_is_good = signature_is_good;
2186 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002187
2188 continue;
2189 }
2190
Jerome Forissier5b25c762020-04-07 11:18:49 +02002191 *r_parent = parent;
2192 *r_signature_is_good = signature_is_good;
2193
Jens Wiklander817466c2018-05-22 13:49:31 +02002194 break;
2195 }
2196
Jens Wiklander32b31802023-10-06 16:59:46 +02002197 if (parent == NULL) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002198 *r_parent = fallback_parent;
2199 *r_signature_is_good = fallback_signature_is_good;
Jens Wiklander817466c2018-05-22 13:49:31 +02002200 }
2201
Jens Wiklander32b31802023-10-06 16:59:46 +02002202 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02002203}
2204
2205/*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002206 * Find a parent in trusted CAs or the provided chain, or return NULL.
2207 *
2208 * Searches in trusted CAs first, and return the first suitable parent found
2209 * (see find_parent_in() for definition of suitable).
2210 *
2211 * Arguments:
2212 * - [in] child: certificate for which we're looking for a parent, followed
2213 * by a chain of possible intermediates
2214 * - [in] trust_ca: list of locally trusted certificates
2215 * - [out] parent: parent found (or NULL)
2216 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2217 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2218 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2219 * - [in] self_cnt: number of self-signed certs in the chain so far
2220 * (will always be no greater than path_cnt)
2221 * - [in-out] rs_ctx: context for restarting operations
2222 *
2223 * Return value:
2224 * - 0 on success
2225 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
2226 */
2227static int x509_crt_find_parent(
Jens Wiklander32b31802023-10-06 16:59:46 +02002228 mbedtls_x509_crt *child,
2229 mbedtls_x509_crt *trust_ca,
2230 mbedtls_x509_crt **parent,
2231 int *parent_is_trusted,
2232 int *signature_is_good,
2233 unsigned path_cnt,
2234 unsigned self_cnt,
2235 mbedtls_x509_crt_restart_ctx *rs_ctx)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002236{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002238 mbedtls_x509_crt *search_list;
2239
2240 *parent_is_trusted = 1;
2241
2242#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2243 /* restore then clear saved state if we have some stored */
Jens Wiklander32b31802023-10-06 16:59:46 +02002244 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002245 *parent_is_trusted = rs_ctx->parent_is_trusted;
2246 rs_ctx->parent_is_trusted = -1;
2247 }
2248#endif
2249
Jens Wiklander32b31802023-10-06 16:59:46 +02002250 while (1) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002251 search_list = *parent_is_trusted ? trust_ca : child->next;
2252
Jens Wiklander32b31802023-10-06 16:59:46 +02002253 ret = x509_crt_find_parent_in(child, search_list,
2254 parent, signature_is_good,
2255 *parent_is_trusted,
2256 path_cnt, self_cnt, rs_ctx);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002257
2258#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Jens Wiklander32b31802023-10-06 16:59:46 +02002259 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002260 /* save state */
2261 rs_ctx->parent_is_trusted = *parent_is_trusted;
Jens Wiklander32b31802023-10-06 16:59:46 +02002262 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002263 }
2264#else
2265 (void) ret;
2266#endif
2267
2268 /* stop here if found or already in second iteration */
Jens Wiklander32b31802023-10-06 16:59:46 +02002269 if (*parent != NULL || *parent_is_trusted == 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002270 break;
Jens Wiklander32b31802023-10-06 16:59:46 +02002271 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002272
2273 /* prepare second iteration */
2274 *parent_is_trusted = 0;
2275 }
2276
2277 /* extra precaution against mistakes in the caller */
Jens Wiklander32b31802023-10-06 16:59:46 +02002278 if (*parent == NULL) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002279 *parent_is_trusted = 0;
2280 *signature_is_good = 0;
2281 }
2282
Jens Wiklander32b31802023-10-06 16:59:46 +02002283 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002284}
2285
2286/*
2287 * Check if an end-entity certificate is locally trusted
2288 *
2289 * Currently we require such certificates to be self-signed (actually only
2290 * check for self-issued as self-signatures are not checked)
2291 */
2292static int x509_crt_check_ee_locally_trusted(
Jens Wiklander32b31802023-10-06 16:59:46 +02002293 mbedtls_x509_crt *crt,
2294 mbedtls_x509_crt *trust_ca)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002295{
2296 mbedtls_x509_crt *cur;
2297
2298 /* must be self-issued */
Jens Wiklander32b31802023-10-06 16:59:46 +02002299 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2300 return -1;
2301 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002302
2303 /* look for an exact match with trusted cert */
Jens Wiklander32b31802023-10-06 16:59:46 +02002304 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2305 if (crt->raw.len == cur->raw.len &&
2306 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2307 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002308 }
2309 }
2310
2311 /* too bad */
Jens Wiklander32b31802023-10-06 16:59:46 +02002312 return -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002313}
2314
2315/*
2316 * Build and verify a certificate chain
2317 *
2318 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2319 * a list of trusted certs R1, ... Rp, try to build and verify a chain
2320 * EE, Ci1, ... Ciq [, Rj]
2321 * such that every cert in the chain is a child of the next one,
2322 * jumping to a trusted root as early as possible.
2323 *
2324 * Verify that chain and return it with flags for all issues found.
2325 *
2326 * Special cases:
2327 * - EE == Rj -> return a one-element list containing it
2328 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2329 * -> return that chain with NOT_TRUSTED set on Ciq
2330 *
2331 * Tests for (aspects of) this function should include at least:
2332 * - trusted EE
2333 * - EE -> trusted root
Jerome Forissier5b25c762020-04-07 11:18:49 +02002334 * - EE -> intermediate CA -> trusted root
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002335 * - if relevant: EE untrusted
2336 * - if relevant: EE -> intermediate, untrusted
2337 * with the aspect under test checked at each relevant level (EE, int, root).
2338 * For some aspects longer chains are required, but usually length 2 is
2339 * enough (but length 1 is not in general).
2340 *
2341 * Arguments:
2342 * - [in] crt: the cert list EE, C1, ..., Cn
2343 * - [in] trust_ca: the trusted list R1, ..., Rp
2344 * - [in] ca_crl, profile: as in verify_with_profile()
2345 * - [out] ver_chain: the built and verified chain
2346 * Only valid when return value is 0, may contain garbage otherwise!
2347 * Restart note: need not be the same when calling again to resume.
2348 * - [in-out] rs_ctx: context for restarting operations
2349 *
2350 * Return value:
2351 * - non-zero if the chain could not be fully built and examined
2352 * - 0 is the chain was successfully built and examined,
2353 * even if it was found to be invalid
2354 */
2355static int x509_crt_verify_chain(
Jens Wiklander32b31802023-10-06 16:59:46 +02002356 mbedtls_x509_crt *crt,
2357 mbedtls_x509_crt *trust_ca,
2358 mbedtls_x509_crl *ca_crl,
2359 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2360 void *p_ca_cb,
2361 const mbedtls_x509_crt_profile *profile,
2362 mbedtls_x509_crt_verify_chain *ver_chain,
2363 mbedtls_x509_crt_restart_ctx *rs_ctx)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002364{
2365 /* Don't initialize any of those variables here, so that the compiler can
2366 * catch potential issues with jumping ahead when restarting */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002367 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002368 uint32_t *flags;
2369 mbedtls_x509_crt_verify_chain_item *cur;
2370 mbedtls_x509_crt *child;
2371 mbedtls_x509_crt *parent;
2372 int parent_is_trusted;
2373 int child_is_trusted;
2374 int signature_is_good;
2375 unsigned self_cnt;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002376 mbedtls_x509_crt *cur_trust_ca = NULL;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002377
2378#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2379 /* resume if we had an operation in progress */
Jens Wiklander32b31802023-10-06 16:59:46 +02002380 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002381 /* restore saved state */
2382 *ver_chain = rs_ctx->ver_chain; /* struct copy */
2383 self_cnt = rs_ctx->self_cnt;
2384
2385 /* restore derived state */
2386 cur = &ver_chain->items[ver_chain->len - 1];
2387 child = cur->crt;
2388 flags = &cur->flags;
2389
2390 goto find_parent;
2391 }
2392#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
2393
2394 child = crt;
2395 self_cnt = 0;
2396 parent_is_trusted = 0;
2397 child_is_trusted = 0;
2398
Jens Wiklander32b31802023-10-06 16:59:46 +02002399 while (1) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002400 /* Add certificate to the verification chain */
2401 cur = &ver_chain->items[ver_chain->len];
2402 cur->crt = child;
2403 cur->flags = 0;
2404 ver_chain->len++;
2405 flags = &cur->flags;
2406
2407 /* Check time-validity (all certificates) */
Jens Wiklander32b31802023-10-06 16:59:46 +02002408 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002409 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Jens Wiklander32b31802023-10-06 16:59:46 +02002410 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002411
Jens Wiklander32b31802023-10-06 16:59:46 +02002412 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002413 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Jens Wiklander32b31802023-10-06 16:59:46 +02002414 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002415
2416 /* Stop here for trusted roots (but not for trusted EE certs) */
Jens Wiklander32b31802023-10-06 16:59:46 +02002417 if (child_is_trusted) {
2418 return 0;
2419 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002420
2421 /* Check signature algorithm: MD & PK algs */
Jens Wiklander32b31802023-10-06 16:59:46 +02002422 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002423 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Jens Wiklander32b31802023-10-06 16:59:46 +02002424 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002425
Jens Wiklander32b31802023-10-06 16:59:46 +02002426 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002427 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Jens Wiklander32b31802023-10-06 16:59:46 +02002428 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002429
2430 /* Special case: EE certs that are locally trusted */
Jens Wiklander32b31802023-10-06 16:59:46 +02002431 if (ver_chain->len == 1 &&
2432 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2433 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002434 }
2435
2436#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2437find_parent:
2438#endif
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002439
2440 /* Obtain list of potential trusted signers from CA callback,
2441 * or use statically provided list. */
2442#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Jens Wiklander32b31802023-10-06 16:59:46 +02002443 if (f_ca_cb != NULL) {
2444 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2445 mbedtls_free(ver_chain->trust_ca_cb_result);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002446 ver_chain->trust_ca_cb_result = NULL;
2447
Jens Wiklander32b31802023-10-06 16:59:46 +02002448 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2449 if (ret != 0) {
2450 return MBEDTLS_ERR_X509_FATAL_ERROR;
2451 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002452
2453 cur_trust_ca = ver_chain->trust_ca_cb_result;
Jens Wiklander32b31802023-10-06 16:59:46 +02002454 } else
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002455#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2456 {
2457 ((void) f_ca_cb);
2458 ((void) p_ca_cb);
2459 cur_trust_ca = trust_ca;
2460 }
2461
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002462 /* Look for a parent in trusted CAs or up the chain */
Jens Wiklander32b31802023-10-06 16:59:46 +02002463 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2464 &parent_is_trusted, &signature_is_good,
2465 ver_chain->len - 1, self_cnt, rs_ctx);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002466
2467#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Jens Wiklander32b31802023-10-06 16:59:46 +02002468 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002469 /* save state */
2470 rs_ctx->in_progress = x509_crt_rs_find_parent;
2471 rs_ctx->self_cnt = self_cnt;
2472 rs_ctx->ver_chain = *ver_chain; /* struct copy */
2473
Jens Wiklander32b31802023-10-06 16:59:46 +02002474 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002475 }
2476#else
2477 (void) ret;
2478#endif
2479
2480 /* No parent? We're done here */
Jens Wiklander32b31802023-10-06 16:59:46 +02002481 if (parent == NULL) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002482 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Jens Wiklander32b31802023-10-06 16:59:46 +02002483 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002484 }
2485
2486 /* Count intermediate self-issued (not necessarily self-signed) certs.
2487 * These can occur with some strategies for key rollover, see [SIRO],
2488 * and should be excluded from max_pathlen checks. */
Jens Wiklander32b31802023-10-06 16:59:46 +02002489 if (ver_chain->len != 1 &&
2490 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002491 self_cnt++;
2492 }
2493
2494 /* path_cnt is 0 for the first intermediate CA,
2495 * and if parent is trusted it's not an intermediate CA */
Jens Wiklander32b31802023-10-06 16:59:46 +02002496 if (!parent_is_trusted &&
2497 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002498 /* return immediately to avoid overflow the chain array */
Jens Wiklander32b31802023-10-06 16:59:46 +02002499 return MBEDTLS_ERR_X509_FATAL_ERROR;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002500 }
2501
2502 /* signature was checked while searching parent */
Jens Wiklander32b31802023-10-06 16:59:46 +02002503 if (!signature_is_good) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002504 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Jens Wiklander32b31802023-10-06 16:59:46 +02002505 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002506
2507 /* check size of signing key */
Jens Wiklander32b31802023-10-06 16:59:46 +02002508 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002509 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Jens Wiklander32b31802023-10-06 16:59:46 +02002510 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002511
2512#if defined(MBEDTLS_X509_CRL_PARSE_C)
2513 /* Check trusted CA's CRL for the given crt */
Jens Wiklander32b31802023-10-06 16:59:46 +02002514 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002515#else
2516 (void) ca_crl;
2517#endif
2518
2519 /* prepare for next iteration */
2520 child = parent;
2521 parent = NULL;
2522 child_is_trusted = parent_is_trusted;
2523 signature_is_good = 0;
2524 }
2525}
2526
2527/*
2528 * Check for CN match
2529 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002530static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2531 const char *cn, size_t cn_len)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002532{
2533 /* try exact match */
Jens Wiklander32b31802023-10-06 16:59:46 +02002534 if (name->len == cn_len &&
2535 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2536 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002537 }
2538
2539 /* try wildcard match */
Jens Wiklander32b31802023-10-06 16:59:46 +02002540 if (x509_check_wildcard(cn, name) == 0) {
2541 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002542 }
2543
Jens Wiklander32b31802023-10-06 16:59:46 +02002544 return -1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002545}
2546
2547/*
Jerome Forissier79013242021-07-28 10:24:04 +02002548 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2549 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002550static int x509_crt_check_san(const mbedtls_x509_buf *name,
2551 const char *cn, size_t cn_len)
Jerome Forissier79013242021-07-28 10:24:04 +02002552{
2553 const unsigned char san_type = (unsigned char) name->tag &
2554 MBEDTLS_ASN1_TAG_VALUE_MASK;
2555
2556 /* dNSName */
Jens Wiklander32b31802023-10-06 16:59:46 +02002557 if (san_type == MBEDTLS_X509_SAN_DNS_NAME) {
2558 return x509_crt_check_cn(name, cn, cn_len);
2559 }
Jerome Forissier79013242021-07-28 10:24:04 +02002560
2561 /* (We may handle other types here later.) */
2562
2563 /* Unrecognized type */
Jens Wiklander32b31802023-10-06 16:59:46 +02002564 return -1;
Jerome Forissier79013242021-07-28 10:24:04 +02002565}
2566
2567/*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002568 * Verify the requested CN - only call this if cn is not NULL!
2569 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002570static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2571 const char *cn,
2572 uint32_t *flags)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002573{
2574 const mbedtls_x509_name *name;
2575 const mbedtls_x509_sequence *cur;
Jens Wiklander32b31802023-10-06 16:59:46 +02002576 size_t cn_len = strlen(cn);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002577
Jens Wiklander32b31802023-10-06 16:59:46 +02002578 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2579 for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) {
2580 if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002581 break;
2582 }
2583 }
2584
Jens Wiklander32b31802023-10-06 16:59:46 +02002585 if (cur == NULL) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002586 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Jens Wiklander32b31802023-10-06 16:59:46 +02002587 }
2588 } else {
2589 for (name = &crt->subject; name != NULL; name = name->next) {
2590 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2591 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
2592 break;
2593 }
2594 }
2595
2596 if (name == NULL) {
2597 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2598 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002599 }
2600}
2601
2602/*
2603 * Merge the flags for all certs in the chain, after calling callback
2604 */
2605static int x509_crt_merge_flags_with_cb(
Jens Wiklander32b31802023-10-06 16:59:46 +02002606 uint32_t *flags,
2607 const mbedtls_x509_crt_verify_chain *ver_chain,
2608 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2609 void *p_vrfy)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002610{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002611 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002612 unsigned i;
2613 uint32_t cur_flags;
2614 const mbedtls_x509_crt_verify_chain_item *cur;
2615
Jens Wiklander32b31802023-10-06 16:59:46 +02002616 for (i = ver_chain->len; i != 0; --i) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002617 cur = &ver_chain->items[i-1];
2618 cur_flags = cur->flags;
2619
Jens Wiklander32b31802023-10-06 16:59:46 +02002620 if (NULL != f_vrfy) {
2621 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
2622 return ret;
2623 }
2624 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002625
2626 *flags |= cur_flags;
2627 }
2628
Jens Wiklander32b31802023-10-06 16:59:46 +02002629 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002630}
2631
2632/*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002633 * Verify the certificate validity, with profile, restartable version
2634 *
2635 * This function:
2636 * - checks the requested CN (if any)
2637 * - checks the type and size of the EE cert's key,
2638 * as that isn't done as part of chain building/verification currently
2639 * - builds and verifies the chain
2640 * - then calls the callback and merges the flags
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002641 *
2642 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
2643 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
2644 * verification routine to search for trusted signers, and CRLs will
2645 * be disabled. Otherwise, `trust_ca` will be used as the static list
2646 * of trusted signers, and `ca_crl` will be use as the static list
2647 * of CRLs.
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002648 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002649static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
2650 mbedtls_x509_crt *trust_ca,
2651 mbedtls_x509_crl *ca_crl,
2652 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2653 void *p_ca_cb,
2654 const mbedtls_x509_crt_profile *profile,
2655 const char *cn, uint32_t *flags,
2656 int (*f_vrfy)(void *,
2657 mbedtls_x509_crt *,
2658 int,
2659 uint32_t *),
2660 void *p_vrfy,
2661 mbedtls_x509_crt_restart_ctx *rs_ctx)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002662{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002663 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02002664 mbedtls_pk_type_t pk_type;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002665 mbedtls_x509_crt_verify_chain ver_chain;
2666 uint32_t ee_flags;
Jens Wiklander817466c2018-05-22 13:49:31 +02002667
2668 *flags = 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002669 ee_flags = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +02002670 x509_crt_verify_chain_reset(&ver_chain);
Jens Wiklander817466c2018-05-22 13:49:31 +02002671
Jens Wiklander32b31802023-10-06 16:59:46 +02002672 if (profile == NULL) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002673 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2674 goto exit;
2675 }
2676
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002677 /* check name if requested */
Jens Wiklander32b31802023-10-06 16:59:46 +02002678 if (cn != NULL) {
2679 x509_crt_verify_name(crt, cn, &ee_flags);
2680 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002681
2682 /* Check the type and size of the key */
Jens Wiklander32b31802023-10-06 16:59:46 +02002683 pk_type = mbedtls_pk_get_type(&crt->pk);
Jens Wiklander817466c2018-05-22 13:49:31 +02002684
Jens Wiklander32b31802023-10-06 16:59:46 +02002685 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002686 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Jens Wiklander32b31802023-10-06 16:59:46 +02002687 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002688
Jens Wiklander32b31802023-10-06 16:59:46 +02002689 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002690 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Jens Wiklander32b31802023-10-06 16:59:46 +02002691 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002692
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002693 /* Check the chain */
Jens Wiklander32b31802023-10-06 16:59:46 +02002694 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
2695 f_ca_cb, p_ca_cb, profile,
2696 &ver_chain, rs_ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +02002697
Jens Wiklander32b31802023-10-06 16:59:46 +02002698 if (ret != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002699 goto exit;
Jens Wiklander32b31802023-10-06 16:59:46 +02002700 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002701
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002702 /* Merge end-entity flags */
2703 ver_chain.items[0].flags |= ee_flags;
2704
2705 /* Build final flags, calling callback on the way if any */
Jens Wiklander32b31802023-10-06 16:59:46 +02002706 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Jens Wiklander817466c2018-05-22 13:49:31 +02002707
2708exit:
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002709
2710#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Jens Wiklander32b31802023-10-06 16:59:46 +02002711 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
2712 mbedtls_free(ver_chain.trust_ca_cb_result);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002713 ver_chain.trust_ca_cb_result = NULL;
2714#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2715
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002716#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Jens Wiklander32b31802023-10-06 16:59:46 +02002717 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
2718 mbedtls_x509_crt_restart_free(rs_ctx);
2719 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002720#endif
2721
Jens Wiklander817466c2018-05-22 13:49:31 +02002722 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2723 * the SSL module for authmode optional, but non-zero return from the
2724 * callback means a fatal error so it shouldn't be ignored */
Jens Wiklander32b31802023-10-06 16:59:46 +02002725 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Jens Wiklander817466c2018-05-22 13:49:31 +02002726 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Jens Wiklander817466c2018-05-22 13:49:31 +02002727 }
2728
Jens Wiklander32b31802023-10-06 16:59:46 +02002729 if (ret != 0) {
2730 *flags = (uint32_t) -1;
2731 return ret;
2732 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002733
Jens Wiklander32b31802023-10-06 16:59:46 +02002734 if (*flags != 0) {
2735 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
2736 }
2737
2738 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02002739}
2740
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002741
2742/*
2743 * Verify the certificate validity (default profile, not restartable)
2744 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002745int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
2746 mbedtls_x509_crt *trust_ca,
2747 mbedtls_x509_crl *ca_crl,
2748 const char *cn, uint32_t *flags,
2749 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2750 void *p_vrfy)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002751{
Jens Wiklander32b31802023-10-06 16:59:46 +02002752 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
2753 NULL, NULL,
2754 &mbedtls_x509_crt_profile_default,
2755 cn, flags,
2756 f_vrfy, p_vrfy, NULL);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002757}
2758
2759/*
2760 * Verify the certificate validity (user-chosen profile, not restartable)
2761 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002762int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
2763 mbedtls_x509_crt *trust_ca,
2764 mbedtls_x509_crl *ca_crl,
2765 const mbedtls_x509_crt_profile *profile,
2766 const char *cn, uint32_t *flags,
2767 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2768 void *p_vrfy)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002769{
Jens Wiklander32b31802023-10-06 16:59:46 +02002770 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
2771 NULL, NULL,
2772 profile, cn, flags,
2773 f_vrfy, p_vrfy, NULL);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002774}
2775
2776#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2777/*
2778 * Verify the certificate validity (user-chosen profile, CA callback,
2779 * not restartable).
2780 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002781int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
2782 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2783 void *p_ca_cb,
2784 const mbedtls_x509_crt_profile *profile,
2785 const char *cn, uint32_t *flags,
2786 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2787 void *p_vrfy)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002788{
Jens Wiklander32b31802023-10-06 16:59:46 +02002789 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
2790 f_ca_cb, p_ca_cb,
2791 profile, cn, flags,
2792 f_vrfy, p_vrfy, NULL);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002793}
2794#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2795
Jens Wiklander32b31802023-10-06 16:59:46 +02002796int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
2797 mbedtls_x509_crt *trust_ca,
2798 mbedtls_x509_crl *ca_crl,
2799 const mbedtls_x509_crt_profile *profile,
2800 const char *cn, uint32_t *flags,
2801 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2802 void *p_vrfy,
2803 mbedtls_x509_crt_restart_ctx *rs_ctx)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002804{
Jens Wiklander32b31802023-10-06 16:59:46 +02002805 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
2806 NULL, NULL,
2807 profile, cn, flags,
2808 f_vrfy, p_vrfy, rs_ctx);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002809}
2810
2811
Jens Wiklander817466c2018-05-22 13:49:31 +02002812/*
2813 * Initialize a certificate chain
2814 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002815void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Jens Wiklander817466c2018-05-22 13:49:31 +02002816{
Jens Wiklander32b31802023-10-06 16:59:46 +02002817 memset(crt, 0, sizeof(mbedtls_x509_crt));
Jens Wiklander817466c2018-05-22 13:49:31 +02002818}
2819
2820/*
2821 * Unallocate all certificate data
2822 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002823void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Jens Wiklander817466c2018-05-22 13:49:31 +02002824{
2825 mbedtls_x509_crt *cert_cur = crt;
2826 mbedtls_x509_crt *cert_prv;
Jens Wiklander817466c2018-05-22 13:49:31 +02002827
Jens Wiklander32b31802023-10-06 16:59:46 +02002828 while (cert_cur != NULL) {
2829 mbedtls_pk_free(&cert_cur->pk);
Jens Wiklander817466c2018-05-22 13:49:31 +02002830
2831#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Jens Wiklander32b31802023-10-06 16:59:46 +02002832 mbedtls_free(cert_cur->sig_opts);
Jens Wiklander817466c2018-05-22 13:49:31 +02002833#endif
2834
Jens Wiklander32b31802023-10-06 16:59:46 +02002835 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
2836 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
2837 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
2838 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
2839 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
2840
2841 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
2842 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
2843 mbedtls_free(cert_cur->raw.p);
Jens Wiklander817466c2018-05-22 13:49:31 +02002844 }
2845
Jens Wiklander817466c2018-05-22 13:49:31 +02002846 cert_prv = cert_cur;
2847 cert_cur = cert_cur->next;
2848
Jens Wiklander32b31802023-10-06 16:59:46 +02002849 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
2850 if (cert_prv != crt) {
2851 mbedtls_free(cert_prv);
2852 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002853 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002854}
2855
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002856#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2857/*
2858 * Initialize a restart context
2859 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002860void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002861{
Jens Wiklander32b31802023-10-06 16:59:46 +02002862 mbedtls_pk_restart_init(&ctx->pk);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002863
2864 ctx->parent = NULL;
2865 ctx->fallback_parent = NULL;
2866 ctx->fallback_signature_is_good = 0;
2867
2868 ctx->parent_is_trusted = -1;
2869
2870 ctx->in_progress = x509_crt_rs_none;
2871 ctx->self_cnt = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +02002872 x509_crt_verify_chain_reset(&ctx->ver_chain);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002873}
2874
2875/*
2876 * Free the components of a restart context
2877 */
Jens Wiklander32b31802023-10-06 16:59:46 +02002878void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002879{
Jens Wiklander32b31802023-10-06 16:59:46 +02002880 if (ctx == NULL) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002881 return;
Jens Wiklander32b31802023-10-06 16:59:46 +02002882 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002883
Jens Wiklander32b31802023-10-06 16:59:46 +02002884 mbedtls_pk_restart_free(&ctx->pk);
2885 mbedtls_x509_crt_restart_init(ctx);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002886}
2887#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
2888
Jens Wiklander817466c2018-05-22 13:49:31 +02002889#endif /* MBEDTLS_X509_CRT_PARSE_C */