blob: 00f310739a0f68e2c9d4a04626572d6f2fc25b23 [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 Rodgman16799db2023-11-02 19:47:20 +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
Harry Ramsey0f6bc412024-10-04 10:36:54 +010020#include "x509_internal.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"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020037#include "psa_util_internal.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010038#include "mbedtls/psa_util.h"
pespacek7599a772022-02-07 14:40:55 +010039#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti3f00b842023-05-15 12:57:06 +020040#include "pk_internal.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010046#endif
47
Daniel Axtensf0710242020-05-28 11:43:41 +100048#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010049#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Sergey Markelov4ed0fde2024-08-14 15:06:03 -070050#ifndef WIN32_LEAN_AND_MEAN
Glenn Straussc26bd762022-10-23 19:48:18 -040051#define WIN32_LEAN_AND_MEAN
Sergey Markelov4ed0fde2024-08-14 15:06:03 -070052#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#include <windows.h>
54#else
55#include <time.h>
56#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100057#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000060#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020061#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#include <sys/types.h>
63#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020064#if defined(__MBED__)
65#include <platform/mbed_retarget.h>
66#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020068#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060069#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000070#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071#endif
72
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020073/*
74 * Item in a verification chain: cert and flags for it
75 */
76typedef struct {
77 mbedtls_x509_crt *crt;
78 uint32_t flags;
79} x509_crt_verify_chain_item;
80
81/*
82 * Max size of verification chain: end-entity + intermediates + trusted root
83 */
Gilles Peskine449bd832023-01-11 14:50:10 +010084#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020085
Gilles Peskineffb92da2021-06-02 00:03:26 +020086/* Default profile. Do not remove items unless there are serious security
87 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020088const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
89{
Gilles Peskineae270bf2021-06-02 00:05:29 +020090 /* Hashes from SHA-256 and above. Note that this selection
91 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +010092 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
93 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
94 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020095 0xFFFFFFF, /* Any PK alg */
Elena Uziunaite8dde3b32024-07-05 12:10:21 +010096#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskineae270bf2021-06-02 00:05:29 +020097 /* Curves at or above 128-bit security level. Note that this selection
98 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +010099 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
100 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
101 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
102 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
103 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
104 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
Gilles Peskine39957502021-06-17 23:17:52 +0200105 0,
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100106#else /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200107 0,
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100108#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200109 2048,
110};
111
Gilles Peskineffb92da2021-06-02 00:03:26 +0200112/* Next-generation profile. Currently identical to the default, but may
113 * be tightened at any time. */
114const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200115{
116 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
118 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
119 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200120 0xFFFFFFF, /* Any PK alg */
121#if defined(MBEDTLS_ECP_C)
122 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
124 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
125 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
126 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
127 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
128 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
129 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200130#else
131 0,
132#endif
133 2048,
134};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200135
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200136/*
137 * NSA Suite B Profile
138 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200139const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
140{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200141 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
143 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200144 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
146 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100147#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200148 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
150 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100151#else /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200152 0,
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100153#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200154 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200155};
156
157/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200158 * Empty / all-forbidden profile
159 */
160const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
161{
162 0,
163 0,
164 0,
165 (uint32_t) -1,
166};
167
168/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200169 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200170 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200171 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100172static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
173 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200174{
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (md_alg == MBEDTLS_MD_NONE) {
176 return -1;
177 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
180 return 0;
181 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200184}
185
186/*
187 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200188 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200189 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
191 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200192{
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (pk_alg == MBEDTLS_PK_NONE) {
194 return -1;
195 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
198 return 0;
199 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200202}
203
204/*
205 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200206 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200207 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100208static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
209 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200210{
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200212
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200213#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
215 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
216 return 0;
217 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200220 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200221#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200222
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100223#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200225 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 pk_alg == MBEDTLS_PK_ECKEY_DH) {
Valerio Settif9362b72023-11-29 08:42:27 +0100227 const mbedtls_ecp_group_id gid = mbedtls_pk_get_ec_group_id(pk);
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 if (gid == MBEDTLS_ECP_DP_NONE) {
230 return -1;
231 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
234 return 0;
235 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200238 }
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100239#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200242}
243
244/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000245 * Like memcmp, but case-insensitive and always returns -1 if different
246 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000248{
249 size_t i;
250 unsigned char diff;
251 const unsigned char *n1 = s1, *n2 = s2;
252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000254 diff = n1[i] ^ n2[i];
255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000257 continue;
258 }
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (diff == 32 &&
261 ((n1[i] >= 'a' && n1[i] <= 'z') ||
262 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
263 continue;
264 }
265
266 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000267 }
268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000270}
271
272/*
273 * Return 0 if name matches wildcard, -1 otherwise
274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000276{
277 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000279
280 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
282 return -1;
283 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 for (i = 0; i < cn_len; ++i) {
286 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000287 cn_idx = i;
288 break;
289 }
290 }
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (cn_idx == 0) {
293 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000294 }
295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (cn_len - cn_idx == name->len - 1 &&
297 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
298 return 0;
299 }
300
301 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000302}
303
304/*
305 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
306 * variations (but not all).
307 *
308 * Return 0 if equal, -1 otherwise.
309 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000311{
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000313 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 memcmp(a->p, b->p, b->len) == 0) {
315 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000316 }
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
319 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000320 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 x509_memcasecmp(a->p, b->p, b->len) == 0) {
322 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000323 }
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000326}
327
328/*
329 * Compare two X.509 Names (aka rdnSequence).
330 *
331 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
332 * we sometimes return unequal when the full algorithm would return equal,
333 * but never the other way. (In particular, we don't do Unicode normalisation
334 * or space folding.)
335 *
336 * Return 0 if equal, -1 otherwise.
337 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100338static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000339{
340 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 while (a != NULL || b != NULL) {
342 if (a == NULL || b == NULL) {
343 return -1;
344 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000345
346 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000348 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
350 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000351 }
352
353 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (x509_string_cmp(&a->val, &b->val) != 0) {
355 return -1;
356 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000357
358 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (a->next_merged != b->next_merged) {
360 return -1;
361 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000362
363 a = a->next;
364 b = b->next;
365 }
366
367 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000369}
370
371/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200372 * Reset (init or clear) a verify_chain
373 */
374static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200376{
377 size_t i;
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200380 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000381 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200382 }
383
384 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000385
386#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
387 ver_chain->trust_ca_cb_result = NULL;
388#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200389}
390
391/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
393 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100394static int x509_get_version(unsigned char **p,
395 const unsigned char *end,
396 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397{
Janos Follath865b3eb2019-12-16 11:46:15 +0000398 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399 size_t len;
400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
402 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
403 0)) != 0) {
404 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407 }
408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410 }
411
412 end = *p + len;
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
415 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
416 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (*p != end) {
419 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
420 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
421 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424}
425
426/*
427 * Validity ::= SEQUENCE {
428 * notBefore Time,
429 * notAfter Time }
430 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100431static int x509_get_dates(unsigned char **p,
432 const unsigned char *end,
433 mbedtls_x509_time *from,
434 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435{
Janos Follath865b3eb2019-12-16 11:46:15 +0000436 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437 size_t len;
438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
440 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
441 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
442 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
444 end = *p + len;
445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
447 return ret;
448 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
451 return ret;
452 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (*p != end) {
455 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
456 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
457 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460}
461
462/*
463 * X.509 v2/v3 unique identifier (not parsed)
464 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100465static int x509_get_uid(unsigned char **p,
466 const unsigned char *end,
467 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468{
Janos Follath865b3eb2019-12-16 11:46:15 +0000469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (*p == end) {
472 return 0;
473 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
475 uid->tag = **p;
476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
478 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
479 n)) != 0) {
480 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
481 return 0;
482 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 }
486
487 uid->p = *p;
488 *p += uid->len;
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491}
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493static int x509_get_basic_constraints(unsigned char **p,
494 const unsigned char *end,
495 int *ca_istrue,
496 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497{
Janos Follath865b3eb2019-12-16 11:46:15 +0000498 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499 size_t len;
500
501 /*
502 * BasicConstraints ::= SEQUENCE {
503 * cA BOOLEAN DEFAULT FALSE,
504 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
505 */
506 *ca_istrue = 0; /* DEFAULT FALSE */
507 *max_pathlen = 0; /* endless */
508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
510 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
511 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512 }
513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (*p == end) {
515 return 0;
516 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
519 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
520 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
521 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (ret != 0) {
524 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
525 }
526
527 if (*ca_istrue != 0) {
528 *ca_istrue = 1;
529 }
530 }
531
532 if (*p == end) {
533 return 0;
534 }
535
536 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
537 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
538 }
539
540 if (*p != end) {
541 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
542 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
543 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544
Andrzej Kurek16050742020-04-14 09:49:52 -0400545 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
546 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (*max_pathlen == INT_MAX) {
548 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
549 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
550 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400551
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552 (*max_pathlen)++;
553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555}
556
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557/*
558 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
559 *
560 * KeyPurposeId ::= OBJECT IDENTIFIER
561 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562static int x509_get_ext_key_usage(unsigned char **p,
563 const unsigned char *end,
564 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565{
Janos Follath865b3eb2019-12-16 11:46:15 +0000566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
569 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
570 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571
572 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 if (ext_key_usage->buf.p == NULL) {
574 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
575 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
576 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579}
580
581/*
toth92ga41954d2021-02-12 16:11:17 +0100582 * SubjectKeyIdentifier ::= KeyIdentifier
583 *
584 * KeyIdentifier ::= OCTET STRING
585 */
586static int x509_get_subject_key_id(unsigned char **p,
587 const unsigned char *end,
588 mbedtls_x509_buf *subject_key_id)
589{
590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
591 size_t len = 0u;
592
593 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
594 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100595 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92ga41954d2021-02-12 16:11:17 +0100596 }
597
Przemek Stekiel61aed062023-05-08 11:14:36 +0200598 subject_key_id->len = len;
599 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
600 subject_key_id->p = *p;
601 *p += len;
602
toth92gd96027a2021-04-27 15:41:25 +0200603 if (*p != end) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100604 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
605 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92gd96027a2021-04-27 15:41:25 +0200606 }
607
toth92ga41954d2021-02-12 16:11:17 +0100608 return 0;
609}
610
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100611/*
toth92g8d435a02021-05-10 15:16:33 +0200612 * AuthorityKeyIdentifier ::= SEQUENCE {
613 * keyIdentifier [0] KeyIdentifier OPTIONAL,
614 * authorityCertIssuer [1] GeneralNames OPTIONAL,
615 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
616 *
617 * KeyIdentifier ::= OCTET STRING
618 */
619static int x509_get_authority_key_id(unsigned char **p,
620 unsigned char *end,
621 mbedtls_x509_authority *authority_key_id)
622{
623 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
624 size_t len = 0u;
625
626 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100627 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100628 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200629 }
630
Przemek Stekiel6ec839a2023-02-01 11:06:08 +0100631 if (*p + len != end) {
632 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
633 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
634 }
635
Przemek Stekieled9fb782023-05-03 16:27:25 +0200636 ret = mbedtls_asn1_get_tag(p, end, &len,
637 MBEDTLS_ASN1_CONTEXT_SPECIFIC);
638
639 /* KeyIdentifier is an OPTIONAL field */
Przemek Stekiel61aed062023-05-08 11:14:36 +0200640 if (ret == 0) {
toth92g8d435a02021-05-10 15:16:33 +0200641 authority_key_id->keyIdentifier.len = len;
642 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200643 /* Setting tag of the keyIdentfier intentionally to 0x04.
644 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
Przemek Stekiel9a7a7252023-04-17 16:06:57 +0200645 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200646 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
647
648 *p += len;
Przemek Stekiel61aed062023-05-08 11:14:36 +0200649 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
650 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200651 }
652
653 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200654 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200655 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
656 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100657 1)) != 0) {
Przemek Stekielf5b8f782023-04-26 08:55:26 +0200658 /* authorityCertIssuer and authorityCertSerialNumber MUST both
659 be present or both be absent. At this point we expect to have both. */
660 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200661 }
Przemek Stekieled9fb782023-05-03 16:27:25 +0200662 /* "end" also includes the CertSerialNumber field so "len" shall be used */
663 ret = mbedtls_x509_get_subject_alt_name_ext(p,
664 (*p+len),
665 &authority_key_id->authorityCertIssuer);
666 if (ret != 0) {
667 return ret;
668 }
669
670 /* Getting authorityCertSerialNumber using the required specific class tag [2] */
671 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
672 MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) {
673 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
674 }
675 authority_key_id->authorityCertSerialNumber.len = len;
676 authority_key_id->authorityCertSerialNumber.p = *p;
677 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER;
678 *p += len;
toth92g8d435a02021-05-10 15:16:33 +0200679 }
680
681 if (*p != end) {
682 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
683 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
684 }
685
686 return 0;
687}
688
689/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200690 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
691 *
692 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
693 *
694 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
695 *
696 * PolicyInformation ::= SEQUENCE {
697 * policyIdentifier CertPolicyId,
698 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
699 * PolicyQualifierInfo OPTIONAL }
700 *
701 * CertPolicyId ::= OBJECT IDENTIFIER
702 *
703 * PolicyQualifierInfo ::= SEQUENCE {
704 * policyQualifierId PolicyQualifierId,
705 * qualifier ANY DEFINED BY policyQualifierId }
706 *
707 * -- policyQualifierIds for Internet policy qualifiers
708 *
709 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
710 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
711 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
712 *
713 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
714 *
715 * Qualifier ::= CHOICE {
716 * cPSuri CPSuri,
717 * userNotice UserNotice }
718 *
719 * CPSuri ::= IA5String
720 *
721 * UserNotice ::= SEQUENCE {
722 * noticeRef NoticeReference OPTIONAL,
723 * explicitText DisplayText OPTIONAL }
724 *
725 * NoticeReference ::= SEQUENCE {
726 * organization DisplayText,
727 * noticeNumbers SEQUENCE OF INTEGER }
728 *
729 * DisplayText ::= CHOICE {
730 * ia5String IA5String (SIZE (1..200)),
731 * visibleString VisibleString (SIZE (1..200)),
732 * bmpString BMPString (SIZE (1..200)),
733 * utf8String UTF8String (SIZE (1..200)) }
734 *
735 * NOTE: we only parse and use anyPolicy without qualifiers at this point
736 * as defined in RFC 5280.
737 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100738static int x509_get_certificate_policies(unsigned char **p,
739 const unsigned char *end,
740 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200741{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300742 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200743 size_t len;
744 mbedtls_asn1_buf *buf;
745 mbedtls_asn1_sequence *cur = certificate_policies;
746
747 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 ret = mbedtls_asn1_get_tag(p, end, &len,
749 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
750 if (ret != 0) {
751 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
752 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if (*p + len != end) {
755 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
756 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
757 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200758
759 /*
760 * Cannot be an empty sequence.
761 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if (len == 0) {
763 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
764 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
765 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200768 mbedtls_x509_buf policy_oid;
769 const unsigned char *policy_end;
770
771 /*
772 * Get the policy sequence
773 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
775 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
776 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
777 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200778
779 policy_end = *p + len;
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
782 MBEDTLS_ASN1_OID)) != 0) {
783 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
784 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200785
786 policy_oid.tag = MBEDTLS_ASN1_OID;
787 policy_oid.len = len;
788 policy_oid.p = *p;
789
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300790 /*
791 * Only AnyPolicy is currently supported when enforcing policy.
792 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300794 /*
795 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200796 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300797 */
798 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
799 }
800
Ron Eldor74d9acc2019-03-21 14:00:03 +0200801 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (cur->buf.p != NULL) {
803 if (cur->next != NULL) {
804 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
805 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (cur->next == NULL) {
810 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
811 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
812 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200813
814 cur = cur->next;
815 }
816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200818 buf->tag = policy_oid.tag;
819 buf->p = policy_oid.p;
820 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300821
822 *p += len;
823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 /*
825 * If there is an optional qualifier, then *p < policy_end
826 * Check the Qualifier len to verify it doesn't exceed policy_end.
827 */
828 if (*p < policy_end) {
829 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
830 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
831 0) {
832 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
833 }
Ron Eldor08063792019-05-13 16:38:39 +0300834 /*
835 * Skip the optional policy qualifiers.
836 */
837 *p += len;
838 }
839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 if (*p != policy_end) {
841 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
842 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
843 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200844 }
845
846 /* Set final sequence entry's next pointer to NULL */
847 cur->next = NULL;
848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 if (*p != end) {
850 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
851 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
852 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200855}
856
857/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200858 * X.509 v3 extensions
859 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200860 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861static int x509_get_crt_ext(unsigned char **p,
862 const unsigned char *end,
863 mbedtls_x509_crt *crt,
864 mbedtls_x509_crt_ext_cb_t cb,
865 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866{
Janos Follath865b3eb2019-12-16 11:46:15 +0000867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200869 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if (*p == end) {
872 return 0;
873 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
876 return ret;
877 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878
Hanno Becker12f62fb2019-02-12 17:22:36 +0000879 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881 /*
882 * Extension ::= SEQUENCE {
883 * extnID OBJECT IDENTIFIER,
884 * critical BOOLEAN DEFAULT FALSE,
885 * extnValue OCTET STRING }
886 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888 int is_critical = 0; /* DEFAULT FALSE */
889 int ext_type = 0;
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
892 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
893 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
894 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895
896 end_ext_data = *p + len;
897
898 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
900 MBEDTLS_ASN1_OID)) != 0) {
901 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
902 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903
k-stachowiak463928a2018-07-24 12:50:59 +0200904 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 extn_oid.p = *p;
906 *p += extn_oid.len;
907
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
910 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
911 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
912 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913
914 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
916 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
917 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
918 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200920 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 end_ext_octet = *p + len;
922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if (end_ext_octet != end_ext_data) {
924 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
925 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
926 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927
928 /*
929 * Detect supported extensions
930 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200934 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (cb != NULL) {
936 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
937 if (ret != 0 && is_critical) {
938 return ret;
939 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200940 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200941 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200942 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200943
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944 /* No parser found, skip extension */
945 *p = end_ext_octet;
946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
950 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952 continue;
953 }
954
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100955 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if ((crt->ext_types & ext_type) != 0) {
957 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
958 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100959
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960 crt->ext_types |= ext_type;
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 switch (ext_type) {
963 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
964 /* Parse basic constraints */
965 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
966 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
967 return ret;
968 }
969 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 case MBEDTLS_X509_EXT_KEY_USAGE:
972 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100973 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
974 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return ret;
976 }
977 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
980 /* Parse extended key usage */
981 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
982 &crt->ext_key_usage)) != 0) {
983 return ret;
984 }
985 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986
toth92ga41954d2021-02-12 16:11:17 +0100987 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
988 /* Parse subject key identifier */
989 if ((ret = x509_get_subject_key_id(p, end_ext_data,
990 &crt->subject_key_id)) != 0) {
991 return ret;
992 }
993 break;
toth92g8d435a02021-05-10 15:16:33 +0200994
toth92ga41954d2021-02-12 16:11:17 +0100995 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
996 /* Parse authority key identifier */
997 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
998 &crt->authority_key_id)) != 0) {
999 return ret;
1000 }
1001 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001003 /* Parse subject alt name
1004 * SubjectAltName ::= GeneralNames
1005 */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001006 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1007 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 return ret;
1009 }
1010 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1013 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001014 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1015 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 return ret;
1017 }
1018 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1021 /* Parse certificate policies type */
1022 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1023 &crt->certificate_policies)) != 0) {
1024 /* Give the callback (if any) a chance to handle the extension
1025 * if it contains unsupported policies */
1026 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1027 cb(p_ctx, crt, &extn_oid, is_critical,
1028 start_ext_octet, end_ext_octet) == 0) {
1029 break;
1030 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 if (is_critical) {
1033 return ret;
1034 } else
1035 /*
1036 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1037 * cannot interpret or enforce the policy. However, it is up to
1038 * the user to choose how to enforce the policies,
1039 * unless the extension is critical.
1040 */
1041 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1042 return ret;
1043 }
1044 }
1045 break;
1046
1047 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001048 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 * If this is a non-critical extension, which the oid layer
1050 * supports, but there isn't an x509 parser for it,
1051 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001052 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (is_critical) {
1054 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1055 } else {
1056 *p = end_ext_octet;
1057 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001058 }
1059 }
1060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (*p != end) {
1062 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1063 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1064 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067}
1068
1069/*
1070 * Parse and fill a single X.509 certificate in DER format
1071 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1073 const unsigned char *buf,
1074 size_t buflen,
1075 int make_copy,
1076 mbedtls_x509_crt_ext_cb_t cb,
1077 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078{
Janos Follath865b3eb2019-12-16 11:46:15 +00001079 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080 size_t len;
1081 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1085 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1086 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001087
1088 /*
1089 * Check for valid input
1090 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (crt == NULL || buf == NULL) {
1092 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1093 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001095 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001097 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 end = p + len;
1099
1100 /*
1101 * Certificate ::= SEQUENCE {
1102 * tbsCertificate TBSCertificate,
1103 * signatureAlgorithm AlgorithmIdentifier,
1104 * signatureValue BIT STRING }
1105 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1107 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1108 mbedtls_x509_crt_free(crt);
1109 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110 }
1111
Janos Follathcc0e49d2016-02-17 14:34:12 +00001112 end = crt_end = p + len;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001113 crt->raw.len = (size_t) (crt_end - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001115 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1117 if (crt->raw.p == NULL) {
1118 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1119 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001122 crt->own_buffer = 1;
1123
1124 p += crt->raw.len - len;
1125 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 } else {
1127 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001128 crt->own_buffer = 0;
1129 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001130
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001131 /*
1132 * TBSCertificate ::= SEQUENCE {
1133 */
1134 crt->tbs.p = p;
1135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1137 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1138 mbedtls_x509_crt_free(crt);
1139 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140 }
1141
1142 end = p + len;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001143 crt->tbs.len = (size_t) (end - crt->tbs.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001144
1145 /*
1146 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1147 *
1148 * CertificateSerialNumber ::= INTEGER
1149 *
1150 * signature AlgorithmIdentifier
1151 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1153 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1154 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1155 &sig_params1)) != 0) {
1156 mbedtls_x509_crt_free(crt);
1157 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158 }
1159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (crt->version < 0 || crt->version > 2) {
1161 mbedtls_x509_crt_free(crt);
1162 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001163 }
1164
Andres AG7ca4a032017-03-09 16:16:11 +00001165 crt->version++;
1166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1168 &crt->sig_md, &crt->sig_pk,
1169 &crt->sig_opts)) != 0) {
1170 mbedtls_x509_crt_free(crt);
1171 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172 }
1173
1174 /*
1175 * issuer Name
1176 */
1177 crt->issuer_raw.p = p;
1178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1180 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1181 mbedtls_x509_crt_free(crt);
1182 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001183 }
1184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1186 mbedtls_x509_crt_free(crt);
1187 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001188 }
1189
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001190 crt->issuer_raw.len = (size_t) (p - crt->issuer_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191
1192 /*
1193 * Validity ::= SEQUENCE {
1194 * notBefore Time,
1195 * notAfter Time }
1196 *
1197 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1199 &crt->valid_to)) != 0) {
1200 mbedtls_x509_crt_free(crt);
1201 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001202 }
1203
1204 /*
1205 * subject Name
1206 */
1207 crt->subject_raw.p = p;
1208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1210 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1211 mbedtls_x509_crt_free(crt);
1212 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001213 }
1214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1216 mbedtls_x509_crt_free(crt);
1217 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001218 }
1219
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001220 crt->subject_raw.len = (size_t) (p - crt->subject_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001221
1222 /*
1223 * SubjectPublicKeyInfo
1224 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001225 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1227 mbedtls_x509_crt_free(crt);
1228 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229 }
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001230 crt->pk_raw.len = (size_t) (p - crt->pk_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001231
1232 /*
1233 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1234 * -- If present, version shall be v2 or v3
1235 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1236 * -- If present, version shall be v2 or v3
1237 * extensions [3] EXPLICIT Extensions OPTIONAL
1238 * -- If present, version shall be v3
1239 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 if (crt->version == 2 || crt->version == 3) {
1241 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1242 if (ret != 0) {
1243 mbedtls_x509_crt_free(crt);
1244 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245 }
1246 }
1247
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (crt->version == 2 || crt->version == 3) {
1249 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1250 if (ret != 0) {
1251 mbedtls_x509_crt_free(crt);
1252 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001253 }
1254 }
1255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (crt->version == 3) {
1257 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1258 if (ret != 0) {
1259 mbedtls_x509_crt_free(crt);
1260 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001261 }
1262 }
1263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 if (p != end) {
1265 mbedtls_x509_crt_free(crt);
1266 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1267 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001268 }
1269
1270 end = crt_end;
1271
1272 /*
1273 * }
1274 * -- end of TBSCertificate
1275 *
1276 * signatureAlgorithm AlgorithmIdentifier,
1277 * signatureValue BIT STRING
1278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1280 mbedtls_x509_crt_free(crt);
1281 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282 }
1283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (crt->sig_oid.len != sig_oid2.len ||
1285 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001286 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001287 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 (sig_params1.len != 0 &&
1289 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1290 mbedtls_x509_crt_free(crt);
1291 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001292 }
1293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1295 mbedtls_x509_crt_free(crt);
1296 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001297 }
1298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (p != end) {
1300 mbedtls_x509_crt_free(crt);
1301 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1302 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001303 }
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001306}
1307
1308/*
1309 * Parse one X.509 certificate in DER format from a buffer and add them to a
1310 * chained list
1311 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001312static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1313 const unsigned char *buf,
1314 size_t buflen,
1315 int make_copy,
1316 mbedtls_x509_crt_ext_cb_t cb,
1317 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001318{
Janos Follath865b3eb2019-12-16 11:46:15 +00001319 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001321
1322 /*
1323 * Check for valid input
1324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 if (crt == NULL || buf == NULL) {
1326 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1327 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001330 prev = crt;
1331 crt = crt->next;
1332 }
1333
1334 /*
1335 * Add new certificate on the end of the chain if needed.
1336 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if (crt->version != 0 && crt->next == NULL) {
1338 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 if (crt->next == NULL) {
1341 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1342 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343
1344 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001346 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001347 }
1348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1350 if (ret != 0) {
1351 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 if (crt != chain) {
1356 mbedtls_free(crt);
1357 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360 }
1361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363}
1364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1366 const unsigned char *buf,
1367 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001368{
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001370}
1371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1373 const unsigned char *buf,
1374 size_t buflen,
1375 int make_copy,
1376 mbedtls_x509_crt_ext_cb_t cb,
1377 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001378{
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001380}
1381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1383 const unsigned char *buf,
1384 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001385{
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001387}
1388
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001390 * Parse one or more PEM certificates from a buffer and add them to the chained
1391 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001393int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1394 const unsigned char *buf,
1395 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001396{
Janos Follath98e28a72016-05-31 14:03:54 +01001397#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001398 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001400#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001401
1402 /*
1403 * Check for valid input
1404 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 if (chain == NULL || buf == NULL) {
1406 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1407 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001408
1409 /*
1410 * Determine buffer content. Buffer contains either one DER certificate or
1411 * one or more PEM certificates.
1412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1415 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001417 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1420 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1421 }
Janos Follath98e28a72016-05-31 14:03:54 +01001422#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001424#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001428 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001430
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001431 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001435
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001436 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 ret = mbedtls_pem_read_buffer(&pem,
1438 "-----BEGIN CERTIFICATE-----",
1439 "-----END CERTIFICATE-----",
1440 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443 /*
1444 * Was PEM encoded
1445 */
1446 buflen -= use_len;
1447 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1449 return ret;
1450 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1451 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452
1453 /*
1454 * PEM header and footer were found
1455 */
1456 buflen -= use_len;
1457 buf += use_len;
1458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001462
Paul Bakker5a5fa922014-09-26 14:53:04 +02001463 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001464 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474 /*
1475 * Quit parsing on a memory error
1476 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1478 return ret;
1479 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001484
1485 total_failed++;
1486 continue;
1487 }
1488
1489 success = 1;
1490 }
1491 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if (success) {
1494 return total_failed;
1495 } else if (first_error) {
1496 return first_error;
1497 } else {
1498 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1499 }
Janos Follath98e28a72016-05-31 14:03:54 +01001500#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001501}
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504/*
1505 * Load one or more certificates and add them to the chained list
1506 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001507int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508{
Janos Follath865b3eb2019-12-16 11:46:15 +00001509 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510 size_t n;
1511 unsigned char *buf;
1512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1514 return ret;
1515 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001518
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001519 mbedtls_zeroize_and_free(buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522}
1523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001525{
1526 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001527#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001528 int w_ret;
1529 WCHAR szDir[MAX_PATH];
1530 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001531 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533
Paul Bakker9af723c2014-05-01 13:03:14 +02001534 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001535 HANDLE hFind;
1536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if (len > MAX_PATH - 3) {
1538 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1539 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 memset(szDir, 0, sizeof(szDir));
1542 memset(filename, 0, MAX_PATH);
1543 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001544 filename[len++] = '\\';
1545 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546 filename[len++] = '*';
1547
Simon Butcher35e5dad2018-03-15 15:00:03 +00001548 /*
Minos Galanakis08a67cc2023-09-22 16:00:06 +01001549 * Note this function uses the code page CP_ACP which is the system default
1550 * ANSI codepage. The input string is always described in BYTES and the
1551 * output length is described in WCHARs.
Simon Butcher35e5dad2018-03-15 15:00:03 +00001552 */
Minos Galanakisa9bb34c2023-09-25 14:41:12 +01001553 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 MAX_PATH - 3);
1555 if (w_ret == 0) {
1556 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1557 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 hFind = FindFirstFileW(szDir, &file_data);
1560 if (hFind == INVALID_HANDLE_VALUE) {
1561 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1562 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001563
1564 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 do {
1566 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001569 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 }
Simon Butcherde573f52018-07-05 09:11:30 +01001571 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Minos Galanakis59108d32023-09-25 14:11:22 +01001572 -1, p, (int) len, NULL, NULL);
Minos Galanakisa277b212023-08-09 16:32:22 +01001573 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001574 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1575 goto cleanup;
1576 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1579 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 }
1584 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589
Ron Eldor36d90422017-01-09 15:09:16 +02001590cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001592#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001593 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001594 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001595 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001596 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001597 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if (dir == NULL) {
1601 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1602 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001603
Ron Eldor63140682017-01-09 19:27:59 +02001604#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1606 closedir(dir);
1607 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001608 }
Ron Eldor63140682017-01-09 19:27:59 +02001609#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001612
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001614 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001617 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001618 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1619 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 } else if (stat(entry_name, &sb) == -1) {
1621 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001622 /* Broken symbolic link - ignore this entry.
1623 stat(2) will return this error for either (a) a dangling
1624 symlink or (b) a missing file.
1625 Given that we have just obtained the filename from readdir,
1626 assume that it does exist and therefore treat this as a
1627 dangling symlink. */
1628 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001630 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001631 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1632 goto cleanup;
1633 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634 }
1635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639
1640 // Ignore parse errors
1641 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1643 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001648 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001649
1650cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001652
Ron Eldor63140682017-01-09 19:27:59 +02001653#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 }
Ron Eldor63140682017-01-09 19:27:59 +02001657#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001658
Paul Bakkerbe089b02013-10-14 15:51:50 +02001659#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001662}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001664
Peter Kolbus9a969b62018-12-11 13:55:56 -06001665#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001666#define PRINT_ITEM(i) \
1667 do { \
1668 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1669 MBEDTLS_X509_SAFE_SNPRINTF; \
1670 sep = ", "; \
1671 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001672
Przemek Stekielf4194942023-04-24 09:52:17 +02001673#define CERT_TYPE(type, name) \
1674 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001675 if (ns_cert_type & (type)) { \
1676 PRINT_ITEM(name); \
1677 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001678 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001679
Przemek Stekielf4194942023-04-24 09:52:17 +02001680#define KEY_USAGE(code, name) \
1681 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001682 if (key_usage & (code)) { \
1683 PRINT_ITEM(name); \
1684 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001685 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687static int x509_info_ext_key_usage(char **buf, size_t *size,
1688 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001689{
Janos Follath865b3eb2019-12-16 11:46:15 +00001690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001691 const char *desc;
1692 size_t n = *size;
1693 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001695 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 while (cur != NULL) {
1698 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001699 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001703 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001704
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001705 sep = ", ";
1706
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001707 cur = cur->next;
1708 }
1709
1710 *size = n;
1711 *buf = p;
1712
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001714}
1715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716static int x509_info_cert_policies(char **buf, size_t *size,
1717 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001718{
Janos Follath865b3eb2019-12-16 11:46:15 +00001719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001720 const char *desc;
1721 size_t n = *size;
1722 char *p = *buf;
1723 const mbedtls_x509_sequence *cur = certificate_policies;
1724 const char *sep = "";
1725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 while (cur != NULL) {
1727 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001728 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001730
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001732 MBEDTLS_X509_SAFE_SNPRINTF;
1733
1734 sep = ", ";
1735
1736 cur = cur->next;
1737 }
1738
1739 *size = n;
1740 *buf = p;
1741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001743}
1744
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745/*
1746 * Return an informational string about the certificate.
1747 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001748#define BEFORE_COLON 18
1749#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001750int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1751 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001752{
Janos Follath865b3eb2019-12-16 11:46:15 +00001753 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001754 size_t n;
1755 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001756 char key_size_str[BEFORE_COLON];
1757
1758 p = buf;
1759 n = size;
1760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 if (NULL == crt) {
1762 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001763 MBEDTLS_X509_SAFE_SNPRINTF;
1764
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001766 }
1767
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1769 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001770 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1772 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001773 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001776 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001779 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001781 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001784 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001786 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1789 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1790 crt->valid_from.year, crt->valid_from.mon,
1791 crt->valid_from.day, crt->valid_from.hour,
1792 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001793 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1796 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1797 crt->valid_to.year, crt->valid_to.mon,
1798 crt->valid_to.day, crt->valid_to.hour,
1799 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001800 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001803 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1806 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001807 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001809 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1811 mbedtls_pk_get_name(&crt->pk))) != 0) {
1812 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001813 }
1814
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1816 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001817 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001818
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001819 /*
1820 * Optional extensions
1821 */
1822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1824 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1825 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001826 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001827
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 if (crt->max_pathlen > 0) {
1829 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001830 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001831 }
1832 }
1833
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1835 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001836 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001837
Przemek Stekiel21c37282023-01-16 08:47:49 +01001838 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1839 &crt->subject_alt_names,
1840 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 return ret;
1842 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001843 }
1844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1846 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001847 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001848
Przemek Stekiel21c37282023-01-16 08:47:49 +01001849 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 return ret;
1851 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001852 }
1853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1855 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001856 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001857
Przemek Stekiel21c37282023-01-16 08:47:49 +01001858 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 return ret;
1860 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001861 }
1862
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1864 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001865 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001866
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 if ((ret = x509_info_ext_key_usage(&p, &n,
1868 &crt->ext_key_usage)) != 0) {
1869 return ret;
1870 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001871 }
1872
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1874 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001875 MBEDTLS_X509_SAFE_SNPRINTF;
1876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 if ((ret = x509_info_cert_policies(&p, &n,
1878 &crt->certificate_policies)) != 0) {
1879 return ret;
1880 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001881 }
1882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001884 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001885
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001887}
1888
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001889struct x509_crt_verify_string {
1890 int code;
1891 const char *string;
1892};
1893
Gilles Peskine449bd832023-01-11 14:50:10 +01001894#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001895static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001896 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001897 { 0, NULL }
1898};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001899#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1902 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001903{
Janos Follath865b3eb2019-12-16 11:46:15 +00001904 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001905 const struct x509_crt_verify_string *cur;
1906 char *p = buf;
1907 size_t n = size;
1908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1910 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001911 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001915 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001916 flags ^= cur->code;
1917 }
1918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 if (flags != 0) {
1920 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1921 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001922 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001923 }
1924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001926}
Hanno Becker612a2f12020-10-09 09:19:39 +01001927#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001928
Gilles Peskine449bd832023-01-11 14:50:10 +01001929int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1930 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001931{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001932 unsigned int usage_must, usage_may;
1933 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1937 return 0;
1938 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001939
1940 usage_must = usage & ~may_mask;
1941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1943 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1944 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001945
1946 usage_may = usage & may_mask;
1947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1949 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1950 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001953}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1956 const char *usage_oid,
1957 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001958{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001960
1961 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1963 return 0;
1964 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001965
1966 /*
1967 * Look for the requested usage (or wildcard ANY) in our list
1968 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001971
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 if (cur_oid->len == usage_len &&
1973 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1974 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001975 }
1976
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1978 return 0;
1979 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001980 }
1981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001983}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001986/*
1987 * Return 1 if the certificate is revoked, or 0 otherwise.
1988 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001989int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001990{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 while (cur != NULL && cur->serial.len != 0) {
1994 if (crt->serial.len == cur->serial.len &&
1995 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
1996 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001997 }
1998
1999 cur = cur->next;
2000 }
2001
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002003}
2004
2005/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002006 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002007 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002008 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002009static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2010 mbedtls_x509_crl *crl_list,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002011 const mbedtls_x509_crt_profile *profile,
2012 const mbedtls_x509_time *now)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002013{
2014 int flags = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002015 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002016#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002017 psa_algorithm_t psa_algorithm;
2018#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002020#endif /* MBEDTLS_USE_PSA_CRYPTO */
2021 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 if (ca == NULL) {
2024 return flags;
2025 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 while (crl_list != NULL) {
2028 if (crl_list->version == 0 ||
2029 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002030 crl_list = crl_list->next;
2031 continue;
2032 }
2033
2034 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002035 * Check if the CA is configured to sign CRLs
2036 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 if (mbedtls_x509_crt_check_key_usage(ca,
2038 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002040 break;
2041 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002042
2043 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002044 * Check if CRL is correctly signed by the trusted CA
2045 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002047 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002051 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002053
pespacek7599a772022-02-07 14:40:55 +01002054#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002055 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if (psa_hash_compute(psa_algorithm,
2057 crl_list->tbs.p,
2058 crl_list->tbs.len,
2059 hash,
2060 sizeof(hash),
2061 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002062 /* Note: this can't happen except after an internal error */
2063 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2064 break;
2065 }
pespacek7599a772022-02-07 14:40:55 +01002066#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2068 hash_length = mbedtls_md_get_size(md_info);
2069 if (mbedtls_md(md_info,
2070 crl_list->tbs.p,
2071 crl_list->tbs.len,
2072 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002073 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002075 break;
2076 }
pespaceka7a64692022-02-14 15:18:43 +01002077#endif /* MBEDTLS_USE_PSA_CRYPTO */
2078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002080 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002082
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2084 crl_list->sig_md, hash, hash_length,
2085 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002087 break;
2088 }
2089
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002090#if defined(MBEDTLS_HAVE_TIME_DATE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002091 /*
2092 * Check for validity of CRL (Do not drop out)
2093 */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002094 if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002097
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002098 if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002099 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002101#else
2102 ((void) now);
2103#endif
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002104
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002105 /*
2106 * Check if certificate is revoked
2107 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002110 break;
2111 }
2112
2113 crl_list = crl_list->next;
2114 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002117}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002119
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002120/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002121 * Check the signature of a certificate by its parent
2122 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002123static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2124 mbedtls_x509_crt *parent,
2125 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002126{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002127 size_t hash_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002128 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002129#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2130 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 md_info = mbedtls_md_info_from_type(child->sig_md);
2132 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002133
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002134 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2136 return -1;
2137 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002138#else
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002139 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002140 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 status = psa_hash_compute(hash_alg,
2143 child->tbs.p,
2144 child->tbs.len,
2145 hash,
2146 sizeof(hash),
2147 &hash_len);
2148 if (status != PSA_SUCCESS) {
2149 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002150 }
2151
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002152#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002153 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2155 return -1;
2156 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002157
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002158#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2160 return mbedtls_pk_verify_restartable(&parent->pk,
2161 child->sig_md, hash, hash_len,
2162 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002163 }
2164#else
2165 (void) rs_ctx;
2166#endif
2167
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2169 child->sig_md, hash, hash_len,
2170 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002171}
2172
2173/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002174 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2175 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002176 *
2177 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002178 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002179static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2180 const mbedtls_x509_crt *parent,
2181 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002182{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002183 int need_ca_bit;
2184
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002185 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2187 return -1;
2188 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002189
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002190 /* Parent must have the basicConstraints CA bit set as a general rule */
2191 need_ca_bit = 1;
2192
2193 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002195 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002196 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if (need_ca_bit && !parent->ca_istrue) {
2199 return -1;
2200 }
2201
2202 if (need_ca_bit &&
2203 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2204 return -1;
2205 }
2206
2207 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002208}
2209
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002210/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002211 * Find a suitable parent for child in candidates, or return NULL.
2212 *
2213 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002214 * 1. subject name matches child's issuer
2215 * 2. if necessary, the CA bit is set and key usage allows signing certs
2216 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002217 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002218 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002219 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002220 * If there's a suitable candidate which is also time-valid, return the first
2221 * such. Otherwise, return the first suitable candidate (or NULL if there is
2222 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002223 *
2224 * The rationale for this rule is that someone could have a list of trusted
2225 * roots with two versions on the same root with different validity periods.
2226 * (At least one user reported having such a list and wanted it to just work.)
2227 * The reason we don't just require time-validity is that generally there is
2228 * only one version, and if it's expired we want the flags to state that
2229 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002230 *
2231 * The rationale for rule 3 (signature for trusted roots) is that users might
2232 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002233 * way we select the correct one is by checking the signature (as we don't
2234 * rely on key identifier extensions). (This is one way users might choose to
2235 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002236 *
2237 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002238 * - [in] child: certificate for which we're looking for a parent
2239 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002240 * - [out] r_parent: parent found (or NULL)
2241 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002242 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2243 * of the chain, 0 otherwise
2244 * - [in] path_cnt: number of intermediates seen so far
2245 * - [in] self_cnt: number of self-signed intermediates seen so far
2246 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002247 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002248 *
2249 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002250 * - 0 on success
2251 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002252 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002253static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 mbedtls_x509_crt *child,
2255 mbedtls_x509_crt *candidates,
2256 mbedtls_x509_crt **r_parent,
2257 int *r_signature_is_good,
2258 int top,
2259 unsigned path_cnt,
2260 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002261 mbedtls_x509_crt_restart_ctx *rs_ctx,
2262 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002263{
Janos Follath865b3eb2019-12-16 11:46:15 +00002264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002265 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002266 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002267
2268#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002269 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002271 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002272 parent = rs_ctx->parent;
2273 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002274 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002275
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002276 /* clear saved state */
2277 rs_ctx->parent = NULL;
2278 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002279 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002280
2281 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002282 goto check_signature;
2283 }
2284#endif
2285
2286 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002287 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002290 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002292 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002294
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002295 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 if (parent->max_pathlen > 0 &&
2297 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002298 continue;
2299 }
2300
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002301 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002302#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2303check_signature:
2304#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002306
2307#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002309 /* save state */
2310 rs_ctx->parent = parent;
2311 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002312 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002315 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002316#else
2317 (void) ret;
2318#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002319
2320 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002322 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002324
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002325#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002326 /* optional time check */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002327 if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2328 mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002330 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002331 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002332 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002333
2334 continue;
2335 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002336#else
2337 ((void) now);
2338#endif
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002339
Andy Gross1f627142019-01-30 10:25:53 -06002340 *r_parent = parent;
2341 *r_signature_is_good = signature_is_good;
2342
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002343 break;
2344 }
2345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002347 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002348 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002349 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002350
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002352}
2353
2354/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002355 * Find a parent in trusted CAs or the provided chain, or return NULL.
2356 *
2357 * Searches in trusted CAs first, and return the first suitable parent found
2358 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002359 *
2360 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002361 * - [in] child: certificate for which we're looking for a parent, followed
2362 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002363 * - [in] trust_ca: list of locally trusted certificates
2364 * - [out] parent: parent found (or NULL)
2365 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2366 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2367 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2368 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002369 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002370 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002371 *
2372 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002373 * - 0 on success
2374 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002375 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002376static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 mbedtls_x509_crt *child,
2378 mbedtls_x509_crt *trust_ca,
2379 mbedtls_x509_crt **parent,
2380 int *parent_is_trusted,
2381 int *signature_is_good,
2382 unsigned path_cnt,
2383 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002384 mbedtls_x509_crt_restart_ctx *rs_ctx,
2385 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002386{
Janos Follath865b3eb2019-12-16 11:46:15 +00002387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002388 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002389
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002390 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002391
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002392#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002393 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002395 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002396 rs_ctx->parent_is_trusted = -1;
2397 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002398#endif
2399
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002401 search_list = *parent_is_trusted ? trust_ca : child->next;
2402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 ret = x509_crt_find_parent_in(child, search_list,
2404 parent, signature_is_good,
2405 *parent_is_trusted,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002406 path_cnt, self_cnt, rs_ctx, now);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002407
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002408#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002410 /* save state */
2411 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002413 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002414#else
2415 (void) ret;
2416#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002417
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002418 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002420 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002422
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002423 /* prepare second iteration */
2424 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002425 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002426
2427 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002429 *parent_is_trusted = 0;
2430 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002431 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002434}
2435
2436/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002437 * Check if an end-entity certificate is locally trusted
2438 *
2439 * Currently we require such certificates to be self-signed (actually only
2440 * check for self-issued as self-signatures are not checked)
2441 */
2442static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 mbedtls_x509_crt *crt,
2444 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002445{
2446 mbedtls_x509_crt *cur;
2447
2448 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2450 return -1;
2451 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002452
2453 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2455 if (crt->raw.len == cur->raw.len &&
2456 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2457 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002458 }
2459 }
2460
2461 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002463}
2464
2465/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002466 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002467 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002468 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2469 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002470 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002471 * such that every cert in the chain is a child of the next one,
2472 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002473 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002474 * Verify that chain and return it with flags for all issues found.
2475 *
2476 * Special cases:
2477 * - EE == Rj -> return a one-element list containing it
2478 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2479 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002480 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002481 * Tests for (aspects of) this function should include at least:
2482 * - trusted EE
2483 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002484 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002485 * - if relevant: EE untrusted
2486 * - if relevant: EE -> intermediate, untrusted
2487 * with the aspect under test checked at each relevant level (EE, int, root).
2488 * For some aspects longer chains are required, but usually length 2 is
2489 * enough (but length 1 is not in general).
2490 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002491 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002492 * - [in] crt: the cert list EE, C1, ..., Cn
2493 * - [in] trust_ca: the trusted list R1, ..., Rp
2494 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002495 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002496 * Only valid when return value is 0, may contain garbage otherwise!
2497 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002498 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002499 *
2500 * Return value:
2501 * - non-zero if the chain could not be fully built and examined
2502 * - 0 is the chain was successfully built and examined,
2503 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002504 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002505static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 mbedtls_x509_crt *crt,
2507 mbedtls_x509_crt *trust_ca,
2508 mbedtls_x509_crl *ca_crl,
2509 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2510 void *p_ca_cb,
2511 const mbedtls_x509_crt_profile *profile,
2512 mbedtls_x509_crt_verify_chain *ver_chain,
2513 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002514{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002515 /* Don't initialize any of those variables here, so that the compiler can
2516 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002517 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002518 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002519 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002520 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002521 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002522 int parent_is_trusted;
2523 int child_is_trusted;
2524 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002525 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002526 mbedtls_x509_crt *cur_trust_ca = NULL;
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002527 mbedtls_x509_time now;
2528
2529#if defined(MBEDTLS_HAVE_TIME_DATE)
2530 if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2531 return MBEDTLS_ERR_X509_FATAL_ERROR;
2532 }
2533#endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002534
2535#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2536 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002538 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002539 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002540 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002541
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002542 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002543 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002544 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002545 flags = &cur->flags;
2546
2547 goto find_parent;
2548 }
2549#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002550
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002551 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002552 self_cnt = 0;
2553 parent_is_trusted = 0;
2554 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002557 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002558 cur = &ver_chain->items[ver_chain->len];
2559 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002560 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002561 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002562 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002563
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002564#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002565 /* Check time-validity (all certificates) */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002566 if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002567 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002569
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002570 if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002571 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002573#endif
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002574
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002575 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 if (child_is_trusted) {
2577 return 0;
2578 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002579
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002580 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002582 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002586 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002588
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002589 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if (ver_chain->len == 1 &&
2591 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2592 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002593 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002594
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002595#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2596find_parent:
2597#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002598
2599 /* Obtain list of potential trusted signers from CA callback,
2600 * or use statically provided list. */
2601#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 if (f_ca_cb != NULL) {
2603 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2604 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002605 ver_chain->trust_ca_cb_result = NULL;
2606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2608 if (ret != 0) {
2609 return MBEDTLS_ERR_X509_FATAL_ERROR;
2610 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002611
2612 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002614#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2615 {
2616 ((void) f_ca_cb);
2617 ((void) p_ca_cb);
2618 cur_trust_ca = trust_ca;
2619 }
2620
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002621 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2623 &parent_is_trusted, &signature_is_good,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002624 ver_chain->len - 1, self_cnt, rs_ctx,
2625 &now);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002626
2627#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002629 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002630 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002631 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002632 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002635 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002636#else
2637 (void) ret;
2638#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002639
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002640 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002642 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002644 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002645
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002646 /* Count intermediate self-issued (not necessarily self-signed) certs.
2647 * These can occur with some strategies for key rollover, see [SIRO],
2648 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 if (ver_chain->len != 1 &&
2650 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002651 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002652 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002653
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002654 /* path_cnt is 0 for the first intermediate CA,
2655 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 if (!parent_is_trusted &&
2657 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002658 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002660 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002661
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002662 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002664 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002666
2667 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002669 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002673 /* Check trusted CA's CRL for the given crt */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002674 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002675#else
2676 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002677#endif
2678
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002679 /* prepare for next iteration */
2680 child = parent;
2681 parent = NULL;
2682 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002683 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002684 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002685}
2686
Glenn Straussc26bd762022-10-23 19:48:18 -04002687#ifdef _WIN32
2688#ifdef _MSC_VER
2689#pragma comment(lib, "ws2_32.lib")
2690#include <winsock2.h>
2691#include <ws2tcpip.h>
2692#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2693#include <winsock2.h>
2694#include <ws2tcpip.h>
Steve Lhommeeb0f18a2023-06-16 14:12:19 +02002695#else
2696/* inet_pton() is not supported, fallback to software version */
2697#define MBEDTLS_TEST_SW_INET_PTON
Glenn Straussc26bd762022-10-23 19:48:18 -04002698#endif
2699#elif defined(__sun)
2700/* Solaris requires -lsocket -lnsl for inet_pton() */
2701#elif defined(__has_include)
2702#if __has_include(<sys/socket.h>)
2703#include <sys/socket.h>
2704#endif
2705#if __has_include(<arpa/inet.h>)
2706#include <arpa/inet.h>
2707#endif
2708#endif
2709
2710/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2711 * the platform inet_pton() or a local implementation (below). The local
2712 * implementation may be used even in cases where the platform provides
2713 * inet_pton(), e.g. when there are different includes required and/or the
2714 * platform implementation requires dependencies on additional libraries.
2715 * Specifically, Windows requires custom includes and additional link
2716 * dependencies, and Solaris requires additional link dependencies.
2717 * Also, as a coarse heuristic, use the local implementation if the compiler
2718 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002719 * provided by headers included (or not) via __has_include() above.
2720 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2721 * despite having a platform that has inet_pton. */
2722#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2723/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002724static int x509_inet_pton_ipv4(const char *src, void *dst);
2725
2726#define li_cton(c, n) \
2727 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2728
2729static int x509_inet_pton_ipv6(const char *src, void *dst)
2730{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002731 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002732 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002733 uint16_t addr[8];
2734 do {
2735 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002736 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002737 for (uint8_t digit; num_digits < 4; num_digits++) {
2738 if (li_cton(*p, digit) == 0) {
2739 break;
2740 }
2741 group = (group << 4) | digit;
2742 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002743 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002744 if (num_digits != 0) {
Dave Rodgmancfa72232023-09-05 16:20:33 +01002745 MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups);
2746 nonzero_groups++;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002747 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002748 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002749 } else if (*p == '.') {
2750 /* Don't accept IPv4 too early or late */
2751 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2752 nonzero_groups >= 7) {
2753 break;
2754 }
2755
2756 /* Walk back to prior ':', then parse as IPv4-mapped */
2757 int steps = 4;
2758 do {
2759 p--;
2760 steps--;
2761 } while (*p != ':' && steps > 0);
2762
2763 if (*p != ':') {
2764 break;
2765 }
2766 p++;
2767 nonzero_groups--;
2768 if (x509_inet_pton_ipv4((const char *) p,
2769 addr + nonzero_groups) != 0) {
2770 break;
2771 }
2772
Andrzej Kurek0d578962023-04-13 09:09:56 -04002773 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002774 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002775 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002776 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002777 return -1;
2778 }
2779 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002780 /* Don't accept a second zero group or an invalid delimiter */
2781 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002782 return -1;
2783 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002784 zero_group_start = nonzero_groups;
2785
2786 /* Accept a zero group at start, but it has to be a double colon */
2787 if (zero_group_start == 0 && *++p != ':') {
2788 return -1;
2789 }
2790
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002791 if (p[1] == '\0') {
2792 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002793 break;
2794 }
2795 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002796 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002797 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002798
2799 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002800 return -1;
2801 }
2802
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002803 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002804 if (nonzero_groups > 6) {
2805 return -1;
2806 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002807 int zero_groups = 8 - nonzero_groups;
2808 int groups_after_zero = nonzero_groups - zero_group_start;
2809
2810 /* Move the non-zero part to after the zeroes */
2811 if (groups_after_zero) {
2812 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002813 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002814 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002815 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002816 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002817 } else {
2818 if (nonzero_groups != 8) {
2819 return -1;
2820 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002821 }
2822 memcpy(dst, addr, sizeof(addr));
2823 return 0;
2824}
2825
2826static int x509_inet_pton_ipv4(const char *src, void *dst)
2827{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002828 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002829 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002830 uint8_t digit, num_digits = 0;
2831 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002832 uint16_t octet;
2833
Glenn Strauss416c2952022-10-24 23:00:02 -04002834 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002835 octet = num_digits = 0;
2836 do {
2837 digit = *p - '0';
2838 if (digit > 9) {
2839 break;
2840 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002841
2842 /* Don't allow leading zeroes. These might mean octal format,
2843 * which this implementation does not support. */
2844 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002845 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002846 }
2847
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002848 octet = octet * 10 + digit;
2849 num_digits++;
2850 p++;
2851 } while (num_digits < 3);
2852
2853 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002854 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002855 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002856 *res++ = (uint8_t) octet;
2857 num_octets++;
2858 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002859 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002860}
Glenn Straussc26bd762022-10-23 19:48:18 -04002861
2862#else
2863
2864static int x509_inet_pton_ipv6(const char *src, void *dst)
2865{
2866 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2867}
2868
2869static int x509_inet_pton_ipv4(const char *src, void *dst)
2870{
2871 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2872}
2873
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002874#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002875
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002876size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002877{
2878 return strchr(cn, ':') == NULL
2879 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2880 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2881}
2882
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002883/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002884 * Check for CN match
2885 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002886static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2887 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002888{
2889 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 if (name->len == cn_len &&
2891 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2892 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002893 }
2894
2895 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 if (x509_check_wildcard(cn, name) == 0) {
2897 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002898 }
2899
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002901}
2902
Glenn Straussc26bd762022-10-23 19:48:18 -04002903static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2904 const char *cn, size_t cn_len)
2905{
2906 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002907 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002908 if (cn_len == 0) {
2909 return -1;
2910 }
2911
2912 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2913 const unsigned char san_type = (unsigned char) cur->buf.tag &
2914 MBEDTLS_ASN1_TAG_VALUE_MASK;
2915 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2916 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2917 return 0;
2918 }
2919 }
2920
2921 return -1;
2922}
2923
Andrzej Kurek199eab92023-05-10 09:57:19 -04002924static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2925 const char *cn, size_t cn_len)
2926{
2927 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2928 const unsigned char san_type = (unsigned char) cur->buf.tag &
2929 MBEDTLS_ASN1_TAG_VALUE_MASK;
2930 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2931 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2932 return 0;
2933 }
2934 }
2935
2936 return -1;
2937}
2938
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002939/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002940 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2941 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002942static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002944{
Glenn Straussc26bd762022-10-23 19:48:18 -04002945 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002946 int san_uri = 0;
2947 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002948 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2949 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002950 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002951 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2952 return 0;
2953 }
2954 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002955 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04002956 san_ip = 1;
2957 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002958 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
2959 san_uri = 1;
2960 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04002961 /* (We may handle other types here later.) */
2962 default: /* Unrecognized type */
2963 break;
2964 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04002966 if (san_ip) {
2967 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
2968 return 0;
2969 }
2970 }
2971 if (san_uri) {
2972 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
2973 return 0;
2974 }
2975 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002976
Andrzej Kurek199eab92023-05-10 09:57:19 -04002977 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002978}
2979
2980/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002981 * Verify the requested CN - only call this if cn is not NULL!
2982 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002983static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2984 const char *cn,
2985 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002986{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002987 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002989
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002991 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
2992 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 }
2994 } else {
2995 for (name = &crt->subject; name != NULL; name = name->next) {
2996 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2997 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002998 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 }
3000 }
3001
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003002 }
Glenn Straussc26bd762022-10-23 19:48:18 -04003003
3004 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003005}
3006
3007/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003008 * Merge the flags for all certs in the chain, after calling callback
3009 */
3010static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 uint32_t *flags,
3012 const mbedtls_x509_crt_verify_chain *ver_chain,
3013 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3014 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003015{
Janos Follath865b3eb2019-12-16 11:46:15 +00003016 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003017 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003018 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003019 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003020
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003022 cur = &ver_chain->items[i-1];
3023 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 if (NULL != f_vrfy) {
3026 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3027 return ret;
3028 }
3029 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003030
3031 *flags |= cur_flags;
3032 }
3033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003035}
3036
3037/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003038 * Verify the certificate validity, with profile, restartable version
3039 *
3040 * This function:
3041 * - checks the requested CN (if any)
3042 * - checks the type and size of the EE cert's key,
3043 * as that isn't done as part of chain building/verification currently
3044 * - builds and verifies the chain
3045 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003046 *
3047 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3048 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3049 * verification routine to search for trusted signers, and CRLs will
3050 * be disabled. Otherwise, `trust_ca` will be used as the static list
3051 * of trusted signers, and `ca_crl` will be use as the static list
3052 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003053 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003054static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3055 mbedtls_x509_crt *trust_ca,
3056 mbedtls_x509_crl *ca_crl,
3057 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3058 void *p_ca_cb,
3059 const mbedtls_x509_crt_profile *profile,
3060 const char *cn, uint32_t *flags,
3061 int (*f_vrfy)(void *,
3062 mbedtls_x509_crt *,
3063 int,
3064 uint32_t *),
3065 void *p_vrfy,
3066 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003067{
Janos Follath865b3eb2019-12-16 11:46:15 +00003068 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003069 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003070 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003071 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003072
3073 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003074 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003078 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3079 goto exit;
3080 }
3081
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003082 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 if (cn != NULL) {
3084 x509_crt_verify_name(crt, cn, &ee_flags);
3085 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003086
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003087 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003091 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003095 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003097
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003098 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3100 f_ca_cb, p_ca_cb, profile,
3101 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003104 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003106
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003107 /* Merge end-entity flags */
3108 ver_chain.items[0].flags |= ee_flags;
3109
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003110 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003112
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003113exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003114
3115#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3117 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003118 ver_chain.trust_ca_cb_result = NULL;
3119#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3120
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003121#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3123 mbedtls_x509_crt_restart_free(rs_ctx);
3124 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003125#endif
3126
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003127 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3128 * the SSL module for authmode optional, but non-zero return from the
3129 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003131 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003132 }
3133
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 if (ret != 0) {
3135 *flags = (uint32_t) -1;
3136 return ret;
3137 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 if (*flags != 0) {
3140 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3141 }
3142
3143 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003144}
3145
Hanno Becker3116fb32019-03-28 13:34:42 +00003146
3147/*
3148 * Verify the certificate validity (default profile, not restartable)
3149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003150int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3151 mbedtls_x509_crt *trust_ca,
3152 mbedtls_x509_crl *ca_crl,
3153 const char *cn, uint32_t *flags,
3154 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3155 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003156{
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3158 NULL, NULL,
3159 &mbedtls_x509_crt_profile_default,
3160 cn, flags,
3161 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003162}
3163
3164/*
3165 * Verify the certificate validity (user-chosen profile, not restartable)
3166 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003167int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3168 mbedtls_x509_crt *trust_ca,
3169 mbedtls_x509_crl *ca_crl,
3170 const mbedtls_x509_crt_profile *profile,
3171 const char *cn, uint32_t *flags,
3172 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3173 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003174{
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3176 NULL, NULL,
3177 profile, cn, flags,
3178 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003179}
3180
3181#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3182/*
3183 * Verify the certificate validity (user-chosen profile, CA callback,
3184 * not restartable).
3185 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003186int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3187 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3188 void *p_ca_cb,
3189 const mbedtls_x509_crt_profile *profile,
3190 const char *cn, uint32_t *flags,
3191 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3192 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003193{
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3195 f_ca_cb, p_ca_cb,
3196 profile, cn, flags,
3197 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003198}
3199#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3200
Gilles Peskine449bd832023-01-11 14:50:10 +01003201int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3202 mbedtls_x509_crt *trust_ca,
3203 mbedtls_x509_crl *ca_crl,
3204 const mbedtls_x509_crt_profile *profile,
3205 const char *cn, uint32_t *flags,
3206 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3207 void *p_vrfy,
3208 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003209{
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3211 NULL, NULL,
3212 profile, cn, flags,
3213 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003214}
3215
3216
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003217/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003218 * Initialize a certificate chain
3219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003220void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003221{
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003223}
3224
3225/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003226 * Unallocate all certificate data
3227 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003228void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 mbedtls_x509_crt *cert_cur = crt;
3231 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003232
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 while (cert_cur != NULL) {
3234 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003238#endif
3239
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3241 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3242 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3243 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3244 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Przemek Stekiel690ff692023-05-15 09:54:02 +02003245 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003246
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003248 mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003249 }
3250
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003251 cert_prv = cert_cur;
3252 cert_cur = cert_cur->next;
3253
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3255 if (cert_prv != crt) {
3256 mbedtls_free(cert_prv);
3257 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003258 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003259}
3260
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003261#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3262/*
3263 * Initialize a restart context
3264 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003265void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003266{
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003268
3269 ctx->parent = NULL;
3270 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003271 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003272
3273 ctx->parent_is_trusted = -1;
3274
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003275 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003276 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003278}
3279
3280/*
3281 * Free the components of a restart context
3282 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003283void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003284{
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003286 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003288
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 mbedtls_pk_restart_free(&ctx->pk);
3290 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003291}
3292#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3293
Minos Galanakis2abbac72024-01-18 17:05:21 +00003294int mbedtls_x509_crt_get_ca_istrue(const mbedtls_x509_crt *crt)
3295{
3296 if ((crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) != 0) {
3297 return crt->MBEDTLS_PRIVATE(ca_istrue);
3298 }
3299 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
3300}
3301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302#endif /* MBEDTLS_X509_CRT_PARSE_C */