blob: f73e215c26f9099403e4f7bf6fff2227e8b677fd [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);
Kevin Kane0ec1e682016-12-15 09:27:16 -08001557 int lengthAsInt = 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
Kevin Kane0ec1e682016-12-15 09:27:16 -08001573 if (FAILED (SizeTToInt(len, &lengthAsInt)))
1574 return(MBEDTLS_ERR_X509_FILE_IO_ERROR);
1575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1577 MAX_PATH - 3);
1578 if (w_ret == 0) {
1579 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1580 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 hFind = FindFirstFileW(szDir, &file_data);
1583 if (hFind == INVALID_HANDLE_VALUE) {
1584 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1585 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001586
1587 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 do {
1589 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgroveb96c3092023-02-10 12:52:13 +00001596 -1,
1597 p, (int) len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 NULL, NULL);
Kevin Kane0ec1e682016-12-15 09:27:16 -08001599 if (FAILED(SizeTToInt(wcslen(file_data.cFileName), &lengthAsInt)))
1600 return(MBEDTLS_ERR_X509_FILE_IO_ERROR);
1601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001603 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1604 goto cleanup;
1605 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1608 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001609 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 }
1613 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001618
Ron Eldor36d90422017-01-09 15:09:16 +02001619cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 FindClose(hFind);
Steve Lhomme369d7c72023-06-16 14:16:03 +02001621#else /* !_WIN32_WINNT_XP */
Antonio de Angelis1ee4d122023-08-16 12:26:37 +01001622#error "mbedtls_x509_crt_parse_path not available before Windows XP"
Steve Lhomme369d7c72023-06-16 14:16:03 +02001623#endif /* !_WIN32_WINNT_XP */
Paul Bakkerbe089b02013-10-14 15:51:50 +02001624#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001625 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001626 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001627 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001628 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001629 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 if (dir == NULL) {
1633 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1634 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635
Ron Eldor63140682017-01-09 19:27:59 +02001636#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1638 closedir(dir);
1639 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001640 }
Ron Eldor63140682017-01-09 19:27:59 +02001641#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001646 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001648
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001649 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001650 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1651 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 } else if (stat(entry_name, &sb) == -1) {
1653 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001654 /* Broken symbolic link - ignore this entry.
1655 stat(2) will return this error for either (a) a dangling
1656 symlink or (b) a missing file.
1657 Given that we have just obtained the filename from readdir,
1658 assume that it does exist and therefore treat this as a
1659 dangling symlink. */
1660 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001662 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001663 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1664 goto cleanup;
1665 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001666 }
1667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001669 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671
1672 // Ignore parse errors
1673 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1675 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001676 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001681
1682cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001684
Ron Eldor63140682017-01-09 19:27:59 +02001685#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 }
Ron Eldor63140682017-01-09 19:27:59 +02001689#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001690
Paul Bakkerbe089b02013-10-14 15:51:50 +02001691#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001694}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001696
Peter Kolbus9a969b62018-12-11 13:55:56 -06001697#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001698#define PRINT_ITEM(i) \
1699 do { \
1700 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1701 MBEDTLS_X509_SAFE_SNPRINTF; \
1702 sep = ", "; \
1703 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001704
Przemek Stekielf4194942023-04-24 09:52:17 +02001705#define CERT_TYPE(type, name) \
1706 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001707 if (ns_cert_type & (type)) { \
1708 PRINT_ITEM(name); \
1709 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001710 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001711
Przemek Stekielf4194942023-04-24 09:52:17 +02001712#define KEY_USAGE(code, name) \
1713 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001714 if (key_usage & (code)) { \
1715 PRINT_ITEM(name); \
1716 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001717 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719static int x509_info_ext_key_usage(char **buf, size_t *size,
1720 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001721{
Janos Follath865b3eb2019-12-16 11:46:15 +00001722 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001723 const char *desc;
1724 size_t n = *size;
1725 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001727 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 while (cur != NULL) {
1730 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001731 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001733
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001735 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001736
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001737 sep = ", ";
1738
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001739 cur = cur->next;
1740 }
1741
1742 *size = n;
1743 *buf = p;
1744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001746}
1747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748static int x509_info_cert_policies(char **buf, size_t *size,
1749 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001750{
Janos Follath865b3eb2019-12-16 11:46:15 +00001751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001752 const char *desc;
1753 size_t n = *size;
1754 char *p = *buf;
1755 const mbedtls_x509_sequence *cur = certificate_policies;
1756 const char *sep = "";
1757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 while (cur != NULL) {
1759 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001760 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001762
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001764 MBEDTLS_X509_SAFE_SNPRINTF;
1765
1766 sep = ", ";
1767
1768 cur = cur->next;
1769 }
1770
1771 *size = n;
1772 *buf = p;
1773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001775}
1776
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001777/*
1778 * Return an informational string about the certificate.
1779 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001780#define BEFORE_COLON 18
1781#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001782int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1783 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001784{
Janos Follath865b3eb2019-12-16 11:46:15 +00001785 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001786 size_t n;
1787 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001788 char key_size_str[BEFORE_COLON];
1789
1790 p = buf;
1791 n = size;
1792
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 if (NULL == crt) {
1794 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001795 MBEDTLS_X509_SAFE_SNPRINTF;
1796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001798 }
1799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1801 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001802 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1804 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001805 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001808 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001811 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001813 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001814
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001816 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001818 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1821 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1822 crt->valid_from.year, crt->valid_from.mon,
1823 crt->valid_from.day, crt->valid_from.hour,
1824 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001825 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1828 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1829 crt->valid_to.year, crt->valid_to.mon,
1830 crt->valid_to.day, crt->valid_to.hour,
1831 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001832 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001833
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001835 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001836
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1838 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001839 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001840
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001841 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1843 mbedtls_pk_get_name(&crt->pk))) != 0) {
1844 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001845 }
1846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1848 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001849 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001850
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001851 /*
1852 * Optional extensions
1853 */
1854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1856 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1857 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001858 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 if (crt->max_pathlen > 0) {
1861 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001862 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001863 }
1864 }
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1867 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001868 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001869
Przemek Stekiel21c37282023-01-16 08:47:49 +01001870 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1871 &crt->subject_alt_names,
1872 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 return ret;
1874 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001875 }
1876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1878 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001879 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001880
Przemek Stekiel21c37282023-01-16 08:47:49 +01001881 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 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_KEY_USAGE) {
1887 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001888 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001889
Przemek Stekiel21c37282023-01-16 08:47:49 +01001890 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 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_EXTENDED_KEY_USAGE) {
1896 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001897 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 if ((ret = x509_info_ext_key_usage(&p, &n,
1900 &crt->ext_key_usage)) != 0) {
1901 return ret;
1902 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001903 }
1904
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1906 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001907 MBEDTLS_X509_SAFE_SNPRINTF;
1908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 if ((ret = x509_info_cert_policies(&p, &n,
1910 &crt->certificate_policies)) != 0) {
1911 return ret;
1912 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001913 }
1914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001916 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001919}
1920
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001921struct x509_crt_verify_string {
1922 int code;
1923 const char *string;
1924};
1925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001927static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001928 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001929 { 0, NULL }
1930};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001931#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001932
Gilles Peskine449bd832023-01-11 14:50:10 +01001933int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1934 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001935{
Janos Follath865b3eb2019-12-16 11:46:15 +00001936 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001937 const struct x509_crt_verify_string *cur;
1938 char *p = buf;
1939 size_t n = size;
1940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1942 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001943 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001947 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001948 flags ^= cur->code;
1949 }
1950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 if (flags != 0) {
1952 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1953 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001954 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001955 }
1956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001958}
Hanno Becker612a2f12020-10-09 09:19:39 +01001959#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1962 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001963{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001964 unsigned int usage_must, usage_may;
1965 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1969 return 0;
1970 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001971
1972 usage_must = usage & ~may_mask;
1973
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1975 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1976 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001977
1978 usage_may = usage & may_mask;
1979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1981 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1982 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001985}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001986
Gilles Peskine449bd832023-01-11 14:50:10 +01001987int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1988 const char *usage_oid,
1989 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001990{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001992
1993 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1995 return 0;
1996 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001997
1998 /*
1999 * Look for the requested usage (or wildcard ANY) in our list
2000 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002003
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 if (cur_oid->len == usage_len &&
2005 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2006 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002007 }
2008
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2010 return 0;
2011 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002012 }
2013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002015}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002018/*
2019 * Return 1 if the certificate is revoked, or 0 otherwise.
2020 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002021int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002022{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 while (cur != NULL && cur->serial.len != 0) {
2026 if (crt->serial.len == cur->serial.len &&
2027 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2028 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002029 }
2030
2031 cur = cur->next;
2032 }
2033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002035}
2036
2037/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002038 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002039 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002041static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2042 mbedtls_x509_crl *crl_list,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002043 const mbedtls_x509_crt_profile *profile,
2044 const mbedtls_x509_time *now)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002045{
2046 int flags = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002047 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002048#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002049 psa_algorithm_t psa_algorithm;
2050#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002052#endif /* MBEDTLS_USE_PSA_CRYPTO */
2053 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 if (ca == NULL) {
2056 return flags;
2057 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 while (crl_list != NULL) {
2060 if (crl_list->version == 0 ||
2061 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002062 crl_list = crl_list->next;
2063 continue;
2064 }
2065
2066 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002067 * Check if the CA is configured to sign CRLs
2068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 if (mbedtls_x509_crt_check_key_usage(ca,
2070 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002072 break;
2073 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002074
2075 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002076 * Check if CRL is correctly signed by the trusted CA
2077 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002079 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002083 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002085
pespacek7599a772022-02-07 14:40:55 +01002086#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002087 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 if (psa_hash_compute(psa_algorithm,
2089 crl_list->tbs.p,
2090 crl_list->tbs.len,
2091 hash,
2092 sizeof(hash),
2093 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002094 /* Note: this can't happen except after an internal error */
2095 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2096 break;
2097 }
pespacek7599a772022-02-07 14:40:55 +01002098#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2100 hash_length = mbedtls_md_get_size(md_info);
2101 if (mbedtls_md(md_info,
2102 crl_list->tbs.p,
2103 crl_list->tbs.len,
2104 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002105 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002107 break;
2108 }
pespaceka7a64692022-02-14 15:18:43 +01002109#endif /* MBEDTLS_USE_PSA_CRYPTO */
2110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002112 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002114
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2116 crl_list->sig_md, hash, hash_length,
2117 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002119 break;
2120 }
2121
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002122#if defined(MBEDTLS_HAVE_TIME_DATE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002123 /*
2124 * Check for validity of CRL (Do not drop out)
2125 */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002126 if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002129
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002130 if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002131 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002133#else
2134 ((void) now);
2135#endif
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002136
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002137 /*
2138 * Check if certificate is revoked
2139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002142 break;
2143 }
2144
2145 crl_list = crl_list->next;
2146 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002151
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002152/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002153 * Check the signature of a certificate by its parent
2154 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002155static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2156 mbedtls_x509_crt *parent,
2157 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002158{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002159 size_t hash_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002160 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002161#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2162 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 md_info = mbedtls_md_info_from_type(child->sig_md);
2164 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002165
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002166 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2168 return -1;
2169 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002170#else
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002171 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002172 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 status = psa_hash_compute(hash_alg,
2175 child->tbs.p,
2176 child->tbs.len,
2177 hash,
2178 sizeof(hash),
2179 &hash_len);
2180 if (status != PSA_SUCCESS) {
2181 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002182 }
2183
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002184#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002185 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2187 return -1;
2188 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002189
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002190#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2192 return mbedtls_pk_verify_restartable(&parent->pk,
2193 child->sig_md, hash, hash_len,
2194 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002195 }
2196#else
2197 (void) rs_ctx;
2198#endif
2199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2201 child->sig_md, hash, hash_len,
2202 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002203}
2204
2205/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002206 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2207 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002208 *
2209 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002211static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2212 const mbedtls_x509_crt *parent,
2213 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002214{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002215 int need_ca_bit;
2216
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002217 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2219 return -1;
2220 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002221
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002222 /* Parent must have the basicConstraints CA bit set as a general rule */
2223 need_ca_bit = 1;
2224
2225 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002227 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002228 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 if (need_ca_bit && !parent->ca_istrue) {
2231 return -1;
2232 }
2233
2234 if (need_ca_bit &&
2235 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2236 return -1;
2237 }
2238
2239 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002240}
2241
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002242/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002243 * Find a suitable parent for child in candidates, or return NULL.
2244 *
2245 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002246 * 1. subject name matches child's issuer
2247 * 2. if necessary, the CA bit is set and key usage allows signing certs
2248 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002249 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002250 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002251 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002252 * If there's a suitable candidate which is also time-valid, return the first
2253 * such. Otherwise, return the first suitable candidate (or NULL if there is
2254 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002255 *
2256 * The rationale for this rule is that someone could have a list of trusted
2257 * roots with two versions on the same root with different validity periods.
2258 * (At least one user reported having such a list and wanted it to just work.)
2259 * The reason we don't just require time-validity is that generally there is
2260 * only one version, and if it's expired we want the flags to state that
2261 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002262 *
2263 * The rationale for rule 3 (signature for trusted roots) is that users might
2264 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002265 * way we select the correct one is by checking the signature (as we don't
2266 * rely on key identifier extensions). (This is one way users might choose to
2267 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002268 *
2269 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002270 * - [in] child: certificate for which we're looking for a parent
2271 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002272 * - [out] r_parent: parent found (or NULL)
2273 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002274 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2275 * of the chain, 0 otherwise
2276 * - [in] path_cnt: number of intermediates seen so far
2277 * - [in] self_cnt: number of self-signed intermediates seen so far
2278 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002279 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002280 *
2281 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002282 * - 0 on success
2283 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002284 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002285static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 mbedtls_x509_crt *child,
2287 mbedtls_x509_crt *candidates,
2288 mbedtls_x509_crt **r_parent,
2289 int *r_signature_is_good,
2290 int top,
2291 unsigned path_cnt,
2292 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002293 mbedtls_x509_crt_restart_ctx *rs_ctx,
2294 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002295{
Janos Follath865b3eb2019-12-16 11:46:15 +00002296 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002297 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002298 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002299
2300#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002301 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002303 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002304 parent = rs_ctx->parent;
2305 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002306 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002307
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002308 /* clear saved state */
2309 rs_ctx->parent = NULL;
2310 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002311 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002312
2313 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002314 goto check_signature;
2315 }
2316#endif
2317
2318 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002319 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002322 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002324 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002326
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002327 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (parent->max_pathlen > 0 &&
2329 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002330 continue;
2331 }
2332
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002333 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002334#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2335check_signature:
2336#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002338
2339#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002341 /* save state */
2342 rs_ctx->parent = parent;
2343 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002344 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002347 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002348#else
2349 (void) ret;
2350#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002351
2352 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002354 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002356
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002357#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002358 /* optional time check */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002359 if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2360 mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002362 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002363 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002364 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002365
2366 continue;
2367 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002368#else
2369 ((void) now);
2370#endif
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002371
Andy Gross1f627142019-01-30 10:25:53 -06002372 *r_parent = parent;
2373 *r_signature_is_good = signature_is_good;
2374
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002375 break;
2376 }
2377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002379 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002380 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002381 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002384}
2385
2386/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002387 * Find a parent in trusted CAs or the provided chain, or return NULL.
2388 *
2389 * Searches in trusted CAs first, and return the first suitable parent found
2390 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002391 *
2392 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002393 * - [in] child: certificate for which we're looking for a parent, followed
2394 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002395 * - [in] trust_ca: list of locally trusted certificates
2396 * - [out] parent: parent found (or NULL)
2397 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2398 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2399 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2400 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002401 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002402 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002403 *
2404 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002405 * - 0 on success
2406 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002407 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002408static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 mbedtls_x509_crt *child,
2410 mbedtls_x509_crt *trust_ca,
2411 mbedtls_x509_crt **parent,
2412 int *parent_is_trusted,
2413 int *signature_is_good,
2414 unsigned path_cnt,
2415 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002416 mbedtls_x509_crt_restart_ctx *rs_ctx,
2417 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002418{
Janos Follath865b3eb2019-12-16 11:46:15 +00002419 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002420 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002421
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002422 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002423
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002424#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002425 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002427 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002428 rs_ctx->parent_is_trusted = -1;
2429 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002430#endif
2431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002433 search_list = *parent_is_trusted ? trust_ca : child->next;
2434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 ret = x509_crt_find_parent_in(child, search_list,
2436 parent, signature_is_good,
2437 *parent_is_trusted,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002438 path_cnt, self_cnt, rs_ctx, now);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002439
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002440#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002442 /* save state */
2443 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002445 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002446#else
2447 (void) ret;
2448#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002449
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002450 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002452 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002454
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002455 /* prepare second iteration */
2456 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002457 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002458
2459 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002461 *parent_is_trusted = 0;
2462 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002463 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002466}
2467
2468/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002469 * Check if an end-entity certificate is locally trusted
2470 *
2471 * Currently we require such certificates to be self-signed (actually only
2472 * check for self-issued as self-signatures are not checked)
2473 */
2474static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 mbedtls_x509_crt *crt,
2476 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002477{
2478 mbedtls_x509_crt *cur;
2479
2480 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2482 return -1;
2483 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002484
2485 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2487 if (crt->raw.len == cur->raw.len &&
2488 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2489 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002490 }
2491 }
2492
2493 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002495}
2496
2497/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002498 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002499 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002500 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2501 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002502 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002503 * such that every cert in the chain is a child of the next one,
2504 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002505 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002506 * Verify that chain and return it with flags for all issues found.
2507 *
2508 * Special cases:
2509 * - EE == Rj -> return a one-element list containing it
2510 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2511 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002512 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002513 * Tests for (aspects of) this function should include at least:
2514 * - trusted EE
2515 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002516 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002517 * - if relevant: EE untrusted
2518 * - if relevant: EE -> intermediate, untrusted
2519 * with the aspect under test checked at each relevant level (EE, int, root).
2520 * For some aspects longer chains are required, but usually length 2 is
2521 * enough (but length 1 is not in general).
2522 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002523 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002524 * - [in] crt: the cert list EE, C1, ..., Cn
2525 * - [in] trust_ca: the trusted list R1, ..., Rp
2526 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002527 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002528 * Only valid when return value is 0, may contain garbage otherwise!
2529 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002530 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002531 *
2532 * Return value:
2533 * - non-zero if the chain could not be fully built and examined
2534 * - 0 is the chain was successfully built and examined,
2535 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002536 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002537static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 mbedtls_x509_crt *crt,
2539 mbedtls_x509_crt *trust_ca,
2540 mbedtls_x509_crl *ca_crl,
2541 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2542 void *p_ca_cb,
2543 const mbedtls_x509_crt_profile *profile,
2544 mbedtls_x509_crt_verify_chain *ver_chain,
2545 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002546{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002547 /* Don't initialize any of those variables here, so that the compiler can
2548 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002550 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002551 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002552 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002553 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002554 int parent_is_trusted;
2555 int child_is_trusted;
2556 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002557 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002558 mbedtls_x509_crt *cur_trust_ca = NULL;
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002559 mbedtls_x509_time now;
2560
2561#if defined(MBEDTLS_HAVE_TIME_DATE)
2562 if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2563 return MBEDTLS_ERR_X509_FATAL_ERROR;
2564 }
2565#endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002566
2567#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2568 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002570 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002571 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002572 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002573
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002574 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002575 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002576 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002577 flags = &cur->flags;
2578
2579 goto find_parent;
2580 }
2581#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002582
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002583 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002584 self_cnt = 0;
2585 parent_is_trusted = 0;
2586 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002589 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002590 cur = &ver_chain->items[ver_chain->len];
2591 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002592 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002593 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002594 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002595
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002596#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002597 /* Check time-validity (all certificates) */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002598 if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002599 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002601
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002602 if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002603 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002605#endif
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002606
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002607 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 if (child_is_trusted) {
2609 return 0;
2610 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002611
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002612 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002614 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002618 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002620
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002621 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (ver_chain->len == 1 &&
2623 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2624 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002625 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002626
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002627#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2628find_parent:
2629#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002630
2631 /* Obtain list of potential trusted signers from CA callback,
2632 * or use statically provided list. */
2633#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 if (f_ca_cb != NULL) {
2635 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2636 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002637 ver_chain->trust_ca_cb_result = NULL;
2638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2640 if (ret != 0) {
2641 return MBEDTLS_ERR_X509_FATAL_ERROR;
2642 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002643
2644 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002646#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2647 {
2648 ((void) f_ca_cb);
2649 ((void) p_ca_cb);
2650 cur_trust_ca = trust_ca;
2651 }
2652
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002653 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2655 &parent_is_trusted, &signature_is_good,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002656 ver_chain->len - 1, self_cnt, rs_ctx,
2657 &now);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002658
2659#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002661 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002662 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002663 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002664 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002667 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002668#else
2669 (void) ret;
2670#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002671
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002672 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002674 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002676 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002677
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002678 /* Count intermediate self-issued (not necessarily self-signed) certs.
2679 * These can occur with some strategies for key rollover, see [SIRO],
2680 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 if (ver_chain->len != 1 &&
2682 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002683 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002684 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002685
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002686 /* path_cnt is 0 for the first intermediate CA,
2687 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 if (!parent_is_trusted &&
2689 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002690 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002692 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002693
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002694 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002696 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002698
2699 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002701 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002705 /* Check trusted CA's CRL for the given crt */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002706 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002707#else
2708 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002709#endif
2710
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002711 /* prepare for next iteration */
2712 child = parent;
2713 parent = NULL;
2714 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002715 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002716 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002717}
2718
Glenn Straussc26bd762022-10-23 19:48:18 -04002719#ifdef _WIN32
2720#ifdef _MSC_VER
2721#pragma comment(lib, "ws2_32.lib")
2722#include <winsock2.h>
2723#include <ws2tcpip.h>
2724#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2725#include <winsock2.h>
2726#include <ws2tcpip.h>
Steve Lhommeeb0f18a2023-06-16 14:12:19 +02002727#else
2728/* inet_pton() is not supported, fallback to software version */
2729#define MBEDTLS_TEST_SW_INET_PTON
Glenn Straussc26bd762022-10-23 19:48:18 -04002730#endif
2731#elif defined(__sun)
2732/* Solaris requires -lsocket -lnsl for inet_pton() */
2733#elif defined(__has_include)
2734#if __has_include(<sys/socket.h>)
2735#include <sys/socket.h>
2736#endif
2737#if __has_include(<arpa/inet.h>)
2738#include <arpa/inet.h>
2739#endif
2740#endif
2741
2742/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2743 * the platform inet_pton() or a local implementation (below). The local
2744 * implementation may be used even in cases where the platform provides
2745 * inet_pton(), e.g. when there are different includes required and/or the
2746 * platform implementation requires dependencies on additional libraries.
2747 * Specifically, Windows requires custom includes and additional link
2748 * dependencies, and Solaris requires additional link dependencies.
2749 * Also, as a coarse heuristic, use the local implementation if the compiler
2750 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002751 * provided by headers included (or not) via __has_include() above.
2752 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2753 * despite having a platform that has inet_pton. */
2754#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2755/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002756static int x509_inet_pton_ipv4(const char *src, void *dst);
2757
2758#define li_cton(c, n) \
2759 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2760
2761static int x509_inet_pton_ipv6(const char *src, void *dst)
2762{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002763 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002764 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002765 uint16_t addr[8];
2766 do {
2767 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002768 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002769 for (uint8_t digit; num_digits < 4; num_digits++) {
2770 if (li_cton(*p, digit) == 0) {
2771 break;
2772 }
2773 group = (group << 4) | digit;
2774 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002775 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002776 if (num_digits != 0) {
Dave Rodgmancfa72232023-09-05 16:20:33 +01002777 MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups);
2778 nonzero_groups++;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002779 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002780 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002781 } else if (*p == '.') {
2782 /* Don't accept IPv4 too early or late */
2783 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2784 nonzero_groups >= 7) {
2785 break;
2786 }
2787
2788 /* Walk back to prior ':', then parse as IPv4-mapped */
2789 int steps = 4;
2790 do {
2791 p--;
2792 steps--;
2793 } while (*p != ':' && steps > 0);
2794
2795 if (*p != ':') {
2796 break;
2797 }
2798 p++;
2799 nonzero_groups--;
2800 if (x509_inet_pton_ipv4((const char *) p,
2801 addr + nonzero_groups) != 0) {
2802 break;
2803 }
2804
Andrzej Kurek0d578962023-04-13 09:09:56 -04002805 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002806 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002807 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002808 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002809 return -1;
2810 }
2811 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002812 /* Don't accept a second zero group or an invalid delimiter */
2813 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002814 return -1;
2815 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002816 zero_group_start = nonzero_groups;
2817
2818 /* Accept a zero group at start, but it has to be a double colon */
2819 if (zero_group_start == 0 && *++p != ':') {
2820 return -1;
2821 }
2822
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002823 if (p[1] == '\0') {
2824 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002825 break;
2826 }
2827 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002828 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002829 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002830
2831 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002832 return -1;
2833 }
2834
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002835 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002836 if (nonzero_groups > 6) {
2837 return -1;
2838 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002839 int zero_groups = 8 - nonzero_groups;
2840 int groups_after_zero = nonzero_groups - zero_group_start;
2841
2842 /* Move the non-zero part to after the zeroes */
2843 if (groups_after_zero) {
2844 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002845 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002846 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002847 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002848 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002849 } else {
2850 if (nonzero_groups != 8) {
2851 return -1;
2852 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002853 }
2854 memcpy(dst, addr, sizeof(addr));
2855 return 0;
2856}
2857
2858static int x509_inet_pton_ipv4(const char *src, void *dst)
2859{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002860 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002861 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002862 uint8_t digit, num_digits = 0;
2863 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002864 uint16_t octet;
2865
Glenn Strauss416c2952022-10-24 23:00:02 -04002866 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002867 octet = num_digits = 0;
2868 do {
2869 digit = *p - '0';
2870 if (digit > 9) {
2871 break;
2872 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002873
2874 /* Don't allow leading zeroes. These might mean octal format,
2875 * which this implementation does not support. */
2876 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002877 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002878 }
2879
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002880 octet = octet * 10 + digit;
2881 num_digits++;
2882 p++;
2883 } while (num_digits < 3);
2884
2885 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002886 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002887 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002888 *res++ = (uint8_t) octet;
2889 num_octets++;
2890 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002891 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002892}
Glenn Straussc26bd762022-10-23 19:48:18 -04002893
2894#else
2895
2896static int x509_inet_pton_ipv6(const char *src, void *dst)
2897{
2898 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2899}
2900
2901static int x509_inet_pton_ipv4(const char *src, void *dst)
2902{
2903 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2904}
2905
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002906#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002907
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002908size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002909{
2910 return strchr(cn, ':') == NULL
2911 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2912 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2913}
2914
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002915/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002916 * Check for CN match
2917 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002918static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2919 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002920{
2921 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 if (name->len == cn_len &&
2923 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2924 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002925 }
2926
2927 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 if (x509_check_wildcard(cn, name) == 0) {
2929 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002930 }
2931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002933}
2934
Glenn Straussc26bd762022-10-23 19:48:18 -04002935static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2936 const char *cn, size_t cn_len)
2937{
2938 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002939 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002940 if (cn_len == 0) {
2941 return -1;
2942 }
2943
2944 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2945 const unsigned char san_type = (unsigned char) cur->buf.tag &
2946 MBEDTLS_ASN1_TAG_VALUE_MASK;
2947 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2948 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2949 return 0;
2950 }
2951 }
2952
2953 return -1;
2954}
2955
Andrzej Kurek199eab92023-05-10 09:57:19 -04002956static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2957 const char *cn, size_t cn_len)
2958{
2959 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2960 const unsigned char san_type = (unsigned char) cur->buf.tag &
2961 MBEDTLS_ASN1_TAG_VALUE_MASK;
2962 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2963 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2964 return 0;
2965 }
2966 }
2967
2968 return -1;
2969}
2970
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002971/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002972 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2973 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002974static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002976{
Glenn Straussc26bd762022-10-23 19:48:18 -04002977 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002978 int san_uri = 0;
2979 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002980 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2981 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002982 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002983 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2984 return 0;
2985 }
2986 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002987 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04002988 san_ip = 1;
2989 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002990 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
2991 san_uri = 1;
2992 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04002993 /* (We may handle other types here later.) */
2994 default: /* Unrecognized type */
2995 break;
2996 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04002998 if (san_ip) {
2999 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
3000 return 0;
3001 }
3002 }
3003 if (san_uri) {
3004 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
3005 return 0;
3006 }
3007 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003008
Andrzej Kurek199eab92023-05-10 09:57:19 -04003009 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003010}
3011
3012/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003013 * Verify the requested CN - only call this if cn is not NULL!
3014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003015static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3016 const char *cn,
3017 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003018{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003019 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003023 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
3024 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 }
3026 } else {
3027 for (name = &crt->subject; name != NULL; name = name->next) {
3028 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3029 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003030 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 }
3032 }
3033
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003034 }
Glenn Straussc26bd762022-10-23 19:48:18 -04003035
3036 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003037}
3038
3039/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003040 * Merge the flags for all certs in the chain, after calling callback
3041 */
3042static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 uint32_t *flags,
3044 const mbedtls_x509_crt_verify_chain *ver_chain,
3045 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3046 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003047{
Janos Follath865b3eb2019-12-16 11:46:15 +00003048 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003049 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003050 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003051 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003054 cur = &ver_chain->items[i-1];
3055 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003056
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 if (NULL != f_vrfy) {
3058 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3059 return ret;
3060 }
3061 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003062
3063 *flags |= cur_flags;
3064 }
3065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003067}
3068
3069/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003070 * Verify the certificate validity, with profile, restartable version
3071 *
3072 * This function:
3073 * - checks the requested CN (if any)
3074 * - checks the type and size of the EE cert's key,
3075 * as that isn't done as part of chain building/verification currently
3076 * - builds and verifies the chain
3077 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003078 *
3079 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3080 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3081 * verification routine to search for trusted signers, and CRLs will
3082 * be disabled. Otherwise, `trust_ca` will be used as the static list
3083 * of trusted signers, and `ca_crl` will be use as the static list
3084 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003085 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003086static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3087 mbedtls_x509_crt *trust_ca,
3088 mbedtls_x509_crl *ca_crl,
3089 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3090 void *p_ca_cb,
3091 const mbedtls_x509_crt_profile *profile,
3092 const char *cn, uint32_t *flags,
3093 int (*f_vrfy)(void *,
3094 mbedtls_x509_crt *,
3095 int,
3096 uint32_t *),
3097 void *p_vrfy,
3098 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003099{
Janos Follath865b3eb2019-12-16 11:46:15 +00003100 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003101 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003102 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003103 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003104
3105 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003106 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003108
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003110 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3111 goto exit;
3112 }
3113
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003114 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 if (cn != NULL) {
3116 x509_crt_verify_name(crt, cn, &ee_flags);
3117 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003118
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003119 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003123 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003125
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003127 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003129
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003130 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3132 f_ca_cb, p_ca_cb, profile,
3133 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003136 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003138
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003139 /* Merge end-entity flags */
3140 ver_chain.items[0].flags |= ee_flags;
3141
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003142 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003144
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003145exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003146
3147#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3149 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003150 ver_chain.trust_ca_cb_result = NULL;
3151#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3152
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003153#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3155 mbedtls_x509_crt_restart_free(rs_ctx);
3156 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003157#endif
3158
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003159 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3160 * the SSL module for authmode optional, but non-zero return from the
3161 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003163 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003164 }
3165
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 if (ret != 0) {
3167 *flags = (uint32_t) -1;
3168 return ret;
3169 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003170
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 if (*flags != 0) {
3172 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3173 }
3174
3175 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003176}
3177
Hanno Becker3116fb32019-03-28 13:34:42 +00003178
3179/*
3180 * Verify the certificate validity (default profile, not restartable)
3181 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003182int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3183 mbedtls_x509_crt *trust_ca,
3184 mbedtls_x509_crl *ca_crl,
3185 const char *cn, uint32_t *flags,
3186 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3187 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003188{
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3190 NULL, NULL,
3191 &mbedtls_x509_crt_profile_default,
3192 cn, flags,
3193 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003194}
3195
3196/*
3197 * Verify the certificate validity (user-chosen profile, not restartable)
3198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003199int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3200 mbedtls_x509_crt *trust_ca,
3201 mbedtls_x509_crl *ca_crl,
3202 const mbedtls_x509_crt_profile *profile,
3203 const char *cn, uint32_t *flags,
3204 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3205 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003206{
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3208 NULL, NULL,
3209 profile, cn, flags,
3210 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003211}
3212
3213#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3214/*
3215 * Verify the certificate validity (user-chosen profile, CA callback,
3216 * not restartable).
3217 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003218int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3219 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3220 void *p_ca_cb,
3221 const mbedtls_x509_crt_profile *profile,
3222 const char *cn, uint32_t *flags,
3223 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3224 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003225{
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3227 f_ca_cb, p_ca_cb,
3228 profile, cn, flags,
3229 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003230}
3231#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3232
Gilles Peskine449bd832023-01-11 14:50:10 +01003233int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3234 mbedtls_x509_crt *trust_ca,
3235 mbedtls_x509_crl *ca_crl,
3236 const mbedtls_x509_crt_profile *profile,
3237 const char *cn, uint32_t *flags,
3238 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3239 void *p_vrfy,
3240 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003241{
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3243 NULL, NULL,
3244 profile, cn, flags,
3245 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003246}
3247
3248
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003249/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003250 * Initialize a certificate chain
3251 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003252void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003253{
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003255}
3256
3257/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003258 * Unallocate all certificate data
3259 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003260void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262 mbedtls_x509_crt *cert_cur = crt;
3263 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 while (cert_cur != NULL) {
3266 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003270#endif
3271
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3273 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3274 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3275 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3276 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Przemek Stekiel690ff692023-05-15 09:54:02 +02003277 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003278
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003280 mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003281 }
3282
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003283 cert_prv = cert_cur;
3284 cert_cur = cert_cur->next;
3285
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3287 if (cert_prv != crt) {
3288 mbedtls_free(cert_prv);
3289 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003290 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003291}
3292
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003293#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3294/*
3295 * Initialize a restart context
3296 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003297void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003298{
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003300
3301 ctx->parent = NULL;
3302 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003303 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003304
3305 ctx->parent_is_trusted = -1;
3306
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003307 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003308 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003310}
3311
3312/*
3313 * Free the components of a restart context
3314 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003315void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003316{
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003318 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003320
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 mbedtls_pk_restart_free(&ctx->pk);
3322 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003323}
3324#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326#endif /* MBEDTLS_X509_CRT_PARSE_C */