blob: d9df4b220cb75879c2400e0e5ce1e957df677698 [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 */
70#pragma warning( push )
71#pragma warning( disable : 4005 )
72#endif
73#include <intsafe.h>
Simon Butchere068aa72018-03-14 15:10:31 +000074#if defined(_MSC_VER) && _MSC_VER <= 1600
Kevin Kane0ec1e682016-12-15 09:27:16 -080075#pragma warning( pop )
76#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
Simon Butcherde573f52018-07-05 09:11:30 +01001573 if (FAILED (SizeTToInt(len, &length_as_int)))
Kevin Kane0ec1e682016-12-15 09:27:16 -08001574 return(MBEDTLS_ERR_X509_FILE_IO_ERROR);
1575
Simon Butcher35e5dad2018-03-15 15:00:03 +00001576 /*
1577 * Note this function uses the code page CP_ACP, and assumes the incoming
1578 * string is encoded in ANSI, before translating it into Unicode. If the
1579 * incoming string were changed to be UTF-8, then the length check needs to
1580 * change to check the number of characters, not the number of bytes, in the
1581 * incoming string are less than MAX_PATH to avoid a buffer overrun with
1582 * MultiByteToWideChar().
1583 */
Simon Butcherde573f52018-07-05 09:11:30 +01001584 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, length_as_int, szDir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 MAX_PATH - 3);
1586 if (w_ret == 0) {
1587 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1588 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 hFind = FindFirstFileW(szDir, &file_data);
1591 if (hFind == INVALID_HANDLE_VALUE) {
1592 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1593 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594
1595 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 do {
1597 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602
Simon Butcherde573f52018-07-05 09:11:30 +01001603 if (FAILED(SizeTToInt(wcslen(file_data.cFileName), &length_as_int)))
Kevin Kane0ec1e682016-12-15 09:27:16 -08001604 return(MBEDTLS_ERR_X509_FILE_IO_ERROR);
1605
Simon Butcherde573f52018-07-05 09:11:30 +01001606 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
1607 length_as_int,
1608 p, (int) len - 1,
1609 NULL, NULL);
1610 if(w_ret == 0)
1611 {
Ron Eldor36d90422017-01-09 15:09:16 +02001612 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1613 goto cleanup;
1614 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1617 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001618 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 }
1622 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001627
Ron Eldor36d90422017-01-09 15:09:16 +02001628cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 FindClose(hFind);
Steve Lhomme369d7c72023-06-16 14:16:03 +02001630#else /* !_WIN32_WINNT_XP */
Antonio de Angelis1ee4d122023-08-16 12:26:37 +01001631#error "mbedtls_x509_crt_parse_path not available before Windows XP"
Steve Lhomme369d7c72023-06-16 14:16:03 +02001632#endif /* !_WIN32_WINNT_XP */
Paul Bakkerbe089b02013-10-14 15:51:50 +02001633#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001634 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001635 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001637 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001638 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 if (dir == NULL) {
1642 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1643 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644
Ron Eldor63140682017-01-09 19:27:59 +02001645#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1647 closedir(dir);
1648 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001649 }
Ron Eldor63140682017-01-09 19:27:59 +02001650#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001655 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001658 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001659 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1660 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 } else if (stat(entry_name, &sb) == -1) {
1662 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001663 /* Broken symbolic link - ignore this entry.
1664 stat(2) will return this error for either (a) a dangling
1665 symlink or (b) a missing file.
1666 Given that we have just obtained the filename from readdir,
1667 assume that it does exist and therefore treat this as a
1668 dangling symlink. */
1669 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001671 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001672 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1673 goto cleanup;
1674 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675 }
1676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680
1681 // Ignore parse errors
1682 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1684 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001685 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001687 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001689 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001690
1691cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001693
Ron Eldor63140682017-01-09 19:27:59 +02001694#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 }
Ron Eldor63140682017-01-09 19:27:59 +02001698#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001699
Paul Bakkerbe089b02013-10-14 15:51:50 +02001700#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705
Peter Kolbus9a969b62018-12-11 13:55:56 -06001706#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001707#define PRINT_ITEM(i) \
1708 do { \
1709 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1710 MBEDTLS_X509_SAFE_SNPRINTF; \
1711 sep = ", "; \
1712 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001713
Przemek Stekielf4194942023-04-24 09:52:17 +02001714#define CERT_TYPE(type, name) \
1715 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001716 if (ns_cert_type & (type)) { \
1717 PRINT_ITEM(name); \
1718 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001719 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001720
Przemek Stekielf4194942023-04-24 09:52:17 +02001721#define KEY_USAGE(code, name) \
1722 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001723 if (key_usage & (code)) { \
1724 PRINT_ITEM(name); \
1725 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001726 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728static int x509_info_ext_key_usage(char **buf, size_t *size,
1729 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001730{
Janos Follath865b3eb2019-12-16 11:46:15 +00001731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001732 const char *desc;
1733 size_t n = *size;
1734 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001736 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 while (cur != NULL) {
1739 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001740 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001744 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001745
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001746 sep = ", ";
1747
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001748 cur = cur->next;
1749 }
1750
1751 *size = n;
1752 *buf = p;
1753
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001755}
1756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757static int x509_info_cert_policies(char **buf, size_t *size,
1758 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001759{
Janos Follath865b3eb2019-12-16 11:46:15 +00001760 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001761 const char *desc;
1762 size_t n = *size;
1763 char *p = *buf;
1764 const mbedtls_x509_sequence *cur = certificate_policies;
1765 const char *sep = "";
1766
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 while (cur != NULL) {
1768 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001769 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001773 MBEDTLS_X509_SAFE_SNPRINTF;
1774
1775 sep = ", ";
1776
1777 cur = cur->next;
1778 }
1779
1780 *size = n;
1781 *buf = p;
1782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001784}
1785
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001786/*
1787 * Return an informational string about the certificate.
1788 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001789#define BEFORE_COLON 18
1790#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001791int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1792 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001793{
Janos Follath865b3eb2019-12-16 11:46:15 +00001794 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001795 size_t n;
1796 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001797 char key_size_str[BEFORE_COLON];
1798
1799 p = buf;
1800 n = size;
1801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if (NULL == crt) {
1803 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001804 MBEDTLS_X509_SAFE_SNPRINTF;
1805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001807 }
1808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1810 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001811 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1813 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001814 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001815
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001817 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001820 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001822 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001825 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001827 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1830 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1831 crt->valid_from.year, crt->valid_from.mon,
1832 crt->valid_from.day, crt->valid_from.hour,
1833 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001834 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1837 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1838 crt->valid_to.year, crt->valid_to.mon,
1839 crt->valid_to.day, crt->valid_to.hour,
1840 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001841 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001844 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1847 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001848 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001849
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001850 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1852 mbedtls_pk_get_name(&crt->pk))) != 0) {
1853 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001854 }
1855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1857 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001858 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001859
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001860 /*
1861 * Optional extensions
1862 */
1863
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1865 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1866 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001867 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 if (crt->max_pathlen > 0) {
1870 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001871 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001872 }
1873 }
1874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1876 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001877 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001878
Przemek Stekiel21c37282023-01-16 08:47:49 +01001879 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1880 &crt->subject_alt_names,
1881 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 return ret;
1883 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001884 }
1885
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1887 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001888 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001889
Przemek Stekiel21c37282023-01-16 08:47:49 +01001890 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 return ret;
1892 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001893 }
1894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1896 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001897 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001898
Przemek Stekiel21c37282023-01-16 08:47:49 +01001899 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 return ret;
1901 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001902 }
1903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1905 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001906 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 if ((ret = x509_info_ext_key_usage(&p, &n,
1909 &crt->ext_key_usage)) != 0) {
1910 return ret;
1911 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001912 }
1913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1915 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001916 MBEDTLS_X509_SAFE_SNPRINTF;
1917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 if ((ret = x509_info_cert_policies(&p, &n,
1919 &crt->certificate_policies)) != 0) {
1920 return ret;
1921 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001922 }
1923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001925 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001928}
1929
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001930struct x509_crt_verify_string {
1931 int code;
1932 const char *string;
1933};
1934
Gilles Peskine449bd832023-01-11 14:50:10 +01001935#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001936static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001937 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001938 { 0, NULL }
1939};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001940#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1943 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001944{
Janos Follath865b3eb2019-12-16 11:46:15 +00001945 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001946 const struct x509_crt_verify_string *cur;
1947 char *p = buf;
1948 size_t n = size;
1949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1951 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001952 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001956 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001957 flags ^= cur->code;
1958 }
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if (flags != 0) {
1961 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1962 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001963 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001964 }
1965
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001967}
Hanno Becker612a2f12020-10-09 09:19:39 +01001968#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001969
Gilles Peskine449bd832023-01-11 14:50:10 +01001970int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1971 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001972{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001973 unsigned int usage_must, usage_may;
1974 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001976
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1978 return 0;
1979 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001980
1981 usage_must = usage & ~may_mask;
1982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1984 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1985 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001986
1987 usage_may = usage & may_mask;
1988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1990 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1991 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001994}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1997 const char *usage_oid,
1998 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001999{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002001
2002 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
2004 return 0;
2005 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002006
2007 /*
2008 * Look for the requested usage (or wildcard ANY) in our list
2009 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 if (cur_oid->len == usage_len &&
2014 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2015 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002016 }
2017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2019 return 0;
2020 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002021 }
2022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002024}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002027/*
2028 * Return 1 if the certificate is revoked, or 0 otherwise.
2029 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002030int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002031{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 while (cur != NULL && cur->serial.len != 0) {
2035 if (crt->serial.len == cur->serial.len &&
2036 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2037 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002038 }
2039
2040 cur = cur->next;
2041 }
2042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002044}
2045
2046/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002047 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002048 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002049 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002050static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2051 mbedtls_x509_crl *crl_list,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002052 const mbedtls_x509_crt_profile *profile,
2053 const mbedtls_x509_time *now)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002054{
2055 int flags = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002056 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002057#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002058 psa_algorithm_t psa_algorithm;
2059#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002061#endif /* MBEDTLS_USE_PSA_CRYPTO */
2062 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (ca == NULL) {
2065 return flags;
2066 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 while (crl_list != NULL) {
2069 if (crl_list->version == 0 ||
2070 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002071 crl_list = crl_list->next;
2072 continue;
2073 }
2074
2075 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002076 * Check if the CA is configured to sign CRLs
2077 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (mbedtls_x509_crt_check_key_usage(ca,
2079 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002081 break;
2082 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002083
2084 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002085 * Check if CRL is correctly signed by the trusted CA
2086 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002088 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002090
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002092 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002094
pespacek7599a772022-02-07 14:40:55 +01002095#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002096 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 if (psa_hash_compute(psa_algorithm,
2098 crl_list->tbs.p,
2099 crl_list->tbs.len,
2100 hash,
2101 sizeof(hash),
2102 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002103 /* Note: this can't happen except after an internal error */
2104 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2105 break;
2106 }
pespacek7599a772022-02-07 14:40:55 +01002107#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2109 hash_length = mbedtls_md_get_size(md_info);
2110 if (mbedtls_md(md_info,
2111 crl_list->tbs.p,
2112 crl_list->tbs.len,
2113 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002114 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002116 break;
2117 }
pespaceka7a64692022-02-14 15:18:43 +01002118#endif /* MBEDTLS_USE_PSA_CRYPTO */
2119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002121 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2125 crl_list->sig_md, hash, hash_length,
2126 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002128 break;
2129 }
2130
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002131#if defined(MBEDTLS_HAVE_TIME_DATE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002132 /*
2133 * Check for validity of CRL (Do not drop out)
2134 */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002135 if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002138
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002139 if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002140 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002142#else
2143 ((void) now);
2144#endif
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002145
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002146 /*
2147 * Check if certificate is revoked
2148 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002151 break;
2152 }
2153
2154 crl_list = crl_list->next;
2155 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002160
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002161/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002162 * Check the signature of a certificate by its parent
2163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002164static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2165 mbedtls_x509_crt *parent,
2166 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002167{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002168 size_t hash_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002169 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002170#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2171 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 md_info = mbedtls_md_info_from_type(child->sig_md);
2173 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002174
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002175 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2177 return -1;
2178 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002179#else
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002180 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002181 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 status = psa_hash_compute(hash_alg,
2184 child->tbs.p,
2185 child->tbs.len,
2186 hash,
2187 sizeof(hash),
2188 &hash_len);
2189 if (status != PSA_SUCCESS) {
2190 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002191 }
2192
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002193#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002194 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2196 return -1;
2197 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002198
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002199#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2201 return mbedtls_pk_verify_restartable(&parent->pk,
2202 child->sig_md, hash, hash_len,
2203 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002204 }
2205#else
2206 (void) rs_ctx;
2207#endif
2208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2210 child->sig_md, hash, hash_len,
2211 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002212}
2213
2214/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002215 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2216 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002217 *
2218 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002220static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2221 const mbedtls_x509_crt *parent,
2222 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002223{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002224 int need_ca_bit;
2225
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002226 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2228 return -1;
2229 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002230
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002231 /* Parent must have the basicConstraints CA bit set as a general rule */
2232 need_ca_bit = 1;
2233
2234 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002236 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002237 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 if (need_ca_bit && !parent->ca_istrue) {
2240 return -1;
2241 }
2242
2243 if (need_ca_bit &&
2244 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2245 return -1;
2246 }
2247
2248 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002249}
2250
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002251/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002252 * Find a suitable parent for child in candidates, or return NULL.
2253 *
2254 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002255 * 1. subject name matches child's issuer
2256 * 2. if necessary, the CA bit is set and key usage allows signing certs
2257 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002258 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002259 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002260 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002261 * If there's a suitable candidate which is also time-valid, return the first
2262 * such. Otherwise, return the first suitable candidate (or NULL if there is
2263 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002264 *
2265 * The rationale for this rule is that someone could have a list of trusted
2266 * roots with two versions on the same root with different validity periods.
2267 * (At least one user reported having such a list and wanted it to just work.)
2268 * The reason we don't just require time-validity is that generally there is
2269 * only one version, and if it's expired we want the flags to state that
2270 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002271 *
2272 * The rationale for rule 3 (signature for trusted roots) is that users might
2273 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002274 * way we select the correct one is by checking the signature (as we don't
2275 * rely on key identifier extensions). (This is one way users might choose to
2276 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002277 *
2278 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002279 * - [in] child: certificate for which we're looking for a parent
2280 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002281 * - [out] r_parent: parent found (or NULL)
2282 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002283 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2284 * of the chain, 0 otherwise
2285 * - [in] path_cnt: number of intermediates seen so far
2286 * - [in] self_cnt: number of self-signed intermediates seen so far
2287 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002288 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002289 *
2290 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002291 * - 0 on success
2292 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002293 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002294static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 mbedtls_x509_crt *child,
2296 mbedtls_x509_crt *candidates,
2297 mbedtls_x509_crt **r_parent,
2298 int *r_signature_is_good,
2299 int top,
2300 unsigned path_cnt,
2301 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002302 mbedtls_x509_crt_restart_ctx *rs_ctx,
2303 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002304{
Janos Follath865b3eb2019-12-16 11:46:15 +00002305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002306 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002307 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002308
2309#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002310 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002312 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002313 parent = rs_ctx->parent;
2314 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002315 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002316
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002317 /* clear saved state */
2318 rs_ctx->parent = NULL;
2319 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002320 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002321
2322 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002323 goto check_signature;
2324 }
2325#endif
2326
2327 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002328 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002331 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002333 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002335
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002336 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (parent->max_pathlen > 0 &&
2338 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002339 continue;
2340 }
2341
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002342 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002343#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2344check_signature:
2345#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002347
2348#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002350 /* save state */
2351 rs_ctx->parent = parent;
2352 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002353 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002356 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002357#else
2358 (void) ret;
2359#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002360
2361 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002363 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002365
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002366#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002367 /* optional time check */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002368 if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2369 mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002371 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002372 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002373 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002374
2375 continue;
2376 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002377#else
2378 ((void) now);
2379#endif
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002380
Andy Gross1f627142019-01-30 10:25:53 -06002381 *r_parent = parent;
2382 *r_signature_is_good = signature_is_good;
2383
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002384 break;
2385 }
2386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002388 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002389 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002390 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002393}
2394
2395/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002396 * Find a parent in trusted CAs or the provided chain, or return NULL.
2397 *
2398 * Searches in trusted CAs first, and return the first suitable parent found
2399 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002400 *
2401 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002402 * - [in] child: certificate for which we're looking for a parent, followed
2403 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002404 * - [in] trust_ca: list of locally trusted certificates
2405 * - [out] parent: parent found (or NULL)
2406 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2407 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2408 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2409 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002410 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002411 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002412 *
2413 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002414 * - 0 on success
2415 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002416 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002417static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 mbedtls_x509_crt *child,
2419 mbedtls_x509_crt *trust_ca,
2420 mbedtls_x509_crt **parent,
2421 int *parent_is_trusted,
2422 int *signature_is_good,
2423 unsigned path_cnt,
2424 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002425 mbedtls_x509_crt_restart_ctx *rs_ctx,
2426 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002427{
Janos Follath865b3eb2019-12-16 11:46:15 +00002428 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002429 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002430
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002431 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002432
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002433#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002434 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002436 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002437 rs_ctx->parent_is_trusted = -1;
2438 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002439#endif
2440
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002442 search_list = *parent_is_trusted ? trust_ca : child->next;
2443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 ret = x509_crt_find_parent_in(child, search_list,
2445 parent, signature_is_good,
2446 *parent_is_trusted,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002447 path_cnt, self_cnt, rs_ctx, now);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002448
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002449#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002451 /* save state */
2452 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002454 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002455#else
2456 (void) ret;
2457#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002458
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002459 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002461 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002463
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002464 /* prepare second iteration */
2465 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002466 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002467
2468 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002470 *parent_is_trusted = 0;
2471 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002472 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002475}
2476
2477/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002478 * Check if an end-entity certificate is locally trusted
2479 *
2480 * Currently we require such certificates to be self-signed (actually only
2481 * check for self-issued as self-signatures are not checked)
2482 */
2483static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 mbedtls_x509_crt *crt,
2485 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002486{
2487 mbedtls_x509_crt *cur;
2488
2489 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2491 return -1;
2492 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002493
2494 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2496 if (crt->raw.len == cur->raw.len &&
2497 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2498 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002499 }
2500 }
2501
2502 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002504}
2505
2506/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002507 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002508 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002509 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2510 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002511 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002512 * such that every cert in the chain is a child of the next one,
2513 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002514 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002515 * Verify that chain and return it with flags for all issues found.
2516 *
2517 * Special cases:
2518 * - EE == Rj -> return a one-element list containing it
2519 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2520 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002521 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002522 * Tests for (aspects of) this function should include at least:
2523 * - trusted EE
2524 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002525 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002526 * - if relevant: EE untrusted
2527 * - if relevant: EE -> intermediate, untrusted
2528 * with the aspect under test checked at each relevant level (EE, int, root).
2529 * For some aspects longer chains are required, but usually length 2 is
2530 * enough (but length 1 is not in general).
2531 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002532 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002533 * - [in] crt: the cert list EE, C1, ..., Cn
2534 * - [in] trust_ca: the trusted list R1, ..., Rp
2535 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002536 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002537 * Only valid when return value is 0, may contain garbage otherwise!
2538 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002539 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002540 *
2541 * Return value:
2542 * - non-zero if the chain could not be fully built and examined
2543 * - 0 is the chain was successfully built and examined,
2544 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002545 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002546static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 mbedtls_x509_crt *crt,
2548 mbedtls_x509_crt *trust_ca,
2549 mbedtls_x509_crl *ca_crl,
2550 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2551 void *p_ca_cb,
2552 const mbedtls_x509_crt_profile *profile,
2553 mbedtls_x509_crt_verify_chain *ver_chain,
2554 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002555{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002556 /* Don't initialize any of those variables here, so that the compiler can
2557 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002559 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002560 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002561 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002562 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002563 int parent_is_trusted;
2564 int child_is_trusted;
2565 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002566 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002567 mbedtls_x509_crt *cur_trust_ca = NULL;
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002568 mbedtls_x509_time now;
2569
2570#if defined(MBEDTLS_HAVE_TIME_DATE)
2571 if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2572 return MBEDTLS_ERR_X509_FATAL_ERROR;
2573 }
2574#endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002575
2576#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2577 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002579 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002580 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002581 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002582
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002583 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002584 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002585 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002586 flags = &cur->flags;
2587
2588 goto find_parent;
2589 }
2590#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002591
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002592 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002593 self_cnt = 0;
2594 parent_is_trusted = 0;
2595 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002598 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002599 cur = &ver_chain->items[ver_chain->len];
2600 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002601 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002602 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002603 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002604
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002605#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002606 /* Check time-validity (all certificates) */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002607 if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002608 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002610
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002611 if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002612 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002614#endif
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002615
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002616 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (child_is_trusted) {
2618 return 0;
2619 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002620
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002621 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002623 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002625
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002627 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002629
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002630 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 if (ver_chain->len == 1 &&
2632 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2633 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002634 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002635
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002636#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2637find_parent:
2638#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002639
2640 /* Obtain list of potential trusted signers from CA callback,
2641 * or use statically provided list. */
2642#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 if (f_ca_cb != NULL) {
2644 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2645 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002646 ver_chain->trust_ca_cb_result = NULL;
2647
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2649 if (ret != 0) {
2650 return MBEDTLS_ERR_X509_FATAL_ERROR;
2651 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002652
2653 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002655#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2656 {
2657 ((void) f_ca_cb);
2658 ((void) p_ca_cb);
2659 cur_trust_ca = trust_ca;
2660 }
2661
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002662 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2664 &parent_is_trusted, &signature_is_good,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002665 ver_chain->len - 1, self_cnt, rs_ctx,
2666 &now);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002667
2668#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002670 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002671 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002672 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002673 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002674
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002676 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002677#else
2678 (void) ret;
2679#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002680
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002681 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002683 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002685 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002686
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002687 /* Count intermediate self-issued (not necessarily self-signed) certs.
2688 * These can occur with some strategies for key rollover, see [SIRO],
2689 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 if (ver_chain->len != 1 &&
2691 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002692 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002693 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002694
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002695 /* path_cnt is 0 for the first intermediate CA,
2696 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 if (!parent_is_trusted &&
2698 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002699 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002701 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002702
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002703 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002705 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002707
2708 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002710 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002714 /* Check trusted CA's CRL for the given crt */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002715 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002716#else
2717 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002718#endif
2719
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002720 /* prepare for next iteration */
2721 child = parent;
2722 parent = NULL;
2723 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002724 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002725 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002726}
2727
Glenn Straussc26bd762022-10-23 19:48:18 -04002728#ifdef _WIN32
2729#ifdef _MSC_VER
2730#pragma comment(lib, "ws2_32.lib")
2731#include <winsock2.h>
2732#include <ws2tcpip.h>
2733#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2734#include <winsock2.h>
2735#include <ws2tcpip.h>
Steve Lhommeeb0f18a2023-06-16 14:12:19 +02002736#else
2737/* inet_pton() is not supported, fallback to software version */
2738#define MBEDTLS_TEST_SW_INET_PTON
Glenn Straussc26bd762022-10-23 19:48:18 -04002739#endif
2740#elif defined(__sun)
2741/* Solaris requires -lsocket -lnsl for inet_pton() */
2742#elif defined(__has_include)
2743#if __has_include(<sys/socket.h>)
2744#include <sys/socket.h>
2745#endif
2746#if __has_include(<arpa/inet.h>)
2747#include <arpa/inet.h>
2748#endif
2749#endif
2750
2751/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2752 * the platform inet_pton() or a local implementation (below). The local
2753 * implementation may be used even in cases where the platform provides
2754 * inet_pton(), e.g. when there are different includes required and/or the
2755 * platform implementation requires dependencies on additional libraries.
2756 * Specifically, Windows requires custom includes and additional link
2757 * dependencies, and Solaris requires additional link dependencies.
2758 * Also, as a coarse heuristic, use the local implementation if the compiler
2759 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002760 * provided by headers included (or not) via __has_include() above.
2761 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2762 * despite having a platform that has inet_pton. */
2763#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2764/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002765static int x509_inet_pton_ipv4(const char *src, void *dst);
2766
2767#define li_cton(c, n) \
2768 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2769
2770static int x509_inet_pton_ipv6(const char *src, void *dst)
2771{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002772 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002773 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002774 uint16_t addr[8];
2775 do {
2776 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002777 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002778 for (uint8_t digit; num_digits < 4; num_digits++) {
2779 if (li_cton(*p, digit) == 0) {
2780 break;
2781 }
2782 group = (group << 4) | digit;
2783 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002784 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002785 if (num_digits != 0) {
Dave Rodgmancfa72232023-09-05 16:20:33 +01002786 MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups);
2787 nonzero_groups++;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002788 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002789 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002790 } else if (*p == '.') {
2791 /* Don't accept IPv4 too early or late */
2792 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2793 nonzero_groups >= 7) {
2794 break;
2795 }
2796
2797 /* Walk back to prior ':', then parse as IPv4-mapped */
2798 int steps = 4;
2799 do {
2800 p--;
2801 steps--;
2802 } while (*p != ':' && steps > 0);
2803
2804 if (*p != ':') {
2805 break;
2806 }
2807 p++;
2808 nonzero_groups--;
2809 if (x509_inet_pton_ipv4((const char *) p,
2810 addr + nonzero_groups) != 0) {
2811 break;
2812 }
2813
Andrzej Kurek0d578962023-04-13 09:09:56 -04002814 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002815 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002816 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002817 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002818 return -1;
2819 }
2820 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002821 /* Don't accept a second zero group or an invalid delimiter */
2822 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002823 return -1;
2824 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002825 zero_group_start = nonzero_groups;
2826
2827 /* Accept a zero group at start, but it has to be a double colon */
2828 if (zero_group_start == 0 && *++p != ':') {
2829 return -1;
2830 }
2831
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002832 if (p[1] == '\0') {
2833 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002834 break;
2835 }
2836 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002837 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002838 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002839
2840 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002841 return -1;
2842 }
2843
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002844 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002845 if (nonzero_groups > 6) {
2846 return -1;
2847 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002848 int zero_groups = 8 - nonzero_groups;
2849 int groups_after_zero = nonzero_groups - zero_group_start;
2850
2851 /* Move the non-zero part to after the zeroes */
2852 if (groups_after_zero) {
2853 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002854 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002855 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002856 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002857 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002858 } else {
2859 if (nonzero_groups != 8) {
2860 return -1;
2861 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002862 }
2863 memcpy(dst, addr, sizeof(addr));
2864 return 0;
2865}
2866
2867static int x509_inet_pton_ipv4(const char *src, void *dst)
2868{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002869 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002870 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002871 uint8_t digit, num_digits = 0;
2872 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002873 uint16_t octet;
2874
Glenn Strauss416c2952022-10-24 23:00:02 -04002875 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002876 octet = num_digits = 0;
2877 do {
2878 digit = *p - '0';
2879 if (digit > 9) {
2880 break;
2881 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002882
2883 /* Don't allow leading zeroes. These might mean octal format,
2884 * which this implementation does not support. */
2885 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002886 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002887 }
2888
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002889 octet = octet * 10 + digit;
2890 num_digits++;
2891 p++;
2892 } while (num_digits < 3);
2893
2894 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002895 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002896 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002897 *res++ = (uint8_t) octet;
2898 num_octets++;
2899 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002900 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002901}
Glenn Straussc26bd762022-10-23 19:48:18 -04002902
2903#else
2904
2905static int x509_inet_pton_ipv6(const char *src, void *dst)
2906{
2907 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2908}
2909
2910static int x509_inet_pton_ipv4(const char *src, void *dst)
2911{
2912 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2913}
2914
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002915#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002916
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002917size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002918{
2919 return strchr(cn, ':') == NULL
2920 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2921 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2922}
2923
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002924/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002925 * Check for CN match
2926 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002927static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2928 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002929{
2930 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 if (name->len == cn_len &&
2932 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2933 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002934 }
2935
2936 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 if (x509_check_wildcard(cn, name) == 0) {
2938 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002939 }
2940
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002942}
2943
Glenn Straussc26bd762022-10-23 19:48:18 -04002944static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2945 const char *cn, size_t cn_len)
2946{
2947 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002948 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002949 if (cn_len == 0) {
2950 return -1;
2951 }
2952
2953 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2954 const unsigned char san_type = (unsigned char) cur->buf.tag &
2955 MBEDTLS_ASN1_TAG_VALUE_MASK;
2956 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2957 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2958 return 0;
2959 }
2960 }
2961
2962 return -1;
2963}
2964
Andrzej Kurek199eab92023-05-10 09:57:19 -04002965static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2966 const char *cn, size_t cn_len)
2967{
2968 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2969 const unsigned char san_type = (unsigned char) cur->buf.tag &
2970 MBEDTLS_ASN1_TAG_VALUE_MASK;
2971 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2972 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2973 return 0;
2974 }
2975 }
2976
2977 return -1;
2978}
2979
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002980/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002981 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2982 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002983static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002985{
Glenn Straussc26bd762022-10-23 19:48:18 -04002986 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002987 int san_uri = 0;
2988 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002989 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2990 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002991 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002992 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2993 return 0;
2994 }
2995 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002996 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04002997 san_ip = 1;
2998 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002999 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
3000 san_uri = 1;
3001 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04003002 /* (We may handle other types here later.) */
3003 default: /* Unrecognized type */
3004 break;
3005 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04003007 if (san_ip) {
3008 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
3009 return 0;
3010 }
3011 }
3012 if (san_uri) {
3013 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
3014 return 0;
3015 }
3016 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003017
Andrzej Kurek199eab92023-05-10 09:57:19 -04003018 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003019}
3020
3021/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003022 * Verify the requested CN - only call this if cn is not NULL!
3023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003024static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3025 const char *cn,
3026 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003027{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003028 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003030
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003032 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
3033 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 }
3035 } else {
3036 for (name = &crt->subject; name != NULL; name = name->next) {
3037 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3038 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003039 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 }
3041 }
3042
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003043 }
Glenn Straussc26bd762022-10-23 19:48:18 -04003044
3045 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003046}
3047
3048/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003049 * Merge the flags for all certs in the chain, after calling callback
3050 */
3051static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 uint32_t *flags,
3053 const mbedtls_x509_crt_verify_chain *ver_chain,
3054 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3055 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003056{
Janos Follath865b3eb2019-12-16 11:46:15 +00003057 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003058 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003059 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003060 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003061
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003063 cur = &ver_chain->items[i-1];
3064 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 if (NULL != f_vrfy) {
3067 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3068 return ret;
3069 }
3070 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003071
3072 *flags |= cur_flags;
3073 }
3074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003076}
3077
3078/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003079 * Verify the certificate validity, with profile, restartable version
3080 *
3081 * This function:
3082 * - checks the requested CN (if any)
3083 * - checks the type and size of the EE cert's key,
3084 * as that isn't done as part of chain building/verification currently
3085 * - builds and verifies the chain
3086 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003087 *
3088 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3089 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3090 * verification routine to search for trusted signers, and CRLs will
3091 * be disabled. Otherwise, `trust_ca` will be used as the static list
3092 * of trusted signers, and `ca_crl` will be use as the static list
3093 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003094 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003095static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3096 mbedtls_x509_crt *trust_ca,
3097 mbedtls_x509_crl *ca_crl,
3098 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3099 void *p_ca_cb,
3100 const mbedtls_x509_crt_profile *profile,
3101 const char *cn, uint32_t *flags,
3102 int (*f_vrfy)(void *,
3103 mbedtls_x509_crt *,
3104 int,
3105 uint32_t *),
3106 void *p_vrfy,
3107 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003108{
Janos Follath865b3eb2019-12-16 11:46:15 +00003109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003110 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003111 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003112 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003113
3114 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003115 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003117
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003119 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3120 goto exit;
3121 }
3122
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003123 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 if (cn != NULL) {
3125 x509_crt_verify_name(crt, cn, &ee_flags);
3126 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003127
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003128 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003132 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003136 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003138
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003139 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3141 f_ca_cb, p_ca_cb, profile,
3142 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003143
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003145 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003147
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003148 /* Merge end-entity flags */
3149 ver_chain.items[0].flags |= ee_flags;
3150
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003151 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003153
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003154exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003155
3156#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3158 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003159 ver_chain.trust_ca_cb_result = NULL;
3160#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3161
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003162#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3164 mbedtls_x509_crt_restart_free(rs_ctx);
3165 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003166#endif
3167
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003168 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3169 * the SSL module for authmode optional, but non-zero return from the
3170 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003172 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003173 }
3174
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 if (ret != 0) {
3176 *flags = (uint32_t) -1;
3177 return ret;
3178 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003179
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 if (*flags != 0) {
3181 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3182 }
3183
3184 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003185}
3186
Hanno Becker3116fb32019-03-28 13:34:42 +00003187
3188/*
3189 * Verify the certificate validity (default profile, not restartable)
3190 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003191int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3192 mbedtls_x509_crt *trust_ca,
3193 mbedtls_x509_crl *ca_crl,
3194 const char *cn, uint32_t *flags,
3195 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3196 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003197{
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3199 NULL, NULL,
3200 &mbedtls_x509_crt_profile_default,
3201 cn, flags,
3202 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003203}
3204
3205/*
3206 * Verify the certificate validity (user-chosen profile, not restartable)
3207 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003208int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3209 mbedtls_x509_crt *trust_ca,
3210 mbedtls_x509_crl *ca_crl,
3211 const mbedtls_x509_crt_profile *profile,
3212 const char *cn, uint32_t *flags,
3213 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3214 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003215{
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3217 NULL, NULL,
3218 profile, cn, flags,
3219 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003220}
3221
3222#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3223/*
3224 * Verify the certificate validity (user-chosen profile, CA callback,
3225 * not restartable).
3226 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003227int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3228 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3229 void *p_ca_cb,
3230 const mbedtls_x509_crt_profile *profile,
3231 const char *cn, uint32_t *flags,
3232 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3233 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003234{
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3236 f_ca_cb, p_ca_cb,
3237 profile, cn, flags,
3238 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003239}
3240#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3241
Gilles Peskine449bd832023-01-11 14:50:10 +01003242int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3243 mbedtls_x509_crt *trust_ca,
3244 mbedtls_x509_crl *ca_crl,
3245 const mbedtls_x509_crt_profile *profile,
3246 const char *cn, uint32_t *flags,
3247 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3248 void *p_vrfy,
3249 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003250{
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3252 NULL, NULL,
3253 profile, cn, flags,
3254 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003255}
3256
3257
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003258/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003259 * Initialize a certificate chain
3260 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003261void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003262{
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003264}
3265
3266/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003267 * Unallocate all certificate data
3268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003269void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003270{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 mbedtls_x509_crt *cert_cur = crt;
3272 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003273
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 while (cert_cur != NULL) {
3275 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003279#endif
3280
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3282 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3283 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3284 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3285 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Przemek Stekiel690ff692023-05-15 09:54:02 +02003286 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003287
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003289 mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003290 }
3291
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003292 cert_prv = cert_cur;
3293 cert_cur = cert_cur->next;
3294
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3296 if (cert_prv != crt) {
3297 mbedtls_free(cert_prv);
3298 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003299 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003300}
3301
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003302#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3303/*
3304 * Initialize a restart context
3305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003306void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003307{
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003309
3310 ctx->parent = NULL;
3311 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003312 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003313
3314 ctx->parent_is_trusted = -1;
3315
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003316 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003317 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003319}
3320
3321/*
3322 * Free the components of a restart context
3323 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003324void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003325{
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003327 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003329
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 mbedtls_pk_restart_free(&ctx->pk);
3331 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003332}
3333#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335#endif /* MBEDTLS_X509_CRT_PARSE_C */