blob: 2ad051fdaa8aa88f4a02dbd389c9d98c921364bd [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
Simon Butcher35e5dad2018-03-15 15:00:03 +00001576 /*
1577 * Note this function uses the code page CP_ACP, and assumes the incoming
1578 * string is encoded in ANSI, before translating it into Unicode. If the
1579 * incoming string were changed to be UTF-8, then the length check needs to
1580 * change to check the number of characters, not the number of bytes, in the
1581 * incoming string are less than MAX_PATH to avoid a buffer overrun with
1582 * MultiByteToWideChar().
1583 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1585 MAX_PATH - 3);
1586 if (w_ret == 0) {
1587 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1588 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 hFind = FindFirstFileW(szDir, &file_data);
1591 if (hFind == INVALID_HANDLE_VALUE) {
1592 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1593 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594
1595 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 do {
1597 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgroveb96c3092023-02-10 12:52:13 +00001604 -1,
1605 p, (int) len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 NULL, NULL);
Kevin Kane0ec1e682016-12-15 09:27:16 -08001607 if (FAILED(SizeTToInt(wcslen(file_data.cFileName), &lengthAsInt)))
1608 return(MBEDTLS_ERR_X509_FILE_IO_ERROR);
1609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001611 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1612 goto cleanup;
1613 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1616 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 }
1621 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001626
Ron Eldor36d90422017-01-09 15:09:16 +02001627cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 FindClose(hFind);
Steve Lhomme369d7c72023-06-16 14:16:03 +02001629#else /* !_WIN32_WINNT_XP */
Antonio de Angelis1ee4d122023-08-16 12:26:37 +01001630#error "mbedtls_x509_crt_parse_path not available before Windows XP"
Steve Lhomme369d7c72023-06-16 14:16:03 +02001631#endif /* !_WIN32_WINNT_XP */
Paul Bakkerbe089b02013-10-14 15:51:50 +02001632#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001633 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001634 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001636 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001637 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 if (dir == NULL) {
1641 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1642 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001643
Ron Eldor63140682017-01-09 19:27:59 +02001644#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1646 closedir(dir);
1647 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001648 }
Ron Eldor63140682017-01-09 19:27:59 +02001649#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001650
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001654 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001656
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001657 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001658 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1659 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 } else if (stat(entry_name, &sb) == -1) {
1661 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001662 /* Broken symbolic link - ignore this entry.
1663 stat(2) will return this error for either (a) a dangling
1664 symlink or (b) a missing file.
1665 Given that we have just obtained the filename from readdir,
1666 assume that it does exist and therefore treat this as a
1667 dangling symlink. */
1668 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001670 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001671 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1672 goto cleanup;
1673 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001674 }
1675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001677 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679
1680 // Ignore parse errors
1681 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1683 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001684 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001686 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001688 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001689
1690cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001692
Ron Eldor63140682017-01-09 19:27:59 +02001693#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 }
Ron Eldor63140682017-01-09 19:27:59 +02001697#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001698
Paul Bakkerbe089b02013-10-14 15:51:50 +02001699#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001704
Peter Kolbus9a969b62018-12-11 13:55:56 -06001705#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001706#define PRINT_ITEM(i) \
1707 do { \
1708 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1709 MBEDTLS_X509_SAFE_SNPRINTF; \
1710 sep = ", "; \
1711 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001712
Przemek Stekielf4194942023-04-24 09:52:17 +02001713#define CERT_TYPE(type, name) \
1714 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001715 if (ns_cert_type & (type)) { \
1716 PRINT_ITEM(name); \
1717 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001718 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001719
Przemek Stekielf4194942023-04-24 09:52:17 +02001720#define KEY_USAGE(code, name) \
1721 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001722 if (key_usage & (code)) { \
1723 PRINT_ITEM(name); \
1724 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001725 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727static int x509_info_ext_key_usage(char **buf, size_t *size,
1728 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001729{
Janos Follath865b3eb2019-12-16 11:46:15 +00001730 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001731 const char *desc;
1732 size_t n = *size;
1733 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001735 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001736
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 while (cur != NULL) {
1738 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001739 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001743 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001744
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001745 sep = ", ";
1746
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001747 cur = cur->next;
1748 }
1749
1750 *size = n;
1751 *buf = p;
1752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001754}
1755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756static int x509_info_cert_policies(char **buf, size_t *size,
1757 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001758{
Janos Follath865b3eb2019-12-16 11:46:15 +00001759 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001760 const char *desc;
1761 size_t n = *size;
1762 char *p = *buf;
1763 const mbedtls_x509_sequence *cur = certificate_policies;
1764 const char *sep = "";
1765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 while (cur != NULL) {
1767 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001768 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001772 MBEDTLS_X509_SAFE_SNPRINTF;
1773
1774 sep = ", ";
1775
1776 cur = cur->next;
1777 }
1778
1779 *size = n;
1780 *buf = p;
1781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001783}
1784
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001785/*
1786 * Return an informational string about the certificate.
1787 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001788#define BEFORE_COLON 18
1789#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001790int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1791 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001792{
Janos Follath865b3eb2019-12-16 11:46:15 +00001793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001794 size_t n;
1795 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001796 char key_size_str[BEFORE_COLON];
1797
1798 p = buf;
1799 n = size;
1800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 if (NULL == crt) {
1802 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001803 MBEDTLS_X509_SAFE_SNPRINTF;
1804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001806 }
1807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1809 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001810 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1812 prefix);
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_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001816 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001817
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001819 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001821 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001824 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001826 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001827
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1829 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1830 crt->valid_from.year, crt->valid_from.mon,
1831 crt->valid_from.day, crt->valid_from.hour,
1832 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001833 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1836 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1837 crt->valid_to.year, crt->valid_to.mon,
1838 crt->valid_to.day, crt->valid_to.hour,
1839 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001840 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001841
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001843 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1846 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001847 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001848
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001849 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1851 mbedtls_pk_get_name(&crt->pk))) != 0) {
1852 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001853 }
1854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1856 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001857 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001858
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001859 /*
1860 * Optional extensions
1861 */
1862
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1864 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1865 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001866 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 if (crt->max_pathlen > 0) {
1869 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001870 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001871 }
1872 }
1873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1875 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001876 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001877
Przemek Stekiel21c37282023-01-16 08:47:49 +01001878 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1879 &crt->subject_alt_names,
1880 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 return ret;
1882 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001883 }
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1886 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001887 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001888
Przemek Stekiel21c37282023-01-16 08:47:49 +01001889 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 return ret;
1891 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001892 }
1893
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1895 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001896 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001897
Przemek Stekiel21c37282023-01-16 08:47:49 +01001898 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 return ret;
1900 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001901 }
1902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1904 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001905 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 if ((ret = x509_info_ext_key_usage(&p, &n,
1908 &crt->ext_key_usage)) != 0) {
1909 return ret;
1910 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001911 }
1912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1914 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001915 MBEDTLS_X509_SAFE_SNPRINTF;
1916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 if ((ret = x509_info_cert_policies(&p, &n,
1918 &crt->certificate_policies)) != 0) {
1919 return ret;
1920 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001921 }
1922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001924 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001927}
1928
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001929struct x509_crt_verify_string {
1930 int code;
1931 const char *string;
1932};
1933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001935static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001936 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001937 { 0, NULL }
1938};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001939#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1942 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001943{
Janos Follath865b3eb2019-12-16 11:46:15 +00001944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001945 const struct x509_crt_verify_string *cur;
1946 char *p = buf;
1947 size_t n = size;
1948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1950 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001951 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001955 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001956 flags ^= cur->code;
1957 }
1958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 if (flags != 0) {
1960 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1961 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001962 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001963 }
1964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001966}
Hanno Becker612a2f12020-10-09 09:19:39 +01001967#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1970 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001971{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001972 unsigned int usage_must, usage_may;
1973 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001975
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1977 return 0;
1978 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001979
1980 usage_must = usage & ~may_mask;
1981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1983 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1984 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001985
1986 usage_may = usage & may_mask;
1987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1989 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1990 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001993}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1996 const char *usage_oid,
1997 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001998{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002000
2001 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
2003 return 0;
2004 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002005
2006 /*
2007 * Look for the requested usage (or wildcard ANY) in our list
2008 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 if (cur_oid->len == usage_len &&
2013 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2014 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002015 }
2016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2018 return 0;
2019 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002020 }
2021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002023}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002026/*
2027 * Return 1 if the certificate is revoked, or 0 otherwise.
2028 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002029int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002030{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 while (cur != NULL && cur->serial.len != 0) {
2034 if (crt->serial.len == cur->serial.len &&
2035 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2036 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002037 }
2038
2039 cur = cur->next;
2040 }
2041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002043}
2044
2045/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002046 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002047 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002048 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002049static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2050 mbedtls_x509_crl *crl_list,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002051 const mbedtls_x509_crt_profile *profile,
2052 const mbedtls_x509_time *now)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002053{
2054 int flags = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002055 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002056#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002057 psa_algorithm_t psa_algorithm;
2058#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002060#endif /* MBEDTLS_USE_PSA_CRYPTO */
2061 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 if (ca == NULL) {
2064 return flags;
2065 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 while (crl_list != NULL) {
2068 if (crl_list->version == 0 ||
2069 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002070 crl_list = crl_list->next;
2071 continue;
2072 }
2073
2074 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002075 * Check if the CA is configured to sign CRLs
2076 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if (mbedtls_x509_crt_check_key_usage(ca,
2078 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002080 break;
2081 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002082
2083 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002084 * Check if CRL is correctly signed by the trusted CA
2085 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002087 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002091 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002093
pespacek7599a772022-02-07 14:40:55 +01002094#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002095 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if (psa_hash_compute(psa_algorithm,
2097 crl_list->tbs.p,
2098 crl_list->tbs.len,
2099 hash,
2100 sizeof(hash),
2101 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002102 /* Note: this can't happen except after an internal error */
2103 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2104 break;
2105 }
pespacek7599a772022-02-07 14:40:55 +01002106#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2108 hash_length = mbedtls_md_get_size(md_info);
2109 if (mbedtls_md(md_info,
2110 crl_list->tbs.p,
2111 crl_list->tbs.len,
2112 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002113 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002115 break;
2116 }
pespaceka7a64692022-02-14 15:18:43 +01002117#endif /* MBEDTLS_USE_PSA_CRYPTO */
2118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002120 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2124 crl_list->sig_md, hash, hash_length,
2125 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002127 break;
2128 }
2129
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002130#if defined(MBEDTLS_HAVE_TIME_DATE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131 /*
2132 * Check for validity of CRL (Do not drop out)
2133 */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002134 if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002137
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002138 if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002139 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002141#else
2142 ((void) now);
2143#endif
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002144
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002145 /*
2146 * Check if certificate is revoked
2147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002150 break;
2151 }
2152
2153 crl_list = crl_list->next;
2154 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002157}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002159
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002160/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002161 * Check the signature of a certificate by its parent
2162 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002163static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2164 mbedtls_x509_crt *parent,
2165 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002166{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002167 size_t hash_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002168 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002169#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2170 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 md_info = mbedtls_md_info_from_type(child->sig_md);
2172 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002173
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002174 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2176 return -1;
2177 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002178#else
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002179 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002180 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 status = psa_hash_compute(hash_alg,
2183 child->tbs.p,
2184 child->tbs.len,
2185 hash,
2186 sizeof(hash),
2187 &hash_len);
2188 if (status != PSA_SUCCESS) {
2189 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002190 }
2191
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002192#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002193 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2195 return -1;
2196 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002197
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002198#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2200 return mbedtls_pk_verify_restartable(&parent->pk,
2201 child->sig_md, hash, hash_len,
2202 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002203 }
2204#else
2205 (void) rs_ctx;
2206#endif
2207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2209 child->sig_md, hash, hash_len,
2210 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002211}
2212
2213/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002214 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2215 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002216 *
2217 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002218 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002219static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2220 const mbedtls_x509_crt *parent,
2221 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002222{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002223 int need_ca_bit;
2224
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002225 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2227 return -1;
2228 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002229
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002230 /* Parent must have the basicConstraints CA bit set as a general rule */
2231 need_ca_bit = 1;
2232
2233 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002235 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002236 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 if (need_ca_bit && !parent->ca_istrue) {
2239 return -1;
2240 }
2241
2242 if (need_ca_bit &&
2243 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2244 return -1;
2245 }
2246
2247 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002248}
2249
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002250/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002251 * Find a suitable parent for child in candidates, or return NULL.
2252 *
2253 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002254 * 1. subject name matches child's issuer
2255 * 2. if necessary, the CA bit is set and key usage allows signing certs
2256 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002257 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002258 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002259 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002260 * If there's a suitable candidate which is also time-valid, return the first
2261 * such. Otherwise, return the first suitable candidate (or NULL if there is
2262 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002263 *
2264 * The rationale for this rule is that someone could have a list of trusted
2265 * roots with two versions on the same root with different validity periods.
2266 * (At least one user reported having such a list and wanted it to just work.)
2267 * The reason we don't just require time-validity is that generally there is
2268 * only one version, and if it's expired we want the flags to state that
2269 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002270 *
2271 * The rationale for rule 3 (signature for trusted roots) is that users might
2272 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002273 * way we select the correct one is by checking the signature (as we don't
2274 * rely on key identifier extensions). (This is one way users might choose to
2275 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002276 *
2277 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002278 * - [in] child: certificate for which we're looking for a parent
2279 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002280 * - [out] r_parent: parent found (or NULL)
2281 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002282 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2283 * of the chain, 0 otherwise
2284 * - [in] path_cnt: number of intermediates seen so far
2285 * - [in] self_cnt: number of self-signed intermediates seen so far
2286 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002287 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002288 *
2289 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002290 * - 0 on success
2291 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002292 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002293static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 mbedtls_x509_crt *child,
2295 mbedtls_x509_crt *candidates,
2296 mbedtls_x509_crt **r_parent,
2297 int *r_signature_is_good,
2298 int top,
2299 unsigned path_cnt,
2300 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002301 mbedtls_x509_crt_restart_ctx *rs_ctx,
2302 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002303{
Janos Follath865b3eb2019-12-16 11:46:15 +00002304 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002305 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002306 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002307
2308#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002309 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002311 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002312 parent = rs_ctx->parent;
2313 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002314 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002315
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002316 /* clear saved state */
2317 rs_ctx->parent = NULL;
2318 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002319 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002320
2321 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002322 goto check_signature;
2323 }
2324#endif
2325
2326 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002327 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002330 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002332 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002334
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002335 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (parent->max_pathlen > 0 &&
2337 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002338 continue;
2339 }
2340
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002341 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002342#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2343check_signature:
2344#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002346
2347#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002349 /* save state */
2350 rs_ctx->parent = parent;
2351 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002352 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002355 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002356#else
2357 (void) ret;
2358#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002359
2360 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002362 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002364
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002365#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002366 /* optional time check */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002367 if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2368 mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002370 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002371 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002372 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002373
2374 continue;
2375 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002376#else
2377 ((void) now);
2378#endif
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002379
Andy Gross1f627142019-01-30 10:25:53 -06002380 *r_parent = parent;
2381 *r_signature_is_good = signature_is_good;
2382
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002383 break;
2384 }
2385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002387 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002388 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002389 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002392}
2393
2394/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002395 * Find a parent in trusted CAs or the provided chain, or return NULL.
2396 *
2397 * Searches in trusted CAs first, and return the first suitable parent found
2398 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002399 *
2400 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002401 * - [in] child: certificate for which we're looking for a parent, followed
2402 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002403 * - [in] trust_ca: list of locally trusted certificates
2404 * - [out] parent: parent found (or NULL)
2405 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2406 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2407 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2408 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002409 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002410 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002411 *
2412 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002413 * - 0 on success
2414 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002415 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002416static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 mbedtls_x509_crt *child,
2418 mbedtls_x509_crt *trust_ca,
2419 mbedtls_x509_crt **parent,
2420 int *parent_is_trusted,
2421 int *signature_is_good,
2422 unsigned path_cnt,
2423 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002424 mbedtls_x509_crt_restart_ctx *rs_ctx,
2425 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002426{
Janos Follath865b3eb2019-12-16 11:46:15 +00002427 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002428 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002429
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002430 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002431
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002432#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002433 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002435 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002436 rs_ctx->parent_is_trusted = -1;
2437 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002438#endif
2439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002441 search_list = *parent_is_trusted ? trust_ca : child->next;
2442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 ret = x509_crt_find_parent_in(child, search_list,
2444 parent, signature_is_good,
2445 *parent_is_trusted,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002446 path_cnt, self_cnt, rs_ctx, now);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002447
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002448#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002450 /* save state */
2451 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002453 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002454#else
2455 (void) ret;
2456#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002457
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002458 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002460 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002462
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002463 /* prepare second iteration */
2464 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002465 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002466
2467 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002469 *parent_is_trusted = 0;
2470 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002471 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002474}
2475
2476/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002477 * Check if an end-entity certificate is locally trusted
2478 *
2479 * Currently we require such certificates to be self-signed (actually only
2480 * check for self-issued as self-signatures are not checked)
2481 */
2482static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 mbedtls_x509_crt *crt,
2484 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002485{
2486 mbedtls_x509_crt *cur;
2487
2488 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2490 return -1;
2491 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002492
2493 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2495 if (crt->raw.len == cur->raw.len &&
2496 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2497 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002498 }
2499 }
2500
2501 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002503}
2504
2505/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002506 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002507 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002508 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2509 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002510 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002511 * such that every cert in the chain is a child of the next one,
2512 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002513 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002514 * Verify that chain and return it with flags for all issues found.
2515 *
2516 * Special cases:
2517 * - EE == Rj -> return a one-element list containing it
2518 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2519 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002520 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002521 * Tests for (aspects of) this function should include at least:
2522 * - trusted EE
2523 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002524 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002525 * - if relevant: EE untrusted
2526 * - if relevant: EE -> intermediate, untrusted
2527 * with the aspect under test checked at each relevant level (EE, int, root).
2528 * For some aspects longer chains are required, but usually length 2 is
2529 * enough (but length 1 is not in general).
2530 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002531 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002532 * - [in] crt: the cert list EE, C1, ..., Cn
2533 * - [in] trust_ca: the trusted list R1, ..., Rp
2534 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002535 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002536 * Only valid when return value is 0, may contain garbage otherwise!
2537 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002538 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002539 *
2540 * Return value:
2541 * - non-zero if the chain could not be fully built and examined
2542 * - 0 is the chain was successfully built and examined,
2543 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002544 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002545static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 mbedtls_x509_crt *crt,
2547 mbedtls_x509_crt *trust_ca,
2548 mbedtls_x509_crl *ca_crl,
2549 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2550 void *p_ca_cb,
2551 const mbedtls_x509_crt_profile *profile,
2552 mbedtls_x509_crt_verify_chain *ver_chain,
2553 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002554{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002555 /* Don't initialize any of those variables here, so that the compiler can
2556 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002558 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002559 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002560 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002561 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002562 int parent_is_trusted;
2563 int child_is_trusted;
2564 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002565 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002566 mbedtls_x509_crt *cur_trust_ca = NULL;
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002567 mbedtls_x509_time now;
2568
2569#if defined(MBEDTLS_HAVE_TIME_DATE)
2570 if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2571 return MBEDTLS_ERR_X509_FATAL_ERROR;
2572 }
2573#endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002574
2575#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2576 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002578 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002579 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002580 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002581
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002582 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002583 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002584 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002585 flags = &cur->flags;
2586
2587 goto find_parent;
2588 }
2589#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002590
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002591 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002592 self_cnt = 0;
2593 parent_is_trusted = 0;
2594 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002597 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002598 cur = &ver_chain->items[ver_chain->len];
2599 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002600 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002601 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002602 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002603
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002604#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002605 /* Check time-validity (all certificates) */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002606 if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002607 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002609
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002610 if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002611 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002613#endif
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002614
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002615 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (child_is_trusted) {
2617 return 0;
2618 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002619
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002620 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002622 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002626 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002628
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002629 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 if (ver_chain->len == 1 &&
2631 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2632 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002633 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002634
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002635#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2636find_parent:
2637#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002638
2639 /* Obtain list of potential trusted signers from CA callback,
2640 * or use statically provided list. */
2641#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 if (f_ca_cb != NULL) {
2643 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2644 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002645 ver_chain->trust_ca_cb_result = NULL;
2646
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2648 if (ret != 0) {
2649 return MBEDTLS_ERR_X509_FATAL_ERROR;
2650 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002651
2652 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002654#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2655 {
2656 ((void) f_ca_cb);
2657 ((void) p_ca_cb);
2658 cur_trust_ca = trust_ca;
2659 }
2660
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002661 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2663 &parent_is_trusted, &signature_is_good,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002664 ver_chain->len - 1, self_cnt, rs_ctx,
2665 &now);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002666
2667#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002669 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002670 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002671 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002672 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002675 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002676#else
2677 (void) ret;
2678#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002679
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002680 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002682 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002684 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002685
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002686 /* Count intermediate self-issued (not necessarily self-signed) certs.
2687 * These can occur with some strategies for key rollover, see [SIRO],
2688 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 if (ver_chain->len != 1 &&
2690 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002691 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002692 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002693
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002694 /* path_cnt is 0 for the first intermediate CA,
2695 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 if (!parent_is_trusted &&
2697 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002698 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002700 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002701
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002702 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002704 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002706
2707 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002709 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002713 /* Check trusted CA's CRL for the given crt */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002714 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002715#else
2716 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002717#endif
2718
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002719 /* prepare for next iteration */
2720 child = parent;
2721 parent = NULL;
2722 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002723 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002724 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002725}
2726
Glenn Straussc26bd762022-10-23 19:48:18 -04002727#ifdef _WIN32
2728#ifdef _MSC_VER
2729#pragma comment(lib, "ws2_32.lib")
2730#include <winsock2.h>
2731#include <ws2tcpip.h>
2732#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2733#include <winsock2.h>
2734#include <ws2tcpip.h>
Steve Lhommeeb0f18a2023-06-16 14:12:19 +02002735#else
2736/* inet_pton() is not supported, fallback to software version */
2737#define MBEDTLS_TEST_SW_INET_PTON
Glenn Straussc26bd762022-10-23 19:48:18 -04002738#endif
2739#elif defined(__sun)
2740/* Solaris requires -lsocket -lnsl for inet_pton() */
2741#elif defined(__has_include)
2742#if __has_include(<sys/socket.h>)
2743#include <sys/socket.h>
2744#endif
2745#if __has_include(<arpa/inet.h>)
2746#include <arpa/inet.h>
2747#endif
2748#endif
2749
2750/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2751 * the platform inet_pton() or a local implementation (below). The local
2752 * implementation may be used even in cases where the platform provides
2753 * inet_pton(), e.g. when there are different includes required and/or the
2754 * platform implementation requires dependencies on additional libraries.
2755 * Specifically, Windows requires custom includes and additional link
2756 * dependencies, and Solaris requires additional link dependencies.
2757 * Also, as a coarse heuristic, use the local implementation if the compiler
2758 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002759 * provided by headers included (or not) via __has_include() above.
2760 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2761 * despite having a platform that has inet_pton. */
2762#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2763/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002764static int x509_inet_pton_ipv4(const char *src, void *dst);
2765
2766#define li_cton(c, n) \
2767 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2768
2769static int x509_inet_pton_ipv6(const char *src, void *dst)
2770{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002771 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002772 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002773 uint16_t addr[8];
2774 do {
2775 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002776 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002777 for (uint8_t digit; num_digits < 4; num_digits++) {
2778 if (li_cton(*p, digit) == 0) {
2779 break;
2780 }
2781 group = (group << 4) | digit;
2782 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002783 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002784 if (num_digits != 0) {
Dave Rodgmancfa72232023-09-05 16:20:33 +01002785 MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups);
2786 nonzero_groups++;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002787 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002788 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002789 } else if (*p == '.') {
2790 /* Don't accept IPv4 too early or late */
2791 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2792 nonzero_groups >= 7) {
2793 break;
2794 }
2795
2796 /* Walk back to prior ':', then parse as IPv4-mapped */
2797 int steps = 4;
2798 do {
2799 p--;
2800 steps--;
2801 } while (*p != ':' && steps > 0);
2802
2803 if (*p != ':') {
2804 break;
2805 }
2806 p++;
2807 nonzero_groups--;
2808 if (x509_inet_pton_ipv4((const char *) p,
2809 addr + nonzero_groups) != 0) {
2810 break;
2811 }
2812
Andrzej Kurek0d578962023-04-13 09:09:56 -04002813 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002814 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002815 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002816 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002817 return -1;
2818 }
2819 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002820 /* Don't accept a second zero group or an invalid delimiter */
2821 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002822 return -1;
2823 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002824 zero_group_start = nonzero_groups;
2825
2826 /* Accept a zero group at start, but it has to be a double colon */
2827 if (zero_group_start == 0 && *++p != ':') {
2828 return -1;
2829 }
2830
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002831 if (p[1] == '\0') {
2832 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002833 break;
2834 }
2835 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002836 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002837 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002838
2839 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002840 return -1;
2841 }
2842
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002843 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002844 if (nonzero_groups > 6) {
2845 return -1;
2846 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002847 int zero_groups = 8 - nonzero_groups;
2848 int groups_after_zero = nonzero_groups - zero_group_start;
2849
2850 /* Move the non-zero part to after the zeroes */
2851 if (groups_after_zero) {
2852 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002853 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002854 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002855 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002856 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002857 } else {
2858 if (nonzero_groups != 8) {
2859 return -1;
2860 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002861 }
2862 memcpy(dst, addr, sizeof(addr));
2863 return 0;
2864}
2865
2866static int x509_inet_pton_ipv4(const char *src, void *dst)
2867{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002868 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002869 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002870 uint8_t digit, num_digits = 0;
2871 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002872 uint16_t octet;
2873
Glenn Strauss416c2952022-10-24 23:00:02 -04002874 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002875 octet = num_digits = 0;
2876 do {
2877 digit = *p - '0';
2878 if (digit > 9) {
2879 break;
2880 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002881
2882 /* Don't allow leading zeroes. These might mean octal format,
2883 * which this implementation does not support. */
2884 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002885 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002886 }
2887
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002888 octet = octet * 10 + digit;
2889 num_digits++;
2890 p++;
2891 } while (num_digits < 3);
2892
2893 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002894 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002895 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002896 *res++ = (uint8_t) octet;
2897 num_octets++;
2898 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002899 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002900}
Glenn Straussc26bd762022-10-23 19:48:18 -04002901
2902#else
2903
2904static int x509_inet_pton_ipv6(const char *src, void *dst)
2905{
2906 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2907}
2908
2909static int x509_inet_pton_ipv4(const char *src, void *dst)
2910{
2911 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2912}
2913
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002914#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002915
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002916size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002917{
2918 return strchr(cn, ':') == NULL
2919 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2920 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2921}
2922
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002923/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002924 * Check for CN match
2925 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002926static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2927 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002928{
2929 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 if (name->len == cn_len &&
2931 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2932 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002933 }
2934
2935 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 if (x509_check_wildcard(cn, name) == 0) {
2937 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002938 }
2939
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002941}
2942
Glenn Straussc26bd762022-10-23 19:48:18 -04002943static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2944 const char *cn, size_t cn_len)
2945{
2946 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002947 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002948 if (cn_len == 0) {
2949 return -1;
2950 }
2951
2952 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2953 const unsigned char san_type = (unsigned char) cur->buf.tag &
2954 MBEDTLS_ASN1_TAG_VALUE_MASK;
2955 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2956 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2957 return 0;
2958 }
2959 }
2960
2961 return -1;
2962}
2963
Andrzej Kurek199eab92023-05-10 09:57:19 -04002964static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2965 const char *cn, size_t cn_len)
2966{
2967 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2968 const unsigned char san_type = (unsigned char) cur->buf.tag &
2969 MBEDTLS_ASN1_TAG_VALUE_MASK;
2970 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2971 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2972 return 0;
2973 }
2974 }
2975
2976 return -1;
2977}
2978
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002979/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002980 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2981 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002982static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002984{
Glenn Straussc26bd762022-10-23 19:48:18 -04002985 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002986 int san_uri = 0;
2987 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002988 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2989 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002990 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002991 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2992 return 0;
2993 }
2994 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002995 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04002996 san_ip = 1;
2997 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002998 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
2999 san_uri = 1;
3000 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04003001 /* (We may handle other types here later.) */
3002 default: /* Unrecognized type */
3003 break;
3004 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04003006 if (san_ip) {
3007 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
3008 return 0;
3009 }
3010 }
3011 if (san_uri) {
3012 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
3013 return 0;
3014 }
3015 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003016
Andrzej Kurek199eab92023-05-10 09:57:19 -04003017 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003018}
3019
3020/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003021 * Verify the requested CN - only call this if cn is not NULL!
3022 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003023static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3024 const char *cn,
3025 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003026{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003027 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003031 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
3032 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 }
3034 } else {
3035 for (name = &crt->subject; name != NULL; name = name->next) {
3036 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3037 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003038 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 }
3040 }
3041
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003042 }
Glenn Straussc26bd762022-10-23 19:48:18 -04003043
3044 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003045}
3046
3047/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003048 * Merge the flags for all certs in the chain, after calling callback
3049 */
3050static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 uint32_t *flags,
3052 const mbedtls_x509_crt_verify_chain *ver_chain,
3053 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3054 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003055{
Janos Follath865b3eb2019-12-16 11:46:15 +00003056 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003057 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003058 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003059 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003060
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003062 cur = &ver_chain->items[i-1];
3063 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003064
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 if (NULL != f_vrfy) {
3066 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3067 return ret;
3068 }
3069 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003070
3071 *flags |= cur_flags;
3072 }
3073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003075}
3076
3077/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003078 * Verify the certificate validity, with profile, restartable version
3079 *
3080 * This function:
3081 * - checks the requested CN (if any)
3082 * - checks the type and size of the EE cert's key,
3083 * as that isn't done as part of chain building/verification currently
3084 * - builds and verifies the chain
3085 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003086 *
3087 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3088 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3089 * verification routine to search for trusted signers, and CRLs will
3090 * be disabled. Otherwise, `trust_ca` will be used as the static list
3091 * of trusted signers, and `ca_crl` will be use as the static list
3092 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003093 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003094static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3095 mbedtls_x509_crt *trust_ca,
3096 mbedtls_x509_crl *ca_crl,
3097 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3098 void *p_ca_cb,
3099 const mbedtls_x509_crt_profile *profile,
3100 const char *cn, uint32_t *flags,
3101 int (*f_vrfy)(void *,
3102 mbedtls_x509_crt *,
3103 int,
3104 uint32_t *),
3105 void *p_vrfy,
3106 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003107{
Janos Follath865b3eb2019-12-16 11:46:15 +00003108 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003109 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003110 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003111 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003112
3113 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003114 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003116
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003118 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3119 goto exit;
3120 }
3121
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003122 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 if (cn != NULL) {
3124 x509_crt_verify_name(crt, cn, &ee_flags);
3125 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003126
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003127 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003131 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003133
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003135 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003137
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003138 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3140 f_ca_cb, p_ca_cb, profile,
3141 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003142
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003144 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003146
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003147 /* Merge end-entity flags */
3148 ver_chain.items[0].flags |= ee_flags;
3149
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003150 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003152
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003153exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003154
3155#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3157 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003158 ver_chain.trust_ca_cb_result = NULL;
3159#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3160
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003161#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3163 mbedtls_x509_crt_restart_free(rs_ctx);
3164 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003165#endif
3166
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003167 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3168 * the SSL module for authmode optional, but non-zero return from the
3169 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003171 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003172 }
3173
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 if (ret != 0) {
3175 *flags = (uint32_t) -1;
3176 return ret;
3177 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 if (*flags != 0) {
3180 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3181 }
3182
3183 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003184}
3185
Hanno Becker3116fb32019-03-28 13:34:42 +00003186
3187/*
3188 * Verify the certificate validity (default profile, not restartable)
3189 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003190int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3191 mbedtls_x509_crt *trust_ca,
3192 mbedtls_x509_crl *ca_crl,
3193 const char *cn, uint32_t *flags,
3194 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3195 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003196{
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3198 NULL, NULL,
3199 &mbedtls_x509_crt_profile_default,
3200 cn, flags,
3201 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003202}
3203
3204/*
3205 * Verify the certificate validity (user-chosen profile, not restartable)
3206 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003207int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3208 mbedtls_x509_crt *trust_ca,
3209 mbedtls_x509_crl *ca_crl,
3210 const mbedtls_x509_crt_profile *profile,
3211 const char *cn, uint32_t *flags,
3212 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3213 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003214{
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3216 NULL, NULL,
3217 profile, cn, flags,
3218 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003219}
3220
3221#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3222/*
3223 * Verify the certificate validity (user-chosen profile, CA callback,
3224 * not restartable).
3225 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003226int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3227 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3228 void *p_ca_cb,
3229 const mbedtls_x509_crt_profile *profile,
3230 const char *cn, uint32_t *flags,
3231 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3232 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003233{
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3235 f_ca_cb, p_ca_cb,
3236 profile, cn, flags,
3237 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003238}
3239#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3240
Gilles Peskine449bd832023-01-11 14:50:10 +01003241int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3242 mbedtls_x509_crt *trust_ca,
3243 mbedtls_x509_crl *ca_crl,
3244 const mbedtls_x509_crt_profile *profile,
3245 const char *cn, uint32_t *flags,
3246 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3247 void *p_vrfy,
3248 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003249{
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3251 NULL, NULL,
3252 profile, cn, flags,
3253 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003254}
3255
3256
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003257/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003258 * Initialize a certificate chain
3259 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003260void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003261{
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003263}
3264
3265/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003266 * Unallocate all certificate data
3267 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003268void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003269{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 mbedtls_x509_crt *cert_cur = crt;
3271 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003272
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 while (cert_cur != NULL) {
3274 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003278#endif
3279
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3281 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3282 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3283 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3284 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Przemek Stekiel690ff692023-05-15 09:54:02 +02003285 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003286
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003288 mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003289 }
3290
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003291 cert_prv = cert_cur;
3292 cert_cur = cert_cur->next;
3293
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3295 if (cert_prv != crt) {
3296 mbedtls_free(cert_prv);
3297 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003298 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003299}
3300
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003301#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3302/*
3303 * Initialize a restart context
3304 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003305void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003306{
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003308
3309 ctx->parent = NULL;
3310 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003311 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003312
3313 ctx->parent_is_trusted = -1;
3314
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003315 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003316 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003318}
3319
3320/*
3321 * Free the components of a restart context
3322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003323void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003324{
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003326 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003328
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 mbedtls_pk_restart_free(&ctx->pk);
3330 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003331}
3332#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334#endif /* MBEDTLS_X509_CRT_PARSE_C */