blob: d4a6dbb77788e1a959aba5af4c58bc670ce71884 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020049#include "psa_util_internal.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020050#include "md_psa.h"
pespacek7599a772022-02-07 14:40:55 +010051#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti3f00b842023-05-15 12:57:06 +020052#include "pk_internal.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040053
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010058#endif
59
Daniel Axtensf0710242020-05-28 11:43:41 +100060#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010061#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Glenn Straussc26bd762022-10-23 19:48:18 -040062#define WIN32_LEAN_AND_MEAN
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063#include <windows.h>
Simon Butchere068aa72018-03-14 15:10:31 +000064#if defined(_MSC_VER) && _MSC_VER <= 1600
Simon Butcherdef90f42018-03-14 17:02:16 +000065/* Visual Studio 2010 and earlier issue a warning when both <stdint.h> and
66 * <intsafe.h> are included, as they redefine a number of <TYPE>_MAX constants.
67 * These constants are guaranteed to be the same, though, so we suppress the
68 * warning when including intsafe.h.
Kevin Kane0ec1e682016-12-15 09:27:16 -080069 */
Minos Galanakisa277b212023-08-09 16:32:22 +010070#pragma warning(push )
71#pragma warning(disable : 4005)
Kevin Kane0ec1e682016-12-15 09:27:16 -080072#endif
73#include <intsafe.h>
Simon Butchere068aa72018-03-14 15:10:31 +000074#if defined(_MSC_VER) && _MSC_VER <= 1600
Minos Galanakisa277b212023-08-09 16:32:22 +010075#pragma warning(pop)
Kevin Kane0ec1e682016-12-15 09:27:16 -080076#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#else
78#include <time.h>
79#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100080#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000083#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020084#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#include <sys/types.h>
86#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020087#if defined(__MBED__)
88#include <platform/mbed_retarget.h>
89#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020091#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060092#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000093#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094#endif
95
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020096/*
97 * Item in a verification chain: cert and flags for it
98 */
99typedef struct {
100 mbedtls_x509_crt *crt;
101 uint32_t flags;
102} x509_crt_verify_chain_item;
103
104/*
105 * Max size of verification chain: end-entity + intermediates + trusted root
106 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +0200108
Gilles Peskineffb92da2021-06-02 00:03:26 +0200109/* Default profile. Do not remove items unless there are serious security
110 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200111const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
112{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200113 /* Hashes from SHA-256 and above. Note that this selection
114 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
116 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
117 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200118 0xFFFFFFF, /* Any PK alg */
Valerio Setti8c3404f2023-06-26 15:49:48 +0200119#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200120 /* Curves at or above 128-bit security level. Note that this selection
121 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
123 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
124 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
125 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
126 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
127 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
Gilles Peskine39957502021-06-17 23:17:52 +0200128 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200129#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200130 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200131#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200132 2048,
133};
134
Gilles Peskineffb92da2021-06-02 00:03:26 +0200135/* Next-generation profile. Currently identical to the default, but may
136 * be tightened at any time. */
137const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200138{
139 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
141 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
142 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200143 0xFFFFFFF, /* Any PK alg */
144#if defined(MBEDTLS_ECP_C)
145 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
147 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
148 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
149 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
150 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
151 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
152 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200153#else
154 0,
155#endif
156 2048,
157};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200158
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200159/*
160 * NSA Suite B Profile
161 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200162const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
163{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200164 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
166 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200167 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
169 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Valerio Setti8c3404f2023-06-26 15:49:48 +0200170#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200171 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
173 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Valerio Setti8c3404f2023-06-26 15:49:48 +0200174#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200175 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200176#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200177 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200178};
179
180/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200181 * Empty / all-forbidden profile
182 */
183const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
184{
185 0,
186 0,
187 0,
188 (uint32_t) -1,
189};
190
191/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200192 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200193 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200194 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
196 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200197{
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (md_alg == MBEDTLS_MD_NONE) {
199 return -1;
200 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
203 return 0;
204 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200207}
208
209/*
210 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200211 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200212 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100213static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
214 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200215{
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 if (pk_alg == MBEDTLS_PK_NONE) {
217 return -1;
218 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
221 return 0;
222 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200225}
226
227/*
228 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200229 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200230 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
232 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200233{
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200235
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200236#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
238 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
239 return 0;
240 }
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 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200244#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200245
Valerio Setti8c3404f2023-06-26 15:49:48 +0200246#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200248 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 pk_alg == MBEDTLS_PK_ECKEY_DH) {
Valerio Setti97207782023-05-18 18:59:06 +0200250 const mbedtls_ecp_group_id gid = mbedtls_pk_get_group_id(pk);
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if (gid == MBEDTLS_ECP_DP_NONE) {
253 return -1;
254 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
257 return 0;
258 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200261 }
Valerio Setti8c3404f2023-06-26 15:49:48 +0200262#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200265}
266
267/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000268 * Like memcmp, but case-insensitive and always returns -1 if different
269 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000271{
272 size_t i;
273 unsigned char diff;
274 const unsigned char *n1 = s1, *n2 = s2;
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000277 diff = n1[i] ^ n2[i];
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000280 continue;
281 }
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (diff == 32 &&
284 ((n1[i] >= 'a' && n1[i] <= 'z') ||
285 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
286 continue;
287 }
288
289 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000290 }
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000293}
294
295/*
296 * Return 0 if name matches wildcard, -1 otherwise
297 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100298static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000299{
300 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000302
303 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
305 return -1;
306 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 for (i = 0; i < cn_len; ++i) {
309 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000310 cn_idx = i;
311 break;
312 }
313 }
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (cn_idx == 0) {
316 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000317 }
318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if (cn_len - cn_idx == name->len - 1 &&
320 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
321 return 0;
322 }
323
324 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000325}
326
327/*
328 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
329 * variations (but not all).
330 *
331 * Return 0 if equal, -1 otherwise.
332 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100333static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000334{
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000336 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 memcmp(a->p, b->p, b->len) == 0) {
338 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000339 }
340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
342 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000343 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 x509_memcasecmp(a->p, b->p, b->len) == 0) {
345 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000346 }
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000349}
350
351/*
352 * Compare two X.509 Names (aka rdnSequence).
353 *
354 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
355 * we sometimes return unequal when the full algorithm would return equal,
356 * but never the other way. (In particular, we don't do Unicode normalisation
357 * or space folding.)
358 *
359 * Return 0 if equal, -1 otherwise.
360 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100361static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000362{
363 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 while (a != NULL || b != NULL) {
365 if (a == NULL || b == NULL) {
366 return -1;
367 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000368
369 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000371 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
373 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000374 }
375
376 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (x509_string_cmp(&a->val, &b->val) != 0) {
378 return -1;
379 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000380
381 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (a->next_merged != b->next_merged) {
383 return -1;
384 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000385
386 a = a->next;
387 b = b->next;
388 }
389
390 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000392}
393
394/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200395 * Reset (init or clear) a verify_chain
396 */
397static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200399{
400 size_t i;
401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200403 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000404 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200405 }
406
407 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000408
409#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
410 ver_chain->trust_ca_cb_result = NULL;
411#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200412}
413
414/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
416 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100417static int x509_get_version(unsigned char **p,
418 const unsigned char *end,
419 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420{
Janos Follath865b3eb2019-12-16 11:46:15 +0000421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422 size_t len;
423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
425 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
426 0)) != 0) {
427 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430 }
431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433 }
434
435 end = *p + len;
436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
438 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
439 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (*p != end) {
442 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
443 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
444 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447}
448
449/*
450 * Validity ::= SEQUENCE {
451 * notBefore Time,
452 * notAfter Time }
453 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100454static int x509_get_dates(unsigned char **p,
455 const unsigned char *end,
456 mbedtls_x509_time *from,
457 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458{
Janos Follath865b3eb2019-12-16 11:46:15 +0000459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460 size_t len;
461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
463 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
464 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
465 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
467 end = *p + len;
468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
470 return ret;
471 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
474 return ret;
475 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if (*p != end) {
478 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
479 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
480 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483}
484
485/*
486 * X.509 v2/v3 unique identifier (not parsed)
487 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100488static int x509_get_uid(unsigned char **p,
489 const unsigned char *end,
490 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491{
Janos Follath865b3eb2019-12-16 11:46:15 +0000492 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (*p == end) {
495 return 0;
496 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497
498 uid->tag = **p;
499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
501 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
502 n)) != 0) {
503 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
504 return 0;
505 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508 }
509
510 uid->p = *p;
511 *p += uid->len;
512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514}
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516static int x509_get_basic_constraints(unsigned char **p,
517 const unsigned char *end,
518 int *ca_istrue,
519 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520{
Janos Follath865b3eb2019-12-16 11:46:15 +0000521 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522 size_t len;
523
524 /*
525 * BasicConstraints ::= SEQUENCE {
526 * cA BOOLEAN DEFAULT FALSE,
527 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
528 */
529 *ca_istrue = 0; /* DEFAULT FALSE */
530 *max_pathlen = 0; /* endless */
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
533 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
534 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535 }
536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (*p == end) {
538 return 0;
539 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
542 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
543 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
544 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (ret != 0) {
547 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
548 }
549
550 if (*ca_istrue != 0) {
551 *ca_istrue = 1;
552 }
553 }
554
555 if (*p == end) {
556 return 0;
557 }
558
559 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
560 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
561 }
562
563 if (*p != end) {
564 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
565 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
566 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
Andrzej Kurek16050742020-04-14 09:49:52 -0400568 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
569 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (*max_pathlen == INT_MAX) {
571 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
572 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
573 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400574
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575 (*max_pathlen)++;
576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578}
579
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580/*
581 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
582 *
583 * KeyPurposeId ::= OBJECT IDENTIFIER
584 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100585static int x509_get_ext_key_usage(unsigned char **p,
586 const unsigned char *end,
587 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588{
Janos Follath865b3eb2019-12-16 11:46:15 +0000589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
592 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
593 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594
595 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (ext_key_usage->buf.p == NULL) {
597 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
598 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
599 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602}
603
604/*
toth92ga41954d2021-02-12 16:11:17 +0100605 * SubjectKeyIdentifier ::= KeyIdentifier
606 *
607 * KeyIdentifier ::= OCTET STRING
608 */
609static int x509_get_subject_key_id(unsigned char **p,
610 const unsigned char *end,
611 mbedtls_x509_buf *subject_key_id)
612{
613 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
614 size_t len = 0u;
615
616 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
617 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100618 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92ga41954d2021-02-12 16:11:17 +0100619 }
620
Przemek Stekiel61aed062023-05-08 11:14:36 +0200621 subject_key_id->len = len;
622 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
623 subject_key_id->p = *p;
624 *p += len;
625
toth92gd96027a2021-04-27 15:41:25 +0200626 if (*p != end) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100627 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
628 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92gd96027a2021-04-27 15:41:25 +0200629 }
630
toth92ga41954d2021-02-12 16:11:17 +0100631 return 0;
632}
633
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100634/*
toth92g8d435a02021-05-10 15:16:33 +0200635 * AuthorityKeyIdentifier ::= SEQUENCE {
636 * keyIdentifier [0] KeyIdentifier OPTIONAL,
637 * authorityCertIssuer [1] GeneralNames OPTIONAL,
638 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
639 *
640 * KeyIdentifier ::= OCTET STRING
641 */
642static int x509_get_authority_key_id(unsigned char **p,
643 unsigned char *end,
644 mbedtls_x509_authority *authority_key_id)
645{
646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
647 size_t len = 0u;
648
649 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100650 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100651 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200652 }
653
Przemek Stekiel6ec839a2023-02-01 11:06:08 +0100654 if (*p + len != end) {
655 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
656 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
657 }
658
Przemek Stekieled9fb782023-05-03 16:27:25 +0200659 ret = mbedtls_asn1_get_tag(p, end, &len,
660 MBEDTLS_ASN1_CONTEXT_SPECIFIC);
661
662 /* KeyIdentifier is an OPTIONAL field */
Przemek Stekiel61aed062023-05-08 11:14:36 +0200663 if (ret == 0) {
toth92g8d435a02021-05-10 15:16:33 +0200664 authority_key_id->keyIdentifier.len = len;
665 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200666 /* Setting tag of the keyIdentfier intentionally to 0x04.
667 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
Przemek Stekiel9a7a7252023-04-17 16:06:57 +0200668 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200669 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
670
671 *p += len;
Przemek Stekiel61aed062023-05-08 11:14:36 +0200672 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
673 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200674 }
675
676 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200677 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200678 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
679 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100680 1)) != 0) {
Przemek Stekielf5b8f782023-04-26 08:55:26 +0200681 /* authorityCertIssuer and authorityCertSerialNumber MUST both
682 be present or both be absent. At this point we expect to have both. */
683 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200684 }
Przemek Stekieled9fb782023-05-03 16:27:25 +0200685 /* "end" also includes the CertSerialNumber field so "len" shall be used */
686 ret = mbedtls_x509_get_subject_alt_name_ext(p,
687 (*p+len),
688 &authority_key_id->authorityCertIssuer);
689 if (ret != 0) {
690 return ret;
691 }
692
693 /* Getting authorityCertSerialNumber using the required specific class tag [2] */
694 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
695 MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) {
696 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
697 }
698 authority_key_id->authorityCertSerialNumber.len = len;
699 authority_key_id->authorityCertSerialNumber.p = *p;
700 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER;
701 *p += len;
toth92g8d435a02021-05-10 15:16:33 +0200702 }
703
704 if (*p != end) {
705 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
706 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
707 }
708
709 return 0;
710}
711
712/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200713 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
714 *
715 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
716 *
717 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
718 *
719 * PolicyInformation ::= SEQUENCE {
720 * policyIdentifier CertPolicyId,
721 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
722 * PolicyQualifierInfo OPTIONAL }
723 *
724 * CertPolicyId ::= OBJECT IDENTIFIER
725 *
726 * PolicyQualifierInfo ::= SEQUENCE {
727 * policyQualifierId PolicyQualifierId,
728 * qualifier ANY DEFINED BY policyQualifierId }
729 *
730 * -- policyQualifierIds for Internet policy qualifiers
731 *
732 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
733 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
734 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
735 *
736 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
737 *
738 * Qualifier ::= CHOICE {
739 * cPSuri CPSuri,
740 * userNotice UserNotice }
741 *
742 * CPSuri ::= IA5String
743 *
744 * UserNotice ::= SEQUENCE {
745 * noticeRef NoticeReference OPTIONAL,
746 * explicitText DisplayText OPTIONAL }
747 *
748 * NoticeReference ::= SEQUENCE {
749 * organization DisplayText,
750 * noticeNumbers SEQUENCE OF INTEGER }
751 *
752 * DisplayText ::= CHOICE {
753 * ia5String IA5String (SIZE (1..200)),
754 * visibleString VisibleString (SIZE (1..200)),
755 * bmpString BMPString (SIZE (1..200)),
756 * utf8String UTF8String (SIZE (1..200)) }
757 *
758 * NOTE: we only parse and use anyPolicy without qualifiers at this point
759 * as defined in RFC 5280.
760 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761static int x509_get_certificate_policies(unsigned char **p,
762 const unsigned char *end,
763 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200764{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300765 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200766 size_t len;
767 mbedtls_asn1_buf *buf;
768 mbedtls_asn1_sequence *cur = certificate_policies;
769
770 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 ret = mbedtls_asn1_get_tag(p, end, &len,
772 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
773 if (ret != 0) {
774 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
775 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (*p + len != end) {
778 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
779 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
780 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200781
782 /*
783 * Cannot be an empty sequence.
784 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (len == 0) {
786 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
787 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
788 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200791 mbedtls_x509_buf policy_oid;
792 const unsigned char *policy_end;
793
794 /*
795 * Get the policy sequence
796 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
798 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
799 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
800 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200801
802 policy_end = *p + len;
803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
805 MBEDTLS_ASN1_OID)) != 0) {
806 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
807 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200808
809 policy_oid.tag = MBEDTLS_ASN1_OID;
810 policy_oid.len = len;
811 policy_oid.p = *p;
812
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300813 /*
814 * Only AnyPolicy is currently supported when enforcing policy.
815 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300817 /*
818 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200819 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300820 */
821 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
822 }
823
Ron Eldor74d9acc2019-03-21 14:00:03 +0200824 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (cur->buf.p != NULL) {
826 if (cur->next != NULL) {
827 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
828 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (cur->next == NULL) {
833 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
834 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
835 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200836
837 cur = cur->next;
838 }
839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200841 buf->tag = policy_oid.tag;
842 buf->p = policy_oid.p;
843 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300844
845 *p += len;
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 /*
848 * If there is an optional qualifier, then *p < policy_end
849 * Check the Qualifier len to verify it doesn't exceed policy_end.
850 */
851 if (*p < policy_end) {
852 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
853 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
854 0) {
855 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
856 }
Ron Eldor08063792019-05-13 16:38:39 +0300857 /*
858 * Skip the optional policy qualifiers.
859 */
860 *p += len;
861 }
862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 if (*p != policy_end) {
864 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
865 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
866 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200867 }
868
869 /* Set final sequence entry's next pointer to NULL */
870 cur->next = NULL;
871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if (*p != end) {
873 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
874 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
875 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200878}
879
880/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881 * X.509 v3 extensions
882 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100884static int x509_get_crt_ext(unsigned char **p,
885 const unsigned char *end,
886 mbedtls_x509_crt *crt,
887 mbedtls_x509_crt_ext_cb_t cb,
888 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889{
Janos Follath865b3eb2019-12-16 11:46:15 +0000890 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200891 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200892 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (*p == end) {
895 return 0;
896 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
899 return ret;
900 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901
Hanno Becker12f62fb2019-02-12 17:22:36 +0000902 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 /*
905 * Extension ::= SEQUENCE {
906 * extnID OBJECT IDENTIFIER,
907 * critical BOOLEAN DEFAULT FALSE,
908 * extnValue OCTET STRING }
909 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911 int is_critical = 0; /* DEFAULT FALSE */
912 int ext_type = 0;
913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
915 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
916 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
917 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918
919 end_ext_data = *p + len;
920
921 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
923 MBEDTLS_ASN1_OID)) != 0) {
924 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
925 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926
k-stachowiak463928a2018-07-24 12:50:59 +0200927 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928 extn_oid.p = *p;
929 *p += extn_oid.len;
930
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
933 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
934 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
935 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936
937 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
939 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
940 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
941 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200943 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944 end_ext_octet = *p + len;
945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (end_ext_octet != end_ext_data) {
947 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
948 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
949 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950
951 /*
952 * Detect supported extensions
953 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200957 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if (cb != NULL) {
959 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
960 if (ret != 0 && is_critical) {
961 return ret;
962 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200963 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200964 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200965 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200966
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 /* No parser found, skip extension */
968 *p = end_ext_octet;
969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
973 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975 continue;
976 }
977
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100978 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if ((crt->ext_types & ext_type) != 0) {
980 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
981 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100982
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983 crt->ext_types |= ext_type;
984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 switch (ext_type) {
986 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
987 /* Parse basic constraints */
988 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
989 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
990 return ret;
991 }
992 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 case MBEDTLS_X509_EXT_KEY_USAGE:
995 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100996 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
997 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return ret;
999 }
1000 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
1003 /* Parse extended key usage */
1004 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
1005 &crt->ext_key_usage)) != 0) {
1006 return ret;
1007 }
1008 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009
toth92ga41954d2021-02-12 16:11:17 +01001010 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
1011 /* Parse subject key identifier */
1012 if ((ret = x509_get_subject_key_id(p, end_ext_data,
1013 &crt->subject_key_id)) != 0) {
1014 return ret;
1015 }
1016 break;
toth92g8d435a02021-05-10 15:16:33 +02001017
toth92ga41954d2021-02-12 16:11:17 +01001018 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1019 /* Parse authority key identifier */
1020 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
1021 &crt->authority_key_id)) != 0) {
1022 return ret;
1023 }
1024 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001026 /* Parse subject alt name
1027 * SubjectAltName ::= GeneralNames
1028 */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001029 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1030 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 return ret;
1032 }
1033 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1036 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001037 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1038 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 return ret;
1040 }
1041 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1044 /* Parse certificate policies type */
1045 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1046 &crt->certificate_policies)) != 0) {
1047 /* Give the callback (if any) a chance to handle the extension
1048 * if it contains unsupported policies */
1049 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1050 cb(p_ctx, crt, &extn_oid, is_critical,
1051 start_ext_octet, end_ext_octet) == 0) {
1052 break;
1053 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (is_critical) {
1056 return ret;
1057 } else
1058 /*
1059 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1060 * cannot interpret or enforce the policy. However, it is up to
1061 * the user to choose how to enforce the policies,
1062 * unless the extension is critical.
1063 */
1064 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1065 return ret;
1066 }
1067 }
1068 break;
1069
1070 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001071 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 * If this is a non-critical extension, which the oid layer
1073 * supports, but there isn't an x509 parser for it,
1074 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001075 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if (is_critical) {
1077 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1078 } else {
1079 *p = end_ext_octet;
1080 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081 }
1082 }
1083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if (*p != end) {
1085 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1086 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1087 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090}
1091
1092/*
1093 * Parse and fill a single X.509 certificate in DER format
1094 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1096 const unsigned char *buf,
1097 size_t buflen,
1098 int make_copy,
1099 mbedtls_x509_crt_ext_cb_t cb,
1100 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101{
Janos Follath865b3eb2019-12-16 11:46:15 +00001102 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103 size_t len;
1104 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1108 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1109 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110
1111 /*
1112 * Check for valid input
1113 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if (crt == NULL || buf == NULL) {
1115 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1116 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001118 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001120 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121 end = p + len;
1122
1123 /*
1124 * Certificate ::= SEQUENCE {
1125 * tbsCertificate TBSCertificate,
1126 * signatureAlgorithm AlgorithmIdentifier,
1127 * signatureValue BIT STRING }
1128 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1131 mbedtls_x509_crt_free(crt);
1132 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001133 }
1134
Janos Follathcc0e49d2016-02-17 14:34:12 +00001135 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001136 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001138 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1140 if (crt->raw.p == NULL) {
1141 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1142 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001145 crt->own_buffer = 1;
1146
1147 p += crt->raw.len - len;
1148 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 } else {
1150 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001151 crt->own_buffer = 0;
1152 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001153
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154 /*
1155 * TBSCertificate ::= SEQUENCE {
1156 */
1157 crt->tbs.p = p;
1158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1160 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1161 mbedtls_x509_crt_free(crt);
1162 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001163 }
1164
1165 end = p + len;
1166 crt->tbs.len = end - crt->tbs.p;
1167
1168 /*
1169 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1170 *
1171 * CertificateSerialNumber ::= INTEGER
1172 *
1173 * signature AlgorithmIdentifier
1174 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1176 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1177 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1178 &sig_params1)) != 0) {
1179 mbedtls_x509_crt_free(crt);
1180 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181 }
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 if (crt->version < 0 || crt->version > 2) {
1184 mbedtls_x509_crt_free(crt);
1185 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186 }
1187
Andres AG7ca4a032017-03-09 16:16:11 +00001188 crt->version++;
1189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1191 &crt->sig_md, &crt->sig_pk,
1192 &crt->sig_opts)) != 0) {
1193 mbedtls_x509_crt_free(crt);
1194 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001195 }
1196
1197 /*
1198 * issuer Name
1199 */
1200 crt->issuer_raw.p = p;
1201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1203 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1204 mbedtls_x509_crt_free(crt);
1205 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206 }
1207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1209 mbedtls_x509_crt_free(crt);
1210 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001211 }
1212
1213 crt->issuer_raw.len = p - crt->issuer_raw.p;
1214
1215 /*
1216 * Validity ::= SEQUENCE {
1217 * notBefore Time,
1218 * notAfter Time }
1219 *
1220 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1222 &crt->valid_to)) != 0) {
1223 mbedtls_x509_crt_free(crt);
1224 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001225 }
1226
1227 /*
1228 * subject Name
1229 */
1230 crt->subject_raw.p = p;
1231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1233 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1234 mbedtls_x509_crt_free(crt);
1235 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001236 }
1237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1239 mbedtls_x509_crt_free(crt);
1240 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001241 }
1242
1243 crt->subject_raw.len = p - crt->subject_raw.p;
1244
1245 /*
1246 * SubjectPublicKeyInfo
1247 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001248 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1250 mbedtls_x509_crt_free(crt);
1251 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001252 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001253 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001254
1255 /*
1256 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1257 * -- If present, version shall be v2 or v3
1258 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1259 * -- If present, version shall be v2 or v3
1260 * extensions [3] EXPLICIT Extensions OPTIONAL
1261 * -- If present, version shall be v3
1262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 if (crt->version == 2 || crt->version == 3) {
1264 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1265 if (ret != 0) {
1266 mbedtls_x509_crt_free(crt);
1267 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001268 }
1269 }
1270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 if (crt->version == 2 || crt->version == 3) {
1272 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1273 if (ret != 0) {
1274 mbedtls_x509_crt_free(crt);
1275 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 }
1277 }
1278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 if (crt->version == 3) {
1280 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1281 if (ret != 0) {
1282 mbedtls_x509_crt_free(crt);
1283 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001284 }
1285 }
1286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 if (p != end) {
1288 mbedtls_x509_crt_free(crt);
1289 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1290 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001291 }
1292
1293 end = crt_end;
1294
1295 /*
1296 * }
1297 * -- end of TBSCertificate
1298 *
1299 * signatureAlgorithm AlgorithmIdentifier,
1300 * signatureValue BIT STRING
1301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1303 mbedtls_x509_crt_free(crt);
1304 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001305 }
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (crt->sig_oid.len != sig_oid2.len ||
1308 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001309 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001310 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 (sig_params1.len != 0 &&
1312 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1313 mbedtls_x509_crt_free(crt);
1314 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001315 }
1316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1318 mbedtls_x509_crt_free(crt);
1319 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001320 }
1321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 if (p != end) {
1323 mbedtls_x509_crt_free(crt);
1324 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1325 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001326 }
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001329}
1330
1331/*
1332 * Parse one X.509 certificate in DER format from a buffer and add them to a
1333 * chained list
1334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1336 const unsigned char *buf,
1337 size_t buflen,
1338 int make_copy,
1339 mbedtls_x509_crt_ext_cb_t cb,
1340 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001341{
Janos Follath865b3eb2019-12-16 11:46:15 +00001342 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344
1345 /*
1346 * Check for valid input
1347 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 if (crt == NULL || buf == NULL) {
1349 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1350 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353 prev = crt;
1354 crt = crt->next;
1355 }
1356
1357 /*
1358 * Add new certificate on the end of the chain if needed.
1359 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 if (crt->version != 0 && crt->next == NULL) {
1361 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (crt->next == NULL) {
1364 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1365 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366
1367 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370 }
1371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1373 if (ret != 0) {
1374 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001375 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if (crt != chain) {
1379 mbedtls_free(crt);
1380 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383 }
1384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001386}
1387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1389 const unsigned char *buf,
1390 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001391{
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001393}
1394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1396 const unsigned char *buf,
1397 size_t buflen,
1398 int make_copy,
1399 mbedtls_x509_crt_ext_cb_t cb,
1400 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001401{
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001403}
1404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1406 const unsigned char *buf,
1407 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001408{
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001410}
1411
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001412/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001413 * Parse one or more PEM certificates from a buffer and add them to the chained
1414 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001416int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1417 const unsigned char *buf,
1418 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001419{
Janos Follath98e28a72016-05-31 14:03:54 +01001420#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001421 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001423#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001424
1425 /*
1426 * Check for valid input
1427 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 if (chain == NULL || buf == NULL) {
1429 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1430 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431
1432 /*
1433 * Determine buffer content. Buffer contains either one DER certificate or
1434 * one or more PEM certificates.
1435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1438 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001440 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1443 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1444 }
Janos Follath98e28a72016-05-31 14:03:54 +01001445#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001447#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001454 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001456 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001458
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001459 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 ret = mbedtls_pem_read_buffer(&pem,
1461 "-----BEGIN CERTIFICATE-----",
1462 "-----END CERTIFICATE-----",
1463 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466 /*
1467 * Was PEM encoded
1468 */
1469 buflen -= use_len;
1470 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1472 return ret;
1473 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1474 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475
1476 /*
1477 * PEM header and footer were found
1478 */
1479 buflen -= use_len;
1480 buf += use_len;
1481
Gilles 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
Paul Bakker5a5fa922014-09-26 14:53:04 +02001486 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001487 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001489 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001497 /*
1498 * Quit parsing on a memory error
1499 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1501 return ret;
1502 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001505 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507
1508 total_failed++;
1509 continue;
1510 }
1511
1512 success = 1;
1513 }
1514 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 if (success) {
1517 return total_failed;
1518 } else if (first_error) {
1519 return first_error;
1520 } else {
1521 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1522 }
Janos Follath98e28a72016-05-31 14:03:54 +01001523#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524}
1525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001527/*
1528 * Load one or more certificates and add them to the chained list
1529 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531{
Janos Follath865b3eb2019-12-16 11:46:15 +00001532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533 size_t n;
1534 unsigned char *buf;
1535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1537 return ret;
1538 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001542 mbedtls_zeroize_and_free(buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545}
1546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548{
1549 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001550#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Steve Lhomme369d7c72023-06-16 14:16:03 +02001551#if _WIN32_WINNT >= 0x0501 /* _WIN32_WINNT_XP */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552 int w_ret;
1553 WCHAR szDir[MAX_PATH];
1554 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001555 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 size_t len = strlen(path);
Simon Butcherde573f52018-07-05 09:11:30 +01001557 int length_as_int = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558
Paul Bakker9af723c2014-05-01 13:03:14 +02001559 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560 HANDLE hFind;
1561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if (len > MAX_PATH - 3) {
1563 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1564 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 memset(szDir, 0, sizeof(szDir));
1567 memset(filename, 0, MAX_PATH);
1568 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001569 filename[len++] = '\\';
1570 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571 filename[len++] = '*';
1572
Minos Galanakisa277b212023-08-09 16:32:22 +01001573 if (FAILED(SizeTToInt(len, &length_as_int))) {
1574 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1575 }
Kevin Kane0ec1e682016-12-15 09:27:16 -08001576
Simon Butcher35e5dad2018-03-15 15:00:03 +00001577 /*
1578 * Note this function uses the code page CP_ACP, and assumes the incoming
1579 * string is encoded in ANSI, before translating it into Unicode. If the
1580 * incoming string were changed to be UTF-8, then the length check needs to
1581 * change to check the number of characters, not the number of bytes, in the
1582 * incoming string are less than MAX_PATH to avoid a buffer overrun with
1583 * MultiByteToWideChar().
1584 */
Simon Butcherde573f52018-07-05 09:11:30 +01001585 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, length_as_int, szDir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 MAX_PATH - 3);
1587 if (w_ret == 0) {
1588 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1589 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 hFind = FindFirstFileW(szDir, &file_data);
1592 if (hFind == INVALID_HANDLE_VALUE) {
1593 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1594 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001595
1596 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 do {
1598 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001601 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001603
Minos Galanakisa277b212023-08-09 16:32:22 +01001604 if (FAILED(SizeTToInt(wcslen(file_data.cFileName), &length_as_int))) {
1605 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1606 }
Kevin Kane0ec1e682016-12-15 09:27:16 -08001607
Simon Butcherde573f52018-07-05 09:11:30 +01001608 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
1609 length_as_int,
1610 p, (int) len - 1,
1611 NULL, NULL);
Minos Galanakisa277b212023-08-09 16:32:22 +01001612 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001613 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1614 goto cleanup;
1615 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1618 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 }
1623 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628
Ron Eldor36d90422017-01-09 15:09:16 +02001629cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 FindClose(hFind);
Steve Lhomme369d7c72023-06-16 14:16:03 +02001631#else /* !_WIN32_WINNT_XP */
Antonio de Angelis1ee4d122023-08-16 12:26:37 +01001632#error "mbedtls_x509_crt_parse_path not available before Windows XP"
Steve Lhomme369d7c72023-06-16 14:16:03 +02001633#endif /* !_WIN32_WINNT_XP */
Paul Bakkerbe089b02013-10-14 15:51:50 +02001634#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001635 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001636 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001638 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001639 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 if (dir == NULL) {
1643 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1644 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001645
Ron Eldor63140682017-01-09 19:27:59 +02001646#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1648 closedir(dir);
1649 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001650 }
Ron Eldor63140682017-01-09 19:27:59 +02001651#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001656 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001658
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001659 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001660 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1661 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 } else if (stat(entry_name, &sb) == -1) {
1663 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001664 /* Broken symbolic link - ignore this entry.
1665 stat(2) will return this error for either (a) a dangling
1666 symlink or (b) a missing file.
1667 Given that we have just obtained the filename from readdir,
1668 assume that it does exist and therefore treat this as a
1669 dangling symlink. */
1670 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001672 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001673 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1674 goto cleanup;
1675 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001676 }
1677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001681
1682 // Ignore parse errors
1683 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1685 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001686 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001688 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001690 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001691
1692cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001694
Ron Eldor63140682017-01-09 19:27:59 +02001695#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 }
Ron Eldor63140682017-01-09 19:27:59 +02001699#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001700
Paul Bakkerbe089b02013-10-14 15:51:50 +02001701#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001704}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001706
Peter Kolbus9a969b62018-12-11 13:55:56 -06001707#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001708#define PRINT_ITEM(i) \
1709 do { \
1710 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1711 MBEDTLS_X509_SAFE_SNPRINTF; \
1712 sep = ", "; \
1713 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001714
Przemek Stekielf4194942023-04-24 09:52:17 +02001715#define CERT_TYPE(type, name) \
1716 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001717 if (ns_cert_type & (type)) { \
1718 PRINT_ITEM(name); \
1719 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001720 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001721
Przemek Stekielf4194942023-04-24 09:52:17 +02001722#define KEY_USAGE(code, name) \
1723 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001724 if (key_usage & (code)) { \
1725 PRINT_ITEM(name); \
1726 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001727 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729static int x509_info_ext_key_usage(char **buf, size_t *size,
1730 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001731{
Janos Follath865b3eb2019-12-16 11:46:15 +00001732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001733 const char *desc;
1734 size_t n = *size;
1735 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001737 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 while (cur != NULL) {
1740 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001741 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001745 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001746
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001747 sep = ", ";
1748
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001749 cur = cur->next;
1750 }
1751
1752 *size = n;
1753 *buf = p;
1754
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001756}
1757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758static int x509_info_cert_policies(char **buf, size_t *size,
1759 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001760{
Janos Follath865b3eb2019-12-16 11:46:15 +00001761 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001762 const char *desc;
1763 size_t n = *size;
1764 char *p = *buf;
1765 const mbedtls_x509_sequence *cur = certificate_policies;
1766 const char *sep = "";
1767
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 while (cur != NULL) {
1769 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001770 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001774 MBEDTLS_X509_SAFE_SNPRINTF;
1775
1776 sep = ", ";
1777
1778 cur = cur->next;
1779 }
1780
1781 *size = n;
1782 *buf = p;
1783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001785}
1786
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001787/*
1788 * Return an informational string about the certificate.
1789 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001790#define BEFORE_COLON 18
1791#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001792int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1793 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001794{
Janos Follath865b3eb2019-12-16 11:46:15 +00001795 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001796 size_t n;
1797 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001798 char key_size_str[BEFORE_COLON];
1799
1800 p = buf;
1801 n = size;
1802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if (NULL == crt) {
1804 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001805 MBEDTLS_X509_SAFE_SNPRINTF;
1806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001808 }
1809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1811 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001812 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1814 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001815 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001818 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001821 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001823 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001826 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001828 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1831 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1832 crt->valid_from.year, crt->valid_from.mon,
1833 crt->valid_from.day, crt->valid_from.hour,
1834 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001835 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001836
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1838 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1839 crt->valid_to.year, crt->valid_to.mon,
1840 crt->valid_to.day, crt->valid_to.hour,
1841 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001842 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001845 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1848 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001849 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001850
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001851 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1853 mbedtls_pk_get_name(&crt->pk))) != 0) {
1854 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001855 }
1856
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1858 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001859 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001860
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001861 /*
1862 * Optional extensions
1863 */
1864
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1866 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1867 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001868 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 if (crt->max_pathlen > 0) {
1871 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001872 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001873 }
1874 }
1875
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1877 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001878 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001879
Przemek Stekiel21c37282023-01-16 08:47:49 +01001880 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1881 &crt->subject_alt_names,
1882 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 return ret;
1884 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001885 }
1886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1888 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001889 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001890
Przemek Stekiel21c37282023-01-16 08:47:49 +01001891 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 return ret;
1893 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001894 }
1895
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1897 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001898 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001899
Przemek Stekiel21c37282023-01-16 08:47:49 +01001900 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 return ret;
1902 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001903 }
1904
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1906 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001907 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 if ((ret = x509_info_ext_key_usage(&p, &n,
1910 &crt->ext_key_usage)) != 0) {
1911 return ret;
1912 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001913 }
1914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1916 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001917 MBEDTLS_X509_SAFE_SNPRINTF;
1918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 if ((ret = x509_info_cert_policies(&p, &n,
1920 &crt->certificate_policies)) != 0) {
1921 return ret;
1922 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001923 }
1924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001926 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001929}
1930
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001931struct x509_crt_verify_string {
1932 int code;
1933 const char *string;
1934};
1935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001937static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001938 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001939 { 0, NULL }
1940};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001941#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1944 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001945{
Janos Follath865b3eb2019-12-16 11:46:15 +00001946 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001947 const struct x509_crt_verify_string *cur;
1948 char *p = buf;
1949 size_t n = size;
1950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1952 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001953 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001957 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001958 flags ^= cur->code;
1959 }
1960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 if (flags != 0) {
1962 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1963 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001964 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001965 }
1966
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001968}
Hanno Becker612a2f12020-10-09 09:19:39 +01001969#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001970
Gilles Peskine449bd832023-01-11 14:50:10 +01001971int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1972 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001973{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001974 unsigned int usage_must, usage_may;
1975 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1979 return 0;
1980 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001981
1982 usage_must = usage & ~may_mask;
1983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1985 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1986 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001987
1988 usage_may = usage & may_mask;
1989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1991 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1992 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001995}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1998 const char *usage_oid,
1999 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002000{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002002
2003 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
2005 return 0;
2006 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002007
2008 /*
2009 * Look for the requested usage (or wildcard ANY) in our list
2010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 if (cur_oid->len == usage_len &&
2015 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2016 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002017 }
2018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2020 return 0;
2021 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002022 }
2023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002025}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002028/*
2029 * Return 1 if the certificate is revoked, or 0 otherwise.
2030 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002031int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002032{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 while (cur != NULL && cur->serial.len != 0) {
2036 if (crt->serial.len == cur->serial.len &&
2037 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2038 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002039 }
2040
2041 cur = cur->next;
2042 }
2043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002045}
2046
2047/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002048 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002049 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002050 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002051static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2052 mbedtls_x509_crl *crl_list,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002053 const mbedtls_x509_crt_profile *profile,
2054 const mbedtls_x509_time *now)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002055{
2056 int flags = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002057 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002058#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002059 psa_algorithm_t psa_algorithm;
2060#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002062#endif /* MBEDTLS_USE_PSA_CRYPTO */
2063 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 if (ca == NULL) {
2066 return flags;
2067 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 while (crl_list != NULL) {
2070 if (crl_list->version == 0 ||
2071 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002072 crl_list = crl_list->next;
2073 continue;
2074 }
2075
2076 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002077 * Check if the CA is configured to sign CRLs
2078 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 if (mbedtls_x509_crt_check_key_usage(ca,
2080 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002082 break;
2083 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002084
2085 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002086 * Check if CRL is correctly signed by the trusted CA
2087 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002089 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002091
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002093 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002095
pespacek7599a772022-02-07 14:40:55 +01002096#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002097 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 if (psa_hash_compute(psa_algorithm,
2099 crl_list->tbs.p,
2100 crl_list->tbs.len,
2101 hash,
2102 sizeof(hash),
2103 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002104 /* Note: this can't happen except after an internal error */
2105 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2106 break;
2107 }
pespacek7599a772022-02-07 14:40:55 +01002108#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2110 hash_length = mbedtls_md_get_size(md_info);
2111 if (mbedtls_md(md_info,
2112 crl_list->tbs.p,
2113 crl_list->tbs.len,
2114 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002115 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002117 break;
2118 }
pespaceka7a64692022-02-14 15:18:43 +01002119#endif /* MBEDTLS_USE_PSA_CRYPTO */
2120
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002122 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2126 crl_list->sig_md, hash, hash_length,
2127 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002129 break;
2130 }
2131
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002132#if defined(MBEDTLS_HAVE_TIME_DATE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002133 /*
2134 * Check for validity of CRL (Do not drop out)
2135 */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002136 if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002139
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002140 if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002141 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002143#else
2144 ((void) now);
2145#endif
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002146
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002147 /*
2148 * Check if certificate is revoked
2149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002152 break;
2153 }
2154
2155 crl_list = crl_list->next;
2156 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002161
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002162/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002163 * Check the signature of a certificate by its parent
2164 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002165static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2166 mbedtls_x509_crt *parent,
2167 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002168{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002169 size_t hash_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002170 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002171#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2172 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 md_info = mbedtls_md_info_from_type(child->sig_md);
2174 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002175
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002176 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2178 return -1;
2179 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002180#else
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002181 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002182 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 status = psa_hash_compute(hash_alg,
2185 child->tbs.p,
2186 child->tbs.len,
2187 hash,
2188 sizeof(hash),
2189 &hash_len);
2190 if (status != PSA_SUCCESS) {
2191 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002192 }
2193
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002194#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002195 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2197 return -1;
2198 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002199
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002200#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2202 return mbedtls_pk_verify_restartable(&parent->pk,
2203 child->sig_md, hash, hash_len,
2204 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002205 }
2206#else
2207 (void) rs_ctx;
2208#endif
2209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2211 child->sig_md, hash, hash_len,
2212 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002213}
2214
2215/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002216 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2217 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002218 *
2219 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002220 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002221static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2222 const mbedtls_x509_crt *parent,
2223 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002224{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002225 int need_ca_bit;
2226
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002227 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2229 return -1;
2230 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002231
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002232 /* Parent must have the basicConstraints CA bit set as a general rule */
2233 need_ca_bit = 1;
2234
2235 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002237 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002238 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002239
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 if (need_ca_bit && !parent->ca_istrue) {
2241 return -1;
2242 }
2243
2244 if (need_ca_bit &&
2245 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2246 return -1;
2247 }
2248
2249 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002250}
2251
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002252/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002253 * Find a suitable parent for child in candidates, or return NULL.
2254 *
2255 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002256 * 1. subject name matches child's issuer
2257 * 2. if necessary, the CA bit is set and key usage allows signing certs
2258 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002259 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002260 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002261 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002262 * If there's a suitable candidate which is also time-valid, return the first
2263 * such. Otherwise, return the first suitable candidate (or NULL if there is
2264 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002265 *
2266 * The rationale for this rule is that someone could have a list of trusted
2267 * roots with two versions on the same root with different validity periods.
2268 * (At least one user reported having such a list and wanted it to just work.)
2269 * The reason we don't just require time-validity is that generally there is
2270 * only one version, and if it's expired we want the flags to state that
2271 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002272 *
2273 * The rationale for rule 3 (signature for trusted roots) is that users might
2274 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002275 * way we select the correct one is by checking the signature (as we don't
2276 * rely on key identifier extensions). (This is one way users might choose to
2277 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002278 *
2279 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002280 * - [in] child: certificate for which we're looking for a parent
2281 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002282 * - [out] r_parent: parent found (or NULL)
2283 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002284 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2285 * of the chain, 0 otherwise
2286 * - [in] path_cnt: number of intermediates seen so far
2287 * - [in] self_cnt: number of self-signed intermediates seen so far
2288 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002289 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002290 *
2291 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002292 * - 0 on success
2293 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002294 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002295static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 mbedtls_x509_crt *child,
2297 mbedtls_x509_crt *candidates,
2298 mbedtls_x509_crt **r_parent,
2299 int *r_signature_is_good,
2300 int top,
2301 unsigned path_cnt,
2302 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002303 mbedtls_x509_crt_restart_ctx *rs_ctx,
2304 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002305{
Janos Follath865b3eb2019-12-16 11:46:15 +00002306 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002307 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002308 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002309
2310#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002311 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002313 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002314 parent = rs_ctx->parent;
2315 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002316 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002317
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002318 /* clear saved state */
2319 rs_ctx->parent = NULL;
2320 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002321 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002322
2323 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002324 goto check_signature;
2325 }
2326#endif
2327
2328 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002329 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002332 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002334 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002336
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002337 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 if (parent->max_pathlen > 0 &&
2339 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002340 continue;
2341 }
2342
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002343 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002344#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2345check_signature:
2346#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002348
2349#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002351 /* save state */
2352 rs_ctx->parent = parent;
2353 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002354 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002357 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002358#else
2359 (void) ret;
2360#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002361
2362 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002364 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002366
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002367#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002368 /* optional time check */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002369 if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2370 mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002372 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002373 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002374 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002375
2376 continue;
2377 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002378#else
2379 ((void) now);
2380#endif
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002381
Andy Gross1f627142019-01-30 10:25:53 -06002382 *r_parent = parent;
2383 *r_signature_is_good = signature_is_good;
2384
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002385 break;
2386 }
2387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002389 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002390 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002391 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002394}
2395
2396/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002397 * Find a parent in trusted CAs or the provided chain, or return NULL.
2398 *
2399 * Searches in trusted CAs first, and return the first suitable parent found
2400 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002401 *
2402 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002403 * - [in] child: certificate for which we're looking for a parent, followed
2404 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002405 * - [in] trust_ca: list of locally trusted certificates
2406 * - [out] parent: parent found (or NULL)
2407 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2408 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2409 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2410 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002411 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002412 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002413 *
2414 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002415 * - 0 on success
2416 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002417 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002418static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 mbedtls_x509_crt *child,
2420 mbedtls_x509_crt *trust_ca,
2421 mbedtls_x509_crt **parent,
2422 int *parent_is_trusted,
2423 int *signature_is_good,
2424 unsigned path_cnt,
2425 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002426 mbedtls_x509_crt_restart_ctx *rs_ctx,
2427 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002428{
Janos Follath865b3eb2019-12-16 11:46:15 +00002429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002430 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002431
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002432 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002433
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002434#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002435 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002437 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002438 rs_ctx->parent_is_trusted = -1;
2439 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002440#endif
2441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002443 search_list = *parent_is_trusted ? trust_ca : child->next;
2444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 ret = x509_crt_find_parent_in(child, search_list,
2446 parent, signature_is_good,
2447 *parent_is_trusted,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002448 path_cnt, self_cnt, rs_ctx, now);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002449
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002450#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002452 /* save state */
2453 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002455 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002456#else
2457 (void) ret;
2458#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002459
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002460 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002462 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002464
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002465 /* prepare second iteration */
2466 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002467 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002468
2469 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002471 *parent_is_trusted = 0;
2472 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002473 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002476}
2477
2478/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002479 * Check if an end-entity certificate is locally trusted
2480 *
2481 * Currently we require such certificates to be self-signed (actually only
2482 * check for self-issued as self-signatures are not checked)
2483 */
2484static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 mbedtls_x509_crt *crt,
2486 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002487{
2488 mbedtls_x509_crt *cur;
2489
2490 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2492 return -1;
2493 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002494
2495 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2497 if (crt->raw.len == cur->raw.len &&
2498 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2499 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002500 }
2501 }
2502
2503 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002505}
2506
2507/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002508 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002509 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002510 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2511 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002512 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002513 * such that every cert in the chain is a child of the next one,
2514 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002515 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002516 * Verify that chain and return it with flags for all issues found.
2517 *
2518 * Special cases:
2519 * - EE == Rj -> return a one-element list containing it
2520 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2521 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002522 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002523 * Tests for (aspects of) this function should include at least:
2524 * - trusted EE
2525 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002526 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002527 * - if relevant: EE untrusted
2528 * - if relevant: EE -> intermediate, untrusted
2529 * with the aspect under test checked at each relevant level (EE, int, root).
2530 * For some aspects longer chains are required, but usually length 2 is
2531 * enough (but length 1 is not in general).
2532 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002533 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002534 * - [in] crt: the cert list EE, C1, ..., Cn
2535 * - [in] trust_ca: the trusted list R1, ..., Rp
2536 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002537 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002538 * Only valid when return value is 0, may contain garbage otherwise!
2539 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002540 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002541 *
2542 * Return value:
2543 * - non-zero if the chain could not be fully built and examined
2544 * - 0 is the chain was successfully built and examined,
2545 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002546 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002547static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 mbedtls_x509_crt *crt,
2549 mbedtls_x509_crt *trust_ca,
2550 mbedtls_x509_crl *ca_crl,
2551 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2552 void *p_ca_cb,
2553 const mbedtls_x509_crt_profile *profile,
2554 mbedtls_x509_crt_verify_chain *ver_chain,
2555 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002556{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002557 /* Don't initialize any of those variables here, so that the compiler can
2558 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002560 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002561 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002562 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002563 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002564 int parent_is_trusted;
2565 int child_is_trusted;
2566 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002567 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002568 mbedtls_x509_crt *cur_trust_ca = NULL;
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002569 mbedtls_x509_time now;
2570
2571#if defined(MBEDTLS_HAVE_TIME_DATE)
2572 if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2573 return MBEDTLS_ERR_X509_FATAL_ERROR;
2574 }
2575#endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002576
2577#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2578 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002580 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002581 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002582 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002583
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002584 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002585 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002586 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002587 flags = &cur->flags;
2588
2589 goto find_parent;
2590 }
2591#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002592
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002593 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002594 self_cnt = 0;
2595 parent_is_trusted = 0;
2596 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002599 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002600 cur = &ver_chain->items[ver_chain->len];
2601 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002602 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002603 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002604 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002605
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002606#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002607 /* Check time-validity (all certificates) */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002608 if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002609 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002611
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002612 if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002613 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002615#endif
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002616
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002617 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (child_is_trusted) {
2619 return 0;
2620 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002621
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002622 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002624 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002628 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002630
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002631 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if (ver_chain->len == 1 &&
2633 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2634 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002635 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002636
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002637#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2638find_parent:
2639#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002640
2641 /* Obtain list of potential trusted signers from CA callback,
2642 * or use statically provided list. */
2643#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 if (f_ca_cb != NULL) {
2645 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2646 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002647 ver_chain->trust_ca_cb_result = NULL;
2648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2650 if (ret != 0) {
2651 return MBEDTLS_ERR_X509_FATAL_ERROR;
2652 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002653
2654 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002656#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2657 {
2658 ((void) f_ca_cb);
2659 ((void) p_ca_cb);
2660 cur_trust_ca = trust_ca;
2661 }
2662
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002663 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2665 &parent_is_trusted, &signature_is_good,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002666 ver_chain->len - 1, self_cnt, rs_ctx,
2667 &now);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002668
2669#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002671 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002672 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002673 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002674 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002677 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002678#else
2679 (void) ret;
2680#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002681
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002682 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002684 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002686 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002687
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002688 /* Count intermediate self-issued (not necessarily self-signed) certs.
2689 * These can occur with some strategies for key rollover, see [SIRO],
2690 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 if (ver_chain->len != 1 &&
2692 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002693 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002694 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002695
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002696 /* path_cnt is 0 for the first intermediate CA,
2697 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (!parent_is_trusted &&
2699 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002700 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002702 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002703
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002704 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002706 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002708
2709 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002711 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002715 /* Check trusted CA's CRL for the given crt */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002716 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002717#else
2718 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002719#endif
2720
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002721 /* prepare for next iteration */
2722 child = parent;
2723 parent = NULL;
2724 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002725 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002726 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002727}
2728
Glenn Straussc26bd762022-10-23 19:48:18 -04002729#ifdef _WIN32
2730#ifdef _MSC_VER
2731#pragma comment(lib, "ws2_32.lib")
2732#include <winsock2.h>
2733#include <ws2tcpip.h>
2734#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2735#include <winsock2.h>
2736#include <ws2tcpip.h>
Steve Lhommeeb0f18a2023-06-16 14:12:19 +02002737#else
2738/* inet_pton() is not supported, fallback to software version */
2739#define MBEDTLS_TEST_SW_INET_PTON
Glenn Straussc26bd762022-10-23 19:48:18 -04002740#endif
2741#elif defined(__sun)
2742/* Solaris requires -lsocket -lnsl for inet_pton() */
2743#elif defined(__has_include)
2744#if __has_include(<sys/socket.h>)
2745#include <sys/socket.h>
2746#endif
2747#if __has_include(<arpa/inet.h>)
2748#include <arpa/inet.h>
2749#endif
2750#endif
2751
2752/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2753 * the platform inet_pton() or a local implementation (below). The local
2754 * implementation may be used even in cases where the platform provides
2755 * inet_pton(), e.g. when there are different includes required and/or the
2756 * platform implementation requires dependencies on additional libraries.
2757 * Specifically, Windows requires custom includes and additional link
2758 * dependencies, and Solaris requires additional link dependencies.
2759 * Also, as a coarse heuristic, use the local implementation if the compiler
2760 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002761 * provided by headers included (or not) via __has_include() above.
2762 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2763 * despite having a platform that has inet_pton. */
2764#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2765/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002766static int x509_inet_pton_ipv4(const char *src, void *dst);
2767
2768#define li_cton(c, n) \
2769 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2770
2771static int x509_inet_pton_ipv6(const char *src, void *dst)
2772{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002773 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002774 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002775 uint16_t addr[8];
2776 do {
2777 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002778 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002779 for (uint8_t digit; num_digits < 4; num_digits++) {
2780 if (li_cton(*p, digit) == 0) {
2781 break;
2782 }
2783 group = (group << 4) | digit;
2784 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002785 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002786 if (num_digits != 0) {
Dave Rodgmancfa72232023-09-05 16:20:33 +01002787 MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups);
2788 nonzero_groups++;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002789 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002790 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002791 } else if (*p == '.') {
2792 /* Don't accept IPv4 too early or late */
2793 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2794 nonzero_groups >= 7) {
2795 break;
2796 }
2797
2798 /* Walk back to prior ':', then parse as IPv4-mapped */
2799 int steps = 4;
2800 do {
2801 p--;
2802 steps--;
2803 } while (*p != ':' && steps > 0);
2804
2805 if (*p != ':') {
2806 break;
2807 }
2808 p++;
2809 nonzero_groups--;
2810 if (x509_inet_pton_ipv4((const char *) p,
2811 addr + nonzero_groups) != 0) {
2812 break;
2813 }
2814
Andrzej Kurek0d578962023-04-13 09:09:56 -04002815 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002816 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002817 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002818 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002819 return -1;
2820 }
2821 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002822 /* Don't accept a second zero group or an invalid delimiter */
2823 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002824 return -1;
2825 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002826 zero_group_start = nonzero_groups;
2827
2828 /* Accept a zero group at start, but it has to be a double colon */
2829 if (zero_group_start == 0 && *++p != ':') {
2830 return -1;
2831 }
2832
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002833 if (p[1] == '\0') {
2834 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002835 break;
2836 }
2837 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002838 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002839 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002840
2841 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002842 return -1;
2843 }
2844
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002845 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002846 if (nonzero_groups > 6) {
2847 return -1;
2848 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002849 int zero_groups = 8 - nonzero_groups;
2850 int groups_after_zero = nonzero_groups - zero_group_start;
2851
2852 /* Move the non-zero part to after the zeroes */
2853 if (groups_after_zero) {
2854 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002855 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002856 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002857 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002858 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002859 } else {
2860 if (nonzero_groups != 8) {
2861 return -1;
2862 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002863 }
2864 memcpy(dst, addr, sizeof(addr));
2865 return 0;
2866}
2867
2868static int x509_inet_pton_ipv4(const char *src, void *dst)
2869{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002870 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002871 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002872 uint8_t digit, num_digits = 0;
2873 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002874 uint16_t octet;
2875
Glenn Strauss416c2952022-10-24 23:00:02 -04002876 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002877 octet = num_digits = 0;
2878 do {
2879 digit = *p - '0';
2880 if (digit > 9) {
2881 break;
2882 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002883
2884 /* Don't allow leading zeroes. These might mean octal format,
2885 * which this implementation does not support. */
2886 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002887 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002888 }
2889
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002890 octet = octet * 10 + digit;
2891 num_digits++;
2892 p++;
2893 } while (num_digits < 3);
2894
2895 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002896 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002897 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002898 *res++ = (uint8_t) octet;
2899 num_octets++;
2900 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002901 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002902}
Glenn Straussc26bd762022-10-23 19:48:18 -04002903
2904#else
2905
2906static int x509_inet_pton_ipv6(const char *src, void *dst)
2907{
2908 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2909}
2910
2911static int x509_inet_pton_ipv4(const char *src, void *dst)
2912{
2913 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2914}
2915
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002916#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002917
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002918size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002919{
2920 return strchr(cn, ':') == NULL
2921 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2922 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2923}
2924
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002925/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002926 * Check for CN match
2927 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002928static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2929 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002930{
2931 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 if (name->len == cn_len &&
2933 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2934 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002935 }
2936
2937 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 if (x509_check_wildcard(cn, name) == 0) {
2939 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002940 }
2941
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002943}
2944
Glenn Straussc26bd762022-10-23 19:48:18 -04002945static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2946 const char *cn, size_t cn_len)
2947{
2948 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002949 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002950 if (cn_len == 0) {
2951 return -1;
2952 }
2953
2954 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2955 const unsigned char san_type = (unsigned char) cur->buf.tag &
2956 MBEDTLS_ASN1_TAG_VALUE_MASK;
2957 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2958 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2959 return 0;
2960 }
2961 }
2962
2963 return -1;
2964}
2965
Andrzej Kurek199eab92023-05-10 09:57:19 -04002966static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2967 const char *cn, size_t cn_len)
2968{
2969 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2970 const unsigned char san_type = (unsigned char) cur->buf.tag &
2971 MBEDTLS_ASN1_TAG_VALUE_MASK;
2972 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2973 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2974 return 0;
2975 }
2976 }
2977
2978 return -1;
2979}
2980
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002981/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002982 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2983 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002984static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002986{
Glenn Straussc26bd762022-10-23 19:48:18 -04002987 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002988 int san_uri = 0;
2989 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002990 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2991 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002992 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002993 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2994 return 0;
2995 }
2996 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002997 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04002998 san_ip = 1;
2999 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04003000 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
3001 san_uri = 1;
3002 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04003003 /* (We may handle other types here later.) */
3004 default: /* Unrecognized type */
3005 break;
3006 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04003008 if (san_ip) {
3009 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
3010 return 0;
3011 }
3012 }
3013 if (san_uri) {
3014 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
3015 return 0;
3016 }
3017 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003018
Andrzej Kurek199eab92023-05-10 09:57:19 -04003019 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003020}
3021
3022/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003023 * Verify the requested CN - only call this if cn is not NULL!
3024 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003025static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3026 const char *cn,
3027 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003028{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003029 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003031
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003033 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
3034 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 }
3036 } else {
3037 for (name = &crt->subject; name != NULL; name = name->next) {
3038 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3039 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003040 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 }
3042 }
3043
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003044 }
Glenn Straussc26bd762022-10-23 19:48:18 -04003045
3046 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003047}
3048
3049/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003050 * Merge the flags for all certs in the chain, after calling callback
3051 */
3052static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 uint32_t *flags,
3054 const mbedtls_x509_crt_verify_chain *ver_chain,
3055 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3056 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003057{
Janos Follath865b3eb2019-12-16 11:46:15 +00003058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003059 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003060 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003061 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003062
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003064 cur = &ver_chain->items[i-1];
3065 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003066
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 if (NULL != f_vrfy) {
3068 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3069 return ret;
3070 }
3071 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003072
3073 *flags |= cur_flags;
3074 }
3075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003077}
3078
3079/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003080 * Verify the certificate validity, with profile, restartable version
3081 *
3082 * This function:
3083 * - checks the requested CN (if any)
3084 * - checks the type and size of the EE cert's key,
3085 * as that isn't done as part of chain building/verification currently
3086 * - builds and verifies the chain
3087 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003088 *
3089 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3090 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3091 * verification routine to search for trusted signers, and CRLs will
3092 * be disabled. Otherwise, `trust_ca` will be used as the static list
3093 * of trusted signers, and `ca_crl` will be use as the static list
3094 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003095 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003096static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3097 mbedtls_x509_crt *trust_ca,
3098 mbedtls_x509_crl *ca_crl,
3099 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3100 void *p_ca_cb,
3101 const mbedtls_x509_crt_profile *profile,
3102 const char *cn, uint32_t *flags,
3103 int (*f_vrfy)(void *,
3104 mbedtls_x509_crt *,
3105 int,
3106 uint32_t *),
3107 void *p_vrfy,
3108 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003109{
Janos Follath865b3eb2019-12-16 11:46:15 +00003110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003111 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003112 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003113 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003114
3115 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003116 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003118
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003120 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3121 goto exit;
3122 }
3123
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003124 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 if (cn != NULL) {
3126 x509_crt_verify_name(crt, cn, &ee_flags);
3127 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003128
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003129 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003131
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003133 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003135
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003137 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003139
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003140 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3142 f_ca_cb, p_ca_cb, profile,
3143 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003144
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003148
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003149 /* Merge end-entity flags */
3150 ver_chain.items[0].flags |= ee_flags;
3151
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003152 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003154
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003155exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003156
3157#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3159 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003160 ver_chain.trust_ca_cb_result = NULL;
3161#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3162
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003163#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3165 mbedtls_x509_crt_restart_free(rs_ctx);
3166 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003167#endif
3168
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003169 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3170 * the SSL module for authmode optional, but non-zero return from the
3171 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003173 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003174 }
3175
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 if (ret != 0) {
3177 *flags = (uint32_t) -1;
3178 return ret;
3179 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003180
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 if (*flags != 0) {
3182 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3183 }
3184
3185 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003186}
3187
Hanno Becker3116fb32019-03-28 13:34:42 +00003188
3189/*
3190 * Verify the certificate validity (default profile, not restartable)
3191 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003192int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3193 mbedtls_x509_crt *trust_ca,
3194 mbedtls_x509_crl *ca_crl,
3195 const char *cn, uint32_t *flags,
3196 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3197 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003198{
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3200 NULL, NULL,
3201 &mbedtls_x509_crt_profile_default,
3202 cn, flags,
3203 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003204}
3205
3206/*
3207 * Verify the certificate validity (user-chosen profile, not restartable)
3208 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003209int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3210 mbedtls_x509_crt *trust_ca,
3211 mbedtls_x509_crl *ca_crl,
3212 const mbedtls_x509_crt_profile *profile,
3213 const char *cn, uint32_t *flags,
3214 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3215 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003216{
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3218 NULL, NULL,
3219 profile, cn, flags,
3220 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003221}
3222
3223#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3224/*
3225 * Verify the certificate validity (user-chosen profile, CA callback,
3226 * not restartable).
3227 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003228int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3229 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3230 void *p_ca_cb,
3231 const mbedtls_x509_crt_profile *profile,
3232 const char *cn, uint32_t *flags,
3233 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3234 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003235{
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3237 f_ca_cb, p_ca_cb,
3238 profile, cn, flags,
3239 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003240}
3241#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3242
Gilles Peskine449bd832023-01-11 14:50:10 +01003243int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3244 mbedtls_x509_crt *trust_ca,
3245 mbedtls_x509_crl *ca_crl,
3246 const mbedtls_x509_crt_profile *profile,
3247 const char *cn, uint32_t *flags,
3248 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3249 void *p_vrfy,
3250 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003251{
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3253 NULL, NULL,
3254 profile, cn, flags,
3255 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003256}
3257
3258
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003259/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003260 * Initialize a certificate chain
3261 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003262void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003263{
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003265}
3266
3267/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003268 * Unallocate all certificate data
3269 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003270void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003271{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 mbedtls_x509_crt *cert_cur = crt;
3273 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003274
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 while (cert_cur != NULL) {
3276 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003280#endif
3281
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3283 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3284 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3285 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3286 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Przemek Stekiel690ff692023-05-15 09:54:02 +02003287 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003288
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003290 mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003291 }
3292
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003293 cert_prv = cert_cur;
3294 cert_cur = cert_cur->next;
3295
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3297 if (cert_prv != crt) {
3298 mbedtls_free(cert_prv);
3299 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003300 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003301}
3302
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003303#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3304/*
3305 * Initialize a restart context
3306 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003307void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003308{
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003310
3311 ctx->parent = NULL;
3312 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003313 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003314
3315 ctx->parent_is_trusted = -1;
3316
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003317 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003318 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003320}
3321
3322/*
3323 * Free the components of a restart context
3324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003325void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003326{
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003328 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003330
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 mbedtls_pk_restart_free(&ctx->pk);
3332 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003333}
3334#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336#endif /* MBEDTLS_X509_CRT_PARSE_C */