blob: a3a4525b94780ebc45ea0a37a43d5ea544a4dcb9 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006 */
7/*
8 * The ITU-T X.509 standard defines a certificate format for PKI.
9 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020010 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
11 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
12 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020013 *
14 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
15 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020016 *
17 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000025#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033#endif
34
Andrzej Kurekd4a65532018-10-31 06:18:39 -040035#if defined(MBEDTLS_USE_PSA_CRYPTO)
36#include "psa/crypto.h"
37#include "mbedtls/psa_util.h"
38#endif
39
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010044#endif
45
Daniel Axtens301db662020-05-28 11:43:41 +100046#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010047#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020048#include <windows.h>
49#else
50#include <time.h>
51#endif
Daniel Axtens301db662020-05-28 11:43:41 +100052#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000055#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020056#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057#include <sys/types.h>
58#include <sys/stat.h>
59#include <dirent.h>
Eduardo Silva32ffb2b2019-04-25 10:43:26 -060060#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000061#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#endif
63
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020064/*
65 * Item in a verification chain: cert and flags for it
66 */
67typedef struct {
68 mbedtls_x509_crt *crt;
69 uint32_t flags;
70} x509_crt_verify_chain_item;
71
72/*
73 * Max size of verification chain: end-entity + intermediates + trusted root
74 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020076
Gilles Peskine0ecd7192021-06-07 21:24:26 +020077/* Default profile. Do not remove items unless there are serious security
78 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020079const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
80{
Gilles Peskine5e79cb32017-05-04 16:17:21 +020081 /* Only SHA-2 hashes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
83 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
84 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
85 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020086 0xFFFFFFF, /* Any PK alg */
87 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020088 2048,
89};
90
91/*
92 * Next-default profile
93 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020094const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
95{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020096 /* Hashes from SHA-256 and above */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
98 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
99 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200100 0xFFFFFFF, /* Any PK alg */
101#if defined(MBEDTLS_ECP_C)
102 /* Curves at or above 128-bit security level */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
104 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
105 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
106 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
107 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
108 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
109 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200110#else
111 0,
112#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200113 2048,
114};
115
116/*
117 * NSA Suite B Profile
118 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200119const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
120{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200121 /* Only SHA-256 and 384 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
123 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200124 /* Only ECDSA */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
126 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200127#if defined(MBEDTLS_ECP_C)
128 /* Only NIST P-256 and P-384 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
130 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200131#else
132 0,
133#endif
134 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200135};
136
137/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200138 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200139 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200140 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
142 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200143{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 if (md_alg == MBEDTLS_MD_NONE) {
145 return -1;
146 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
149 return 0;
150 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200153}
154
155/*
156 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200157 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200158 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
160 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200161{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 if (pk_alg == MBEDTLS_PK_NONE) {
163 return -1;
164 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
167 return 0;
168 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200171}
172
173/*
174 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200175 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200176 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
178 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200179{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200181
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200182#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
184 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
185 return 0;
186 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200189 }
190#endif
191
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200192#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200194 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 pk_alg == MBEDTLS_PK_ECKEY_DH) {
196 const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 if (gid == MBEDTLS_ECP_DP_NONE) {
199 return -1;
200 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
203 return 0;
204 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200207 }
208#endif
209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200211}
212
213/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000214 * Like memcmp, but case-insensitive and always returns -1 if different
215 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000217{
218 size_t i;
219 unsigned char diff;
220 const unsigned char *n1 = s1, *n2 = s2;
221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000223 diff = n1[i] ^ n2[i];
224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000226 continue;
227 }
228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 if (diff == 32 &&
230 ((n1[i] >= 'a' && n1[i] <= 'z') ||
231 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
232 continue;
233 }
234
235 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000236 }
237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000239}
240
241/*
242 * Return 0 if name matches wildcard, -1 otherwise
243 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000245{
246 size_t i;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000248
249 /* We can't have a match if there is no wildcard to match */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
251 return -1;
252 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 for (i = 0; i < cn_len; ++i) {
255 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000256 cn_idx = i;
257 break;
258 }
259 }
260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 if (cn_idx == 0) {
262 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000263 }
264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 if (cn_len - cn_idx == name->len - 1 &&
266 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
267 return 0;
268 }
269
270 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000271}
272
273/*
274 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
275 * variations (but not all).
276 *
277 * Return 0 if equal, -1 otherwise.
278 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000280{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000282 a->len == b->len &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 memcmp(a->p, b->p, b->len) == 0) {
284 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000285 }
286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
288 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000289 a->len == b->len &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 x509_memcasecmp(a->p, b->p, b->len) == 0) {
291 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000292 }
293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000295}
296
297/*
298 * Compare two X.509 Names (aka rdnSequence).
299 *
300 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
301 * we sometimes return unequal when the full algorithm would return equal,
302 * but never the other way. (In particular, we don't do Unicode normalisation
303 * or space folding.)
304 *
305 * Return 0 if equal, -1 otherwise.
306 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000308{
309 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 while (a != NULL || b != NULL) {
311 if (a == NULL || b == NULL) {
312 return -1;
313 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000314
315 /* type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000317 a->oid.len != b->oid.len ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
319 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000320 }
321
322 /* value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 if (x509_string_cmp(&a->val, &b->val) != 0) {
324 return -1;
325 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000326
327 /* structure of the list of sets */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 if (a->next_merged != b->next_merged) {
329 return -1;
330 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000331
332 a = a->next;
333 b = b->next;
334 }
335
336 /* a == NULL == b */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000338}
339
340/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200341 * Reset (init or clear) a verify_chain
342 */
343static void x509_crt_verify_chain_reset(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200345{
346 size_t i;
347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200349 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000350 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200351 }
352
353 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000354
355#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
356 ver_chain->trust_ca_cb_result = NULL;
357#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200358}
359
360/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
362 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363static int x509_get_version(unsigned char **p,
364 const unsigned char *end,
365 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366{
Janos Follath865b3eb2019-12-16 11:46:15 +0000367 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368 size_t len;
369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
371 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
372 0)) != 0) {
373 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374 *ver = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376 }
377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379 }
380
381 end = *p + len;
382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
384 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
385 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 if (*p != end) {
388 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
389 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
390 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100392 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393}
394
395/*
396 * Validity ::= SEQUENCE {
397 * notBefore Time,
398 * notAfter Time }
399 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400static int x509_get_dates(unsigned char **p,
401 const unsigned char *end,
402 mbedtls_x509_time *from,
403 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404{
Janos Follath865b3eb2019-12-16 11:46:15 +0000405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 size_t len;
407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100408 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
409 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
410 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
411 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412
413 end = *p + len;
414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
416 return ret;
417 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
420 return ret;
421 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 if (*p != end) {
424 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
425 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
426 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429}
430
431/*
432 * X.509 v2/v3 unique identifier (not parsed)
433 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434static int x509_get_uid(unsigned char **p,
435 const unsigned char *end,
436 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437{
Janos Follath865b3eb2019-12-16 11:46:15 +0000438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 if (*p == end) {
441 return 0;
442 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
444 uid->tag = **p;
445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
447 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
448 n)) != 0) {
449 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
450 return 0;
451 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454 }
455
456 uid->p = *p;
457 *p += uid->len;
458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460}
461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462static int x509_get_basic_constraints(unsigned char **p,
463 const unsigned char *end,
464 int *ca_istrue,
465 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466{
Janos Follath865b3eb2019-12-16 11:46:15 +0000467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468 size_t len;
469
470 /*
471 * BasicConstraints ::= SEQUENCE {
472 * cA BOOLEAN DEFAULT FALSE,
473 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
474 */
475 *ca_istrue = 0; /* DEFAULT FALSE */
476 *max_pathlen = 0; /* endless */
477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
479 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
480 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481 }
482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100483 if (*p == end) {
484 return 0;
485 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
488 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
489 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
490 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100492 if (ret != 0) {
493 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
494 }
495
496 if (*ca_istrue != 0) {
497 *ca_istrue = 1;
498 }
499 }
500
501 if (*p == end) {
502 return 0;
503 }
504
505 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
506 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
507 }
508
509 if (*p != end) {
510 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
511 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
512 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513
Andrzej Kurek16050742020-04-14 09:49:52 -0400514 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
515 * overflow, which is an undefined behavior. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 if (*max_pathlen == INT_MAX) {
517 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
518 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
519 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400520
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521 (*max_pathlen)++;
522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100523 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524}
525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100526static int x509_get_ns_cert_type(unsigned char **p,
527 const unsigned char *end,
528 unsigned char *ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529{
Janos Follath865b3eb2019-12-16 11:46:15 +0000530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
534 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
535 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100537 if (bs.len != 1) {
538 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
539 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
540 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541
542 /* Get actual bitstring */
543 *ns_cert_type = *bs.p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100544 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545}
546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100547static int x509_get_key_usage(unsigned char **p,
548 const unsigned char *end,
549 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550{
Janos Follath865b3eb2019-12-16 11:46:15 +0000551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200552 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
556 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
557 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100559 if (bs.len < 1) {
560 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
561 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
562 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563
564 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200565 *key_usage = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100566 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200567 *key_usage |= (unsigned int) bs.p[i] << (8*i);
568 }
569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100570 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571}
572
573/*
574 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
575 *
576 * KeyPurposeId ::= OBJECT IDENTIFIER
577 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100578static int x509_get_ext_key_usage(unsigned char **p,
579 const unsigned char *end,
580 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581{
Janos Follath865b3eb2019-12-16 11:46:15 +0000582 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100584 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
585 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
586 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587
588 /* Sequence length must be >= 1 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100589 if (ext_key_usage->buf.p == NULL) {
590 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
591 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
592 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100594 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595}
596
597/*
598 * SubjectAltName ::= GeneralNames
599 *
600 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
601 *
602 * GeneralName ::= CHOICE {
603 * otherName [0] OtherName,
604 * rfc822Name [1] IA5String,
605 * dNSName [2] IA5String,
606 * x400Address [3] ORAddress,
607 * directoryName [4] Name,
608 * ediPartyName [5] EDIPartyName,
609 * uniformResourceIdentifier [6] IA5String,
610 * iPAddress [7] OCTET STRING,
611 * registeredID [8] OBJECT IDENTIFIER }
612 *
613 * OtherName ::= SEQUENCE {
614 * type-id OBJECT IDENTIFIER,
615 * value [0] EXPLICIT ANY DEFINED BY type-id }
616 *
617 * EDIPartyName ::= SEQUENCE {
618 * nameAssigner [0] DirectoryString OPTIONAL,
619 * partyName [1] DirectoryString }
620 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300621 * NOTE: we list all types, but only use dNSName and otherName
622 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624static int x509_get_subject_alt_name(unsigned char **p,
625 const unsigned char *end,
626 mbedtls_x509_sequence *subject_alt_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627{
Janos Follath865b3eb2019-12-16 11:46:15 +0000628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631
632 /* Get main sequence tag */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
634 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
635 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
636 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100638 if (*p + len != end) {
639 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
640 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
641 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100643 while (*p < end) {
Ron Eldordbbd9662019-05-15 15:46:03 +0300644 mbedtls_x509_subject_alternative_name dummy_san_buf;
hanno-becker75ab0762023-02-08 08:46:42 -0500645 mbedtls_x509_buf tmp_san_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100646 memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
Ron Eldordbbd9662019-05-15 15:46:03 +0300647
hanno-becker75ab0762023-02-08 08:46:42 -0500648 tmp_san_buf.tag = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649 (*p)++;
hanno-becker75ab0762023-02-08 08:46:42 -0500650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100651 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
652 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
653 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654
hanno-becker75ab0762023-02-08 08:46:42 -0500655 tmp_san_buf.p = *p;
656 tmp_san_buf.len = tag_len;
657
658 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
660 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
661 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100662 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663
Ron Eldordbbd9662019-05-15 15:46:03 +0300664 /*
irwir74aee1c2019-09-21 18:21:48 +0300665 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300666 */
hanno-becker75ab0762023-02-08 08:46:42 -0500667 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &dummy_san_buf);
Ron Eldordbbd9662019-05-15 15:46:03 +0300668 /*
669 * In case the extension is malformed, return an error,
670 * and clear the allocated sequences.
671 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100672 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
Ron Eldordbbd9662019-05-15 15:46:03 +0300673 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
674 mbedtls_x509_sequence *seq_prv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100675 while (seq_cur != NULL) {
Ron Eldordbbd9662019-05-15 15:46:03 +0300676 seq_prv = seq_cur;
677 seq_cur = seq_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100678 mbedtls_platform_zeroize(seq_prv,
679 sizeof(mbedtls_x509_sequence));
680 mbedtls_free(seq_prv);
Ron Eldordbbd9662019-05-15 15:46:03 +0300681 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300682 subject_alt_name->next = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683 return ret;
Ron Eldordbbd9662019-05-15 15:46:03 +0300684 }
685
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686 /* Allocate and assign next pointer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100687 if (cur->buf.p != NULL) {
688 if (cur->next != NULL) {
689 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
690 }
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100692 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100694 if (cur->next == NULL) {
695 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
696 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
697 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699 cur = cur->next;
700 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200701
hanno-becker75ab0762023-02-08 08:46:42 -0500702 cur->buf = tmp_san_buf;
703 *p += tmp_san_buf.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704 }
705
706 /* Set final sequence entry's next pointer to NULL */
707 cur->next = NULL;
708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709 if (*p != end) {
710 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
711 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
712 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100714 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715}
716
717/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200718 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
719 *
720 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
721 *
722 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
723 *
724 * PolicyInformation ::= SEQUENCE {
725 * policyIdentifier CertPolicyId,
726 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
727 * PolicyQualifierInfo OPTIONAL }
728 *
729 * CertPolicyId ::= OBJECT IDENTIFIER
730 *
731 * PolicyQualifierInfo ::= SEQUENCE {
732 * policyQualifierId PolicyQualifierId,
733 * qualifier ANY DEFINED BY policyQualifierId }
734 *
735 * -- policyQualifierIds for Internet policy qualifiers
736 *
737 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
738 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
739 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
740 *
741 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
742 *
743 * Qualifier ::= CHOICE {
744 * cPSuri CPSuri,
745 * userNotice UserNotice }
746 *
747 * CPSuri ::= IA5String
748 *
749 * UserNotice ::= SEQUENCE {
750 * noticeRef NoticeReference OPTIONAL,
751 * explicitText DisplayText OPTIONAL }
752 *
753 * NoticeReference ::= SEQUENCE {
754 * organization DisplayText,
755 * noticeNumbers SEQUENCE OF INTEGER }
756 *
757 * DisplayText ::= CHOICE {
758 * ia5String IA5String (SIZE (1..200)),
759 * visibleString VisibleString (SIZE (1..200)),
760 * bmpString BMPString (SIZE (1..200)),
761 * utf8String UTF8String (SIZE (1..200)) }
762 *
763 * NOTE: we only parse and use anyPolicy without qualifiers at this point
764 * as defined in RFC 5280.
765 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100766static int x509_get_certificate_policies(unsigned char **p,
767 const unsigned char *end,
768 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200769{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300770 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200771 size_t len;
772 mbedtls_asn1_buf *buf;
773 mbedtls_asn1_sequence *cur = certificate_policies;
774
775 /* Get main sequence tag */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100776 ret = mbedtls_asn1_get_tag(p, end, &len,
777 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
778 if (ret != 0) {
779 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
780 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100782 if (*p + len != end) {
783 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
784 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
785 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200786
787 /*
788 * Cannot be an empty sequence.
789 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790 if (len == 0) {
791 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
792 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
793 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200794
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100795 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200796 mbedtls_x509_buf policy_oid;
797 const unsigned char *policy_end;
798
799 /*
800 * Get the policy sequence
801 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100802 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
803 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
804 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
805 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200806
807 policy_end = *p + len;
808
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100809 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
810 MBEDTLS_ASN1_OID)) != 0) {
811 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
812 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200813
814 policy_oid.tag = MBEDTLS_ASN1_OID;
815 policy_oid.len = len;
816 policy_oid.p = *p;
817
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300818 /*
819 * Only AnyPolicy is currently supported when enforcing policy.
820 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300822 /*
823 * Set the parsing return code but continue parsing, in case this
824 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
825 * is configured.
826 */
827 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
828 }
829
Ron Eldor74d9acc2019-03-21 14:00:03 +0200830 /* Allocate and assign next pointer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100831 if (cur->buf.p != NULL) {
832 if (cur->next != NULL) {
833 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
834 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100836 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100838 if (cur->next == NULL) {
839 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
840 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
841 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200842
843 cur = cur->next;
844 }
845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100846 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200847 buf->tag = policy_oid.tag;
848 buf->p = policy_oid.p;
849 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300850
851 *p += len;
852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100853 /*
854 * If there is an optional qualifier, then *p < policy_end
855 * Check the Qualifier len to verify it doesn't exceed policy_end.
856 */
857 if (*p < policy_end) {
858 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
859 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
860 0) {
861 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
862 }
Ron Eldor08063792019-05-13 16:38:39 +0300863 /*
864 * Skip the optional policy qualifiers.
865 */
866 *p += len;
867 }
868
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100869 if (*p != policy_end) {
870 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
871 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
872 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200873 }
874
875 /* Set final sequence entry's next pointer to NULL */
876 cur->next = NULL;
877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100878 if (*p != end) {
879 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
880 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
881 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100883 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200884}
885
886/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 * X.509 v3 extensions
888 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100890static int x509_get_crt_ext(unsigned char **p,
891 const unsigned char *end,
892 mbedtls_x509_crt *crt,
893 mbedtls_x509_crt_ext_cb_t cb,
894 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895{
Janos Follath865b3eb2019-12-16 11:46:15 +0000896 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200898 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100900 if (*p == end) {
901 return 0;
902 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000903
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100904 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
905 return ret;
906 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907
Hanno Becker12f62fb2019-02-12 17:22:36 +0000908 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100909 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910 /*
911 * Extension ::= SEQUENCE {
912 * extnID OBJECT IDENTIFIER,
913 * critical BOOLEAN DEFAULT FALSE,
914 * extnValue OCTET STRING }
915 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100916 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917 int is_critical = 0; /* DEFAULT FALSE */
918 int ext_type = 0;
919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100920 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
921 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
922 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
923 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924
925 end_ext_data = *p + len;
926
927 /* Get extension ID */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100928 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
929 MBEDTLS_ASN1_OID)) != 0) {
930 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
931 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932
k-stachowiak463928a2018-07-24 12:50:59 +0200933 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934 extn_oid.p = *p;
935 *p += extn_oid.len;
936
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937 /* Get optional critical */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100938 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
939 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
940 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
941 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942
943 /* Data should be octet string type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100944 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
945 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
946 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
947 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200949 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950 end_ext_octet = *p + len;
951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100952 if (end_ext_octet != end_ext_data) {
953 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
954 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
955 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956
957 /*
958 * Detect supported extensions
959 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100960 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100962 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200963 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100964 if (cb != NULL) {
965 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
966 if (ret != 0 && is_critical) {
967 return ret;
968 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200969 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200970 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200971 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200972
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973 /* No parser found, skip extension */
974 *p = end_ext_octet;
975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100977 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978 /* Data is marked as critical: fail */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100979 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
980 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 }
982#endif
983 continue;
984 }
985
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100986 /* Forbid repeated extensions */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100987 if ((crt->ext_types & ext_type) != 0) {
988 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
989 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100990
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991 crt->ext_types |= ext_type;
992
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100993 switch (ext_type) {
994 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
995 /* Parse basic constraints */
996 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
997 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
998 return ret;
999 }
1000 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001002 case MBEDTLS_X509_EXT_KEY_USAGE:
1003 /* Parse key usage */
1004 if ((ret = x509_get_key_usage(p, end_ext_octet,
1005 &crt->key_usage)) != 0) {
1006 return ret;
1007 }
1008 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001010 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
1011 /* Parse extended key usage */
1012 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
1013 &crt->ext_key_usage)) != 0) {
1014 return ret;
1015 }
1016 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001018 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
1019 /* Parse subject alt name */
1020 if ((ret = x509_get_subject_alt_name(p, end_ext_octet,
1021 &crt->subject_alt_names)) != 0) {
1022 return ret;
1023 }
1024 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001026 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1027 /* Parse netscape certificate type */
1028 if ((ret = x509_get_ns_cert_type(p, end_ext_octet,
1029 &crt->ns_cert_type)) != 0) {
1030 return ret;
1031 }
1032 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001034 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1035 /* Parse certificate policies type */
1036 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1037 &crt->certificate_policies)) != 0) {
1038 /* Give the callback (if any) a chance to handle the extension
1039 * if it contains unsupported policies */
1040 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1041 cb(p_ctx, crt, &extn_oid, is_critical,
1042 start_ext_octet, end_ext_octet) == 0) {
1043 break;
1044 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001045
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001046#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001047 if (is_critical) {
1048 return ret;
1049 } else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001050#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001051 /*
1052 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1053 * cannot interpret or enforce the policy. However, it is up to
1054 * the user to choose how to enforce the policies,
1055 * unless the extension is critical.
1056 */
1057 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1058 return ret;
1059 }
1060 }
1061 break;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001063 default:
1064 /*
1065 * If this is a non-critical extension, which the oid layer
1066 * supports, but there isn't an x509 parser for it,
1067 * skip the extension.
1068 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001069#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001070 if (is_critical) {
1071 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1072 } else
Ron Eldordf48efa2019-04-08 13:28:24 +03001073#endif
1074 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075 }
1076 }
1077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001078 if (*p != end) {
1079 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1080 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1081 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001083 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001084}
1085
1086/*
1087 * Parse and fill a single X.509 certificate in DER format
1088 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001089static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1090 const unsigned char *buf,
1091 size_t buflen,
1092 int make_copy,
1093 mbedtls_x509_crt_ext_cb_t cb,
1094 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095{
Janos Follath865b3eb2019-12-16 11:46:15 +00001096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097 size_t len;
1098 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001101 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1102 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1103 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104
1105 /*
1106 * Check for valid input
1107 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001108 if (crt == NULL || buf == NULL) {
1109 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1110 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001112 /* Use the original buffer until we figure out actual length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001113 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001114 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001115 end = p + len;
1116
1117 /*
1118 * Certificate ::= SEQUENCE {
1119 * tbsCertificate TBSCertificate,
1120 * signatureAlgorithm AlgorithmIdentifier,
1121 * signatureValue BIT STRING }
1122 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001123 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1124 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1125 mbedtls_x509_crt_free(crt);
1126 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127 }
1128
Janos Follathcc0e49d2016-02-17 14:34:12 +00001129 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001130 crt->raw.len = crt_end - buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001131 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001132 /* Create and populate a new buffer for the raw field. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001133 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1134 if (crt->raw.p == NULL) {
1135 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1136 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001138 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001139 crt->own_buffer = 1;
1140
1141 p += crt->raw.len - len;
1142 end = crt_end = p + len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001143 } else {
1144 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001145 crt->own_buffer = 0;
1146 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001147
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 /*
1149 * TBSCertificate ::= SEQUENCE {
1150 */
1151 crt->tbs.p = p;
1152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001153 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1154 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1155 mbedtls_x509_crt_free(crt);
1156 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 }
1158
1159 end = p + len;
1160 crt->tbs.len = end - crt->tbs.p;
1161
1162 /*
1163 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1164 *
1165 * CertificateSerialNumber ::= INTEGER
1166 *
1167 * signature AlgorithmIdentifier
1168 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001169 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1170 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1171 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1172 &sig_params1)) != 0) {
1173 mbedtls_x509_crt_free(crt);
1174 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001175 }
1176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001177 if (crt->version < 0 || crt->version > 2) {
1178 mbedtls_x509_crt_free(crt);
1179 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 }
1181
Andres AG7ca4a032017-03-09 16:16:11 +00001182 crt->version++;
1183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001184 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1185 &crt->sig_md, &crt->sig_pk,
1186 &crt->sig_opts)) != 0) {
1187 mbedtls_x509_crt_free(crt);
1188 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189 }
1190
1191 /*
1192 * issuer Name
1193 */
1194 crt->issuer_raw.p = p;
1195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1197 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1198 mbedtls_x509_crt_free(crt);
1199 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001200 }
1201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001202 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1203 mbedtls_x509_crt_free(crt);
1204 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001205 }
1206
1207 crt->issuer_raw.len = p - crt->issuer_raw.p;
1208
1209 /*
1210 * Validity ::= SEQUENCE {
1211 * notBefore Time,
1212 * notAfter Time }
1213 *
1214 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001215 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1216 &crt->valid_to)) != 0) {
1217 mbedtls_x509_crt_free(crt);
1218 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219 }
1220
1221 /*
1222 * subject Name
1223 */
1224 crt->subject_raw.p = p;
1225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001226 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1227 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1228 mbedtls_x509_crt_free(crt);
1229 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001230 }
1231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001232 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1233 mbedtls_x509_crt_free(crt);
1234 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235 }
1236
1237 crt->subject_raw.len = p - crt->subject_raw.p;
1238
1239 /*
1240 * SubjectPublicKeyInfo
1241 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001242 crt->pk_raw.p = p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001243 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1244 mbedtls_x509_crt_free(crt);
1245 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001246 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001247 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001248
1249 /*
1250 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1251 * -- If present, version shall be v2 or v3
1252 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1253 * -- If present, version shall be v2 or v3
1254 * extensions [3] EXPLICIT Extensions OPTIONAL
1255 * -- If present, version shall be v3
1256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001257 if (crt->version == 2 || crt->version == 3) {
1258 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1259 if (ret != 0) {
1260 mbedtls_x509_crt_free(crt);
1261 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001262 }
1263 }
1264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001265 if (crt->version == 2 || crt->version == 3) {
1266 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1267 if (ret != 0) {
1268 mbedtls_x509_crt_free(crt);
1269 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001270 }
1271 }
1272
David Horstmannab617512022-10-27 11:45:01 +01001273 int extensions_allowed = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001275 if (crt->version != 3) {
David Horstmannab617512022-10-27 11:45:01 +01001276 extensions_allowed = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001277 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001278#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001279 if (extensions_allowed) {
1280 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1281 if (ret != 0) {
1282 mbedtls_x509_crt_free(crt);
1283 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001284 }
1285 }
1286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001287 if (p != end) {
1288 mbedtls_x509_crt_free(crt);
1289 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1290 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001291 }
1292
1293 end = crt_end;
1294
1295 /*
1296 * }
1297 * -- end of TBSCertificate
1298 *
1299 * signatureAlgorithm AlgorithmIdentifier,
1300 * signatureValue BIT STRING
1301 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001302 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1303 mbedtls_x509_crt_free(crt);
1304 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001305 }
1306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001307 if (crt->sig_oid.len != sig_oid2.len ||
1308 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001309 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001310 sig_params1.len != sig_params2.len ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001311 (sig_params1.len != 0 &&
1312 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1313 mbedtls_x509_crt_free(crt);
1314 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001315 }
1316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001317 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1318 mbedtls_x509_crt_free(crt);
1319 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001320 }
1321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001322 if (p != end) {
1323 mbedtls_x509_crt_free(crt);
1324 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1325 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001326 }
1327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001328 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001329}
1330
1331/*
1332 * Parse one X.509 certificate in DER format from a buffer and add them to a
1333 * chained list
1334 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001335static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1336 const unsigned char *buf,
1337 size_t buflen,
1338 int make_copy,
1339 mbedtls_x509_crt_ext_cb_t cb,
1340 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001341{
Janos Follath865b3eb2019-12-16 11:46:15 +00001342 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344
1345 /*
1346 * Check for valid input
1347 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001348 if (crt == NULL || buf == NULL) {
1349 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1350 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001352 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353 prev = crt;
1354 crt = crt->next;
1355 }
1356
1357 /*
1358 * Add new certificate on the end of the chain if needed.
1359 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001360 if (crt->version != 0 && crt->next == NULL) {
1361 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001363 if (crt->next == NULL) {
1364 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1365 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366
1367 prev = crt;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001368 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370 }
1371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001372 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1373 if (ret != 0) {
1374 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001375 prev->next = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001376 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001378 if (crt != chain) {
1379 mbedtls_free(crt);
1380 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001382 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383 }
1384
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001385 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001386}
1387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001388int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1389 const unsigned char *buf,
1390 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001391{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001392 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001393}
1394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001395int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1396 const unsigned char *buf,
1397 size_t buflen,
1398 int make_copy,
1399 mbedtls_x509_crt_ext_cb_t cb,
1400 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001401{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001402 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001403}
1404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001405int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1406 const unsigned char *buf,
1407 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001408{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001409 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001410}
1411
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001412/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001413 * Parse one or more PEM certificates from a buffer and add them to the chained
1414 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001416int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1417 const unsigned char *buf,
1418 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001419{
Janos Follath98e28a72016-05-31 14:03:54 +01001420#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001421 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001423#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001424
1425 /*
1426 * Check for valid input
1427 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001428 if (chain == NULL || buf == NULL) {
1429 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1430 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431
1432 /*
1433 * Determine buffer content. Buffer contains either one DER certificate or
1434 * one or more PEM certificates.
1435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001437 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1438 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001440 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001442 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1443 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1444 }
Janos Follath98e28a72016-05-31 14:03:54 +01001445#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001446 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001447#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001450 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001454 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001455 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001456 size_t use_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001457 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001458
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001459 /* If we get there, we know the string is null-terminated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001460 ret = mbedtls_pem_read_buffer(&pem,
1461 "-----BEGIN CERTIFICATE-----",
1462 "-----END CERTIFICATE-----",
1463 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001465 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466 /*
1467 * Was PEM encoded
1468 */
1469 buflen -= use_len;
1470 buf += use_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001471 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1472 return ret;
1473 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1474 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475
1476 /*
1477 * PEM header and footer were found
1478 */
1479 buflen -= use_len;
1480 buf += use_len;
1481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001482 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001483 first_error = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001484 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485
Paul Bakker5a5fa922014-09-26 14:53:04 +02001486 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001487 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001488 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001489 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001490 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001492 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001494 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001496 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001497 /*
1498 * Quit parsing on a memory error
1499 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001500 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1501 return ret;
1502 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001504 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001505 first_error = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001506 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507
1508 total_failed++;
1509 continue;
1510 }
1511
1512 success = 1;
1513 }
1514 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001516 if (success) {
1517 return total_failed;
1518 } else if (first_error) {
1519 return first_error;
1520 } else {
1521 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1522 }
Janos Follath98e28a72016-05-31 14:03:54 +01001523#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524}
1525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001527/*
1528 * Load one or more certificates and add them to the chained list
1529 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001530int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531{
Janos Follath865b3eb2019-12-16 11:46:15 +00001532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533 size_t n;
1534 unsigned char *buf;
1535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001536 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1537 return ret;
1538 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001540 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001542 mbedtls_platform_zeroize(buf, n);
1543 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001545 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546}
1547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001548int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549{
1550 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001551#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552 int w_ret;
1553 WCHAR szDir[MAX_PATH];
1554 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001555 char *p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001556 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557
Paul Bakker9af723c2014-05-01 13:03:14 +02001558 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559 HANDLE hFind;
1560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001561 if (len > MAX_PATH - 3) {
1562 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1563 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001565 memset(szDir, 0, sizeof(szDir));
1566 memset(filename, 0, MAX_PATH);
1567 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001568 filename[len++] = '\\';
1569 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570 filename[len++] = '*';
1571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001572 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1573 MAX_PATH - 3);
1574 if (w_ret == 0) {
1575 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1576 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001578 hFind = FindFirstFileW(szDir, &file_data);
1579 if (hFind == INVALID_HANDLE_VALUE) {
1580 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1581 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582
1583 len = MAX_PATH - len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001584 do {
1585 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001587 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001588 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001589 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001591 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgrove0289c192023-02-10 12:52:13 +00001592 -1,
1593 p, (int) len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001594 NULL, NULL);
1595 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001596 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1597 goto cleanup;
1598 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001600 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1601 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602 ret++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001603 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001604 ret += w_ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001605 }
1606 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001608 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001610 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611
Ron Eldor36d90422017-01-09 15:09:16 +02001612cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001613 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001614#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001615 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001616 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001618 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001619 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001620 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001622 if (dir == NULL) {
1623 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1624 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625
Ron Eldor63140682017-01-09 19:27:59 +02001626#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001627 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1628 closedir(dir);
1629 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001630 }
Ron Eldor63140682017-01-09 19:27:59 +02001631#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001633 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001635 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman18688702023-02-02 12:40:50 +00001636 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001637 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638
Dave Rodgman18688702023-02-02 12:40:50 +00001639 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001640 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1641 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001642 } else if (stat(entry_name, &sb) == -1) {
1643 if (errno == ENOENT) {
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001644 /* Broken symbolic link - ignore this entry.
1645 stat(2) will return this error for either (a) a dangling
1646 symlink or (b) a missing file.
1647 Given that we have just obtained the filename from readdir,
1648 assume that it does exist and therefore treat this as a
1649 dangling symlink. */
1650 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001651 } else {
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001652 /* Some other file error; report the error. */
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001653 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1654 goto cleanup;
1655 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001656 }
1657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001658 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001660 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661
1662 // Ignore parse errors
1663 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001664 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1665 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001666 ret++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001667 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001668 ret += t_ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001669 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001671
1672cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001673 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001674
Ron Eldor63140682017-01-09 19:27:59 +02001675#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001676 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001678 }
Ron Eldor63140682017-01-09 19:27:59 +02001679#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001680
Paul Bakkerbe089b02013-10-14 15:51:50 +02001681#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001683 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001684}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001686
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001687/*
1688 * OtherName ::= SEQUENCE {
1689 * type-id OBJECT IDENTIFIER,
1690 * value [0] EXPLICIT ANY DEFINED BY type-id }
1691 *
1692 * HardwareModuleName ::= SEQUENCE {
1693 * hwType OBJECT IDENTIFIER,
1694 * hwSerialNum OCTET STRING }
1695 *
1696 * NOTE: we currently only parse and use otherName of type HwModuleName,
1697 * as defined in RFC 4108.
1698 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001699static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1700 mbedtls_x509_san_other_name *other_name)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001701{
Janos Follathab23cd12019-05-09 13:53:57 +01001702 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001703 size_t len;
1704 unsigned char *p = subject_alt_name->p;
1705 const unsigned char *end = p + subject_alt_name->len;
1706 mbedtls_x509_buf cur_oid;
1707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001708 if ((subject_alt_name->tag &
1709 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1710 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001711 /*
1712 * The given subject alternative name is not of type "othername".
1713 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001714 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001715 }
1716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001717 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1718 MBEDTLS_ASN1_OID)) != 0) {
1719 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1720 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001721
1722 cur_oid.tag = MBEDTLS_ASN1_OID;
1723 cur_oid.p = p;
1724 cur_oid.len = len;
1725
1726 /*
1727 * Only HwModuleName is currently supported.
1728 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001729 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1730 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001731 }
David Horstmann4a46d232023-08-18 18:36:02 +01001732 other_name->type_id = cur_oid;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001733
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001734 p += len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001735 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1736 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1737 0) {
1738 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1739 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001740
hanno-beckerc4f885d2023-02-08 08:50:01 -05001741 if (end != p + len) {
1742 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1743 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1744 }
1745
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001746 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1747 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1748 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1749 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001750
hanno-beckerc4f885d2023-02-08 08:50:01 -05001751 if (end != p + len) {
1752 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1753 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1754 }
1755
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001756 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1757 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1758 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001759
1760 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1761 other_name->value.hardware_module_name.oid.p = p;
1762 other_name->value.hardware_module_name.oid.len = len;
1763
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001764 p += len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001765 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1766 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1767 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1768 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001769
1770 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1771 other_name->value.hardware_module_name.val.p = p;
1772 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001773 p += len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001774 if (p != end) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001775 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1776 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001777 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001778 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001779}
1780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001781static int x509_info_subject_alt_name(char **buf, size_t *size,
1782 const mbedtls_x509_sequence
1783 *subject_alt_name,
1784 const char *prefix)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001785{
Janos Follath865b3eb2019-12-16 11:46:15 +00001786 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -03001787 size_t i;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001788 size_t n = *size;
1789 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001791 mbedtls_x509_subject_alternative_name san;
1792 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001794 while (cur != NULL) {
1795 memset(&san, 0, sizeof(san));
1796 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1797 if (parse_ret != 0) {
1798 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1799 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001800 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001801 } else {
1802 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001803 MBEDTLS_X509_SAFE_SNPRINTF;
1804 }
1805 cur = cur->next;
1806 continue;
1807 }
1808
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001809 switch (san.type) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001810 /*
1811 * otherName
1812 */
Ron Eldora2913912019-05-16 16:17:38 +03001813 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001814 {
1815 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1816
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001817 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001818 MBEDTLS_X509_SAFE_SNPRINTF;
1819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001820 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
David Horstmanndcf73262023-08-18 19:12:59 +01001821 &other_name->type_id) == 0) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001822 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001823 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001824 ret =
1825 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001826 MBEDTLS_X509_SAFE_SNPRINTF;
1827
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001828 ret = mbedtls_oid_get_numeric_string(p,
1829 n,
1830 &other_name->value.hardware_module_name.oid);
Ron Eldor890819a2019-05-13 19:03:04 +03001831 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001833 ret =
1834 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001835 MBEDTLS_X509_SAFE_SNPRINTF;
1836
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001837 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1838 ret = mbedtls_snprintf(p,
1839 n,
1840 "%02X",
1841 other_name->value.hardware_module_name.val.p[i]);
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -03001842 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath22f605f2019-05-10 10:37:17 +01001843 }
Ron Eldor890819a2019-05-13 19:03:04 +03001844 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1845 }
1846 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001847
1848 /*
1849 * dNSName
1850 */
Ron Eldora2913912019-05-16 16:17:38 +03001851 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001852 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001853 ret = mbedtls_snprintf(p, n, "\n%s dNSName : ", prefix);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001854 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001855 if (san.san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001856 *p = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001857 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001858 }
Ron Eldora2913912019-05-16 16:17:38 +03001859
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001860 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
Ron Eldora2913912019-05-16 16:17:38 +03001861 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001862 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001863 }
1864 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001865
1866 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001867 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001868 */
1869 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001870 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
Janos Follath22f605f2019-05-10 10:37:17 +01001871 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001872 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001873 }
1874
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001875 cur = cur->next;
1876 }
1877
1878 *p = '\0';
1879
1880 *size = n;
1881 *buf = p;
1882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001883 return 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001884}
1885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001886int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1887 mbedtls_x509_subject_alternative_name *san)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001888{
Janos Follath865b3eb2019-12-16 11:46:15 +00001889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001890 switch (san_buf->tag &
1891 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1892 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
Ron Eldor890819a2019-05-13 19:03:04 +03001893 /*
1894 * otherName
1895 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001896 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001897 {
Ron Eldor890819a2019-05-13 19:03:04 +03001898 mbedtls_x509_san_other_name other_name;
1899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001900 ret = x509_get_other_name(san_buf, &other_name);
1901 if (ret != 0) {
1902 return ret;
1903 }
Ron Eldor890819a2019-05-13 19:03:04 +03001904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001905 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
Ron Eldor890819a2019-05-13 19:03:04 +03001906 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001907 memcpy(&san->san.other_name,
1908 &other_name, sizeof(other_name));
Ron Eldor890819a2019-05-13 19:03:04 +03001909
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001910 }
Ron Eldor890819a2019-05-13 19:03:04 +03001911 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001912
Ron Eldor890819a2019-05-13 19:03:04 +03001913 /*
1914 * dNSName
1915 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001916 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001917 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001918 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
Ron Eldor890819a2019-05-13 19:03:04 +03001919 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001921 memcpy(&san->san.unstructured_name,
1922 san_buf, sizeof(*san_buf));
Janos Follath6c379b42019-05-10 14:17:16 +01001923
Ron Eldor890819a2019-05-13 19:03:04 +03001924 }
1925 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001926
Ron Eldor890819a2019-05-13 19:03:04 +03001927 /*
1928 * Type not supported
1929 */
1930 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001931 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Ron Eldor890819a2019-05-13 19:03:04 +03001932 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001933 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001934}
1935
Demi Marie Obenour0e207412022-12-04 04:24:22 -05001936#define PRINT_ITEM(i) \
1937 do { \
1938 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1939 MBEDTLS_X509_SAFE_SNPRINTF; \
1940 sep = ", "; \
1941 } while (0)
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001942
Demi Marie Obenour0e207412022-12-04 04:24:22 -05001943#define CERT_TYPE(type, name) \
1944 do { \
1945 if (ns_cert_type & (type)) { \
1946 PRINT_ITEM(name); \
1947 } \
1948 } while (0)
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001950static int x509_info_cert_type(char **buf, size_t *size,
1951 unsigned char ns_cert_type)
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001952{
Janos Follath865b3eb2019-12-16 11:46:15 +00001953 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001954 size_t n = *size;
1955 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001956 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001957
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001958 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1959 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1960 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1961 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1962 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1963 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1964 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1965 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001966
1967 *size = n;
1968 *buf = p;
1969
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001970 return 0;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001971}
1972
Demi Marie Obenour0e207412022-12-04 04:24:22 -05001973#define KEY_USAGE(code, name) \
1974 do { \
1975 if (key_usage & (code)) { \
1976 PRINT_ITEM(name); \
1977 } \
1978 } while (0)
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001979
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001980static int x509_info_key_usage(char **buf, size_t *size,
1981 unsigned int key_usage)
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001982{
Janos Follath865b3eb2019-12-16 11:46:15 +00001983 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001984 size_t n = *size;
1985 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001986 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001987
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001988 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1989 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1990 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1991 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1992 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1993 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1994 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1995 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1996 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001997
1998 *size = n;
1999 *buf = p;
2000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002001 return 0;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002002}
2003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002004static int x509_info_ext_key_usage(char **buf, size_t *size,
2005 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002006{
Janos Follath865b3eb2019-12-16 11:46:15 +00002007 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002008 const char *desc;
2009 size_t n = *size;
2010 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002012 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002013
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002014 while (cur != NULL) {
2015 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002016 desc = "???";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002017 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002019 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002020 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002021
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002022 sep = ", ";
2023
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002024 cur = cur->next;
2025 }
2026
2027 *size = n;
2028 *buf = p;
2029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002030 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002031}
2032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002033static int x509_info_cert_policies(char **buf, size_t *size,
2034 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02002035{
Janos Follath865b3eb2019-12-16 11:46:15 +00002036 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002037 const char *desc;
2038 size_t n = *size;
2039 char *p = *buf;
2040 const mbedtls_x509_sequence *cur = certificate_policies;
2041 const char *sep = "";
2042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002043 while (cur != NULL) {
2044 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02002045 desc = "???";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002046 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002048 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002049 MBEDTLS_X509_SAFE_SNPRINTF;
2050
2051 sep = ", ";
2052
2053 cur = cur->next;
2054 }
2055
2056 *size = n;
2057 *buf = p;
2058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002059 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002060}
2061
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002062/*
2063 * Return an informational string about the certificate.
2064 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002065#define BEFORE_COLON 18
2066#define BC "18"
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002067int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
2068 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002069{
Janos Follath865b3eb2019-12-16 11:46:15 +00002070 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002071 size_t n;
2072 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002073 char key_size_str[BEFORE_COLON];
2074
2075 p = buf;
2076 n = size;
2077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002078 if (NULL == crt) {
2079 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01002080 MBEDTLS_X509_SAFE_SNPRINTF;
2081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002082 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01002083 }
2084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002085 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
2086 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002087 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002088 ret = mbedtls_snprintf(p, n, "%sserial number : ",
2089 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002090 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002092 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002093 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002095 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002096 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002097 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002098 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002100 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002101 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002102 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002103 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002105 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
2106 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2107 crt->valid_from.year, crt->valid_from.mon,
2108 crt->valid_from.day, crt->valid_from.hour,
2109 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002110 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002112 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
2113 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2114 crt->valid_to.year, crt->valid_to.mon,
2115 crt->valid_to.day, crt->valid_to.hour,
2116 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002117 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002119 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002120 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002122 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
2123 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002124 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002125
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002126 /* Key size */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002127 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
2128 mbedtls_pk_get_name(&crt->pk))) != 0) {
2129 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002130 }
2131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002132 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
2133 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002134 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002135
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002136 /*
2137 * Optional extensions
2138 */
2139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002140 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
2141 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
2142 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002143 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002145 if (crt->max_pathlen > 0) {
2146 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002147 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002148 }
2149 }
2150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002151 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2152 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002153 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002155 if ((ret = x509_info_subject_alt_name(&p, &n,
2156 &crt->subject_alt_names,
2157 prefix)) != 0) {
2158 return ret;
2159 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002160 }
2161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002162 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
2163 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002164 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002166 if ((ret = x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
2167 return ret;
2168 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002169 }
2170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002171 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
2172 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002173 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002175 if ((ret = x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
2176 return ret;
2177 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002178 }
2179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002180 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
2181 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002182 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002184 if ((ret = x509_info_ext_key_usage(&p, &n,
2185 &crt->ext_key_usage)) != 0) {
2186 return ret;
2187 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002188 }
2189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002190 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
2191 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002192 MBEDTLS_X509_SAFE_SNPRINTF;
2193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002194 if ((ret = x509_info_cert_policies(&p, &n,
2195 &crt->certificate_policies)) != 0) {
2196 return ret;
2197 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002198 }
2199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002200 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002201 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002203 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002204}
2205
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002206struct x509_crt_verify_string {
2207 int code;
2208 const char *string;
2209};
2210
2211static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002212 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002213 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002214 { MBEDTLS_X509_BADCERT_CN_MISMATCH,
2215 "The certificate Common Name (CN) does not match with the expected CN" },
2216 { MBEDTLS_X509_BADCERT_NOT_TRUSTED,
2217 "The certificate is not correctly signed by the trusted CA" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002218 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2219 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002220 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2221 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2222 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002223 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002224 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2225 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2226 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2227 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002228 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002229 { MBEDTLS_X509_BADCERT_BAD_PK,
2230 "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2231 { MBEDTLS_X509_BADCERT_BAD_KEY,
2232 "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002233 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002234 { MBEDTLS_X509_BADCRL_BAD_PK,
2235 "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2236 { MBEDTLS_X509_BADCRL_BAD_KEY,
2237 "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002238 { 0, NULL }
2239};
2240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002241int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
2242 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002243{
Janos Follath865b3eb2019-12-16 11:46:15 +00002244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002245 const struct x509_crt_verify_string *cur;
2246 char *p = buf;
2247 size_t n = size;
2248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002249 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
2250 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002251 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002252 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002254 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002255 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002256 flags ^= cur->code;
2257 }
2258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002259 if (flags != 0) {
2260 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
2261 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002262 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002263 }
2264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002265 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002266}
2267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002269int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
2270 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002271{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002272 unsigned int usage_must, usage_may;
2273 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002274 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002276 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
2277 return 0;
2278 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002279
2280 usage_must = usage & ~may_mask;
2281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002282 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
2283 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2284 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002285
2286 usage_may = usage & may_mask;
2287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002288 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
2289 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2290 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002292 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002293}
2294#endif
2295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002297int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
2298 const char *usage_oid,
2299 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002300{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002302
2303 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002304 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
2305 return 0;
2306 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002307
2308 /*
2309 * Look for the requested usage (or wildcard ANY) in our list
2310 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002311 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002314 if (cur_oid->len == usage_len &&
2315 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2316 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002317 }
2318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002319 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2320 return 0;
2321 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002322 }
2323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002324 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002325}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002329/*
2330 * Return 1 if the certificate is revoked, or 0 otherwise.
2331 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002332int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002336 while (cur != NULL && cur->serial.len != 0) {
2337 if (crt->serial.len == cur->serial.len &&
2338 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2339 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002340 }
2341
2342 cur = cur->next;
2343 }
2344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002345 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002346}
2347
2348/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002349 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002350 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002351 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002352static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2353 mbedtls_x509_crl *crl_list,
2354 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002355{
2356 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2358 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002360 if (ca == NULL) {
2361 return flags;
2362 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002364 while (crl_list != NULL) {
2365 if (crl_list->version == 0 ||
2366 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002367 crl_list = crl_list->next;
2368 continue;
2369 }
2370
2371 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002372 * Check if the CA is configured to sign CRLs
2373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002375 if (mbedtls_x509_crt_check_key_usage(ca,
2376 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002378 break;
2379 }
2380#endif
2381
2382 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002383 * Check if CRL is correctly signed by the trusted CA
2384 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002385 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002386 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002387 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002389 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002390 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002391 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002393 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2394 if (mbedtls_md(md_info, crl_list->tbs.p, crl_list->tbs.len, hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002395 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002397 break;
2398 }
2399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002400 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002401 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002402 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002403
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002404 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2405 crl_list->sig_md, hash, mbedtls_md_get_size(md_info),
2406 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002408 break;
2409 }
2410
2411 /*
2412 * Check for validity of CRL (Do not drop out)
2413 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002414 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002416 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002418 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002419 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002420 }
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002421
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002422 /*
2423 * Check if certificate is revoked
2424 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002425 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002427 break;
2428 }
2429
2430 crl_list = crl_list->next;
2431 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002433 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002434}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002436
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002437/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002438 * Check the signature of a certificate by its parent
2439 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002440static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2441 mbedtls_x509_crt *parent,
2442 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002443{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002444 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002445 size_t hash_len;
2446#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2447 const mbedtls_md_info_t *md_info;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002448 md_info = mbedtls_md_info_from_type(child->sig_md);
2449 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002450
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002451 /* Note: hash errors can happen only after an internal error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002452 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2453 return -1;
2454 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002455#else
Jaeden Amero34973232019-02-20 10:32:28 +00002456 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002457 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(child->sig_md);
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002459 if (psa_hash_setup(&hash_operation, hash_alg) != PSA_SUCCESS) {
2460 return -1;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002461 }
2462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002463 if (psa_hash_update(&hash_operation, child->tbs.p, child->tbs.len)
2464 != PSA_SUCCESS) {
2465 return -1;
2466 }
2467
2468 if (psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_len)
2469 != PSA_SUCCESS) {
2470 return -1;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002471 }
2472#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002473 /* Skip expensive computation on obvious mismatch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002474 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2475 return -1;
2476 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002477
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002478#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002479 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2480 return mbedtls_pk_verify_restartable(&parent->pk,
2481 child->sig_md, hash, hash_len,
2482 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002483 }
2484#else
2485 (void) rs_ctx;
2486#endif
2487
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002488 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2489 child->sig_md, hash, hash_len,
2490 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002491}
2492
2493/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002494 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2495 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002496 *
2497 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002498 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002499static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2500 const mbedtls_x509_crt *parent,
2501 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002502{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002503 int need_ca_bit;
2504
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002505 /* Parent must be the issuer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002506 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2507 return -1;
2508 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002509
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002510 /* Parent must have the basicConstraints CA bit set as a general rule */
2511 need_ca_bit = 1;
2512
2513 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002514 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002515 need_ca_bit = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002516 }
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002518 if (need_ca_bit && !parent->ca_istrue) {
2519 return -1;
2520 }
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002523 if (need_ca_bit &&
2524 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2525 return -1;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002526 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002527#endif
2528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002529 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002530}
2531
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002532/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002533 * Find a suitable parent for child in candidates, or return NULL.
2534 *
2535 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002536 * 1. subject name matches child's issuer
2537 * 2. if necessary, the CA bit is set and key usage allows signing certs
2538 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002539 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002540 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002541 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002542 * If there's a suitable candidate which is also time-valid, return the first
2543 * such. Otherwise, return the first suitable candidate (or NULL if there is
2544 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002545 *
2546 * The rationale for this rule is that someone could have a list of trusted
2547 * roots with two versions on the same root with different validity periods.
2548 * (At least one user reported having such a list and wanted it to just work.)
2549 * The reason we don't just require time-validity is that generally there is
2550 * only one version, and if it's expired we want the flags to state that
2551 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002552 *
2553 * The rationale for rule 3 (signature for trusted roots) is that users might
2554 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002555 * way we select the correct one is by checking the signature (as we don't
2556 * rely on key identifier extensions). (This is one way users might choose to
2557 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002558 *
2559 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002560 * - [in] child: certificate for which we're looking for a parent
2561 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002562 * - [out] r_parent: parent found (or NULL)
2563 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002564 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2565 * of the chain, 0 otherwise
2566 * - [in] path_cnt: number of intermediates seen so far
2567 * - [in] self_cnt: number of self-signed intermediates seen so far
2568 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002569 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002570 *
2571 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002572 * - 0 on success
2573 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002574 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002575static int x509_crt_find_parent_in(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002576 mbedtls_x509_crt *child,
2577 mbedtls_x509_crt *candidates,
2578 mbedtls_x509_crt **r_parent,
2579 int *r_signature_is_good,
2580 int top,
2581 unsigned path_cnt,
2582 unsigned self_cnt,
2583 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002584{
Janos Follath865b3eb2019-12-16 11:46:15 +00002585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002586 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002587 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002588
2589#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002590 /* did we have something in progress? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002591 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002592 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002593 parent = rs_ctx->parent;
2594 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002595 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002596
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002597 /* clear saved state */
2598 rs_ctx->parent = NULL;
2599 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002600 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002601
2602 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002603 goto check_signature;
2604 }
2605#endif
2606
2607 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002608 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002610 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002611 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002612 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002613 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002614 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002615
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002616 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002617 if (parent->max_pathlen > 0 &&
2618 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002619 continue;
2620 }
2621
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002622 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002623#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2624check_signature:
2625#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002626 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002627
2628#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002629 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002630 /* save state */
2631 rs_ctx->parent = parent;
2632 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002633 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002635 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002636 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002637#else
2638 (void) ret;
2639#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002640
2641 signature_is_good = ret == 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002642 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002643 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002644 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002645
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002646 /* optional time check */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002647 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2648 mbedtls_x509_time_is_future(&parent->valid_from)) {
2649 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002650 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002651 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002652 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002653
2654 continue;
2655 }
2656
Andy Gross1f627142019-01-30 10:25:53 -06002657 *r_parent = parent;
2658 *r_signature_is_good = signature_is_good;
2659
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002660 break;
2661 }
2662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002663 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002664 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002665 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002666 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002668 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002669}
2670
2671/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002672 * Find a parent in trusted CAs or the provided chain, or return NULL.
2673 *
2674 * Searches in trusted CAs first, and return the first suitable parent found
2675 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002676 *
2677 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002678 * - [in] child: certificate for which we're looking for a parent, followed
2679 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002680 * - [in] trust_ca: list of locally trusted certificates
2681 * - [out] parent: parent found (or NULL)
2682 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2683 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2684 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2685 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002686 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002687 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002688 *
2689 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002690 * - 0 on success
2691 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002692 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002693static int x509_crt_find_parent(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002694 mbedtls_x509_crt *child,
2695 mbedtls_x509_crt *trust_ca,
2696 mbedtls_x509_crt **parent,
2697 int *parent_is_trusted,
2698 int *signature_is_good,
2699 unsigned path_cnt,
2700 unsigned self_cnt,
2701 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002702{
Janos Follath865b3eb2019-12-16 11:46:15 +00002703 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002704 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002705
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002706 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002707
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002708#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002709 /* restore then clear saved state if we have some stored */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002710 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002711 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002712 rs_ctx->parent_is_trusted = -1;
2713 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002714#endif
2715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002716 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002717 search_list = *parent_is_trusted ? trust_ca : child->next;
2718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002719 ret = x509_crt_find_parent_in(child, search_list,
2720 parent, signature_is_good,
2721 *parent_is_trusted,
2722 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002723
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002724#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002725 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002726 /* save state */
2727 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002728 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002729 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002730#else
2731 (void) ret;
2732#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002733
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002734 /* stop here if found or already in second iteration */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002735 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002736 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002737 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002738
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002739 /* prepare second iteration */
2740 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002741 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002742
2743 /* extra precaution against mistakes in the caller */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002744 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002745 *parent_is_trusted = 0;
2746 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002747 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002749 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002750}
2751
2752/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002753 * Check if an end-entity certificate is locally trusted
2754 *
2755 * Currently we require such certificates to be self-signed (actually only
2756 * check for self-issued as self-signatures are not checked)
2757 */
2758static int x509_crt_check_ee_locally_trusted(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002759 mbedtls_x509_crt *crt,
2760 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002761{
2762 mbedtls_x509_crt *cur;
2763
2764 /* must be self-issued */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002765 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2766 return -1;
2767 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002768
2769 /* look for an exact match with trusted cert */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002770 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2771 if (crt->raw.len == cur->raw.len &&
2772 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2773 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002774 }
2775 }
2776
2777 /* too bad */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002778 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002779}
2780
2781/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002782 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002783 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002784 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2785 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002786 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002787 * such that every cert in the chain is a child of the next one,
2788 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002789 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002790 * Verify that chain and return it with flags for all issues found.
2791 *
2792 * Special cases:
2793 * - EE == Rj -> return a one-element list containing it
2794 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2795 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002796 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002797 * Tests for (aspects of) this function should include at least:
2798 * - trusted EE
2799 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002800 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002801 * - if relevant: EE untrusted
2802 * - if relevant: EE -> intermediate, untrusted
2803 * with the aspect under test checked at each relevant level (EE, int, root).
2804 * For some aspects longer chains are required, but usually length 2 is
2805 * enough (but length 1 is not in general).
2806 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002807 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002808 * - [in] crt: the cert list EE, C1, ..., Cn
2809 * - [in] trust_ca: the trusted list R1, ..., Rp
2810 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002811 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002812 * Only valid when return value is 0, may contain garbage otherwise!
2813 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002814 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002815 *
2816 * Return value:
2817 * - non-zero if the chain could not be fully built and examined
2818 * - 0 is the chain was successfully built and examined,
2819 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002820 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002821static int x509_crt_verify_chain(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002822 mbedtls_x509_crt *crt,
2823 mbedtls_x509_crt *trust_ca,
2824 mbedtls_x509_crl *ca_crl,
2825 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2826 void *p_ca_cb,
2827 const mbedtls_x509_crt_profile *profile,
2828 mbedtls_x509_crt_verify_chain *ver_chain,
2829 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002830{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002831 /* Don't initialize any of those variables here, so that the compiler can
2832 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002833 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002834 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002835 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002836 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002837 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002838 int parent_is_trusted;
2839 int child_is_trusted;
2840 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002841 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002842 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002843
2844#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2845 /* resume if we had an operation in progress */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002846 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002847 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002848 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002849 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002850
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002851 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002852 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002853 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002854 flags = &cur->flags;
2855
2856 goto find_parent;
2857 }
2858#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002859
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002860 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002861 self_cnt = 0;
2862 parent_is_trusted = 0;
2863 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002865 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002866 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002867 cur = &ver_chain->items[ver_chain->len];
2868 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002869 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002870 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002871 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002872
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002873 /* Check time-validity (all certificates) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002874 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002875 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002876 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002878 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002879 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002880 }
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002881
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002882 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002883 if (child_is_trusted) {
2884 return 0;
2885 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002886
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002887 /* Check signature algorithm: MD & PK algs */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002888 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002889 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002890 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002891
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002892 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002893 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002894 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002895
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002896 /* Special case: EE certs that are locally trusted */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002897 if (ver_chain->len == 1 &&
2898 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2899 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002900 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002901
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002902#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2903find_parent:
2904#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002905
2906 /* Obtain list of potential trusted signers from CA callback,
2907 * or use statically provided list. */
2908#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002909 if (f_ca_cb != NULL) {
2910 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2911 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002912 ver_chain->trust_ca_cb_result = NULL;
2913
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002914 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2915 if (ret != 0) {
2916 return MBEDTLS_ERR_X509_FATAL_ERROR;
2917 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002918
2919 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002920 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002921#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2922 {
2923 ((void) f_ca_cb);
2924 ((void) p_ca_cb);
2925 cur_trust_ca = trust_ca;
2926 }
2927
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002928 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002929 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2930 &parent_is_trusted, &signature_is_good,
2931 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002932
2933#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002934 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002935 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002936 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002937 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002938 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002940 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002941 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002942#else
2943 (void) ret;
2944#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002945
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002946 /* No parent? We're done here */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002947 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002948 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002949 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002950 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002951
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002952 /* Count intermediate self-issued (not necessarily self-signed) certs.
2953 * These can occur with some strategies for key rollover, see [SIRO],
2954 * and should be excluded from max_pathlen checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002955 if (ver_chain->len != 1 &&
2956 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002957 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002958 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002959
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002960 /* path_cnt is 0 for the first intermediate CA,
2961 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002962 if (!parent_is_trusted &&
2963 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002964 /* return immediately to avoid overflow the chain array */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002965 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002966 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002967
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002968 /* signature was checked while searching parent */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002969 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002970 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002971 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002972
2973 /* check size of signing key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002974 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002975 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002976 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002979 /* Check trusted CA's CRL for the given crt */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002980 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002981#else
2982 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002983#endif
2984
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002985 /* prepare for next iteration */
2986 child = parent;
2987 parent = NULL;
2988 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002989 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002990 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002991}
2992
2993/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002994 * Check for CN match
2995 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002996static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2997 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002998{
2999 /* try exact match */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003000 if (name->len == cn_len &&
3001 x509_memcasecmp(cn, name->p, cn_len) == 0) {
3002 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003003 }
3004
3005 /* try wildcard match */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003006 if (x509_check_wildcard(cn, name) == 0) {
3007 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003008 }
3009
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003010 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003011}
3012
3013/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003014 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3015 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003016static int x509_crt_check_san(const mbedtls_x509_buf *name,
3017 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003018{
3019 const unsigned char san_type = (unsigned char) name->tag &
3020 MBEDTLS_ASN1_TAG_VALUE_MASK;
3021
3022 /* dNSName */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003023 if (san_type == MBEDTLS_X509_SAN_DNS_NAME) {
3024 return x509_crt_check_cn(name, cn, cn_len);
3025 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003026
3027 /* (We may handle other types here later.) */
3028
3029 /* Unrecognized type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003030 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003031}
3032
3033/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003034 * Verify the requested CN - only call this if cn is not NULL!
3035 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003036static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3037 const char *cn,
3038 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003039{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003040 const mbedtls_x509_name *name;
3041 const mbedtls_x509_sequence *cur;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003042 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003044 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
3045 for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) {
3046 if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0) {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003047 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003048 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003049 }
3050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003051 if (cur == NULL) {
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003052 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003053 }
3054 } else {
3055 for (name = &crt->subject; name != NULL; name = name->next) {
3056 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3057 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
3058 break;
3059 }
3060 }
3061
3062 if (name == NULL) {
3063 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3064 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003065 }
3066}
3067
3068/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003069 * Merge the flags for all certs in the chain, after calling callback
3070 */
3071static int x509_crt_merge_flags_with_cb(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003072 uint32_t *flags,
3073 const mbedtls_x509_crt_verify_chain *ver_chain,
3074 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3075 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003076{
Janos Follath865b3eb2019-12-16 11:46:15 +00003077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003078 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003079 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003080 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003083 cur = &ver_chain->items[i-1];
3084 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003086 if (NULL != f_vrfy) {
3087 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3088 return ret;
3089 }
3090 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003091
3092 *flags |= cur_flags;
3093 }
3094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003095 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003096}
3097
3098/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003099 * Verify the certificate validity, with profile, restartable version
3100 *
3101 * This function:
3102 * - checks the requested CN (if any)
3103 * - checks the type and size of the EE cert's key,
3104 * as that isn't done as part of chain building/verification currently
3105 * - builds and verifies the chain
3106 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003107 *
3108 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3109 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3110 * verification routine to search for trusted signers, and CRLs will
3111 * be disabled. Otherwise, `trust_ca` will be used as the static list
3112 * of trusted signers, and `ca_crl` will be use as the static list
3113 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003114 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003115static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3116 mbedtls_x509_crt *trust_ca,
3117 mbedtls_x509_crl *ca_crl,
3118 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3119 void *p_ca_cb,
3120 const mbedtls_x509_crt_profile *profile,
3121 const char *cn, uint32_t *flags,
3122 int (*f_vrfy)(void *,
3123 mbedtls_x509_crt *,
3124 int,
3125 uint32_t *),
3126 void *p_vrfy,
3127 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003128{
Janos Follath865b3eb2019-12-16 11:46:15 +00003129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003130 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003131 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003132 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003133
3134 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003135 ee_flags = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003136 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003138 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003139 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3140 goto exit;
3141 }
3142
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003143 /* check name if requested */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003144 if (cn != NULL) {
3145 x509_crt_verify_name(crt, cn, &ee_flags);
3146 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003147
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003148 /* Check the type and size of the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003149 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003151 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003152 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003153 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003155 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003156 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003157 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003158
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003159 /* Check the chain */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003160 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3161 f_ca_cb, p_ca_cb, profile,
3162 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003164 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003165 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003166 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003167
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003168 /* Merge end-entity flags */
3169 ver_chain.items[0].flags |= ee_flags;
3170
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003171 /* Build final flags, calling callback on the way if any */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003172 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003173
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003174exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003175
3176#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003177 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3178 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003179 ver_chain.trust_ca_cb_result = NULL;
3180#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3181
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003182#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003183 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3184 mbedtls_x509_crt_restart_free(rs_ctx);
3185 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003186#endif
3187
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003188 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3189 * the SSL module for authmode optional, but non-zero return from the
3190 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003191 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003192 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003193 }
3194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003195 if (ret != 0) {
3196 *flags = (uint32_t) -1;
3197 return ret;
3198 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003200 if (*flags != 0) {
3201 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3202 }
3203
3204 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003205}
3206
Hanno Becker3116fb32019-03-28 13:34:42 +00003207
3208/*
3209 * Verify the certificate validity (default profile, not restartable)
3210 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003211int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3212 mbedtls_x509_crt *trust_ca,
3213 mbedtls_x509_crl *ca_crl,
3214 const char *cn, uint32_t *flags,
3215 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3216 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003217{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003218 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3219 NULL, NULL,
3220 &mbedtls_x509_crt_profile_default,
3221 cn, flags,
3222 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003223}
3224
3225/*
3226 * Verify the certificate validity (user-chosen profile, not restartable)
3227 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003228int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3229 mbedtls_x509_crt *trust_ca,
3230 mbedtls_x509_crl *ca_crl,
3231 const mbedtls_x509_crt_profile *profile,
3232 const char *cn, uint32_t *flags,
3233 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3234 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003235{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003236 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3237 NULL, NULL,
3238 profile, cn, flags,
3239 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003240}
3241
3242#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3243/*
3244 * Verify the certificate validity (user-chosen profile, CA callback,
3245 * not restartable).
3246 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003247int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3248 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3249 void *p_ca_cb,
3250 const mbedtls_x509_crt_profile *profile,
3251 const char *cn, uint32_t *flags,
3252 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3253 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003254{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003255 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3256 f_ca_cb, p_ca_cb,
3257 profile, cn, flags,
3258 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003259}
3260#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003262int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3263 mbedtls_x509_crt *trust_ca,
3264 mbedtls_x509_crl *ca_crl,
3265 const mbedtls_x509_crt_profile *profile,
3266 const char *cn, uint32_t *flags,
3267 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3268 void *p_vrfy,
3269 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003270{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003271 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3272 NULL, NULL,
3273 profile, cn, flags,
3274 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003275}
3276
3277
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003278/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003279 * Initialize a certificate chain
3280 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003281void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003282{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003283 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003284}
3285
3286/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003287 * Unallocate all certificate data
3288 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003289void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003290{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291 mbedtls_x509_crt *cert_cur = crt;
3292 mbedtls_x509_crt *cert_prv;
3293 mbedtls_x509_name *name_cur;
3294 mbedtls_x509_name *name_prv;
3295 mbedtls_x509_sequence *seq_cur;
3296 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003298 if (crt == NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003299 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003300 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003302 do {
3303 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003306 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003307#endif
3308
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003309 name_cur = cert_cur->issuer.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003310 while (name_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003311 name_prv = name_cur;
3312 name_cur = name_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003313 mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
3314 mbedtls_free(name_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003315 }
3316
3317 name_cur = cert_cur->subject.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003318 while (name_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003319 name_prv = name_cur;
3320 name_cur = name_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003321 mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
3322 mbedtls_free(name_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003323 }
3324
3325 seq_cur = cert_cur->ext_key_usage.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003326 while (seq_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003327 seq_prv = seq_cur;
3328 seq_cur = seq_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003329 mbedtls_platform_zeroize(seq_prv,
3330 sizeof(mbedtls_x509_sequence));
3331 mbedtls_free(seq_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003332 }
3333
3334 seq_cur = cert_cur->subject_alt_names.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003335 while (seq_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003336 seq_prv = seq_cur;
3337 seq_cur = seq_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003338 mbedtls_platform_zeroize(seq_prv,
3339 sizeof(mbedtls_x509_sequence));
3340 mbedtls_free(seq_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003341 }
3342
Ron Eldor74d9acc2019-03-21 14:00:03 +02003343 seq_cur = cert_cur->certificate_policies.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003344 while (seq_cur != NULL) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02003345 seq_prv = seq_cur;
3346 seq_cur = seq_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003347 mbedtls_platform_zeroize(seq_prv,
3348 sizeof(mbedtls_x509_sequence));
3349 mbedtls_free(seq_prv);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003350 }
3351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003352 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3353 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
3354 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003355 }
3356
3357 cert_cur = cert_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003358 } while (cert_cur != NULL);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003359
3360 cert_cur = crt;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003361 do {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003362 cert_prv = cert_cur;
3363 cert_cur = cert_cur->next;
3364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003365 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3366 if (cert_prv != crt) {
3367 mbedtls_free(cert_prv);
3368 }
3369 } while (cert_cur != NULL);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003370}
3371
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003372#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3373/*
3374 * Initialize a restart context
3375 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003376void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003377{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003378 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003379
3380 ctx->parent = NULL;
3381 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003382 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003383
3384 ctx->parent_is_trusted = -1;
3385
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003386 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003387 ctx->self_cnt = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003388 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003389}
3390
3391/*
3392 * Free the components of a restart context
3393 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003394void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003395{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003396 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003397 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003398 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003400 mbedtls_pk_restart_free(&ctx->pk);
3401 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003402}
3403#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405#endif /* MBEDTLS_X509_CRT_PARSE_C */