blob: 4ac5d9b7e63e26ec6ae8963163b588197dd45140 [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"
Gilles Peskinecd4c0d72025-05-07 23:45:12 +020026#include "mbedtls/oid.h"
Gilles Peskine86a47f82025-05-07 20:20:12 +020027#include "x509_oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050028#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000029
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <string.h>
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034#endif
35
Andrzej Kurekd4a65532018-10-31 06:18:39 -040036#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"
Valerio Setti3f00b842023-05-15 12:57:06 +020039#include "pk_internal.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040040
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010045#endif
46
Daniel Axtensf0710242020-05-28 11:43:41 +100047#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010048#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Sergey Markelov4ed0fde2024-08-14 15:06:03 -070049#ifndef WIN32_LEAN_AND_MEAN
Glenn Straussc26bd762022-10-23 19:48:18 -040050#define WIN32_LEAN_AND_MEAN
Sergey Markelov4ed0fde2024-08-14 15:06:03 -070051#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052#include <windows.h>
53#else
54#include <time.h>
55#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100056#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000059#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020060#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#include <sys/types.h>
62#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020063#if defined(__MBED__)
64#include <platform/mbed_retarget.h>
65#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020067#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060068#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000069#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070#endif
71
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020072/*
73 * Item in a verification chain: cert and flags for it
74 */
75typedef struct {
76 mbedtls_x509_crt *crt;
77 uint32_t flags;
78} x509_crt_verify_chain_item;
79
80/*
81 * Max size of verification chain: end-entity + intermediates + trusted root
82 */
Gilles Peskine449bd832023-01-11 14:50:10 +010083#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020084
Gilles Peskineffb92da2021-06-02 00:03:26 +020085/* Default profile. Do not remove items unless there are serious security
86 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020087const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
88{
Gilles Peskineae270bf2021-06-02 00:05:29 +020089 /* Hashes from SHA-256 and above. Note that this selection
90 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +010091 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
92 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
93 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020094 0xFFFFFFF, /* Any PK alg */
Elena Uziunaite8dde3b32024-07-05 12:10:21 +010095#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskineae270bf2021-06-02 00:05:29 +020096 /* Curves at or above 128-bit security level. Note that this selection
97 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +010098 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
99 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
100 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
101 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
102 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
103 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
Gilles Peskine39957502021-06-17 23:17:52 +0200104 0,
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100105#else /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200106 0,
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100107#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200108 2048,
109};
110
Gilles Peskineffb92da2021-06-02 00:03:26 +0200111/* Next-generation profile. Currently identical to the default, but may
112 * be tightened at any time. */
113const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200114{
115 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
117 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
118 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200119 0xFFFFFFF, /* Any PK alg */
120#if defined(MBEDTLS_ECP_C)
121 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
123 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
124 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
125 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
126 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
127 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
128 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200129#else
130 0,
131#endif
132 2048,
133};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200134
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200135/*
136 * NSA Suite B Profile
137 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200138const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
139{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200140 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
142 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200143 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
145 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100146#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200147 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
149 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100150#else /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200151 0,
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100152#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200153 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200154};
155
156/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200157 * Empty / all-forbidden profile
158 */
159const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
160{
161 0,
162 0,
163 0,
164 (uint32_t) -1,
165};
166
167/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200168 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200169 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200170 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100171static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
172 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200173{
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (md_alg == MBEDTLS_MD_NONE) {
175 return -1;
176 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
179 return 0;
180 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200183}
184
185/*
186 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200187 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200188 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
190 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200191{
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (pk_alg == MBEDTLS_PK_NONE) {
193 return -1;
194 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
197 return 0;
198 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200201}
202
203/*
204 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200205 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200206 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
208 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200209{
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200211
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200212#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
214 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
215 return 0;
216 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200219 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200220#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200221
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100222#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200224 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 pk_alg == MBEDTLS_PK_ECKEY_DH) {
Valerio Settif9362b72023-11-29 08:42:27 +0100226 const mbedtls_ecp_group_id gid = mbedtls_pk_get_ec_group_id(pk);
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (gid == MBEDTLS_ECP_DP_NONE) {
229 return -1;
230 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
233 return 0;
234 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200237 }
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100238#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200241}
242
243/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000244 * Like memcmp, but case-insensitive and always returns -1 if different
245 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000247{
248 size_t i;
249 unsigned char diff;
250 const unsigned char *n1 = s1, *n2 = s2;
251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000253 diff = n1[i] ^ n2[i];
254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000256 continue;
257 }
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (diff == 32 &&
260 ((n1[i] >= 'a' && n1[i] <= 'z') ||
261 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
262 continue;
263 }
264
265 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000266 }
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000269}
270
271/*
272 * Return 0 if name matches wildcard, -1 otherwise
273 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000275{
276 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000278
279 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
281 return -1;
282 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 for (i = 0; i < cn_len; ++i) {
285 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000286 cn_idx = i;
287 break;
288 }
289 }
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (cn_idx == 0) {
292 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000293 }
294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (cn_len - cn_idx == name->len - 1 &&
296 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
297 return 0;
298 }
299
300 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000301}
302
303/*
304 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
305 * variations (but not all).
306 *
307 * Return 0 if equal, -1 otherwise.
308 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000310{
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000312 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 memcmp(a->p, b->p, b->len) == 0) {
314 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000315 }
316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
318 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000319 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 x509_memcasecmp(a->p, b->p, b->len) == 0) {
321 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000322 }
323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000325}
326
327/*
328 * Compare two X.509 Names (aka rdnSequence).
329 *
330 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
331 * we sometimes return unequal when the full algorithm would return equal,
332 * but never the other way. (In particular, we don't do Unicode normalisation
333 * or space folding.)
334 *
335 * Return 0 if equal, -1 otherwise.
336 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000338{
339 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 while (a != NULL || b != NULL) {
341 if (a == NULL || b == NULL) {
342 return -1;
343 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000344
345 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000347 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
349 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000350 }
351
352 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (x509_string_cmp(&a->val, &b->val) != 0) {
354 return -1;
355 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000356
357 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (a->next_merged != b->next_merged) {
359 return -1;
360 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000361
362 a = a->next;
363 b = b->next;
364 }
365
366 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000368}
369
370/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200371 * Reset (init or clear) a verify_chain
372 */
373static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200375{
376 size_t i;
377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200379 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000380 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200381 }
382
383 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000384
385#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
386 ver_chain->trust_ca_cb_result = NULL;
387#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200388}
389
390/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
392 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100393static int x509_get_version(unsigned char **p,
394 const unsigned char *end,
395 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396{
Janos Follath865b3eb2019-12-16 11:46:15 +0000397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398 size_t len;
399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
401 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
402 0)) != 0) {
403 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 }
407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409 }
410
411 end = *p + len;
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
414 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
415 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (*p != end) {
418 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
419 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
420 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423}
424
425/*
426 * Validity ::= SEQUENCE {
427 * notBefore Time,
428 * notAfter Time }
429 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100430static int x509_get_dates(unsigned char **p,
431 const unsigned char *end,
432 mbedtls_x509_time *from,
433 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434{
Janos Follath865b3eb2019-12-16 11:46:15 +0000435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436 size_t len;
437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
439 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
440 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
441 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442
443 end = *p + len;
444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
446 return ret;
447 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
450 return ret;
451 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (*p != end) {
454 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
455 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
456 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459}
460
461/*
462 * X.509 v2/v3 unique identifier (not parsed)
463 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100464static int x509_get_uid(unsigned char **p,
465 const unsigned char *end,
466 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467{
Janos Follath865b3eb2019-12-16 11:46:15 +0000468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (*p == end) {
471 return 0;
472 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473
474 uid->tag = **p;
475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
477 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
478 n)) != 0) {
479 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
480 return 0;
481 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484 }
485
486 uid->p = *p;
487 *p += uid->len;
488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490}
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492static int x509_get_basic_constraints(unsigned char **p,
493 const unsigned char *end,
494 int *ca_istrue,
495 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496{
Janos Follath865b3eb2019-12-16 11:46:15 +0000497 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498 size_t len;
499
500 /*
501 * BasicConstraints ::= SEQUENCE {
502 * cA BOOLEAN DEFAULT FALSE,
503 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
504 */
505 *ca_istrue = 0; /* DEFAULT FALSE */
506 *max_pathlen = 0; /* endless */
507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
509 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
510 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511 }
512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if (*p == end) {
514 return 0;
515 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
518 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
519 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
520 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (ret != 0) {
523 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
524 }
525
526 if (*ca_istrue != 0) {
527 *ca_istrue = 1;
528 }
529 }
530
531 if (*p == end) {
532 return 0;
533 }
534
535 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
536 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
537 }
538
539 if (*p != end) {
540 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
541 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
542 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543
Andrzej Kurek16050742020-04-14 09:49:52 -0400544 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
545 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (*max_pathlen == INT_MAX) {
547 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
548 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
549 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400550
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551 (*max_pathlen)++;
552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554}
555
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556/*
557 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
558 *
559 * KeyPurposeId ::= OBJECT IDENTIFIER
560 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561static int x509_get_ext_key_usage(unsigned char **p,
562 const unsigned char *end,
563 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564{
Janos Follath865b3eb2019-12-16 11:46:15 +0000565 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
568 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
569 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200570
571 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (ext_key_usage->buf.p == NULL) {
573 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
574 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
575 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578}
579
580/*
toth92ga41954d2021-02-12 16:11:17 +0100581 * SubjectKeyIdentifier ::= KeyIdentifier
582 *
583 * KeyIdentifier ::= OCTET STRING
584 */
585static int x509_get_subject_key_id(unsigned char **p,
586 const unsigned char *end,
587 mbedtls_x509_buf *subject_key_id)
588{
589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
590 size_t len = 0u;
591
592 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
593 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100594 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92ga41954d2021-02-12 16:11:17 +0100595 }
596
Przemek Stekiel61aed062023-05-08 11:14:36 +0200597 subject_key_id->len = len;
598 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
599 subject_key_id->p = *p;
600 *p += len;
601
toth92gd96027a2021-04-27 15:41:25 +0200602 if (*p != end) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100603 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
604 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92gd96027a2021-04-27 15:41:25 +0200605 }
606
toth92ga41954d2021-02-12 16:11:17 +0100607 return 0;
608}
609
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100610/*
toth92g8d435a02021-05-10 15:16:33 +0200611 * AuthorityKeyIdentifier ::= SEQUENCE {
612 * keyIdentifier [0] KeyIdentifier OPTIONAL,
613 * authorityCertIssuer [1] GeneralNames OPTIONAL,
614 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
615 *
616 * KeyIdentifier ::= OCTET STRING
617 */
618static int x509_get_authority_key_id(unsigned char **p,
619 unsigned char *end,
620 mbedtls_x509_authority *authority_key_id)
621{
622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
623 size_t len = 0u;
624
625 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100626 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100627 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200628 }
629
Przemek Stekiel6ec839a2023-02-01 11:06:08 +0100630 if (*p + len != end) {
631 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
632 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
633 }
634
Przemek Stekieled9fb782023-05-03 16:27:25 +0200635 ret = mbedtls_asn1_get_tag(p, end, &len,
636 MBEDTLS_ASN1_CONTEXT_SPECIFIC);
637
638 /* KeyIdentifier is an OPTIONAL field */
Przemek Stekiel61aed062023-05-08 11:14:36 +0200639 if (ret == 0) {
toth92g8d435a02021-05-10 15:16:33 +0200640 authority_key_id->keyIdentifier.len = len;
641 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200642 /* Setting tag of the keyIdentfier intentionally to 0x04.
643 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
Przemek Stekiel9a7a7252023-04-17 16:06:57 +0200644 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200645 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
646
647 *p += len;
Przemek Stekiel61aed062023-05-08 11:14:36 +0200648 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
649 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200650 }
651
652 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200653 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200654 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
655 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100656 1)) != 0) {
Przemek Stekielf5b8f782023-04-26 08:55:26 +0200657 /* authorityCertIssuer and authorityCertSerialNumber MUST both
658 be present or both be absent. At this point we expect to have both. */
659 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200660 }
Przemek Stekieled9fb782023-05-03 16:27:25 +0200661 /* "end" also includes the CertSerialNumber field so "len" shall be used */
662 ret = mbedtls_x509_get_subject_alt_name_ext(p,
663 (*p+len),
664 &authority_key_id->authorityCertIssuer);
665 if (ret != 0) {
666 return ret;
667 }
668
669 /* Getting authorityCertSerialNumber using the required specific class tag [2] */
670 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
671 MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) {
672 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
673 }
674 authority_key_id->authorityCertSerialNumber.len = len;
675 authority_key_id->authorityCertSerialNumber.p = *p;
676 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER;
677 *p += len;
toth92g8d435a02021-05-10 15:16:33 +0200678 }
679
680 if (*p != end) {
Gilles Peskine8085f512024-09-15 12:45:44 +0200681 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
682 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92g8d435a02021-05-10 15:16:33 +0200683 }
684
685 return 0;
686}
687
688/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200689 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
690 *
691 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
692 *
693 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
694 *
695 * PolicyInformation ::= SEQUENCE {
696 * policyIdentifier CertPolicyId,
697 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
698 * PolicyQualifierInfo OPTIONAL }
699 *
700 * CertPolicyId ::= OBJECT IDENTIFIER
701 *
702 * PolicyQualifierInfo ::= SEQUENCE {
703 * policyQualifierId PolicyQualifierId,
704 * qualifier ANY DEFINED BY policyQualifierId }
705 *
706 * -- policyQualifierIds for Internet policy qualifiers
707 *
708 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
709 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
710 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
711 *
712 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
713 *
714 * Qualifier ::= CHOICE {
715 * cPSuri CPSuri,
716 * userNotice UserNotice }
717 *
718 * CPSuri ::= IA5String
719 *
720 * UserNotice ::= SEQUENCE {
721 * noticeRef NoticeReference OPTIONAL,
722 * explicitText DisplayText OPTIONAL }
723 *
724 * NoticeReference ::= SEQUENCE {
725 * organization DisplayText,
726 * noticeNumbers SEQUENCE OF INTEGER }
727 *
728 * DisplayText ::= CHOICE {
729 * ia5String IA5String (SIZE (1..200)),
730 * visibleString VisibleString (SIZE (1..200)),
731 * bmpString BMPString (SIZE (1..200)),
732 * utf8String UTF8String (SIZE (1..200)) }
733 *
734 * NOTE: we only parse and use anyPolicy without qualifiers at this point
735 * as defined in RFC 5280.
736 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737static int x509_get_certificate_policies(unsigned char **p,
738 const unsigned char *end,
739 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200740{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300741 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200742 size_t len;
743 mbedtls_asn1_buf *buf;
744 mbedtls_asn1_sequence *cur = certificate_policies;
745
746 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 ret = mbedtls_asn1_get_tag(p, end, &len,
748 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
749 if (ret != 0) {
750 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
751 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (*p + len != end) {
754 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
755 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
756 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200757
758 /*
759 * Cannot be an empty sequence.
760 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if (len == 0) {
762 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
763 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
764 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200767 mbedtls_x509_buf policy_oid;
768 const unsigned char *policy_end;
769
770 /*
771 * Get the policy sequence
772 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
774 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
775 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
776 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200777
778 policy_end = *p + len;
779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
781 MBEDTLS_ASN1_OID)) != 0) {
782 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
783 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200784
785 policy_oid.tag = MBEDTLS_ASN1_OID;
786 policy_oid.len = len;
787 policy_oid.p = *p;
788
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300789 /*
790 * Only AnyPolicy is currently supported when enforcing policy.
791 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300793 /*
794 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200795 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300796 */
797 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
798 }
799
Ron Eldor74d9acc2019-03-21 14:00:03 +0200800 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if (cur->buf.p != NULL) {
802 if (cur->next != NULL) {
803 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
804 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if (cur->next == NULL) {
809 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
810 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
811 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200812
813 cur = cur->next;
814 }
815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200817 buf->tag = policy_oid.tag;
818 buf->p = policy_oid.p;
819 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300820
821 *p += len;
822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 /*
824 * If there is an optional qualifier, then *p < policy_end
825 * Check the Qualifier len to verify it doesn't exceed policy_end.
826 */
827 if (*p < policy_end) {
828 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
829 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
830 0) {
831 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
832 }
Ron Eldor08063792019-05-13 16:38:39 +0300833 /*
834 * Skip the optional policy qualifiers.
835 */
836 *p += len;
837 }
838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (*p != policy_end) {
840 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
841 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
842 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200843 }
844
845 /* Set final sequence entry's next pointer to NULL */
846 cur->next = NULL;
847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (*p != end) {
849 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
850 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
851 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200854}
855
856/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200857 * X.509 v3 extensions
858 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200859 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860static int x509_get_crt_ext(unsigned char **p,
861 const unsigned char *end,
862 mbedtls_x509_crt *crt,
863 mbedtls_x509_crt_ext_cb_t cb,
864 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200865{
Janos Follath865b3eb2019-12-16 11:46:15 +0000866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200867 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200868 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (*p == end) {
871 return 0;
872 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
875 return ret;
876 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200877
Hanno Becker12f62fb2019-02-12 17:22:36 +0000878 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 /*
881 * Extension ::= SEQUENCE {
882 * extnID OBJECT IDENTIFIER,
883 * critical BOOLEAN DEFAULT FALSE,
884 * extnValue OCTET STRING }
885 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 int is_critical = 0; /* DEFAULT FALSE */
888 int ext_type = 0;
889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
891 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
892 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
893 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894
895 end_ext_data = *p + len;
896
897 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
899 MBEDTLS_ASN1_OID)) != 0) {
900 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
901 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902
k-stachowiak463928a2018-07-24 12:50:59 +0200903 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 extn_oid.p = *p;
905 *p += extn_oid.len;
906
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
909 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
910 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
911 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912
913 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
915 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
916 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
917 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200919 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920 end_ext_octet = *p + len;
921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if (end_ext_octet != end_ext_data) {
923 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
924 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
925 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926
927 /*
928 * Detect supported extensions
929 */
Gilles Peskine532e3ee2025-05-07 20:37:15 +0200930 ret = mbedtls_x509_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200933 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if (cb != NULL) {
935 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
936 if (ret != 0 && is_critical) {
937 return ret;
938 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200939 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200940 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200941 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200942
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943 /* No parser found, skip extension */
944 *p = end_ext_octet;
945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
949 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 continue;
952 }
953
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100954 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 if ((crt->ext_types & ext_type) != 0) {
956 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
957 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100958
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 crt->ext_types |= ext_type;
960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 switch (ext_type) {
962 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
963 /* Parse basic constraints */
964 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
965 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
966 return ret;
967 }
968 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 case MBEDTLS_X509_EXT_KEY_USAGE:
971 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100972 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
973 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 return ret;
975 }
976 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
979 /* Parse extended key usage */
980 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
981 &crt->ext_key_usage)) != 0) {
982 return ret;
983 }
984 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985
toth92ga41954d2021-02-12 16:11:17 +0100986 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
987 /* Parse subject key identifier */
988 if ((ret = x509_get_subject_key_id(p, end_ext_data,
989 &crt->subject_key_id)) != 0) {
990 return ret;
991 }
992 break;
toth92g8d435a02021-05-10 15:16:33 +0200993
toth92ga41954d2021-02-12 16:11:17 +0100994 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
995 /* Parse authority key identifier */
996 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
997 &crt->authority_key_id)) != 0) {
998 return ret;
999 }
1000 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001002 /* Parse subject alt name
1003 * SubjectAltName ::= GeneralNames
1004 */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001005 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1006 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 return ret;
1008 }
1009 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1012 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001013 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1014 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 return ret;
1016 }
1017 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018
Gilles Peskine32a11122025-04-09 21:51:46 +02001019 case MBEDTLS_X509_EXT_CERTIFICATE_POLICIES:
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 /* Parse certificate policies type */
1021 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1022 &crt->certificate_policies)) != 0) {
1023 /* Give the callback (if any) a chance to handle the extension
1024 * if it contains unsupported policies */
1025 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1026 cb(p_ctx, crt, &extn_oid, is_critical,
1027 start_ext_octet, end_ext_octet) == 0) {
1028 break;
1029 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (is_critical) {
1032 return ret;
1033 } else
1034 /*
1035 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1036 * cannot interpret or enforce the policy. However, it is up to
1037 * the user to choose how to enforce the policies,
1038 * unless the extension is critical.
1039 */
1040 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1041 return ret;
1042 }
1043 }
1044 break;
1045
1046 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001047 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 * If this is a non-critical extension, which the oid layer
1049 * supports, but there isn't an x509 parser for it,
1050 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001051 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (is_critical) {
1053 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1054 } else {
1055 *p = end_ext_octet;
1056 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001057 }
1058 }
1059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (*p != end) {
1061 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1062 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1063 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001066}
1067
1068/*
1069 * Parse and fill a single X.509 certificate in DER format
1070 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1072 const unsigned char *buf,
1073 size_t buflen,
1074 int make_copy,
1075 mbedtls_x509_crt_ext_cb_t cb,
1076 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077{
Janos Follath865b3eb2019-12-16 11:46:15 +00001078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079 size_t len;
1080 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1084 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1085 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086
1087 /*
1088 * Check for valid input
1089 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if (crt == NULL || buf == NULL) {
1091 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1092 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001094 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001096 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097 end = p + len;
1098
1099 /*
1100 * Certificate ::= SEQUENCE {
1101 * tbsCertificate TBSCertificate,
1102 * signatureAlgorithm AlgorithmIdentifier,
1103 * signatureValue BIT STRING }
1104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1106 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1107 mbedtls_x509_crt_free(crt);
1108 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109 }
1110
Janos Follathcc0e49d2016-02-17 14:34:12 +00001111 end = crt_end = p + len;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001112 crt->raw.len = (size_t) (crt_end - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001114 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1116 if (crt->raw.p == NULL) {
1117 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1118 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001121 crt->own_buffer = 1;
1122
1123 p += crt->raw.len - len;
1124 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 } else {
1126 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001127 crt->own_buffer = 0;
1128 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001129
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001130 /*
1131 * TBSCertificate ::= SEQUENCE {
1132 */
1133 crt->tbs.p = p;
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1137 mbedtls_x509_crt_free(crt);
1138 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139 }
1140
1141 end = p + len;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001142 crt->tbs.len = (size_t) (end - crt->tbs.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001143
1144 /*
1145 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1146 *
1147 * CertificateSerialNumber ::= INTEGER
1148 *
1149 * signature AlgorithmIdentifier
1150 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1152 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1153 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1154 &sig_params1)) != 0) {
1155 mbedtls_x509_crt_free(crt);
1156 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 }
1158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if (crt->version < 0 || crt->version > 2) {
1160 mbedtls_x509_crt_free(crt);
1161 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162 }
1163
Andres AG7ca4a032017-03-09 16:16:11 +00001164 crt->version++;
1165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
Valerio Setti68878cc2025-04-10 23:30:26 +02001167 &crt->sig_md, &crt->sig_pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 mbedtls_x509_crt_free(crt);
1169 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170 }
1171
1172 /*
1173 * issuer Name
1174 */
1175 crt->issuer_raw.p = p;
1176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1178 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1179 mbedtls_x509_crt_free(crt);
1180 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181 }
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1184 mbedtls_x509_crt_free(crt);
1185 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186 }
1187
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001188 crt->issuer_raw.len = (size_t) (p - crt->issuer_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189
1190 /*
1191 * Validity ::= SEQUENCE {
1192 * notBefore Time,
1193 * notAfter Time }
1194 *
1195 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1197 &crt->valid_to)) != 0) {
1198 mbedtls_x509_crt_free(crt);
1199 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001200 }
1201
1202 /*
1203 * subject Name
1204 */
1205 crt->subject_raw.p = p;
1206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1208 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1209 mbedtls_x509_crt_free(crt);
1210 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001211 }
1212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1214 mbedtls_x509_crt_free(crt);
1215 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001216 }
1217
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001218 crt->subject_raw.len = (size_t) (p - crt->subject_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219
1220 /*
1221 * SubjectPublicKeyInfo
1222 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001223 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1225 mbedtls_x509_crt_free(crt);
1226 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227 }
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001228 crt->pk_raw.len = (size_t) (p - crt->pk_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229
1230 /*
1231 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1232 * -- If present, version shall be v2 or v3
1233 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1234 * -- If present, version shall be v2 or v3
1235 * extensions [3] EXPLICIT Extensions OPTIONAL
1236 * -- If present, version shall be v3
1237 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if (crt->version == 2 || crt->version == 3) {
1239 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1240 if (ret != 0) {
1241 mbedtls_x509_crt_free(crt);
1242 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001243 }
1244 }
1245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (crt->version == 2 || crt->version == 3) {
1247 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1248 if (ret != 0) {
1249 mbedtls_x509_crt_free(crt);
1250 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001251 }
1252 }
1253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 if (crt->version == 3) {
1255 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1256 if (ret != 0) {
1257 mbedtls_x509_crt_free(crt);
1258 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001259 }
1260 }
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (p != end) {
1263 mbedtls_x509_crt_free(crt);
1264 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1265 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001266 }
1267
1268 end = crt_end;
1269
1270 /*
1271 * }
1272 * -- end of TBSCertificate
1273 *
1274 * signatureAlgorithm AlgorithmIdentifier,
1275 * signatureValue BIT STRING
1276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1278 mbedtls_x509_crt_free(crt);
1279 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001280 }
1281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (crt->sig_oid.len != sig_oid2.len ||
1283 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001284 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001285 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 (sig_params1.len != 0 &&
1287 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1288 mbedtls_x509_crt_free(crt);
1289 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001290 }
1291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1293 mbedtls_x509_crt_free(crt);
1294 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001295 }
1296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (p != end) {
1298 mbedtls_x509_crt_free(crt);
1299 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1300 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001301 }
1302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304}
1305
1306/*
1307 * Parse one X.509 certificate in DER format from a buffer and add them to a
1308 * chained list
1309 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001310static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1311 const unsigned char *buf,
1312 size_t buflen,
1313 int make_copy,
1314 mbedtls_x509_crt_ext_cb_t cb,
1315 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001316{
Janos Follath865b3eb2019-12-16 11:46:15 +00001317 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319
1320 /*
1321 * Check for valid input
1322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (crt == NULL || buf == NULL) {
1324 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1325 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001326
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328 prev = crt;
1329 crt = crt->next;
1330 }
1331
1332 /*
1333 * Add new certificate on the end of the chain if needed.
1334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (crt->version != 0 && crt->next == NULL) {
1336 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if (crt->next == NULL) {
1339 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1340 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001341
1342 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001345 }
1346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1348 if (ret != 0) {
1349 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001350 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 if (crt != chain) {
1354 mbedtls_free(crt);
1355 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001358 }
1359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361}
1362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1364 const unsigned char *buf,
1365 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001366{
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001368}
1369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1371 const unsigned char *buf,
1372 size_t buflen,
1373 int make_copy,
1374 mbedtls_x509_crt_ext_cb_t cb,
1375 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001376{
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001378}
1379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1381 const unsigned char *buf,
1382 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001383{
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001385}
1386
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001387/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001388 * Parse one or more PEM certificates from a buffer and add them to the chained
1389 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001390 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001391int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1392 const unsigned char *buf,
1393 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001394{
Janos Follath98e28a72016-05-31 14:03:54 +01001395#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001396 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001398#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001399
1400 /*
1401 * Check for valid input
1402 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if (chain == NULL || buf == NULL) {
1404 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1405 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406
1407 /*
1408 * Determine buffer content. Buffer contains either one DER certificate or
1409 * one or more PEM certificates.
1410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1413 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001415 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1418 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1419 }
Janos Follath98e28a72016-05-31 14:03:54 +01001420#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001422#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001426 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001428
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001429 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001434 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 ret = mbedtls_pem_read_buffer(&pem,
1436 "-----BEGIN CERTIFICATE-----",
1437 "-----END CERTIFICATE-----",
1438 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441 /*
1442 * Was PEM encoded
1443 */
1444 buflen -= use_len;
1445 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1447 return ret;
1448 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1449 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001450
1451 /*
1452 * PEM header and footer were found
1453 */
1454 buflen -= use_len;
1455 buf += use_len;
1456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001458 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460
Paul Bakker5a5fa922014-09-26 14:53:04 +02001461 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001462 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001464 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472 /*
1473 * Quit parsing on a memory error
1474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1476 return ret;
1477 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482
1483 total_failed++;
1484 continue;
1485 }
1486
1487 success = 1;
1488 }
1489 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (success) {
1492 return total_failed;
1493 } else if (first_error) {
1494 return first_error;
1495 } else {
1496 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1497 }
Janos Follath98e28a72016-05-31 14:03:54 +01001498#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001499}
1500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001502/*
1503 * Load one or more certificates and add them to the chained list
1504 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001505int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506{
Janos Follath865b3eb2019-12-16 11:46:15 +00001507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508 size_t n;
1509 unsigned char *buf;
1510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1512 return ret;
1513 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001517 mbedtls_zeroize_and_free(buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520}
1521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001523{
1524 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001525#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526 int w_ret;
1527 WCHAR szDir[MAX_PATH];
1528 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001529 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531
Paul Bakker9af723c2014-05-01 13:03:14 +02001532 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533 HANDLE hFind;
1534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 if (len > MAX_PATH - 3) {
1536 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1537 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 memset(szDir, 0, sizeof(szDir));
1540 memset(filename, 0, MAX_PATH);
1541 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001542 filename[len++] = '\\';
1543 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544 filename[len++] = '*';
1545
Simon Butcher35e5dad2018-03-15 15:00:03 +00001546 /*
Minos Galanakis08a67cc2023-09-22 16:00:06 +01001547 * Note this function uses the code page CP_ACP which is the system default
1548 * ANSI codepage. The input string is always described in BYTES and the
1549 * output length is described in WCHARs.
Simon Butcher35e5dad2018-03-15 15:00:03 +00001550 */
Minos Galanakisa9bb34c2023-09-25 14:41:12 +01001551 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 MAX_PATH - 3);
1553 if (w_ret == 0) {
1554 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1555 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 hFind = FindFirstFileW(szDir, &file_data);
1558 if (hFind == INVALID_HANDLE_VALUE) {
1559 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1560 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561
1562 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 do {
1564 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 }
Simon Butcherde573f52018-07-05 09:11:30 +01001569 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Minos Galanakis59108d32023-09-25 14:11:22 +01001570 -1, p, (int) len, NULL, NULL);
Minos Galanakisa277b212023-08-09 16:32:22 +01001571 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001572 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1573 goto cleanup;
1574 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1577 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 }
1582 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587
Ron Eldor36d90422017-01-09 15:09:16 +02001588cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001590#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001591 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001592 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001594 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001595 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (dir == NULL) {
1599 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1600 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001601
Ron Eldor63140682017-01-09 19:27:59 +02001602#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1604 closedir(dir);
1605 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001606 }
Ron Eldor63140682017-01-09 19:27:59 +02001607#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001612 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001615 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001616 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1617 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 } else if (stat(entry_name, &sb) == -1) {
1619 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001620 /* Broken symbolic link - ignore this entry.
1621 stat(2) will return this error for either (a) a dangling
1622 symlink or (b) a missing file.
1623 Given that we have just obtained the filename from readdir,
1624 assume that it does exist and therefore treat this as a
1625 dangling symlink. */
1626 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001628 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001629 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1630 goto cleanup;
1631 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001632 }
1633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637
1638 // Ignore parse errors
1639 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1641 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001642 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001647
1648cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001650
Ron Eldor63140682017-01-09 19:27:59 +02001651#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 }
Ron Eldor63140682017-01-09 19:27:59 +02001655#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001656
Paul Bakkerbe089b02013-10-14 15:51:50 +02001657#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001662
Peter Kolbus9a969b62018-12-11 13:55:56 -06001663#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001664#define PRINT_ITEM(i) \
1665 do { \
1666 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1667 MBEDTLS_X509_SAFE_SNPRINTF; \
1668 sep = ", "; \
1669 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001670
Przemek Stekielf4194942023-04-24 09:52:17 +02001671#define CERT_TYPE(type, name) \
1672 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001673 if (ns_cert_type & (type)) { \
1674 PRINT_ITEM(name); \
1675 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001676 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001677
Przemek Stekielf4194942023-04-24 09:52:17 +02001678#define KEY_USAGE(code, name) \
1679 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001680 if (key_usage & (code)) { \
1681 PRINT_ITEM(name); \
1682 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001683 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685static int x509_info_ext_key_usage(char **buf, size_t *size,
1686 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001687{
Janos Follath865b3eb2019-12-16 11:46:15 +00001688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001689 const char *desc;
1690 size_t n = *size;
1691 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001693 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 while (cur != NULL) {
Gilles Peskine532e3ee2025-05-07 20:37:15 +02001696 if (mbedtls_x509_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001697 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001701 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001702
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001703 sep = ", ";
1704
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001705 cur = cur->next;
1706 }
1707
1708 *size = n;
1709 *buf = p;
1710
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001712}
1713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714static int x509_info_cert_policies(char **buf, size_t *size,
1715 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001716{
Janos Follath865b3eb2019-12-16 11:46:15 +00001717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001718 const char *desc;
1719 size_t n = *size;
1720 char *p = *buf;
1721 const mbedtls_x509_sequence *cur = certificate_policies;
1722 const char *sep = "";
1723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 while (cur != NULL) {
Gilles Peskine532e3ee2025-05-07 20:37:15 +02001725 if (mbedtls_x509_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001726 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001730 MBEDTLS_X509_SAFE_SNPRINTF;
1731
1732 sep = ", ";
1733
1734 cur = cur->next;
1735 }
1736
1737 *size = n;
1738 *buf = p;
1739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001741}
1742
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001743/*
1744 * Return an informational string about the certificate.
1745 */
Stefan Gloorb5c079b2025-02-21 10:33:51 +01001746#define MBEDTLS_BEFORE_COLON 18
1747#define MBEDTLS_BEFORE_COLON_STR "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001748int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1749 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001750{
Janos Follath865b3eb2019-12-16 11:46:15 +00001751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001752 size_t n;
1753 char *p;
Stefan Gloorb5c079b2025-02-21 10:33:51 +01001754 char key_size_str[MBEDTLS_BEFORE_COLON];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001755
1756 p = buf;
1757 n = size;
1758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 if (NULL == crt) {
1760 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001761 MBEDTLS_X509_SAFE_SNPRINTF;
1762
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001764 }
1765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1767 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001768 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1770 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001771 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001774 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001777 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001779 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001782 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001784 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1787 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1788 crt->valid_from.year, crt->valid_from.mon,
1789 crt->valid_from.day, crt->valid_from.hour,
1790 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001791 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001792
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1794 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1795 crt->valid_to.year, crt->valid_to.mon,
1796 crt->valid_to.day, crt->valid_to.hour,
1797 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001798 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001801 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001802
Valerio Settid24dfad2025-04-23 11:13:02 +02001803 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk, crt->sig_md);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001804 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001805
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001806 /* Key size */
Stefan Gloorb5c079b2025-02-21 10:33:51 +01001807 if ((ret = mbedtls_x509_key_size_helper(key_size_str, MBEDTLS_BEFORE_COLON,
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 mbedtls_pk_get_name(&crt->pk))) != 0) {
1809 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001810 }
1811
Stefan Gloorb5c079b2025-02-21 10:33:51 +01001812 ret = mbedtls_snprintf(p, n, "\n%s%-" MBEDTLS_BEFORE_COLON_STR "s: %d bits",
1813 prefix, key_size_str, (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001814 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001815
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001816 /*
1817 * Optional extensions
1818 */
1819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1821 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1822 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001823 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 if (crt->max_pathlen > 0) {
1826 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001827 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001828 }
1829 }
1830
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1832 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001833 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001834
Przemek Stekiel21c37282023-01-16 08:47:49 +01001835 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1836 &crt->subject_alt_names,
1837 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 return ret;
1839 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001840 }
1841
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1843 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001844 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001845
Przemek Stekiel21c37282023-01-16 08:47:49 +01001846 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 return ret;
1848 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001849 }
1850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1852 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001853 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001854
Przemek Stekiel21c37282023-01-16 08:47:49 +01001855 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 return ret;
1857 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001858 }
1859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1861 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001862 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001863
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if ((ret = x509_info_ext_key_usage(&p, &n,
1865 &crt->ext_key_usage)) != 0) {
1866 return ret;
1867 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001868 }
1869
Gilles Peskine32a11122025-04-09 21:51:46 +02001870 if (crt->ext_types & MBEDTLS_X509_EXT_CERTIFICATE_POLICIES) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001872 MBEDTLS_X509_SAFE_SNPRINTF;
1873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 if ((ret = x509_info_cert_policies(&p, &n,
1875 &crt->certificate_policies)) != 0) {
1876 return ret;
1877 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001878 }
1879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001881 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001884}
1885
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001886struct x509_crt_verify_string {
1887 int code;
1888 const char *string;
1889};
1890
Gilles Peskine449bd832023-01-11 14:50:10 +01001891#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001892static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001893 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001894 { 0, NULL }
1895};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001896#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1899 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001900{
Janos Follath865b3eb2019-12-16 11:46:15 +00001901 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001902 const struct x509_crt_verify_string *cur;
1903 char *p = buf;
1904 size_t n = size;
1905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1907 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001908 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001910
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001912 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001913 flags ^= cur->code;
1914 }
1915
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 if (flags != 0) {
1917 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1918 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001919 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001920 }
1921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001923}
Hanno Becker612a2f12020-10-09 09:19:39 +01001924#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1927 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001928{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001929 unsigned int usage_must, usage_may;
1930 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001932
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1934 return 0;
1935 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001936
1937 usage_must = usage & ~may_mask;
1938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1940 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1941 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001942
1943 usage_may = usage & may_mask;
1944
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1946 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1947 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001950}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1953 const char *usage_oid,
1954 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001957
1958 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1960 return 0;
1961 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001962
1963 /*
1964 * Look for the requested usage (or wildcard ANY) in our list
1965 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (cur_oid->len == usage_len &&
1970 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1971 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001972 }
1973
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1975 return 0;
1976 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001977 }
1978
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001980}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001983/*
1984 * Return 1 if the certificate is revoked, or 0 otherwise.
1985 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001986int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001987{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 while (cur != NULL && cur->serial.len != 0) {
1991 if (crt->serial.len == cur->serial.len &&
1992 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
1993 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001994 }
1995
1996 cur = cur->next;
1997 }
1998
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002000}
2001
2002/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002003 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002004 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002006static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2007 mbedtls_x509_crl *crl_list,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002008 const mbedtls_x509_crt_profile *profile,
2009 const mbedtls_x509_time *now)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002010{
2011 int flags = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002012 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002013 psa_algorithm_t psa_algorithm;
pespacek7599a772022-02-07 14:40:55 +01002014 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002015
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 if (ca == NULL) {
2017 return flags;
2018 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 while (crl_list != NULL) {
2021 if (crl_list->version == 0 ||
2022 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002023 crl_list = crl_list->next;
2024 continue;
2025 }
2026
2027 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002028 * Check if the CA is configured to sign CRLs
2029 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 if (mbedtls_x509_crt_check_key_usage(ca,
2031 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002033 break;
2034 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002035
2036 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002037 * Check if CRL is correctly signed by the trusted CA
2038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002040 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002044 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002046
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002047 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 if (psa_hash_compute(psa_algorithm,
2049 crl_list->tbs.p,
2050 crl_list->tbs.len,
2051 hash,
2052 sizeof(hash),
2053 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002054 /* Note: this can't happen except after an internal error */
2055 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2056 break;
2057 }
pespaceka7a64692022-02-14 15:18:43 +01002058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002060 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002062
Valerio Setti7f6f4e62025-04-23 11:29:51 +02002063 if (mbedtls_pk_verify_ext(crl_list->sig_pk, NULL, &ca->pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 crl_list->sig_md, hash, hash_length,
2065 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002067 break;
2068 }
2069
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002070#if defined(MBEDTLS_HAVE_TIME_DATE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002071 /*
2072 * Check for validity of CRL (Do not drop out)
2073 */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002074 if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002077
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002078 if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002079 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002081#else
2082 ((void) now);
2083#endif
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002084
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002085 /*
2086 * Check if certificate is revoked
2087 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002090 break;
2091 }
2092
2093 crl_list = crl_list->next;
2094 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002097}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002099
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002100/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002101 * Check the signature of a certificate by its parent
2102 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002103static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2104 mbedtls_x509_crt *parent,
2105 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002106{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002107 size_t hash_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002108 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002109 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002110 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 status = psa_hash_compute(hash_alg,
2113 child->tbs.p,
2114 child->tbs.len,
2115 hash,
2116 sizeof(hash),
2117 &hash_len);
2118 if (status != PSA_SUCCESS) {
2119 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002120 }
2121
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002122 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2124 return -1;
2125 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002126
Valerio Settieaf57892025-05-06 17:07:09 +02002127#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2129 return mbedtls_pk_verify_restartable(&parent->pk,
2130 child->sig_md, hash, hash_len,
2131 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002132 }
2133#else
2134 (void) rs_ctx;
2135#endif
2136
Valerio Setti7f6f4e62025-04-23 11:29:51 +02002137 return mbedtls_pk_verify_ext(child->sig_pk, NULL, &parent->pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 child->sig_md, hash, hash_len,
2139 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002140}
2141
2142/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002143 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2144 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002145 *
2146 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002148static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2149 const mbedtls_x509_crt *parent,
2150 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002151{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002152 int need_ca_bit;
2153
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002154 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2156 return -1;
2157 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002158
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002159 /* Parent must have the basicConstraints CA bit set as a general rule */
2160 need_ca_bit = 1;
2161
2162 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002164 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002165 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (need_ca_bit && !parent->ca_istrue) {
2168 return -1;
2169 }
2170
2171 if (need_ca_bit &&
2172 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2173 return -1;
2174 }
2175
2176 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002177}
2178
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002179/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002180 * Find a suitable parent for child in candidates, or return NULL.
2181 *
2182 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002183 * 1. subject name matches child's issuer
2184 * 2. if necessary, the CA bit is set and key usage allows signing certs
2185 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002186 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002187 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002188 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002189 * If there's a suitable candidate which is also time-valid, return the first
2190 * such. Otherwise, return the first suitable candidate (or NULL if there is
2191 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002192 *
2193 * The rationale for this rule is that someone could have a list of trusted
2194 * roots with two versions on the same root with different validity periods.
2195 * (At least one user reported having such a list and wanted it to just work.)
2196 * The reason we don't just require time-validity is that generally there is
2197 * only one version, and if it's expired we want the flags to state that
2198 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002199 *
2200 * The rationale for rule 3 (signature for trusted roots) is that users might
2201 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002202 * way we select the correct one is by checking the signature (as we don't
2203 * rely on key identifier extensions). (This is one way users might choose to
2204 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002205 *
2206 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002207 * - [in] child: certificate for which we're looking for a parent
2208 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002209 * - [out] r_parent: parent found (or NULL)
2210 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002211 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2212 * of the chain, 0 otherwise
2213 * - [in] path_cnt: number of intermediates seen so far
2214 * - [in] self_cnt: number of self-signed intermediates seen so far
2215 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002216 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002217 *
2218 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002219 * - 0 on success
2220 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002221 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002222static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 mbedtls_x509_crt *child,
2224 mbedtls_x509_crt *candidates,
2225 mbedtls_x509_crt **r_parent,
2226 int *r_signature_is_good,
2227 int top,
2228 unsigned path_cnt,
2229 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002230 mbedtls_x509_crt_restart_ctx *rs_ctx,
2231 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002232{
Janos Follath865b3eb2019-12-16 11:46:15 +00002233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002234 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002235 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002236
Valerio Settieaf57892025-05-06 17:07:09 +02002237#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002238 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002240 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002241 parent = rs_ctx->parent;
2242 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002243 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002244
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002245 /* clear saved state */
2246 rs_ctx->parent = NULL;
2247 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002248 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002249
2250 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002251 goto check_signature;
2252 }
2253#endif
2254
2255 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002256 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002259 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002261 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002263
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002264 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 if (parent->max_pathlen > 0 &&
2266 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002267 continue;
2268 }
2269
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002270 /* Signature */
Valerio Settieaf57892025-05-06 17:07:09 +02002271#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002272check_signature:
2273#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002275
Valerio Settieaf57892025-05-06 17:07:09 +02002276#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002278 /* save state */
2279 rs_ctx->parent = parent;
2280 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002281 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002284 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002285#else
2286 (void) ret;
2287#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002288
2289 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002291 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002293
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002294#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002295 /* optional time check */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002296 if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2297 mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002299 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002300 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002301 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002302
2303 continue;
2304 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002305#else
2306 ((void) now);
2307#endif
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002308
Andy Gross1f627142019-01-30 10:25:53 -06002309 *r_parent = parent;
2310 *r_signature_is_good = signature_is_good;
2311
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002312 break;
2313 }
2314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002316 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002317 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002318 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002321}
2322
2323/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002324 * Find a parent in trusted CAs or the provided chain, or return NULL.
2325 *
2326 * Searches in trusted CAs first, and return the first suitable parent found
2327 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002328 *
2329 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002330 * - [in] child: certificate for which we're looking for a parent, followed
2331 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002332 * - [in] trust_ca: list of locally trusted certificates
2333 * - [out] parent: parent found (or NULL)
2334 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2335 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2336 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2337 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002338 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002339 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002340 *
2341 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002342 * - 0 on success
2343 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002344 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002345static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 mbedtls_x509_crt *child,
2347 mbedtls_x509_crt *trust_ca,
2348 mbedtls_x509_crt **parent,
2349 int *parent_is_trusted,
2350 int *signature_is_good,
2351 unsigned path_cnt,
2352 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002353 mbedtls_x509_crt_restart_ctx *rs_ctx,
2354 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002355{
Janos Follath865b3eb2019-12-16 11:46:15 +00002356 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002357 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002358
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002359 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002360
Valerio Settieaf57892025-05-06 17:07:09 +02002361#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002362 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002364 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002365 rs_ctx->parent_is_trusted = -1;
2366 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002367#endif
2368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002370 search_list = *parent_is_trusted ? trust_ca : child->next;
2371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 ret = x509_crt_find_parent_in(child, search_list,
2373 parent, signature_is_good,
2374 *parent_is_trusted,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002375 path_cnt, self_cnt, rs_ctx, now);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002376
Valerio Settieaf57892025-05-06 17:07:09 +02002377#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002379 /* save state */
2380 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002382 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002383#else
2384 (void) ret;
2385#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002386
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002387 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002389 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002391
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002392 /* prepare second iteration */
2393 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002394 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002395
2396 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002398 *parent_is_trusted = 0;
2399 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002400 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002403}
2404
2405/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002406 * Check if an end-entity certificate is locally trusted
2407 *
2408 * Currently we require such certificates to be self-signed (actually only
2409 * check for self-issued as self-signatures are not checked)
2410 */
2411static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 mbedtls_x509_crt *crt,
2413 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002414{
2415 mbedtls_x509_crt *cur;
2416
2417 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2419 return -1;
2420 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002421
2422 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2424 if (crt->raw.len == cur->raw.len &&
2425 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2426 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002427 }
2428 }
2429
2430 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002432}
2433
2434/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002435 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002436 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002437 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2438 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002439 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002440 * such that every cert in the chain is a child of the next one,
2441 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002442 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002443 * Verify that chain and return it with flags for all issues found.
2444 *
2445 * Special cases:
2446 * - EE == Rj -> return a one-element list containing it
2447 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2448 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002449 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002450 * Tests for (aspects of) this function should include at least:
2451 * - trusted EE
2452 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002453 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002454 * - if relevant: EE untrusted
2455 * - if relevant: EE -> intermediate, untrusted
2456 * with the aspect under test checked at each relevant level (EE, int, root).
2457 * For some aspects longer chains are required, but usually length 2 is
2458 * enough (but length 1 is not in general).
2459 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002460 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002461 * - [in] crt: the cert list EE, C1, ..., Cn
2462 * - [in] trust_ca: the trusted list R1, ..., Rp
2463 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002464 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002465 * Only valid when return value is 0, may contain garbage otherwise!
2466 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002467 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002468 *
2469 * Return value:
2470 * - non-zero if the chain could not be fully built and examined
2471 * - 0 is the chain was successfully built and examined,
2472 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002473 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002474static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 mbedtls_x509_crt *crt,
2476 mbedtls_x509_crt *trust_ca,
2477 mbedtls_x509_crl *ca_crl,
2478 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2479 void *p_ca_cb,
2480 const mbedtls_x509_crt_profile *profile,
2481 mbedtls_x509_crt_verify_chain *ver_chain,
2482 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002483{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002484 /* Don't initialize any of those variables here, so that the compiler can
2485 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002486 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002487 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002488 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002489 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002490 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002491 int parent_is_trusted;
2492 int child_is_trusted;
2493 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002494 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002495 mbedtls_x509_crt *cur_trust_ca = NULL;
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002496 mbedtls_x509_time now;
2497
2498#if defined(MBEDTLS_HAVE_TIME_DATE)
2499 if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2500 return MBEDTLS_ERR_X509_FATAL_ERROR;
2501 }
2502#endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002503
Valerio Settieaf57892025-05-06 17:07:09 +02002504#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002505 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002507 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002508 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002509 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002510
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002511 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002512 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002513 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002514 flags = &cur->flags;
2515
2516 goto find_parent;
2517 }
Valerio Settieaf57892025-05-06 17:07:09 +02002518#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002519
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002520 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002521 self_cnt = 0;
2522 parent_is_trusted = 0;
2523 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002526 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002527 cur = &ver_chain->items[ver_chain->len];
2528 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002529 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002530 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002531 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002532
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002533#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002534 /* Check time-validity (all certificates) */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002535 if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002536 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002538
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002539 if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002540 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002542#endif
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002543
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002544 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 if (child_is_trusted) {
2546 return 0;
2547 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002548
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002549 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002551 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002555 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002557
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002558 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (ver_chain->len == 1 &&
2560 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2561 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002562 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002563
Valerio Settieaf57892025-05-06 17:07:09 +02002564#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002565find_parent:
2566#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002567
2568 /* Obtain list of potential trusted signers from CA callback,
2569 * or use statically provided list. */
2570#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 if (f_ca_cb != NULL) {
2572 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2573 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002574 ver_chain->trust_ca_cb_result = NULL;
2575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2577 if (ret != 0) {
2578 return MBEDTLS_ERR_X509_FATAL_ERROR;
2579 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002580
2581 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002583#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2584 {
2585 ((void) f_ca_cb);
2586 ((void) p_ca_cb);
2587 cur_trust_ca = trust_ca;
2588 }
2589
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002590 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2592 &parent_is_trusted, &signature_is_good,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002593 ver_chain->len - 1, self_cnt, rs_ctx,
2594 &now);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002595
Valerio Settieaf57892025-05-06 17:07:09 +02002596#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002598 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002599 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002600 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002601 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002604 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002605#else
2606 (void) ret;
2607#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002608
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002609 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002611 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002613 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002614
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002615 /* Count intermediate self-issued (not necessarily self-signed) certs.
2616 * These can occur with some strategies for key rollover, see [SIRO],
2617 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (ver_chain->len != 1 &&
2619 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002620 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002621 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002622
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002623 /* path_cnt is 0 for the first intermediate CA,
2624 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 if (!parent_is_trusted &&
2626 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002627 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002629 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002630
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002631 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002633 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002635
2636 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002638 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002642 /* Check trusted CA's CRL for the given crt */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002643 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002644#else
2645 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002646#endif
2647
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002648 /* prepare for next iteration */
2649 child = parent;
2650 parent = NULL;
2651 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002652 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002653 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002654}
2655
Glenn Straussc26bd762022-10-23 19:48:18 -04002656#ifdef _WIN32
2657#ifdef _MSC_VER
2658#pragma comment(lib, "ws2_32.lib")
2659#include <winsock2.h>
2660#include <ws2tcpip.h>
2661#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2662#include <winsock2.h>
2663#include <ws2tcpip.h>
Steve Lhommeeb0f18a2023-06-16 14:12:19 +02002664#else
2665/* inet_pton() is not supported, fallback to software version */
2666#define MBEDTLS_TEST_SW_INET_PTON
Glenn Straussc26bd762022-10-23 19:48:18 -04002667#endif
2668#elif defined(__sun)
2669/* Solaris requires -lsocket -lnsl for inet_pton() */
2670#elif defined(__has_include)
2671#if __has_include(<sys/socket.h>)
2672#include <sys/socket.h>
2673#endif
2674#if __has_include(<arpa/inet.h>)
2675#include <arpa/inet.h>
2676#endif
2677#endif
2678
2679/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2680 * the platform inet_pton() or a local implementation (below). The local
2681 * implementation may be used even in cases where the platform provides
2682 * inet_pton(), e.g. when there are different includes required and/or the
2683 * platform implementation requires dependencies on additional libraries.
2684 * Specifically, Windows requires custom includes and additional link
2685 * dependencies, and Solaris requires additional link dependencies.
2686 * Also, as a coarse heuristic, use the local implementation if the compiler
2687 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002688 * provided by headers included (or not) via __has_include() above.
2689 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2690 * despite having a platform that has inet_pton. */
2691#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2692/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002693static int x509_inet_pton_ipv4(const char *src, void *dst);
2694
2695#define li_cton(c, n) \
2696 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2697
2698static int x509_inet_pton_ipv6(const char *src, void *dst)
2699{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002700 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002701 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002702 uint16_t addr[8];
2703 do {
2704 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002705 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002706 for (uint8_t digit; num_digits < 4; num_digits++) {
2707 if (li_cton(*p, digit) == 0) {
2708 break;
2709 }
2710 group = (group << 4) | digit;
2711 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002712 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002713 if (num_digits != 0) {
Dave Rodgmancfa72232023-09-05 16:20:33 +01002714 MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups);
2715 nonzero_groups++;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002716 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002717 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002718 } else if (*p == '.') {
2719 /* Don't accept IPv4 too early or late */
2720 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2721 nonzero_groups >= 7) {
2722 break;
2723 }
2724
2725 /* Walk back to prior ':', then parse as IPv4-mapped */
2726 int steps = 4;
2727 do {
2728 p--;
2729 steps--;
2730 } while (*p != ':' && steps > 0);
2731
2732 if (*p != ':') {
2733 break;
2734 }
2735 p++;
2736 nonzero_groups--;
2737 if (x509_inet_pton_ipv4((const char *) p,
2738 addr + nonzero_groups) != 0) {
2739 break;
2740 }
2741
Andrzej Kurek0d578962023-04-13 09:09:56 -04002742 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002743 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002744 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002745 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002746 return -1;
2747 }
2748 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002749 /* Don't accept a second zero group or an invalid delimiter */
2750 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002751 return -1;
2752 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002753 zero_group_start = nonzero_groups;
2754
2755 /* Accept a zero group at start, but it has to be a double colon */
2756 if (zero_group_start == 0 && *++p != ':') {
2757 return -1;
2758 }
2759
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002760 if (p[1] == '\0') {
2761 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002762 break;
2763 }
2764 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002765 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002766 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002767
2768 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002769 return -1;
2770 }
2771
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002772 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002773 if (nonzero_groups > 6) {
2774 return -1;
2775 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002776 int zero_groups = 8 - nonzero_groups;
2777 int groups_after_zero = nonzero_groups - zero_group_start;
2778
2779 /* Move the non-zero part to after the zeroes */
2780 if (groups_after_zero) {
2781 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002782 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002783 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002784 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002785 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002786 } else {
2787 if (nonzero_groups != 8) {
2788 return -1;
2789 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002790 }
2791 memcpy(dst, addr, sizeof(addr));
2792 return 0;
2793}
2794
2795static int x509_inet_pton_ipv4(const char *src, void *dst)
2796{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002797 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002798 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002799 uint8_t digit, num_digits = 0;
2800 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002801 uint16_t octet;
2802
Glenn Strauss416c2952022-10-24 23:00:02 -04002803 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002804 octet = num_digits = 0;
2805 do {
2806 digit = *p - '0';
2807 if (digit > 9) {
2808 break;
2809 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002810
2811 /* Don't allow leading zeroes. These might mean octal format,
2812 * which this implementation does not support. */
2813 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002814 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002815 }
2816
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002817 octet = octet * 10 + digit;
2818 num_digits++;
2819 p++;
2820 } while (num_digits < 3);
2821
2822 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002823 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002824 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002825 *res++ = (uint8_t) octet;
2826 num_octets++;
2827 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002828 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002829}
Glenn Straussc26bd762022-10-23 19:48:18 -04002830
2831#else
2832
2833static int x509_inet_pton_ipv6(const char *src, void *dst)
2834{
2835 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2836}
2837
2838static int x509_inet_pton_ipv4(const char *src, void *dst)
2839{
2840 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2841}
2842
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002843#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002844
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002845size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002846{
2847 return strchr(cn, ':') == NULL
2848 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2849 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2850}
2851
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002852/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002853 * Check for CN match
2854 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002855static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2856 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002857{
2858 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 if (name->len == cn_len &&
2860 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2861 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002862 }
2863
2864 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 if (x509_check_wildcard(cn, name) == 0) {
2866 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002867 }
2868
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002870}
2871
Glenn Straussc26bd762022-10-23 19:48:18 -04002872static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2873 const char *cn, size_t cn_len)
2874{
2875 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002876 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002877 if (cn_len == 0) {
2878 return -1;
2879 }
2880
2881 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2882 const unsigned char san_type = (unsigned char) cur->buf.tag &
2883 MBEDTLS_ASN1_TAG_VALUE_MASK;
2884 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2885 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2886 return 0;
2887 }
2888 }
2889
2890 return -1;
2891}
2892
Andrzej Kurek199eab92023-05-10 09:57:19 -04002893static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2894 const char *cn, size_t cn_len)
2895{
2896 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2897 const unsigned char san_type = (unsigned char) cur->buf.tag &
2898 MBEDTLS_ASN1_TAG_VALUE_MASK;
2899 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2900 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2901 return 0;
2902 }
2903 }
2904
2905 return -1;
2906}
2907
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002908/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002909 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2910 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002911static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002913{
Glenn Straussc26bd762022-10-23 19:48:18 -04002914 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002915 int san_uri = 0;
2916 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002917 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2918 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002919 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002920 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2921 return 0;
2922 }
2923 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002924 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04002925 san_ip = 1;
2926 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002927 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
2928 san_uri = 1;
2929 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04002930 /* (We may handle other types here later.) */
2931 default: /* Unrecognized type */
2932 break;
2933 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04002935 if (san_ip) {
2936 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
2937 return 0;
2938 }
2939 }
2940 if (san_uri) {
2941 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
2942 return 0;
2943 }
2944 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002945
Andrzej Kurek199eab92023-05-10 09:57:19 -04002946 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002947}
2948
2949/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002950 * Verify the requested CN - only call this if cn is not NULL!
2951 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002952static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2953 const char *cn,
2954 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002955{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002956 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002958
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002960 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
2961 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 }
2963 } else {
2964 for (name = &crt->subject; name != NULL; name = name->next) {
2965 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2966 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002967 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 }
2969 }
2970
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002971 }
Glenn Straussc26bd762022-10-23 19:48:18 -04002972
2973 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002974}
2975
2976/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002977 * Merge the flags for all certs in the chain, after calling callback
2978 */
2979static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 uint32_t *flags,
2981 const mbedtls_x509_crt_verify_chain *ver_chain,
2982 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2983 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002984{
Janos Follath865b3eb2019-12-16 11:46:15 +00002985 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002986 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002987 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002988 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002989
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002991 cur = &ver_chain->items[i-1];
2992 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 if (NULL != f_vrfy) {
2995 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
2996 return ret;
2997 }
2998 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002999
3000 *flags |= cur_flags;
3001 }
3002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003004}
3005
3006/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003007 * Verify the certificate validity, with profile, restartable version
3008 *
3009 * This function:
3010 * - checks the requested CN (if any)
3011 * - checks the type and size of the EE cert's key,
3012 * as that isn't done as part of chain building/verification currently
3013 * - builds and verifies the chain
3014 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003015 *
3016 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3017 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3018 * verification routine to search for trusted signers, and CRLs will
3019 * be disabled. Otherwise, `trust_ca` will be used as the static list
3020 * of trusted signers, and `ca_crl` will be use as the static list
3021 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003022 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003023static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3024 mbedtls_x509_crt *trust_ca,
3025 mbedtls_x509_crl *ca_crl,
3026 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3027 void *p_ca_cb,
3028 const mbedtls_x509_crt_profile *profile,
3029 const char *cn, uint32_t *flags,
3030 int (*f_vrfy)(void *,
3031 mbedtls_x509_crt *,
3032 int,
3033 uint32_t *),
3034 void *p_vrfy,
3035 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003036{
Janos Follath865b3eb2019-12-16 11:46:15 +00003037 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003038 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003039 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003040 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003041
3042 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003043 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003047 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3048 goto exit;
3049 }
3050
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003051 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 if (cn != NULL) {
3053 x509_crt_verify_name(crt, cn, &ee_flags);
3054 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003055
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003056 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003058
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003060 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003062
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003064 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003066
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003067 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3069 f_ca_cb, p_ca_cb, profile,
3070 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003073 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003075
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003076 /* Merge end-entity flags */
3077 ver_chain.items[0].flags |= ee_flags;
3078
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003079 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003081
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003082exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003083
3084#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3086 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003087 ver_chain.trust_ca_cb_result = NULL;
3088#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3089
Valerio Settieaf57892025-05-06 17:07:09 +02003090#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3092 mbedtls_x509_crt_restart_free(rs_ctx);
3093 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003094#endif
3095
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003096 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3097 * the SSL module for authmode optional, but non-zero return from the
3098 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003100 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003101 }
3102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 if (ret != 0) {
3104 *flags = (uint32_t) -1;
3105 return ret;
3106 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003107
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (*flags != 0) {
3109 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3110 }
3111
3112 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003113}
3114
Hanno Becker3116fb32019-03-28 13:34:42 +00003115
3116/*
3117 * Verify the certificate validity (default profile, not restartable)
3118 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003119int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3120 mbedtls_x509_crt *trust_ca,
3121 mbedtls_x509_crl *ca_crl,
3122 const char *cn, uint32_t *flags,
3123 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3124 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003125{
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3127 NULL, NULL,
3128 &mbedtls_x509_crt_profile_default,
3129 cn, flags,
3130 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003131}
3132
3133/*
3134 * Verify the certificate validity (user-chosen profile, not restartable)
3135 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003136int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3137 mbedtls_x509_crt *trust_ca,
3138 mbedtls_x509_crl *ca_crl,
3139 const mbedtls_x509_crt_profile *profile,
3140 const char *cn, uint32_t *flags,
3141 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3142 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003143{
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3145 NULL, NULL,
3146 profile, cn, flags,
3147 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003148}
3149
3150#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3151/*
3152 * Verify the certificate validity (user-chosen profile, CA callback,
3153 * not restartable).
3154 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003155int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3156 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3157 void *p_ca_cb,
3158 const mbedtls_x509_crt_profile *profile,
3159 const char *cn, uint32_t *flags,
3160 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3161 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003162{
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3164 f_ca_cb, p_ca_cb,
3165 profile, cn, flags,
3166 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003167}
3168#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3169
Gilles Peskine449bd832023-01-11 14:50:10 +01003170int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3171 mbedtls_x509_crt *trust_ca,
3172 mbedtls_x509_crl *ca_crl,
3173 const mbedtls_x509_crt_profile *profile,
3174 const char *cn, uint32_t *flags,
3175 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3176 void *p_vrfy,
3177 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003178{
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3180 NULL, NULL,
3181 profile, cn, flags,
3182 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003183}
3184
3185
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003186/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003187 * Initialize a certificate chain
3188 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003189void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003190{
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003192}
3193
3194/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003195 * Unallocate all certificate data
3196 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003197void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003198{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 mbedtls_x509_crt *cert_cur = crt;
3200 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003201
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 while (cert_cur != NULL) {
3203 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3206 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3207 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3208 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3209 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Przemek Stekiel690ff692023-05-15 09:54:02 +02003210 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003211
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003213 mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003214 }
3215
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003216 cert_prv = cert_cur;
3217 cert_cur = cert_cur->next;
3218
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3220 if (cert_prv != crt) {
3221 mbedtls_free(cert_prv);
3222 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003223 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003224}
3225
Valerio Settieaf57892025-05-06 17:07:09 +02003226#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003227/*
3228 * Initialize a restart context
3229 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003230void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003231{
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003233
3234 ctx->parent = NULL;
3235 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003236 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003237
3238 ctx->parent_is_trusted = -1;
3239
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003240 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003241 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003243}
3244
3245/*
3246 * Free the components of a restart context
3247 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003248void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003249{
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003251 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003253
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 mbedtls_pk_restart_free(&ctx->pk);
3255 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003256}
Valerio Settieaf57892025-05-06 17:07:09 +02003257#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003258
Minos Galanakis2abbac72024-01-18 17:05:21 +00003259int mbedtls_x509_crt_get_ca_istrue(const mbedtls_x509_crt *crt)
3260{
3261 if ((crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) != 0) {
3262 return crt->MBEDTLS_PRIVATE(ca_istrue);
3263 }
3264 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
3265}
3266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267#endif /* MBEDTLS_X509_CRT_PARSE_C */