blob: 53cdcf026629987675920519d27add7a8e20795d [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/x509_crt.h"
Valerio Setti25b282e2024-01-17 10:55:32 +010025#include "x509_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000026#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/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#if defined(MBEDTLS_USE_PSA_CRYPTO)
37#include "psa/crypto.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020038#include "psa_util_internal.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010039#include "mbedtls/psa_util.h"
pespacek7599a772022-02-07 14:40:55 +010040#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti3f00b842023-05-15 12:57:06 +020041#include "pk_internal.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040042
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010047#endif
48
Daniel Axtensf0710242020-05-28 11:43:41 +100049#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010050#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Sergey Markelov68c0e3d2024-08-14 15:06:03 -070051#ifndef WIN32_LEAN_AND_MEAN
Glenn Straussc26bd762022-10-23 19:48:18 -040052#define WIN32_LEAN_AND_MEAN
Sergey Markelov68c0e3d2024-08-14 15:06:03 -070053#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#include <windows.h>
55#else
56#include <time.h>
57#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100058#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000061#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020062#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063#include <sys/types.h>
64#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020065#if defined(__MBED__)
66#include <platform/mbed_retarget.h>
67#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020069#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060070#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000071#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#endif
73
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020074/*
75 * Item in a verification chain: cert and flags for it
76 */
77typedef struct {
78 mbedtls_x509_crt *crt;
79 uint32_t flags;
80} x509_crt_verify_chain_item;
81
82/*
83 * Max size of verification chain: end-entity + intermediates + trusted root
84 */
Gilles Peskine449bd832023-01-11 14:50:10 +010085#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020086
Gilles Peskineffb92da2021-06-02 00:03:26 +020087/* Default profile. Do not remove items unless there are serious security
88 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020089const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
90{
Gilles Peskineae270bf2021-06-02 00:05:29 +020091 /* Hashes from SHA-256 and above. Note that this selection
92 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +010093 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
94 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
95 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020096 0xFFFFFFF, /* Any PK alg */
Valerio Setti8c3404f2023-06-26 15:49:48 +020097#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskineae270bf2021-06-02 00:05:29 +020098 /* Curves at or above 128-bit security level. Note that this selection
99 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
101 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
102 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
103 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
104 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
105 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
Gilles Peskine39957502021-06-17 23:17:52 +0200106 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200107#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200108 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200109#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200110 2048,
111};
112
Gilles Peskineffb92da2021-06-02 00:03:26 +0200113/* Next-generation profile. Currently identical to the default, but may
114 * be tightened at any time. */
115const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200116{
117 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
119 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
120 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200121 0xFFFFFFF, /* Any PK alg */
122#if defined(MBEDTLS_ECP_C)
123 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
125 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
126 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
127 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
128 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
129 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
130 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200131#else
132 0,
133#endif
134 2048,
135};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200136
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200137/*
138 * NSA Suite B Profile
139 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200140const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
141{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200142 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
144 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200145 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
147 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Valerio Setti8c3404f2023-06-26 15:49:48 +0200148#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200149 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
151 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Valerio Setti8c3404f2023-06-26 15:49:48 +0200152#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200153 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200154#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200155 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200156};
157
158/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200159 * Empty / all-forbidden profile
160 */
161const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
162{
163 0,
164 0,
165 0,
166 (uint32_t) -1,
167};
168
169/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200170 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200171 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200172 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100173static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
174 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200175{
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 if (md_alg == MBEDTLS_MD_NONE) {
177 return -1;
178 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
181 return 0;
182 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200185}
186
187/*
188 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200189 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200190 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
192 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200193{
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 if (pk_alg == MBEDTLS_PK_NONE) {
195 return -1;
196 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
199 return 0;
200 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200203}
204
205/*
206 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200207 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200208 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
210 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200211{
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200213
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200214#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
216 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
217 return 0;
218 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200221 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200222#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200223
Valerio Setti8c3404f2023-06-26 15:49:48 +0200224#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200226 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 pk_alg == MBEDTLS_PK_ECKEY_DH) {
Valerio Settif9362b72023-11-29 08:42:27 +0100228 const mbedtls_ecp_group_id gid = mbedtls_pk_get_ec_group_id(pk);
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if (gid == MBEDTLS_ECP_DP_NONE) {
231 return -1;
232 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
235 return 0;
236 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200239 }
Valerio Setti8c3404f2023-06-26 15:49:48 +0200240#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200243}
244
245/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000246 * Like memcmp, but case-insensitive and always returns -1 if different
247 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100248static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000249{
250 size_t i;
251 unsigned char diff;
252 const unsigned char *n1 = s1, *n2 = s2;
253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000255 diff = n1[i] ^ n2[i];
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000258 continue;
259 }
260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (diff == 32 &&
262 ((n1[i] >= 'a' && n1[i] <= 'z') ||
263 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
264 continue;
265 }
266
267 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000268 }
269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000271}
272
273/*
274 * Return 0 if name matches wildcard, -1 otherwise
275 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000277{
278 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000280
281 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
283 return -1;
284 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 for (i = 0; i < cn_len; ++i) {
287 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000288 cn_idx = i;
289 break;
290 }
291 }
292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (cn_idx == 0) {
294 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000295 }
296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (cn_len - cn_idx == name->len - 1 &&
298 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
299 return 0;
300 }
301
302 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000303}
304
305/*
306 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
307 * variations (but not all).
308 *
309 * Return 0 if equal, -1 otherwise.
310 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100311static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000312{
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000314 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 memcmp(a->p, b->p, b->len) == 0) {
316 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000317 }
318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
320 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000321 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 x509_memcasecmp(a->p, b->p, b->len) == 0) {
323 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000324 }
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000327}
328
329/*
330 * Compare two X.509 Names (aka rdnSequence).
331 *
332 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
333 * we sometimes return unequal when the full algorithm would return equal,
334 * but never the other way. (In particular, we don't do Unicode normalisation
335 * or space folding.)
336 *
337 * Return 0 if equal, -1 otherwise.
338 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100339static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000340{
341 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 while (a != NULL || b != NULL) {
343 if (a == NULL || b == NULL) {
344 return -1;
345 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000346
347 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000349 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
351 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000352 }
353
354 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (x509_string_cmp(&a->val, &b->val) != 0) {
356 return -1;
357 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000358
359 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (a->next_merged != b->next_merged) {
361 return -1;
362 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000363
364 a = a->next;
365 b = b->next;
366 }
367
368 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000370}
371
372/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200373 * Reset (init or clear) a verify_chain
374 */
375static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200377{
378 size_t i;
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200381 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000382 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200383 }
384
385 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000386
387#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
388 ver_chain->trust_ca_cb_result = NULL;
389#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200390}
391
392/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
394 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100395static int x509_get_version(unsigned char **p,
396 const unsigned char *end,
397 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398{
Janos Follath865b3eb2019-12-16 11:46:15 +0000399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400 size_t len;
401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
403 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
404 0)) != 0) {
405 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408 }
409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411 }
412
413 end = *p + len;
414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
416 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
417 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (*p != end) {
420 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
421 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
422 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425}
426
427/*
428 * Validity ::= SEQUENCE {
429 * notBefore Time,
430 * notAfter Time }
431 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432static int x509_get_dates(unsigned char **p,
433 const unsigned char *end,
434 mbedtls_x509_time *from,
435 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436{
Janos Follath865b3eb2019-12-16 11:46:15 +0000437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438 size_t len;
439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
441 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
442 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
443 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
445 end = *p + len;
446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
448 return ret;
449 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
452 return ret;
453 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (*p != end) {
456 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
457 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
458 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461}
462
463/*
464 * X.509 v2/v3 unique identifier (not parsed)
465 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100466static int x509_get_uid(unsigned char **p,
467 const unsigned char *end,
468 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469{
Janos Follath865b3eb2019-12-16 11:46:15 +0000470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (*p == end) {
473 return 0;
474 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475
476 uid->tag = **p;
477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
479 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
480 n)) != 0) {
481 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
482 return 0;
483 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486 }
487
488 uid->p = *p;
489 *p += uid->len;
490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492}
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494static int x509_get_basic_constraints(unsigned char **p,
495 const unsigned char *end,
496 int *ca_istrue,
497 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498{
Janos Follath865b3eb2019-12-16 11:46:15 +0000499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500 size_t len;
501
502 /*
503 * BasicConstraints ::= SEQUENCE {
504 * cA BOOLEAN DEFAULT FALSE,
505 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
506 */
507 *ca_istrue = 0; /* DEFAULT FALSE */
508 *max_pathlen = 0; /* endless */
509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
511 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
512 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513 }
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (*p == end) {
516 return 0;
517 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
520 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
521 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
522 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (ret != 0) {
525 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
526 }
527
528 if (*ca_istrue != 0) {
529 *ca_istrue = 1;
530 }
531 }
532
533 if (*p == end) {
534 return 0;
535 }
536
537 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
538 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
539 }
540
541 if (*p != end) {
542 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
543 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
544 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545
Andrzej Kurek16050742020-04-14 09:49:52 -0400546 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
547 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if (*max_pathlen == INT_MAX) {
549 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
550 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
551 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400552
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553 (*max_pathlen)++;
554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556}
557
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558/*
559 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
560 *
561 * KeyPurposeId ::= OBJECT IDENTIFIER
562 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100563static int x509_get_ext_key_usage(unsigned char **p,
564 const unsigned char *end,
565 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566{
Janos Follath865b3eb2019-12-16 11:46:15 +0000567 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
570 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
571 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572
573 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (ext_key_usage->buf.p == NULL) {
575 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
576 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
577 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580}
581
582/*
toth92ga41954d2021-02-12 16:11:17 +0100583 * SubjectKeyIdentifier ::= KeyIdentifier
584 *
585 * KeyIdentifier ::= OCTET STRING
586 */
587static int x509_get_subject_key_id(unsigned char **p,
588 const unsigned char *end,
589 mbedtls_x509_buf *subject_key_id)
590{
591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
592 size_t len = 0u;
593
594 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
595 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100596 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92ga41954d2021-02-12 16:11:17 +0100597 }
598
Przemek Stekiel61aed062023-05-08 11:14:36 +0200599 subject_key_id->len = len;
600 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
601 subject_key_id->p = *p;
602 *p += len;
603
toth92gd96027a2021-04-27 15:41:25 +0200604 if (*p != end) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100605 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
606 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92gd96027a2021-04-27 15:41:25 +0200607 }
608
toth92ga41954d2021-02-12 16:11:17 +0100609 return 0;
610}
611
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100612/*
toth92g8d435a02021-05-10 15:16:33 +0200613 * AuthorityKeyIdentifier ::= SEQUENCE {
614 * keyIdentifier [0] KeyIdentifier OPTIONAL,
615 * authorityCertIssuer [1] GeneralNames OPTIONAL,
616 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
617 *
618 * KeyIdentifier ::= OCTET STRING
619 */
620static int x509_get_authority_key_id(unsigned char **p,
621 unsigned char *end,
622 mbedtls_x509_authority *authority_key_id)
623{
624 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
625 size_t len = 0u;
626
627 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100628 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100629 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200630 }
631
Przemek Stekiel6ec839a2023-02-01 11:06:08 +0100632 if (*p + len != end) {
633 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
634 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
635 }
636
Przemek Stekieled9fb782023-05-03 16:27:25 +0200637 ret = mbedtls_asn1_get_tag(p, end, &len,
638 MBEDTLS_ASN1_CONTEXT_SPECIFIC);
639
640 /* KeyIdentifier is an OPTIONAL field */
Przemek Stekiel61aed062023-05-08 11:14:36 +0200641 if (ret == 0) {
toth92g8d435a02021-05-10 15:16:33 +0200642 authority_key_id->keyIdentifier.len = len;
643 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200644 /* Setting tag of the keyIdentfier intentionally to 0x04.
645 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
Przemek Stekiel9a7a7252023-04-17 16:06:57 +0200646 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200647 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
648
649 *p += len;
Przemek Stekiel61aed062023-05-08 11:14:36 +0200650 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
651 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200652 }
653
654 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200655 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200656 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
657 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100658 1)) != 0) {
Przemek Stekielf5b8f782023-04-26 08:55:26 +0200659 /* authorityCertIssuer and authorityCertSerialNumber MUST both
660 be present or both be absent. At this point we expect to have both. */
661 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200662 }
Przemek Stekieled9fb782023-05-03 16:27:25 +0200663 /* "end" also includes the CertSerialNumber field so "len" shall be used */
664 ret = mbedtls_x509_get_subject_alt_name_ext(p,
665 (*p+len),
666 &authority_key_id->authorityCertIssuer);
667 if (ret != 0) {
668 return ret;
669 }
670
671 /* Getting authorityCertSerialNumber using the required specific class tag [2] */
672 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
673 MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) {
674 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
675 }
676 authority_key_id->authorityCertSerialNumber.len = len;
677 authority_key_id->authorityCertSerialNumber.p = *p;
678 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER;
679 *p += len;
toth92g8d435a02021-05-10 15:16:33 +0200680 }
681
682 if (*p != end) {
683 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
684 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
685 }
686
687 return 0;
688}
689
690/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200691 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
692 *
693 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
694 *
695 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
696 *
697 * PolicyInformation ::= SEQUENCE {
698 * policyIdentifier CertPolicyId,
699 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
700 * PolicyQualifierInfo OPTIONAL }
701 *
702 * CertPolicyId ::= OBJECT IDENTIFIER
703 *
704 * PolicyQualifierInfo ::= SEQUENCE {
705 * policyQualifierId PolicyQualifierId,
706 * qualifier ANY DEFINED BY policyQualifierId }
707 *
708 * -- policyQualifierIds for Internet policy qualifiers
709 *
710 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
711 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
712 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
713 *
714 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
715 *
716 * Qualifier ::= CHOICE {
717 * cPSuri CPSuri,
718 * userNotice UserNotice }
719 *
720 * CPSuri ::= IA5String
721 *
722 * UserNotice ::= SEQUENCE {
723 * noticeRef NoticeReference OPTIONAL,
724 * explicitText DisplayText OPTIONAL }
725 *
726 * NoticeReference ::= SEQUENCE {
727 * organization DisplayText,
728 * noticeNumbers SEQUENCE OF INTEGER }
729 *
730 * DisplayText ::= CHOICE {
731 * ia5String IA5String (SIZE (1..200)),
732 * visibleString VisibleString (SIZE (1..200)),
733 * bmpString BMPString (SIZE (1..200)),
734 * utf8String UTF8String (SIZE (1..200)) }
735 *
736 * NOTE: we only parse and use anyPolicy without qualifiers at this point
737 * as defined in RFC 5280.
738 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100739static int x509_get_certificate_policies(unsigned char **p,
740 const unsigned char *end,
741 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200742{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300743 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200744 size_t len;
745 mbedtls_asn1_buf *buf;
746 mbedtls_asn1_sequence *cur = certificate_policies;
747
748 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 ret = mbedtls_asn1_get_tag(p, end, &len,
750 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
751 if (ret != 0) {
752 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
753 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 if (*p + len != end) {
756 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
757 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
758 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200759
760 /*
761 * Cannot be an empty sequence.
762 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 if (len == 0) {
764 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
765 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
766 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200769 mbedtls_x509_buf policy_oid;
770 const unsigned char *policy_end;
771
772 /*
773 * Get the policy sequence
774 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
776 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
777 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
778 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200779
780 policy_end = *p + len;
781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
783 MBEDTLS_ASN1_OID)) != 0) {
784 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
785 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200786
787 policy_oid.tag = MBEDTLS_ASN1_OID;
788 policy_oid.len = len;
789 policy_oid.p = *p;
790
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300791 /*
792 * Only AnyPolicy is currently supported when enforcing policy.
793 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300795 /*
796 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200797 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300798 */
799 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
800 }
801
Ron Eldor74d9acc2019-03-21 14:00:03 +0200802 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (cur->buf.p != NULL) {
804 if (cur->next != NULL) {
805 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
806 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (cur->next == NULL) {
811 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
812 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
813 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200814
815 cur = cur->next;
816 }
817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200819 buf->tag = policy_oid.tag;
820 buf->p = policy_oid.p;
821 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300822
823 *p += len;
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 /*
826 * If there is an optional qualifier, then *p < policy_end
827 * Check the Qualifier len to verify it doesn't exceed policy_end.
828 */
829 if (*p < policy_end) {
830 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
831 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
832 0) {
833 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
834 }
Ron Eldor08063792019-05-13 16:38:39 +0300835 /*
836 * Skip the optional policy qualifiers.
837 */
838 *p += len;
839 }
840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (*p != policy_end) {
842 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
843 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
844 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200845 }
846
847 /* Set final sequence entry's next pointer to NULL */
848 cur->next = NULL;
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (*p != end) {
851 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
852 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
853 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200856}
857
858/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200859 * X.509 v3 extensions
860 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862static int x509_get_crt_ext(unsigned char **p,
863 const unsigned char *end,
864 mbedtls_x509_crt *crt,
865 mbedtls_x509_crt_ext_cb_t cb,
866 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200867{
Janos Follath865b3eb2019-12-16 11:46:15 +0000868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200870 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if (*p == end) {
873 return 0;
874 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
877 return ret;
878 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879
Hanno Becker12f62fb2019-02-12 17:22:36 +0000880 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882 /*
883 * Extension ::= SEQUENCE {
884 * extnID OBJECT IDENTIFIER,
885 * critical BOOLEAN DEFAULT FALSE,
886 * extnValue OCTET STRING }
887 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 int is_critical = 0; /* DEFAULT FALSE */
890 int ext_type = 0;
891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
893 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
894 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
895 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896
897 end_ext_data = *p + len;
898
899 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
901 MBEDTLS_ASN1_OID)) != 0) {
902 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
903 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904
k-stachowiak463928a2018-07-24 12:50:59 +0200905 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906 extn_oid.p = *p;
907 *p += extn_oid.len;
908
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
911 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
912 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
913 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914
915 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
917 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
918 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
919 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200921 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922 end_ext_octet = *p + len;
923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (end_ext_octet != end_ext_data) {
925 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
926 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
927 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928
929 /*
930 * Detect supported extensions
931 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200935 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 if (cb != NULL) {
937 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
938 if (ret != 0 && is_critical) {
939 return ret;
940 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200941 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200942 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200943 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200944
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945 /* No parser found, skip extension */
946 *p = end_ext_octet;
947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
951 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953 continue;
954 }
955
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100956 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if ((crt->ext_types & ext_type) != 0) {
958 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
959 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100960
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961 crt->ext_types |= ext_type;
962
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 switch (ext_type) {
964 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
965 /* Parse basic constraints */
966 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
967 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
968 return ret;
969 }
970 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 case MBEDTLS_X509_EXT_KEY_USAGE:
973 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100974 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
975 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 return ret;
977 }
978 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
981 /* Parse extended key usage */
982 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
983 &crt->ext_key_usage)) != 0) {
984 return ret;
985 }
986 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987
toth92ga41954d2021-02-12 16:11:17 +0100988 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
989 /* Parse subject key identifier */
990 if ((ret = x509_get_subject_key_id(p, end_ext_data,
991 &crt->subject_key_id)) != 0) {
992 return ret;
993 }
994 break;
toth92g8d435a02021-05-10 15:16:33 +0200995
toth92ga41954d2021-02-12 16:11:17 +0100996 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
997 /* Parse authority key identifier */
998 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
999 &crt->authority_key_id)) != 0) {
1000 return ret;
1001 }
1002 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001004 /* Parse subject alt name
1005 * SubjectAltName ::= GeneralNames
1006 */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001007 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1008 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 return ret;
1010 }
1011 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1014 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001015 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1016 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 return ret;
1018 }
1019 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1022 /* Parse certificate policies type */
1023 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1024 &crt->certificate_policies)) != 0) {
1025 /* Give the callback (if any) a chance to handle the extension
1026 * if it contains unsupported policies */
1027 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1028 cb(p_ctx, crt, &extn_oid, is_critical,
1029 start_ext_octet, end_ext_octet) == 0) {
1030 break;
1031 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if (is_critical) {
1034 return ret;
1035 } else
1036 /*
1037 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1038 * cannot interpret or enforce the policy. However, it is up to
1039 * the user to choose how to enforce the policies,
1040 * unless the extension is critical.
1041 */
1042 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1043 return ret;
1044 }
1045 }
1046 break;
1047
1048 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001049 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 * If this is a non-critical extension, which the oid layer
1051 * supports, but there isn't an x509 parser for it,
1052 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001053 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (is_critical) {
1055 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1056 } else {
1057 *p = end_ext_octet;
1058 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059 }
1060 }
1061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if (*p != end) {
1063 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1064 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1065 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068}
1069
1070/*
1071 * Parse and fill a single X.509 certificate in DER format
1072 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001073static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1074 const unsigned char *buf,
1075 size_t buflen,
1076 int make_copy,
1077 mbedtls_x509_crt_ext_cb_t cb,
1078 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079{
Janos Follath865b3eb2019-12-16 11:46:15 +00001080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081 size_t len;
1082 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1086 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1087 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088
1089 /*
1090 * Check for valid input
1091 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 if (crt == NULL || buf == NULL) {
1093 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1094 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001096 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001098 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099 end = p + len;
1100
1101 /*
1102 * Certificate ::= SEQUENCE {
1103 * tbsCertificate TBSCertificate,
1104 * signatureAlgorithm AlgorithmIdentifier,
1105 * signatureValue BIT STRING }
1106 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1108 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1109 mbedtls_x509_crt_free(crt);
1110 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111 }
1112
Janos Follathcc0e49d2016-02-17 14:34:12 +00001113 end = crt_end = p + len;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001114 crt->raw.len = (size_t) (crt_end - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001116 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1118 if (crt->raw.p == NULL) {
1119 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1120 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001123 crt->own_buffer = 1;
1124
1125 p += crt->raw.len - len;
1126 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 } else {
1128 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001129 crt->own_buffer = 0;
1130 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001131
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132 /*
1133 * TBSCertificate ::= SEQUENCE {
1134 */
1135 crt->tbs.p = p;
1136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1138 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1139 mbedtls_x509_crt_free(crt);
1140 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001141 }
1142
1143 end = p + len;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001144 crt->tbs.len = (size_t) (end - crt->tbs.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001145
1146 /*
1147 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1148 *
1149 * CertificateSerialNumber ::= INTEGER
1150 *
1151 * signature AlgorithmIdentifier
1152 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1154 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1155 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1156 &sig_params1)) != 0) {
1157 mbedtls_x509_crt_free(crt);
1158 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001159 }
1160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (crt->version < 0 || crt->version > 2) {
1162 mbedtls_x509_crt_free(crt);
1163 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164 }
1165
Andres AG7ca4a032017-03-09 16:16:11 +00001166 crt->version++;
1167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1169 &crt->sig_md, &crt->sig_pk,
1170 &crt->sig_opts)) != 0) {
1171 mbedtls_x509_crt_free(crt);
1172 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173 }
1174
1175 /*
1176 * issuer Name
1177 */
1178 crt->issuer_raw.p = p;
1179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1181 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1182 mbedtls_x509_crt_free(crt);
1183 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001184 }
1185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1187 mbedtls_x509_crt_free(crt);
1188 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189 }
1190
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001191 crt->issuer_raw.len = (size_t) (p - crt->issuer_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192
1193 /*
1194 * Validity ::= SEQUENCE {
1195 * notBefore Time,
1196 * notAfter Time }
1197 *
1198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1200 &crt->valid_to)) != 0) {
1201 mbedtls_x509_crt_free(crt);
1202 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203 }
1204
1205 /*
1206 * subject Name
1207 */
1208 crt->subject_raw.p = p;
1209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1211 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1212 mbedtls_x509_crt_free(crt);
1213 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001214 }
1215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1217 mbedtls_x509_crt_free(crt);
1218 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219 }
1220
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001221 crt->subject_raw.len = (size_t) (p - crt->subject_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001222
1223 /*
1224 * SubjectPublicKeyInfo
1225 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001226 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1228 mbedtls_x509_crt_free(crt);
1229 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001230 }
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001231 crt->pk_raw.len = (size_t) (p - crt->pk_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001232
1233 /*
1234 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1235 * -- If present, version shall be v2 or v3
1236 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1237 * -- If present, version shall be v2 or v3
1238 * extensions [3] EXPLICIT Extensions OPTIONAL
1239 * -- If present, version shall be v3
1240 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (crt->version == 2 || crt->version == 3) {
1242 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1243 if (ret != 0) {
1244 mbedtls_x509_crt_free(crt);
1245 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001246 }
1247 }
1248
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 if (crt->version == 2 || crt->version == 3) {
1250 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1251 if (ret != 0) {
1252 mbedtls_x509_crt_free(crt);
1253 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001254 }
1255 }
1256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (crt->version == 3) {
1258 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1259 if (ret != 0) {
1260 mbedtls_x509_crt_free(crt);
1261 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001262 }
1263 }
1264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (p != end) {
1266 mbedtls_x509_crt_free(crt);
1267 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1268 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001269 }
1270
1271 end = crt_end;
1272
1273 /*
1274 * }
1275 * -- end of TBSCertificate
1276 *
1277 * signatureAlgorithm AlgorithmIdentifier,
1278 * signatureValue BIT STRING
1279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1281 mbedtls_x509_crt_free(crt);
1282 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001283 }
1284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if (crt->sig_oid.len != sig_oid2.len ||
1286 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001287 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001288 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 (sig_params1.len != 0 &&
1290 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1291 mbedtls_x509_crt_free(crt);
1292 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001293 }
1294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1296 mbedtls_x509_crt_free(crt);
1297 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001298 }
1299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if (p != end) {
1301 mbedtls_x509_crt_free(crt);
1302 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1303 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 }
1305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307}
1308
1309/*
1310 * Parse one X.509 certificate in DER format from a buffer and add them to a
1311 * chained list
1312 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001313static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1314 const unsigned char *buf,
1315 size_t buflen,
1316 int make_copy,
1317 mbedtls_x509_crt_ext_cb_t cb,
1318 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319{
Janos Follath865b3eb2019-12-16 11:46:15 +00001320 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001322
1323 /*
1324 * Check for valid input
1325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 if (crt == NULL || buf == NULL) {
1327 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1328 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001331 prev = crt;
1332 crt = crt->next;
1333 }
1334
1335 /*
1336 * Add new certificate on the end of the chain if needed.
1337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if (crt->version != 0 && crt->next == NULL) {
1339 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 if (crt->next == NULL) {
1342 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1343 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344
1345 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001347 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001348 }
1349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1351 if (ret != 0) {
1352 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 if (crt != chain) {
1357 mbedtls_free(crt);
1358 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361 }
1362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364}
1365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1367 const unsigned char *buf,
1368 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001369{
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001371}
1372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1374 const unsigned char *buf,
1375 size_t buflen,
1376 int make_copy,
1377 mbedtls_x509_crt_ext_cb_t cb,
1378 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001379{
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001381}
1382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1384 const unsigned char *buf,
1385 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001386{
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001388}
1389
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001390/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001391 * Parse one or more PEM certificates from a buffer and add them to the chained
1392 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001394int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1395 const unsigned char *buf,
1396 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001397{
Janos Follath98e28a72016-05-31 14:03:54 +01001398#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001399 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001401#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001402
1403 /*
1404 * Check for valid input
1405 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if (chain == NULL || buf == NULL) {
1407 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1408 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001409
1410 /*
1411 * Determine buffer content. Buffer contains either one DER certificate or
1412 * one or more PEM certificates.
1413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1416 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001418 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1421 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1422 }
Janos Follath98e28a72016-05-31 14:03:54 +01001423#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001425#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001432 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001434 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001437 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 ret = mbedtls_pem_read_buffer(&pem,
1439 "-----BEGIN CERTIFICATE-----",
1440 "-----END CERTIFICATE-----",
1441 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001444 /*
1445 * Was PEM encoded
1446 */
1447 buflen -= use_len;
1448 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1450 return ret;
1451 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1452 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453
1454 /*
1455 * PEM header and footer were found
1456 */
1457 buflen -= use_len;
1458 buf += use_len;
1459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001461 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463
Paul Bakker5a5fa922014-09-26 14:53:04 +02001464 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001465 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475 /*
1476 * Quit parsing on a memory error
1477 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1479 return ret;
1480 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001483 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485
1486 total_failed++;
1487 continue;
1488 }
1489
1490 success = 1;
1491 }
1492 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if (success) {
1495 return total_failed;
1496 } else if (first_error) {
1497 return first_error;
1498 } else {
1499 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1500 }
Janos Follath98e28a72016-05-31 14:03:54 +01001501#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001502}
1503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001505/*
1506 * Load one or more certificates and add them to the chained list
1507 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001508int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001509{
Janos Follath865b3eb2019-12-16 11:46:15 +00001510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001511 size_t n;
1512 unsigned char *buf;
1513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1515 return ret;
1516 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001519
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001520 mbedtls_zeroize_and_free(buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001523}
1524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526{
1527 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001528#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529 int w_ret;
1530 WCHAR szDir[MAX_PATH];
1531 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001532 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534
Paul Bakker9af723c2014-05-01 13:03:14 +02001535 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536 HANDLE hFind;
1537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 if (len > MAX_PATH - 3) {
1539 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1540 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 memset(szDir, 0, sizeof(szDir));
1543 memset(filename, 0, MAX_PATH);
1544 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001545 filename[len++] = '\\';
1546 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547 filename[len++] = '*';
1548
Simon Butcher35e5dad2018-03-15 15:00:03 +00001549 /*
Minos Galanakis08a67cc2023-09-22 16:00:06 +01001550 * Note this function uses the code page CP_ACP which is the system default
1551 * ANSI codepage. The input string is always described in BYTES and the
1552 * output length is described in WCHARs.
Simon Butcher35e5dad2018-03-15 15:00:03 +00001553 */
Minos Galanakisa9bb34c2023-09-25 14:41:12 +01001554 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 MAX_PATH - 3);
1556 if (w_ret == 0) {
1557 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1558 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 hFind = FindFirstFileW(szDir, &file_data);
1561 if (hFind == INVALID_HANDLE_VALUE) {
1562 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1563 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001564
1565 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 do {
1567 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 }
Simon Butcherde573f52018-07-05 09:11:30 +01001572 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Minos Galanakis59108d32023-09-25 14:11:22 +01001573 -1, p, (int) len, NULL, NULL);
Minos Galanakisa277b212023-08-09 16:32:22 +01001574 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001575 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1576 goto cleanup;
1577 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1580 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 }
1585 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590
Ron Eldor36d90422017-01-09 15:09:16 +02001591cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001593#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001594 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001595 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001597 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001598 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 if (dir == NULL) {
1602 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1603 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001604
Ron Eldor63140682017-01-09 19:27:59 +02001605#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1607 closedir(dir);
1608 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001609 }
Ron Eldor63140682017-01-09 19:27:59 +02001610#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001615 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001618 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001619 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1620 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 } else if (stat(entry_name, &sb) == -1) {
1622 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001623 /* Broken symbolic link - ignore this entry.
1624 stat(2) will return this error for either (a) a dangling
1625 symlink or (b) a missing file.
1626 Given that we have just obtained the filename from readdir,
1627 assume that it does exist and therefore treat this as a
1628 dangling symlink. */
1629 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001631 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001632 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1633 goto cleanup;
1634 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635 }
1636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640
1641 // Ignore parse errors
1642 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1644 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001645 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001647 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001649 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001650
1651cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001653
Ron Eldor63140682017-01-09 19:27:59 +02001654#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 }
Ron Eldor63140682017-01-09 19:27:59 +02001658#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001659
Paul Bakkerbe089b02013-10-14 15:51:50 +02001660#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001663}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001665
Peter Kolbus9a969b62018-12-11 13:55:56 -06001666#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001667#define PRINT_ITEM(i) \
1668 do { \
1669 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1670 MBEDTLS_X509_SAFE_SNPRINTF; \
1671 sep = ", "; \
1672 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001673
Przemek Stekielf4194942023-04-24 09:52:17 +02001674#define CERT_TYPE(type, name) \
1675 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001676 if (ns_cert_type & (type)) { \
1677 PRINT_ITEM(name); \
1678 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001679 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001680
Przemek Stekielf4194942023-04-24 09:52:17 +02001681#define KEY_USAGE(code, name) \
1682 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001683 if (key_usage & (code)) { \
1684 PRINT_ITEM(name); \
1685 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001686 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688static int x509_info_ext_key_usage(char **buf, size_t *size,
1689 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001690{
Janos Follath865b3eb2019-12-16 11:46:15 +00001691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001692 const char *desc;
1693 size_t n = *size;
1694 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001696 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 while (cur != NULL) {
1699 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001700 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001704 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001705
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001706 sep = ", ";
1707
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001708 cur = cur->next;
1709 }
1710
1711 *size = n;
1712 *buf = p;
1713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001715}
1716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717static int x509_info_cert_policies(char **buf, size_t *size,
1718 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001719{
Janos Follath865b3eb2019-12-16 11:46:15 +00001720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001721 const char *desc;
1722 size_t n = *size;
1723 char *p = *buf;
1724 const mbedtls_x509_sequence *cur = certificate_policies;
1725 const char *sep = "";
1726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 while (cur != NULL) {
1728 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001729 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001733 MBEDTLS_X509_SAFE_SNPRINTF;
1734
1735 sep = ", ";
1736
1737 cur = cur->next;
1738 }
1739
1740 *size = n;
1741 *buf = p;
1742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001744}
1745
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001746/*
1747 * Return an informational string about the certificate.
1748 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001749#define BEFORE_COLON 18
1750#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001751int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1752 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001753{
Janos Follath865b3eb2019-12-16 11:46:15 +00001754 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001755 size_t n;
1756 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001757 char key_size_str[BEFORE_COLON];
1758
1759 p = buf;
1760 n = size;
1761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if (NULL == crt) {
1763 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001764 MBEDTLS_X509_SAFE_SNPRINTF;
1765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001767 }
1768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1770 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001771 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1773 prefix);
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_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001777 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001780 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001782 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001785 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001787 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1790 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1791 crt->valid_from.year, crt->valid_from.mon,
1792 crt->valid_from.day, crt->valid_from.hour,
1793 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001794 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1797 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1798 crt->valid_to.year, crt->valid_to.mon,
1799 crt->valid_to.day, crt->valid_to.hour,
1800 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001801 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001804 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1807 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001808 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001809
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001810 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1812 mbedtls_pk_get_name(&crt->pk))) != 0) {
1813 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001814 }
1815
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1817 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001818 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001819
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001820 /*
1821 * Optional extensions
1822 */
1823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1825 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1826 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001827 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 if (crt->max_pathlen > 0) {
1830 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001831 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001832 }
1833 }
1834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1836 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001837 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001838
Przemek Stekiel21c37282023-01-16 08:47:49 +01001839 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1840 &crt->subject_alt_names,
1841 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 return ret;
1843 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001844 }
1845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1847 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001848 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001849
Przemek Stekiel21c37282023-01-16 08:47:49 +01001850 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 return ret;
1852 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001853 }
1854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1856 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001857 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001858
Przemek Stekiel21c37282023-01-16 08:47:49 +01001859 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 return ret;
1861 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001862 }
1863
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1865 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001866 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 if ((ret = x509_info_ext_key_usage(&p, &n,
1869 &crt->ext_key_usage)) != 0) {
1870 return ret;
1871 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001872 }
1873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1875 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001876 MBEDTLS_X509_SAFE_SNPRINTF;
1877
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 if ((ret = x509_info_cert_policies(&p, &n,
1879 &crt->certificate_policies)) != 0) {
1880 return ret;
1881 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001882 }
1883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001885 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001888}
1889
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001890struct x509_crt_verify_string {
1891 int code;
1892 const char *string;
1893};
1894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001896static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001897 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001898 { 0, NULL }
1899};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001900#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001901
Gilles Peskine449bd832023-01-11 14:50:10 +01001902int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1903 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001904{
Janos Follath865b3eb2019-12-16 11:46:15 +00001905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001906 const struct x509_crt_verify_string *cur;
1907 char *p = buf;
1908 size_t n = size;
1909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1911 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001912 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001916 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001917 flags ^= cur->code;
1918 }
1919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 if (flags != 0) {
1921 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1922 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001923 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001924 }
1925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001927}
Hanno Becker612a2f12020-10-09 09:19:39 +01001928#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1931 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001932{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001933 unsigned int usage_must, usage_may;
1934 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1938 return 0;
1939 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001940
1941 usage_must = usage & ~may_mask;
1942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1944 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1945 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001946
1947 usage_may = usage & may_mask;
1948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1950 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1951 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001954}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1957 const char *usage_oid,
1958 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001959{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001961
1962 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1964 return 0;
1965 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001966
1967 /*
1968 * Look for the requested usage (or wildcard ANY) in our list
1969 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001972
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 if (cur_oid->len == usage_len &&
1974 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1975 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001976 }
1977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1979 return 0;
1980 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001981 }
1982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001984}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001987/*
1988 * Return 1 if the certificate is revoked, or 0 otherwise.
1989 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001990int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001991{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 while (cur != NULL && cur->serial.len != 0) {
1995 if (crt->serial.len == cur->serial.len &&
1996 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
1997 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001998 }
1999
2000 cur = cur->next;
2001 }
2002
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002004}
2005
2006/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002007 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002008 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002009 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002010static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2011 mbedtls_x509_crl *crl_list,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002012 const mbedtls_x509_crt_profile *profile,
2013 const mbedtls_x509_time *now)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002014{
2015 int flags = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002016 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002017#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002018 psa_algorithm_t psa_algorithm;
2019#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002021#endif /* MBEDTLS_USE_PSA_CRYPTO */
2022 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (ca == NULL) {
2025 return flags;
2026 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 while (crl_list != NULL) {
2029 if (crl_list->version == 0 ||
2030 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002031 crl_list = crl_list->next;
2032 continue;
2033 }
2034
2035 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002036 * Check if the CA is configured to sign CRLs
2037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 if (mbedtls_x509_crt_check_key_usage(ca,
2039 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002041 break;
2042 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002043
2044 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002045 * Check if CRL is correctly signed by the trusted CA
2046 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002048 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002052 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002054
pespacek7599a772022-02-07 14:40:55 +01002055#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002056 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 if (psa_hash_compute(psa_algorithm,
2058 crl_list->tbs.p,
2059 crl_list->tbs.len,
2060 hash,
2061 sizeof(hash),
2062 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002063 /* Note: this can't happen except after an internal error */
2064 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2065 break;
2066 }
pespacek7599a772022-02-07 14:40:55 +01002067#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2069 hash_length = mbedtls_md_get_size(md_info);
2070 if (mbedtls_md(md_info,
2071 crl_list->tbs.p,
2072 crl_list->tbs.len,
2073 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002074 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002076 break;
2077 }
pespaceka7a64692022-02-14 15:18:43 +01002078#endif /* MBEDTLS_USE_PSA_CRYPTO */
2079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002081 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2085 crl_list->sig_md, hash, hash_length,
2086 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002088 break;
2089 }
2090
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002091#if defined(MBEDTLS_HAVE_TIME_DATE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002092 /*
2093 * Check for validity of CRL (Do not drop out)
2094 */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002095 if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002098
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002099 if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002100 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002102#else
2103 ((void) now);
2104#endif
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002105
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002106 /*
2107 * Check if certificate is revoked
2108 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002111 break;
2112 }
2113
2114 crl_list = crl_list->next;
2115 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002120
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002121/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002122 * Check the signature of a certificate by its parent
2123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002124static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2125 mbedtls_x509_crt *parent,
2126 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002127{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002128 size_t hash_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002129 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002130#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2131 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 md_info = mbedtls_md_info_from_type(child->sig_md);
2133 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002134
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002135 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2137 return -1;
2138 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002139#else
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002140 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002141 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 status = psa_hash_compute(hash_alg,
2144 child->tbs.p,
2145 child->tbs.len,
2146 hash,
2147 sizeof(hash),
2148 &hash_len);
2149 if (status != PSA_SUCCESS) {
2150 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002151 }
2152
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002153#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002154 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2156 return -1;
2157 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002158
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002159#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2161 return mbedtls_pk_verify_restartable(&parent->pk,
2162 child->sig_md, hash, hash_len,
2163 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002164 }
2165#else
2166 (void) rs_ctx;
2167#endif
2168
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2170 child->sig_md, hash, hash_len,
2171 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002172}
2173
2174/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002175 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2176 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002177 *
2178 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002179 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002180static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2181 const mbedtls_x509_crt *parent,
2182 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002183{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002184 int need_ca_bit;
2185
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002186 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2188 return -1;
2189 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002190
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002191 /* Parent must have the basicConstraints CA bit set as a general rule */
2192 need_ca_bit = 1;
2193
2194 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002196 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002197 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if (need_ca_bit && !parent->ca_istrue) {
2200 return -1;
2201 }
2202
2203 if (need_ca_bit &&
2204 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2205 return -1;
2206 }
2207
2208 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002209}
2210
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002211/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002212 * Find a suitable parent for child in candidates, or return NULL.
2213 *
2214 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002215 * 1. subject name matches child's issuer
2216 * 2. if necessary, the CA bit is set and key usage allows signing certs
2217 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002218 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002219 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002220 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002221 * If there's a suitable candidate which is also time-valid, return the first
2222 * such. Otherwise, return the first suitable candidate (or NULL if there is
2223 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002224 *
2225 * The rationale for this rule is that someone could have a list of trusted
2226 * roots with two versions on the same root with different validity periods.
2227 * (At least one user reported having such a list and wanted it to just work.)
2228 * The reason we don't just require time-validity is that generally there is
2229 * only one version, and if it's expired we want the flags to state that
2230 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002231 *
2232 * The rationale for rule 3 (signature for trusted roots) is that users might
2233 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002234 * way we select the correct one is by checking the signature (as we don't
2235 * rely on key identifier extensions). (This is one way users might choose to
2236 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002237 *
2238 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002239 * - [in] child: certificate for which we're looking for a parent
2240 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002241 * - [out] r_parent: parent found (or NULL)
2242 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002243 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2244 * of the chain, 0 otherwise
2245 * - [in] path_cnt: number of intermediates seen so far
2246 * - [in] self_cnt: number of self-signed intermediates seen so far
2247 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002248 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002249 *
2250 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002251 * - 0 on success
2252 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002253 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002254static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 mbedtls_x509_crt *child,
2256 mbedtls_x509_crt *candidates,
2257 mbedtls_x509_crt **r_parent,
2258 int *r_signature_is_good,
2259 int top,
2260 unsigned path_cnt,
2261 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002262 mbedtls_x509_crt_restart_ctx *rs_ctx,
2263 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002264{
Janos Follath865b3eb2019-12-16 11:46:15 +00002265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002266 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002267 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002268
2269#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002270 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002272 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002273 parent = rs_ctx->parent;
2274 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002275 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002276
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002277 /* clear saved state */
2278 rs_ctx->parent = NULL;
2279 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002280 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002281
2282 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002283 goto check_signature;
2284 }
2285#endif
2286
2287 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002288 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002291 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002293 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002295
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002296 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 if (parent->max_pathlen > 0 &&
2298 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002299 continue;
2300 }
2301
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002302 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002303#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2304check_signature:
2305#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002307
2308#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002310 /* save state */
2311 rs_ctx->parent = parent;
2312 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002313 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002316 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002317#else
2318 (void) ret;
2319#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002320
2321 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002323 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002325
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002326#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002327 /* optional time check */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002328 if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2329 mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002331 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002332 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002333 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002334
2335 continue;
2336 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002337#else
2338 ((void) now);
2339#endif
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002340
Andy Gross1f627142019-01-30 10:25:53 -06002341 *r_parent = parent;
2342 *r_signature_is_good = signature_is_good;
2343
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002344 break;
2345 }
2346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002348 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002349 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002350 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002353}
2354
2355/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002356 * Find a parent in trusted CAs or the provided chain, or return NULL.
2357 *
2358 * Searches in trusted CAs first, and return the first suitable parent found
2359 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002360 *
2361 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002362 * - [in] child: certificate for which we're looking for a parent, followed
2363 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002364 * - [in] trust_ca: list of locally trusted certificates
2365 * - [out] parent: parent found (or NULL)
2366 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2367 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2368 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2369 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002370 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002371 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002372 *
2373 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002374 * - 0 on success
2375 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002376 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002377static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 mbedtls_x509_crt *child,
2379 mbedtls_x509_crt *trust_ca,
2380 mbedtls_x509_crt **parent,
2381 int *parent_is_trusted,
2382 int *signature_is_good,
2383 unsigned path_cnt,
2384 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002385 mbedtls_x509_crt_restart_ctx *rs_ctx,
2386 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002387{
Janos Follath865b3eb2019-12-16 11:46:15 +00002388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002389 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002390
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002391 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002392
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002393#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002394 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002396 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002397 rs_ctx->parent_is_trusted = -1;
2398 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002399#endif
2400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002402 search_list = *parent_is_trusted ? trust_ca : child->next;
2403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 ret = x509_crt_find_parent_in(child, search_list,
2405 parent, signature_is_good,
2406 *parent_is_trusted,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002407 path_cnt, self_cnt, rs_ctx, now);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002408
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002409#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002411 /* save state */
2412 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002414 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002415#else
2416 (void) ret;
2417#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002418
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002419 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002421 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002423
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002424 /* prepare second iteration */
2425 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002426 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002427
2428 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002430 *parent_is_trusted = 0;
2431 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002432 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002435}
2436
2437/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002438 * Check if an end-entity certificate is locally trusted
2439 *
2440 * Currently we require such certificates to be self-signed (actually only
2441 * check for self-issued as self-signatures are not checked)
2442 */
2443static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 mbedtls_x509_crt *crt,
2445 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002446{
2447 mbedtls_x509_crt *cur;
2448
2449 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2451 return -1;
2452 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002453
2454 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2456 if (crt->raw.len == cur->raw.len &&
2457 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2458 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002459 }
2460 }
2461
2462 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002464}
2465
2466/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002467 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002468 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002469 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2470 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002471 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002472 * such that every cert in the chain is a child of the next one,
2473 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002474 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002475 * Verify that chain and return it with flags for all issues found.
2476 *
2477 * Special cases:
2478 * - EE == Rj -> return a one-element list containing it
2479 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2480 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002481 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002482 * Tests for (aspects of) this function should include at least:
2483 * - trusted EE
2484 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002485 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002486 * - if relevant: EE untrusted
2487 * - if relevant: EE -> intermediate, untrusted
2488 * with the aspect under test checked at each relevant level (EE, int, root).
2489 * For some aspects longer chains are required, but usually length 2 is
2490 * enough (but length 1 is not in general).
2491 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002492 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002493 * - [in] crt: the cert list EE, C1, ..., Cn
2494 * - [in] trust_ca: the trusted list R1, ..., Rp
2495 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002496 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002497 * Only valid when return value is 0, may contain garbage otherwise!
2498 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002499 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002500 *
2501 * Return value:
2502 * - non-zero if the chain could not be fully built and examined
2503 * - 0 is the chain was successfully built and examined,
2504 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002505 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002506static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 mbedtls_x509_crt *crt,
2508 mbedtls_x509_crt *trust_ca,
2509 mbedtls_x509_crl *ca_crl,
2510 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2511 void *p_ca_cb,
2512 const mbedtls_x509_crt_profile *profile,
2513 mbedtls_x509_crt_verify_chain *ver_chain,
2514 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002515{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002516 /* Don't initialize any of those variables here, so that the compiler can
2517 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002519 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002520 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002521 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002522 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002523 int parent_is_trusted;
2524 int child_is_trusted;
2525 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002526 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002527 mbedtls_x509_crt *cur_trust_ca = NULL;
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002528 mbedtls_x509_time now;
2529
2530#if defined(MBEDTLS_HAVE_TIME_DATE)
2531 if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2532 return MBEDTLS_ERR_X509_FATAL_ERROR;
2533 }
2534#endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002535
2536#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2537 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002539 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002540 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002541 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002542
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002543 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002544 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002545 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002546 flags = &cur->flags;
2547
2548 goto find_parent;
2549 }
2550#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002551
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002552 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002553 self_cnt = 0;
2554 parent_is_trusted = 0;
2555 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002558 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002559 cur = &ver_chain->items[ver_chain->len];
2560 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002561 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002562 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002563 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002564
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002565#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002566 /* Check time-validity (all certificates) */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002567 if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002568 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002570
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002571 if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002572 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002574#endif
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002575
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002576 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (child_is_trusted) {
2578 return 0;
2579 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002580
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002581 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002583 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002587 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002589
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002590 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 if (ver_chain->len == 1 &&
2592 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2593 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002594 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002595
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002596#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2597find_parent:
2598#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002599
2600 /* Obtain list of potential trusted signers from CA callback,
2601 * or use statically provided list. */
2602#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 if (f_ca_cb != NULL) {
2604 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2605 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002606 ver_chain->trust_ca_cb_result = NULL;
2607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2609 if (ret != 0) {
2610 return MBEDTLS_ERR_X509_FATAL_ERROR;
2611 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002612
2613 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002615#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2616 {
2617 ((void) f_ca_cb);
2618 ((void) p_ca_cb);
2619 cur_trust_ca = trust_ca;
2620 }
2621
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002622 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2624 &parent_is_trusted, &signature_is_good,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002625 ver_chain->len - 1, self_cnt, rs_ctx,
2626 &now);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002627
2628#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002630 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002631 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002632 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002633 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002636 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002637#else
2638 (void) ret;
2639#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002640
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002641 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002643 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002645 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002646
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002647 /* Count intermediate self-issued (not necessarily self-signed) certs.
2648 * These can occur with some strategies for key rollover, see [SIRO],
2649 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 if (ver_chain->len != 1 &&
2651 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002652 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002653 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002654
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002655 /* path_cnt is 0 for the first intermediate CA,
2656 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 if (!parent_is_trusted &&
2658 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002659 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002661 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002662
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002663 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002665 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002667
2668 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002670 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002674 /* Check trusted CA's CRL for the given crt */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002675 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002676#else
2677 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002678#endif
2679
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002680 /* prepare for next iteration */
2681 child = parent;
2682 parent = NULL;
2683 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002684 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002685 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002686}
2687
Glenn Straussc26bd762022-10-23 19:48:18 -04002688#ifdef _WIN32
2689#ifdef _MSC_VER
2690#pragma comment(lib, "ws2_32.lib")
2691#include <winsock2.h>
2692#include <ws2tcpip.h>
2693#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2694#include <winsock2.h>
2695#include <ws2tcpip.h>
Steve Lhommeeb0f18a2023-06-16 14:12:19 +02002696#else
2697/* inet_pton() is not supported, fallback to software version */
2698#define MBEDTLS_TEST_SW_INET_PTON
Glenn Straussc26bd762022-10-23 19:48:18 -04002699#endif
2700#elif defined(__sun)
2701/* Solaris requires -lsocket -lnsl for inet_pton() */
2702#elif defined(__has_include)
2703#if __has_include(<sys/socket.h>)
2704#include <sys/socket.h>
2705#endif
2706#if __has_include(<arpa/inet.h>)
2707#include <arpa/inet.h>
2708#endif
2709#endif
2710
2711/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2712 * the platform inet_pton() or a local implementation (below). The local
2713 * implementation may be used even in cases where the platform provides
2714 * inet_pton(), e.g. when there are different includes required and/or the
2715 * platform implementation requires dependencies on additional libraries.
2716 * Specifically, Windows requires custom includes and additional link
2717 * dependencies, and Solaris requires additional link dependencies.
2718 * Also, as a coarse heuristic, use the local implementation if the compiler
2719 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002720 * provided by headers included (or not) via __has_include() above.
2721 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2722 * despite having a platform that has inet_pton. */
2723#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2724/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002725static int x509_inet_pton_ipv4(const char *src, void *dst);
2726
2727#define li_cton(c, n) \
2728 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2729
2730static int x509_inet_pton_ipv6(const char *src, void *dst)
2731{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002732 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002733 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002734 uint16_t addr[8];
2735 do {
2736 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002737 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002738 for (uint8_t digit; num_digits < 4; num_digits++) {
2739 if (li_cton(*p, digit) == 0) {
2740 break;
2741 }
2742 group = (group << 4) | digit;
2743 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002744 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002745 if (num_digits != 0) {
Dave Rodgmancfa72232023-09-05 16:20:33 +01002746 MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups);
2747 nonzero_groups++;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002748 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002749 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002750 } else if (*p == '.') {
2751 /* Don't accept IPv4 too early or late */
2752 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2753 nonzero_groups >= 7) {
2754 break;
2755 }
2756
2757 /* Walk back to prior ':', then parse as IPv4-mapped */
2758 int steps = 4;
2759 do {
2760 p--;
2761 steps--;
2762 } while (*p != ':' && steps > 0);
2763
2764 if (*p != ':') {
2765 break;
2766 }
2767 p++;
2768 nonzero_groups--;
2769 if (x509_inet_pton_ipv4((const char *) p,
2770 addr + nonzero_groups) != 0) {
2771 break;
2772 }
2773
Andrzej Kurek0d578962023-04-13 09:09:56 -04002774 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002775 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002776 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002777 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002778 return -1;
2779 }
2780 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002781 /* Don't accept a second zero group or an invalid delimiter */
2782 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002783 return -1;
2784 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002785 zero_group_start = nonzero_groups;
2786
2787 /* Accept a zero group at start, but it has to be a double colon */
2788 if (zero_group_start == 0 && *++p != ':') {
2789 return -1;
2790 }
2791
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002792 if (p[1] == '\0') {
2793 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002794 break;
2795 }
2796 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002797 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002798 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002799
2800 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002801 return -1;
2802 }
2803
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002804 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002805 if (nonzero_groups > 6) {
2806 return -1;
2807 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002808 int zero_groups = 8 - nonzero_groups;
2809 int groups_after_zero = nonzero_groups - zero_group_start;
2810
2811 /* Move the non-zero part to after the zeroes */
2812 if (groups_after_zero) {
2813 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002814 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002815 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002816 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002817 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002818 } else {
2819 if (nonzero_groups != 8) {
2820 return -1;
2821 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002822 }
2823 memcpy(dst, addr, sizeof(addr));
2824 return 0;
2825}
2826
2827static int x509_inet_pton_ipv4(const char *src, void *dst)
2828{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002829 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002830 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002831 uint8_t digit, num_digits = 0;
2832 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002833 uint16_t octet;
2834
Glenn Strauss416c2952022-10-24 23:00:02 -04002835 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002836 octet = num_digits = 0;
2837 do {
2838 digit = *p - '0';
2839 if (digit > 9) {
2840 break;
2841 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002842
2843 /* Don't allow leading zeroes. These might mean octal format,
2844 * which this implementation does not support. */
2845 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002846 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002847 }
2848
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002849 octet = octet * 10 + digit;
2850 num_digits++;
2851 p++;
2852 } while (num_digits < 3);
2853
2854 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002855 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002856 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002857 *res++ = (uint8_t) octet;
2858 num_octets++;
2859 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002860 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002861}
Glenn Straussc26bd762022-10-23 19:48:18 -04002862
2863#else
2864
2865static int x509_inet_pton_ipv6(const char *src, void *dst)
2866{
2867 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2868}
2869
2870static int x509_inet_pton_ipv4(const char *src, void *dst)
2871{
2872 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2873}
2874
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002875#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002876
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002877size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002878{
2879 return strchr(cn, ':') == NULL
2880 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2881 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2882}
2883
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002884/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002885 * Check for CN match
2886 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002887static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2888 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002889{
2890 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 if (name->len == cn_len &&
2892 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2893 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002894 }
2895
2896 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 if (x509_check_wildcard(cn, name) == 0) {
2898 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002899 }
2900
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002902}
2903
Glenn Straussc26bd762022-10-23 19:48:18 -04002904static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2905 const char *cn, size_t cn_len)
2906{
2907 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002908 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002909 if (cn_len == 0) {
2910 return -1;
2911 }
2912
2913 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2914 const unsigned char san_type = (unsigned char) cur->buf.tag &
2915 MBEDTLS_ASN1_TAG_VALUE_MASK;
2916 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2917 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2918 return 0;
2919 }
2920 }
2921
2922 return -1;
2923}
2924
Andrzej Kurek199eab92023-05-10 09:57:19 -04002925static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2926 const char *cn, size_t cn_len)
2927{
2928 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2929 const unsigned char san_type = (unsigned char) cur->buf.tag &
2930 MBEDTLS_ASN1_TAG_VALUE_MASK;
2931 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2932 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2933 return 0;
2934 }
2935 }
2936
2937 return -1;
2938}
2939
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002940/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002941 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2942 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002943static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002945{
Glenn Straussc26bd762022-10-23 19:48:18 -04002946 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002947 int san_uri = 0;
2948 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002949 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2950 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002951 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002952 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2953 return 0;
2954 }
2955 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002956 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04002957 san_ip = 1;
2958 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002959 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
2960 san_uri = 1;
2961 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04002962 /* (We may handle other types here later.) */
2963 default: /* Unrecognized type */
2964 break;
2965 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04002967 if (san_ip) {
2968 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
2969 return 0;
2970 }
2971 }
2972 if (san_uri) {
2973 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
2974 return 0;
2975 }
2976 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002977
Andrzej Kurek199eab92023-05-10 09:57:19 -04002978 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002979}
2980
2981/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002982 * Verify the requested CN - only call this if cn is not NULL!
2983 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002984static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2985 const char *cn,
2986 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002987{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002988 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002990
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002992 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
2993 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 }
2995 } else {
2996 for (name = &crt->subject; name != NULL; name = name->next) {
2997 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2998 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002999 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 }
3001 }
3002
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003003 }
Glenn Straussc26bd762022-10-23 19:48:18 -04003004
3005 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003006}
3007
3008/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003009 * Merge the flags for all certs in the chain, after calling callback
3010 */
3011static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 uint32_t *flags,
3013 const mbedtls_x509_crt_verify_chain *ver_chain,
3014 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3015 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003016{
Janos Follath865b3eb2019-12-16 11:46:15 +00003017 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003018 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003019 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003020 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003023 cur = &ver_chain->items[i-1];
3024 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003025
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 if (NULL != f_vrfy) {
3027 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3028 return ret;
3029 }
3030 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003031
3032 *flags |= cur_flags;
3033 }
3034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003036}
3037
3038/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003039 * Verify the certificate validity, with profile, restartable version
3040 *
3041 * This function:
3042 * - checks the requested CN (if any)
3043 * - checks the type and size of the EE cert's key,
3044 * as that isn't done as part of chain building/verification currently
3045 * - builds and verifies the chain
3046 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003047 *
3048 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3049 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3050 * verification routine to search for trusted signers, and CRLs will
3051 * be disabled. Otherwise, `trust_ca` will be used as the static list
3052 * of trusted signers, and `ca_crl` will be use as the static list
3053 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003054 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003055static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3056 mbedtls_x509_crt *trust_ca,
3057 mbedtls_x509_crl *ca_crl,
3058 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3059 void *p_ca_cb,
3060 const mbedtls_x509_crt_profile *profile,
3061 const char *cn, uint32_t *flags,
3062 int (*f_vrfy)(void *,
3063 mbedtls_x509_crt *,
3064 int,
3065 uint32_t *),
3066 void *p_vrfy,
3067 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003068{
Janos Follath865b3eb2019-12-16 11:46:15 +00003069 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003070 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003071 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003072 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003073
3074 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003075 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003079 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3080 goto exit;
3081 }
3082
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003083 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 if (cn != NULL) {
3085 x509_crt_verify_name(crt, cn, &ee_flags);
3086 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003087
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003088 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003092 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003094
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003096 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003098
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003099 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3101 f_ca_cb, p_ca_cb, profile,
3102 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003103
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003105 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003107
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003108 /* Merge end-entity flags */
3109 ver_chain.items[0].flags |= ee_flags;
3110
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003111 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003113
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003114exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003115
3116#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3118 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003119 ver_chain.trust_ca_cb_result = NULL;
3120#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3121
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003122#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3124 mbedtls_x509_crt_restart_free(rs_ctx);
3125 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003126#endif
3127
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003128 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3129 * the SSL module for authmode optional, but non-zero return from the
3130 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003132 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003133 }
3134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 if (ret != 0) {
3136 *flags = (uint32_t) -1;
3137 return ret;
3138 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003139
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 if (*flags != 0) {
3141 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3142 }
3143
3144 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003145}
3146
Hanno Becker3116fb32019-03-28 13:34:42 +00003147
3148/*
3149 * Verify the certificate validity (default profile, not restartable)
3150 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003151int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3152 mbedtls_x509_crt *trust_ca,
3153 mbedtls_x509_crl *ca_crl,
3154 const char *cn, uint32_t *flags,
3155 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3156 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003157{
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3159 NULL, NULL,
3160 &mbedtls_x509_crt_profile_default,
3161 cn, flags,
3162 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003163}
3164
3165/*
3166 * Verify the certificate validity (user-chosen profile, not restartable)
3167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003168int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3169 mbedtls_x509_crt *trust_ca,
3170 mbedtls_x509_crl *ca_crl,
3171 const mbedtls_x509_crt_profile *profile,
3172 const char *cn, uint32_t *flags,
3173 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3174 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003175{
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3177 NULL, NULL,
3178 profile, cn, flags,
3179 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003180}
3181
3182#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3183/*
3184 * Verify the certificate validity (user-chosen profile, CA callback,
3185 * not restartable).
3186 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003187int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3188 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3189 void *p_ca_cb,
3190 const mbedtls_x509_crt_profile *profile,
3191 const char *cn, uint32_t *flags,
3192 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3193 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003194{
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3196 f_ca_cb, p_ca_cb,
3197 profile, cn, flags,
3198 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003199}
3200#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3201
Gilles Peskine449bd832023-01-11 14:50:10 +01003202int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3203 mbedtls_x509_crt *trust_ca,
3204 mbedtls_x509_crl *ca_crl,
3205 const mbedtls_x509_crt_profile *profile,
3206 const char *cn, uint32_t *flags,
3207 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3208 void *p_vrfy,
3209 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003210{
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3212 NULL, NULL,
3213 profile, cn, flags,
3214 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003215}
3216
3217
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003218/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003219 * Initialize a certificate chain
3220 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003221void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003222{
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003224}
3225
3226/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003227 * Unallocate all certificate data
3228 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003229void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003230{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 mbedtls_x509_crt *cert_cur = crt;
3232 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003233
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 while (cert_cur != NULL) {
3235 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003239#endif
3240
Gilles Peskine449bd832023-01-11 14:50:10 +01003241 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3242 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3243 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3244 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3245 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Przemek Stekiel690ff692023-05-15 09:54:02 +02003246 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003247
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003249 mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003250 }
3251
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003252 cert_prv = cert_cur;
3253 cert_cur = cert_cur->next;
3254
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3256 if (cert_prv != crt) {
3257 mbedtls_free(cert_prv);
3258 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003259 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003260}
3261
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003262#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3263/*
3264 * Initialize a restart context
3265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003266void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003267{
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003269
3270 ctx->parent = NULL;
3271 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003272 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003273
3274 ctx->parent_is_trusted = -1;
3275
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003276 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003277 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003279}
3280
3281/*
3282 * Free the components of a restart context
3283 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003284void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003285{
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003287 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003289
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 mbedtls_pk_restart_free(&ctx->pk);
3291 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003292}
3293#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3294
Minos Galanakis2abbac72024-01-18 17:05:21 +00003295int mbedtls_x509_crt_get_ca_istrue(const mbedtls_x509_crt *crt)
3296{
3297 if ((crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) != 0) {
3298 return crt->MBEDTLS_PRIVATE(ca_istrue);
3299 }
3300 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
3301}
3302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303#endif /* MBEDTLS_X509_CRT_PARSE_C */